PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/TextParser.py

http://wfuzz.googlecode.com/
Python | 139 lines | 134 code | 3 blank | 2 comment | 0 complexity | 49198db32548bb43d7aa1d170949ba9e MD5 | raw file
Possible License(s): AGPL-1.0, MPL-2.0-no-copyleft-exception
  1. #Covered by GPL V2.0
  2. #Coded by Carlos del Ojo Elias (deepbit@gmail.com)
  3. import sys
  4. import re
  5. class TextParser:
  6. def __init__ (self):
  7. self.string=""
  8. self.oldindex=0
  9. self.newindex=0
  10. self.type=""
  11. self.lastFull_line=None
  12. self.lastline = None
  13. pass
  14. def __del__ (self):
  15. if self.type=="file":
  16. self.fd.close()
  17. def setSource (self,t,*args):
  18. '''Se especifica el tipo de entrada. Puede ser fichero o entrada estandard
  19. Ejemplos: setSource("file","/tmp/file")
  20. setSource("stdin")\n'''
  21. if t=="file":
  22. self.type=t
  23. self.fd=file(args[0],"r")
  24. elif t=="stdin":
  25. if self.type=="file":
  26. self.fd.close()
  27. self.type=t
  28. elif t=="string":
  29. if self.type=="file":
  30. self.fd.close()
  31. self.type=t
  32. self.string=args[0]
  33. self.oldindex=0
  34. self.newindex=0
  35. else:
  36. print "Bad argument -- TextParser.setSource()\n"
  37. sys.exit (-1)
  38. def seekinit(self):
  39. self.oldindex=0;
  40. self.newindex=0;
  41. def readUntil (self,pattern,caseSens=True):
  42. "Lee lineas hasta que el patron (pattern) conincide en alguna linea"
  43. while True:
  44. if (self.readLine() == 0):
  45. return False
  46. if (self.search(pattern,caseSens) == True):
  47. break
  48. return True
  49. def search (self,pattern,caseSens=True,debug=0):
  50. "Intenta hacer Matching entre el pattern pasado por parametro y la ultima linea leida"
  51. if not caseSens:
  52. self.regexp=re.compile(pattern,re.IGNORECASE)
  53. else:
  54. self.regexp=re.compile(pattern)
  55. self.matches=self.regexp.findall(self.lastline)
  56. j=0
  57. for i in self.matches:
  58. if not type(i)==type(()):
  59. self.matches[j]=tuple([self.matches[j]])
  60. j+=1
  61. # DEBUG PARA MATCHING
  62. if (debug==1):
  63. print "[",self.lastline,"-",pattern,"]"
  64. print len(self.matches)
  65. print self.matches
  66. if len(self.matches)==0:
  67. return False
  68. else:
  69. return True
  70. def __getitem__ (self,key):
  71. "Para acceder a cada uno de los patrones que coinciden, esta preparado paragrupos de patrones, no para solo un patron"
  72. return self.matches[key]
  73. def skip (self,lines):
  74. "Salta las lines que se indiquen en el parametro"
  75. for i in range(lines):
  76. if (self.readLine() == 0):
  77. return False
  78. return True
  79. def readLine(self):
  80. "Lee la siguiente linea eliminando retornos de carro"
  81. if self.type=="file":
  82. self.lastFull_line=self.fd.readline()
  83. elif self.type=="stdin":
  84. self.lastFull_line=raw_input()
  85. elif self.type=="string":
  86. if self.newindex==-1:
  87. return 0
  88. if self.oldindex>=0:
  89. self.newindex=self.string.find("\n",self.oldindex,len(self.string))
  90. if self.newindex==-1:
  91. self.lastFull_line=self.string[self.oldindex:len(self.string)]
  92. else:
  93. self.lastFull_line=self.string[self.oldindex:self.newindex+1]
  94. self.oldindex=self.newindex+1
  95. else:
  96. self.lastFull_line=''
  97. bytes_read = len(self.lastFull_line)
  98. s=self.lastFull_line
  99. self.lastline=s
  100. if s[-2:] == '\r\n':
  101. self.lastline = s[:-2]
  102. elif s[-1:] == '\r' or s[-1:] == '\n':
  103. self.lastline = s[:-1]
  104. return bytes_read