CMS 3D CMS Logo

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.

00010                                                           :
00011                 """ 
00012                         Applies the (provided) regular expression rules (=rule[1] for rule in parsing_rules)
00013                         to each line and if it matches the line,
00014                         puts the mached information to the dictionary as the specified keys (=rule[0]) which is later returned
00015                         Rule[3] contains whether the field is required to be found. If so and it isn't found the exception would be raised.
00016                         rules = [
00017                           ( (field_name_1_to_match, field_name_2), regular expression, /optionaly: is the field required? if so "req"/ )
00018                         ]
00019                  """
00020                 info = {}
00021                 #we compile the parsing rules
00022                 if compileRules:
00023                         parsing_rules = map(rulesRegexpCompileFunction, parsing_rules)
00024                 """ we dynamicaly check if line passes any of the rules and in this way put the information to the info dict. """
00025                 for line in lines:
00026                         for rule in parsing_rules:
00027                                 if rule[1].match(line):
00028                                         g = rule[1].match(line).groups()
00029                                         #print g
00030                                         #print "rule fields:"  + str(rule[0])
00031                                         i = 0
00032                                         for field_name in rule[0]:
00033                                                 "we use empty field name to mark unneeded parts of regular expression"
00034                                                 if field_name != "":
00035                                                         #print str(i) + ":" + field_name
00036                                                         # we do want to store None values as empty strings ""
00037                                                         #TODO: we might want to change it if we multiple introduced rules having same result targets
00038                                                         if g[i] == None:
00039                                                                 info[field_name] = ""
00040                                                         else:
00041                                                                 info[field_name] = g[i]
00042                                                 i += 1
00043                 #For the values which do not exist we put "" and check for REQUIRED values
00044                 missing_fields = []
00045                 for rule in parsing_rules:
00046                         for field_name in rule[0]:
00047                                 if field_name:
00048                                         if not info.has_key(field_name):
00049                                                 info[field_name] = ""
00050                                         """ check for required fields"""
00051                                         if len(rule) == 3 and rule[2] =="req":
00052                                                 if not info[field_name]:
00053                                                         missing_fields.append(field_name)
00054                 return (info, missing_fields)

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.