loganalyzer.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:sonic-mgmt 作者: Azure 项目源码 文件源码
def create_msg_regex(self, file_lsit):
        '''
        @summary: This method reads input file containing list of regular expressions
                  to be matched against.

        @param file_list : List of file paths, contains search expressions.

        @return: A regex class instance, corresponding to loaded regex expressions.
            Will be used for matching operations by callers.
        '''
        messages_regex = []

        if file_lsit is None or (0 == len(file_lsit)):
            return None

        for filename in file_lsit:
            self.print_diagnostic_message('processing match file:%s' % filename)
            with open(filename, 'rb') as csvfile:
                csvreader = csv.reader(csvfile, quotechar='"', delimiter=',',
                                       skipinitialspace=True)

                for index, row in enumerate(csvreader):
                    self.print_diagnostic_message('[diagnostic]:processing row:%d' % index)
                    self.print_diagnostic_message('row:%s'% row)
                    try:
                        #-- Ignore commented Lines and Empty Lines
                        if (not row or row[0].startswith(comment_key)):
                            self.print_diagnostic_message('[diagnostic]:skipping row[0]:%s' % row[0])
                            continue

                        #-- ('s' | 'r') = (Raw String | Regular Expression)
                        is_regex = row[0]
                        if ('s' == row[0]):
                            is_regex = False
                        elif ('r' == row[0]):
                            is_regex = True
                        else:
                            raise Exception('file:%s, malformed line:%d. '
                                            'must be \'s\'(string) or \'r\'(regex)'
                                            %(filename,index))

                        #-- One error message per line
                        error_string = row[1]

                        if (is_regex):
                            messages_regex.append(error_string)
                        else:
                            messages_regex.append(self.error_to_regx(error_string))

                    except Exception as e:
                        print 'ERROR: line %d is formatted incorrectly in file %s. Skipping line' % (index, filename)
                        print repr(e)
                        sys.exit(err_invalid_string_format)

        if (len(messages_regex)):
            regex = re.compile('|'.join(messages_regex))
        else:
            regex = None
        return regex, messages_regex
    #---------------------------------------------------------------------
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号