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
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
00030
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
00036
00037
00038 if g[i] == None:
00039 info[field_name] = ""
00040 else:
00041 info[field_name] = g[i]
00042 i += 1
00043
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)