Package pyvb :: Module parser
[hide private]
[frames] | no frames]

Source Code for Module pyvb.parser

 1  """ 
 2  parser - pyvb module that holds the implementation of the base parser 
 3  used to parse the VirtualBox command line output. 
 4  """ 
 5   
 6  import types 
 7   
8 -class vbParser:
9 """The base pyvb parser used to construct Python abstractions of the 10 VirtualBox command line output."""
11 - def __init__(self):
12 """Constructor. 13 @return: A new L{pyvb.parser.vbParser} instance. 14 @rtype: L{pyvb.parser.vbParser}""" 15 pass
16
17 - def _parse(self, file):
18 """Construct a list of dictionaries based of the specified file. 19 First we check if we are dealing with a file object of a list 20 object. We iterate through all the lines in the file. If 21 a list parameter was provided, a line is represented by a list 22 element. The inner loop is for the attributes in which we are 23 searching. We match against the attribute's regular expression 24 and add it to the dictionary of found results. The dictionary 25 represnting the current object is considered complete when the 26 number of found attributes matches the number of attributes we 27 are looking for. 28 @param file: The file we are parsing. 29 @type file: File/List 30 @return: A list of parse results. 31 @rtype: List""" 32 parse_results=[] 33 if type(file) is types.FileType: 34 parse_input=file.readlines() 35 elif type(file) is types.ListType: 36 parse_input=file 37 for line in parse_input: 38 for attribute in self.attributes.keys(): 39 try: 40 result=self.attributes[attribute].match(line).groups()[1] 41 self.found_attributes[attribute]=result 42 except AttributeError: 43 pass 44 if len(self.attributes)==len(self.found_attributes): 45 parse_results.append(self.found_attributes) 46 self.found_attributes={} 47 return parse_results
48
49 - def _parseAdditional(self, file, expression):
50 """Parse the specified file for the specified expression 51 and return the results excluding the first result found. 52 @param file: The file we are parsing. 53 @type file: File 54 @param expression: The expression to match against. 55 @type expression: Regex 56 @return: A list of parse results. 57 @rtype: List""" 58 parse_results=[] 59 first=False 60 for line in file.readlines(): 61 try: 62 result=expression.match(line).groups()[1] 63 if first: 64 parse_results.append(result) 65 else: 66 first=True 67 except AttributeError,e: 68 pass 69 return parse_results
70
71 - def addAttribute(self, name, expression):
72 self.attributes[name]=expression
73
74 - def removeAttribute(self, name):
75 del self.attributes[name]
76