Exemplo n.º 1
0
    def __init__(self, input_file, fullname='unknown', 
                 unparsed_file=None, parse_date=True,
                 add_hash='no',
                 **kw):
        """Initialize base parser.

        Parameters:

            input_file - File object (must support readline)

            fullname - For logging, the fully qualified name
                  for the logger (matches 'qualname' in the logging config).

            unparsed_file - File object to place records that caused
                  a parse exception

            parse_date - Whether to parse the ISO date to a number
                         or represent it as a string.

            **kw - Remaining keyword, value pairs are appended to each
                  line of the log. If the same keyword is in a
                  parsed result, the newer value takes precedence.
                  The exception to this is if the parser returns a string
                  instead of a dictionary, e.g. the 'bp' parser:
                  to avoid O(N*M) behavior where N is the number of
                  the keywords and M is the length of the output string,
                  duplicates are not checked.
        """
        if not input_file:
            raise ValueError("input file cannot be empty")
        DoesLogging.__init__(self, fullname)
        # common parameters
        self._add_hash = self.boolParam(add_hash)
        # rest of parameters
        self._infile = nlreadline.BufferedReadline(input_file)
        if hasattr(input_file, 'fileno'):
            self._fake_file = False
            self._infile_rlist = (input_file.fileno(),) # used in read_line
        else:
            # not a real file
            self._fake_file = True
            self._infile_rlist = ()
        try:
            self._offs = self._infile.tell()
        except IOError:
            self._offs = 0
        self._prev_len, self._saved_len = 0, 0
        self._saved = [ ]
        self._name = fullname
        self._ufile = unparsed_file
        self._header_values = { }
        self._parser = NLSimpleParser(parse_date=parse_date)
        # Constant to add to each record
        self._const_nvp = { }
        # add GUID in env, if present
        guid = nlapi.getGuid(create=False)
        if guid:
            self._const_nvp['guid'] = guid
        # add user-provided values (can override guid)
        self._const_nvp.update(kw)
        # cache string-valued version, will be empty string if kw == {}
        self._const_nvp_str = ' '.join(["%s=%s" % (k,v) 
                                        for k,v in self._const_nvp.items()])
        self.parse_date = parse_date
Exemplo n.º 2
0
    def __init__(self,
                 input_file,
                 fullname='unknown',
                 unparsed_file=None,
                 parse_date=True,
                 add_hash='no',
                 **kw):
        """Initialize base parser.

        Parameters:

            input_file - File object (must support readline)

            fullname - For logging, the fully qualified name
                  for the logger (matches 'qualname' in the logging config).

            unparsed_file - File object to place records that caused
                  a parse exception

            parse_date - Whether to parse the ISO date to a number
                         or represent it as a string.

            **kw - Remaining keyword, value pairs are appended to each
                  line of the log. If the same keyword is in a
                  parsed result, the newer value takes precedence.
                  The exception to this is if the parser returns a string
                  instead of a dictionary, e.g. the 'bp' parser:
                  to avoid O(N*M) behavior where N is the number of
                  the keywords and M is the length of the output string,
                  duplicates are not checked.
        """
        if not input_file:
            raise ValueError("input file cannot be empty")
        DoesLogging.__init__(self, fullname)
        # common parameters
        self._add_hash = self.boolParam(add_hash)
        # rest of parameters
        self._infile = nlreadline.BufferedReadline(input_file)
        if hasattr(input_file, 'fileno'):
            self._fake_file = False
            self._infile_rlist = (input_file.fileno(), )  # used in read_line
        else:
            # not a real file
            self._fake_file = True
            self._infile_rlist = ()
        try:
            self._offs = self._infile.tell()
        except IOError:
            self._offs = 0
        self._prev_len, self._saved_len = 0, 0
        self._saved = []
        self._name = fullname
        self._ufile = unparsed_file
        self._header_values = {}
        self._parser = NLSimpleParser(parse_date=parse_date)
        # Constant to add to each record
        self._const_nvp = {}
        # add GUID in env, if present
        guid = nlapi.getGuid(create=False)
        if guid:
            self._const_nvp['guid'] = guid
        # add user-provided values (can override guid)
        self._const_nvp.update(kw)
        # cache string-valued version, will be empty string if kw == {}
        self._const_nvp_str = ' '.join(
            ["%s=%s" % (k, v) for k, v in self._const_nvp.items()])
        self.parse_date = parse_date