def __read_script(script, encoding = 'utf8'): file_reg = '\[(.*)\]' cmd_reg = '(\w+)\s*:\s*(.*)' tag_file = None cmd_set = {} if isinstance(script, str): content = text_file.read_file(script, 'all', encoding, True) elif isinstance(script, list): content = script elif isinstance(script, dict): return script else: raise IOError('script must be a filename or script command list') for line in content: if str_utils.is_empty(line): continue line = line.strip() if str_utils.startswith(line, '#'): continue if regex_utils.check_line(file_reg, line): tag_file = regex_utils.parse_line(file_reg, line)[0] if str_utils.is_not_empty(tag_file): tag_file = tag_file.strip() elif regex_utils.check_line(cmd_reg, line): cmd_item = regex_utils.parse_line(cmd_reg, line) if tag_file is not None and cmd_item is not None and len(cmd_item) == 2: cmd_list = cmd_set.get(tag_file) if cmd_set.has_key(tag_file) else [] cmd_list.append(cmd_item) cmd_set[tag_file] = cmd_list return cmd_set
def parse(string, rule_list): if string_utils.is_blank(string) or not rule_list: return None string = string.strip() for rule in rule_list: rule_type = rule.get('type').strip() if rule.has_key('type') else '' rule_type = 'regex' if rule_type == '' else rule_type pattern = rule.get('pattern') if rule.has_key('pattern') else '' if rule_type == 'regex': if regex_utils.check_line(pattern, string): value_list = regex_utils.parse_line(pattern, string) map_index = __build_map_index(rule) return __map_with_index(value_list, map_index) elif rule_type == 'split': match = rule.get('match') if rule.has_key('match') else '' if __is_match(match, string): value_list = string.split(pattern) map_index = __build_map_index(rule) return __map_with_index(value_list, map_index) elif rule_type == 'keyword': match = rule.get('match') if rule.has_key('match') else '' if __is_match(match, string): map_index = __build_map_index(rule) return __map_with_keyword(string, map_index.values()) return None
def __read_script(script, encoding='utf8'): file_reg = '\[(.*)\]' cmd_reg = '(\w+)\s*:\s*(.*)' tag_file = None cmd_set = {} if isinstance(script, str): content = text_file.read_file(script, 'all', encoding, True) elif isinstance(script, list): content = script elif isinstance(script, dict): return script else: raise IOError('script must be a filename or script command list') for line in content: if str_utils.is_empty(line): continue line = line.strip() if str_utils.startswith(line, '#'): continue if regex_utils.check_line(file_reg, line): tag_file = regex_utils.parse_line(file_reg, line)[0] if str_utils.is_not_empty(tag_file): tag_file = tag_file.strip() elif regex_utils.check_line(cmd_reg, line): cmd_item = regex_utils.parse_line(cmd_reg, line) if tag_file is not None and cmd_item is not None and len( cmd_item) == 2: cmd_list = cmd_set.get(tag_file) if cmd_set.has_key( tag_file) else [] cmd_list.append(cmd_item) cmd_set[tag_file] = cmd_list return cmd_set
def __is_match(match, line): result = False if isinstance(match, basestring): if regex_utils.check_line('(\S+)\((\S+)\)', match): fun, value = regex_utils.parse_line('(\S+)\((\S+)\)', match) if fun == 'startswith': result = line.startswith(value) elif fun == 'endswith': result = line.endswith(value) elif fun == 'in': result = value in line elif type(match) == types.FunctionType: try: result = match(line) except: result = False return result