CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Functions | Variables
parsingRulesHelper Namespace Reference

Functions

def rulesParser
 

Variables

tuple rulesRegexpCompileFunction = lambdax:( len(x)==2 and (x[0], re.compile(x[1])) or (x[0], re.compile(x[1]), x[2]) )
 

Function Documentation

def parsingRulesHelper.rulesParser (   parsing_rules,
  lines,
  compileRules = True 
)
    Applies the (provided) regular expression rules (=rule[1] for rule in parsing_rules)
    to each line and if it matches the line,
    puts the mached information to the dictionary as the specified keys (=rule[0]) which is later returned
    Rule[3] contains whether the field is required to be found. If so and it isn't found the exception would be raised.
    rules = [
      ( (field_name_1_to_match, field_name_2), regular expression, /optionaly: is the field required? if so "req"/ )
    ]

Definition at line 9 of file parsingRulesHelper.py.

References Association.map, and match().

Referenced by parserPerfsuiteMetadata.parserPerfsuiteMetadata._applyParsingRules(), and FileNamesHelper.read_ConfigurationFromSimulationCandles().

9 
10 def rulesParser(parsing_rules, lines, compileRules = True):
11  """
12  Applies the (provided) regular expression rules (=rule[1] for rule in parsing_rules)
13  to each line and if it matches the line,
14  puts the mached information to the dictionary as the specified keys (=rule[0]) which is later returned
15  Rule[3] contains whether the field is required to be found. If so and it isn't found the exception would be raised.
16  rules = [
17  ( (field_name_1_to_match, field_name_2), regular expression, /optionaly: is the field required? if so "req"/ )
18  ]
19  """
20  info = {}
21  #we compile the parsing rules
22  if compileRules:
23  parsing_rules = map(rulesRegexpCompileFunction, parsing_rules)
24  """ we dynamicaly check if line passes any of the rules and in this way put the information to the info dict. """
25  for line in lines:
26  for rule in parsing_rules:
27  if rule[1].match(line):
28  g = rule[1].match(line).groups()
29  #print g
30  #print "rule fields:" + str(rule[0])
31  i = 0
32  for field_name in rule[0]:
33  "we use empty field name to mark unneeded parts of regular expression"
34  if field_name != "":
35  #print str(i) + ":" + field_name
36  # we do want to store None values as empty strings ""
37  #TODO: we might want to change it if we multiple introduced rules having same result targets
38  if g[i] == None:
39  info[field_name] = ""
40  else:
41  info[field_name] = g[i]
42  i += 1
43  #For the values which do not exist we put "" and check for REQUIRED values
44  missing_fields = []
45  for rule in parsing_rules:
46  for field_name in rule[0]:
47  if field_name:
48  if not info.has_key(field_name):
49  info[field_name] = ""
50  """ check for required fields"""
51  if len(rule) == 3 and rule[2] =="req":
52  if not info[field_name]:
53  missing_fields.append(field_name)
54  return (info, missing_fields)
dictionary map
Definition: Association.py:160
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:6

Variable Documentation

tuple parsingRulesHelper.rulesRegexpCompileFunction = lambdax:( len(x)==2 and (x[0], re.compile(x[1])) or (x[0], re.compile(x[1]), x[2]) )

Definition at line 7 of file parsingRulesHelper.py.