comparison mx/outputparser.py @ 7567:a8bc60aeacb8

fix bug in parsing of SPECjvm2008 output
author Doug Simon <doug.simon@oracle.com>
date Wed, 30 Jan 2013 18:19:01 +0100
parents c420a487b10f
children 2025455e7d80
comparison
equal deleted inserted replaced
7564:c420a487b10f 7567:a8bc60aeacb8
30 30
31 def addMatcher(self, matcher): 31 def addMatcher(self, matcher):
32 self.matchers.append(matcher) 32 self.matchers.append(matcher)
33 33
34 def parse(self, output): 34 def parse(self, output):
35 records = [] 35 valueMaps = []
36 for matcher in self.matchers: 36 for matcher in self.matchers:
37 record = matcher.parse(output) 37 matcher.parse(output, valueMaps)
38 if record: 38 return valueMaps
39 records.append(record)
40 return records
41 39
42 """ 40 """
43 Produces some named values for some given text if it matches a given 41 Produces a value map for each match of a given regular expression
44 regular expression. The named values are specified by a dictionary 42 in some text. The value map is specified by a template map
45 where any keys or value may be expressed as named group in the 43 where each key and value in the template map is either a constant
46 regular expression. A named group is enclosed in '<' and '>'. 44 value or a named group in the regular expression. The latter is
45 given as the group name enclosed in '<' and '>'.
47 """ 46 """
48 class ValuesMatcher: 47 class ValuesMatcher:
49 48
50 def __init__(self, regex, valuesTemplate): 49 def __init__(self, regex, valuesTemplate):
51 assert isinstance(valuesTemplate, dict) 50 assert isinstance(valuesTemplate, dict)
52 self.regex = regex 51 self.regex = regex
53 self.valuesTemplate = valuesTemplate 52 self.valuesTemplate = valuesTemplate
54 53
55 def parse(self, text): 54 def parse(self, text, valueMaps):
56 match = self.regex.search(text) 55 for match in self.regex.finditer(text):
57 if not match: 56 valueMap = {}
58 return False 57 for keyTemplate, valueTemplate in self.valuesTemplate.items():
59 values = {} 58 key = self.get_template_value(match, keyTemplate)
60 for key, value in self.valuesTemplate.items(): 59 value = self.get_template_value(match, valueTemplate)
61 values[self.get_template_value(match, key)] = self.get_template_value(match, value) 60 assert not valueMap.has_key(key), key
62 61 valueMap[key] = value
63 return values 62 valueMaps.append(valueMap)
64
65 63
66 def get_template_value(self, match, template): 64 def get_template_value(self, match, template):
67 if template.startswith('<'): 65 if template.startswith('<'):
68 assert template.endswith('>') 66 assert template.endswith('>')
69 groupName = template[1:-1] 67 groupName = template[1:-1]