Exemplo n.º 1
0
    def parse(self, text):
        """
        Parses the given configuration. Asserts that all required keys and
        no invalid keys were specified in the configuration.

        :param text: configuration to parse
        :type text: string or open file
        """
        if not hasattr(text, 'readline'):
            text = StringIO(text)
        ini_parser = SafeConfigParser()
        ini_parser.readfp(text)
        if 'tractor' in ini_parser.sections():
            found_req_keys = []
            invalid_keys = []
            for key, value in ini_parser.items('tractor'):
                if not key in self.KEYS:
                    invalid_keys.append(key)
                    continue
                if key in self.REQUIRED_KEYS:
                    found_req_keys.append(key)
                self.settings[key] = value
            #
            if invalid_keys:
                raise ParsingError('Found invalid keys in [tractor] '
                                   'config section: %s' % (invalid_keys,))
            diff = self.REQUIRED_KEYS.difference(found_req_keys)
            if diff:
                raise ParsingError('Not all required keys found in [tractor] '
                                   'config section (found: %s, missing: %s).'
                                    % (found_req_keys, list(diff)))
Exemplo n.º 2
0
    def _parse_opts(self):
        "The config file parser"
        # parse options
        self._sessions = self._parser.sections()
        for session in self._sessions:
            optkeys = self._parser.options(session)
            options = {}
            if 'methods' in optkeys:
                methods = [
                    el.strip(',')
                    for el in self._parser.get(session, 'methods').split()
                ]
                if not reduce(lambda res, i: res and i in optkeys, methods):
                    raise ParsingError('No options for one of the MVA methods')
            else:
                raise ParsingError('Mandatory field, methods, is absent.')
            for opt in optkeys:
                value = self._parser.get(session, opt)
                if opt.find('cut') == 0 or opt.find('wt') > 0:
                    # remove newlines from selection string
                    options[opt] = value.replace('\n', '')
                else:
                    options[opt] = [el.strip(',') for el in value.split()]
                    if opt.find('mappings') >= 0:
                        options[opt] = [m.split(':') for m in options[opt]]

            try:
                if options['xvalidate'][0].lower() in ['true', 'on', 'yes']:
                    options['xvalidate'] = True
                else:
                    options['xvalidate'] = False
            except KeyError:
                options['xvalidate'] = False

            # check if mandatory properties are present
            def _test_(key1, key2):
                if key1:
                    return key1
                mandatory = ['factory_opts', 'training_opts']
                return key1 in mandatory or key2 in mandatory

            if not reduce(_test_, options.keys()):
                raise ParsingError('Mandatory option field missing')

            # make & set TMVAconfig object
            session_conf = TMVAconfig(session)
            map(lambda kv: setattr(session_conf, kv[0], kv[1]),
                options.items())
            setattr(self, session, session_conf)
    def expandLoops(self):
        """
        If one or more options have the tag $loop{}, it will
        generate new parsers for product of all values in the loop.
        E.g. if option1 = $loop{[100,200]} and option2 = $loop{['a','b']},
        it will generate parsers with option1,option2 = (100,'a'), (100,'b'), (200,'a') and (200,'b')
        
        :return: List of parsers with the options set to the respective value defined by the loop
        """

        loopVars = []
        varValues = []
        #Collect all loop options
        for section in self.sections():
            for option in self.options(section):
                ret = self.get(section, option, raw=True)
                varSectLoop = re.findall(r'\$loop\{(.*)\}', ret)
                if not varSectLoop:
                    continue
                if len(varSectLoop) > 1:
                    raise ParsingError(
                        "Syntax error. Multiple loops found for option %s." %
                        option)
                loopStr = varSectLoop[0]
                try:
                    varList = eval(loopStr)
                except:
                    raise ParsingError("Could not evaluate loop %s" % loopStr)
                if not isinstance(varList, (list, numpy.ndarray, tuple)):
                    raise ParsingError(
                        "Loop expression %s did not generate a list" % loopStr)
                loopVars.append((section, option))
                varValues.append(varList)

        if not loopVars:
            return [self]

        logger.info(" Looping over variables:  " +
                    ", ".join(["%s:%s" % lvar for lvar in loopVars]))
        parserList = []
        for values in itertools.product(*varValues):
            newParser = ConfigParserExt()
            newParser.read_dict(self.toDict(raw=True))
            for i, v in enumerate(values):
                sect, opt = loopVars[i]
                newParser.set(sect, opt, str(v))
            parserList.append(newParser)

        return parserList
Exemplo n.º 4
0
    def read(self, *targs, **kwargs):
        result = SafeConfigParser.read(self, self.path)
        if not result:
            raise ParsingError('Could not parse %r' % (self.path))

        try:
            self.validate_config()
        except Exception as error:
            logger.exception(error)
            raise ConfigError(
                'Validation of configfile %r failed with error: %s.' %
                (self.path, error))

        logger.debug(
            'Parsed config at %r with resulting values:\n\n%s\n', self.path,
            pformat([(section, self.items(section))
                     for section in self.sections()]))
Exemplo n.º 5
0
    def _read(self, fp, fpname):
        """Parse a sectioned setup file.

        The sections in setup file contains a title line at the top,
        indicated by a name in square brackets (`[]'), plus key/value
        options lines, indicated by `name: value' format lines.
        Continuations are represented by an embedded newline then
        leading whitespace.  Blank lines, lines beginning with a '#',
        and just about everything else are ignored.
        """
        cursect = None                            # None, or a dictionary
        optname = None
        lineno = 0
        e = None                                  # None, or an exception
        while True:
            line = fp.readline()
            if not line:
                break
            lineno = lineno + 1
            # comment or blank line?
            if line.strip() == '' or line[0] in '#;':
                continue
            if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":
                # no leading whitespace
                continue
            # continuation line?
            if line[0].isspace() and cursect is not None and optname:
                value = line.strip()
                if value:
                    cursect[optname] = "%s\n%s" % (cursect[optname], value)
            # a section header or option header?
            else:
                # is it a section header?
                mo = self.SECTCRE.match(line)
                if mo:
                    sectname = mo.group('header')
                    if sectname in self._sections:
                        cursect = self._sections[sectname]
                    elif sectname == DEFAULTSECT:
                        cursect = self._defaults
                    else:
                        cursect = self._dict()
                        cursect['__name__'] = sectname
                        self._sections[sectname] = cursect
                        self.linenos[sectname]=lineno
                    # So sections can't start with a continuation line
                    optname = None
                # no section header in the file?
                elif cursect is None:
                    if not e:
                        e = ParsingError(fpname)
                    e.append(lineno, u'(Line outside section) '+line)
                    #raise MissingSectionHeaderError(fpname, lineno, line)
                # an option line?
                else:
                    mo = self.OPTCRE.match(line) # OPTCRE instead of
                                                 # _optcre so it works
                                                 # with python 2.6
                    if mo:
                        optname, vi, optval = mo.group('option', 'vi', 'value')
                        # This check is fine because the OPTCRE cannot
                        # match if it would set optval to None
                        if optval is not None:
                            if vi in ('=', ':') and ';' in optval:
                                # ';' is a comment delimiter only if it follows
                                # a spacing character
                                pos = optval.find(';')
                                if pos != -1 and optval[pos-1].isspace():
                                    optval = optval[:pos]
                            optval = optval.strip()
                        # allow empty values
                        if optval == '""':
                            optval = ''
                        optname = self.optionxform(optname.rstrip())
                        cursect[optname] = optval
                        self.linenos[cursect['__name__']+','+optname]=lineno                        
                    else:
                        # a non-fatal parsing error occurred.  set up the
                        # exception but keep going. the exception will be
                        # raised at the end of the file and will contain a
                        # list of all bogus lines
                        if not e:
                            e = ParsingError(fpname)
                        e.append(lineno, line)
        # if any parsing errors occurred, raise an exception
        if e:
            raise e
Exemplo n.º 6
0
    def _read(self, fp, fpname):
        cursect = None                        # None, or a dictionary
        optname = None
        lineno = 0
        e = None                              # None, or an exception
        while True:
            line = fp.readline()
            if not line:
                break
            lineno = lineno + 1
            # comment or blank line?
            if line.strip() == '' or line[0] in '#;':
                continue
            if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":
                # no leading whitespace
                continue
            # continuation line?
            if line[0].isspace() and cursect is not None and optname:
                value = line.strip()
                if value:
                    cursect[optname].append(value)
            # a section header or option header?
            else:
                # is it a section header?
                mo = self.SECTCRE.match(line)
                if mo:
                    sectname = mo.group('header')
                    if sectname in self._sections:
                        # we're extending/overriding, we're good
                        cursect = self._sections[sectname]
                    elif sectname == DEFAULTSECT:
                        cursect = self._defaults
                    else:
                        cursect = self._dict()
                        cursect['__name__'] = sectname
                        self._sections[sectname] = cursect
                    # So sections can't start with a continuation line
                    optname = None
                # no section header in the file?
                elif cursect is None:
                    raise MissingSectionHeaderError(fpname, lineno, line)
                # an option line?
                else:
                    try:
                        mo = self._optcre.match(line)   # 2.7
                    except AttributeError:
                        mo = self.OPTCRE.match(line)    # 2.6
                    if mo:
                        optname, vi, optval = mo.group('option', 'vi', 'value')
                        optname = self.optionxform(optname.rstrip())
                        # We don't want to override.
                        if optname in cursect:
                            continue
                        # This check is fine because the OPTCRE cannot
                        # match if it would set optval to None
                        if optval is not None:
                            if vi in ('=', ':') and ';' in optval:
                                # ';' is a comment delimiter only if it follows
                                # a spacing character
                                pos = optval.find(';')
                                if pos != -1 and optval[pos - 1].isspace():
                                    optval = optval[:pos]
                            optval = optval.strip()
                            # allow empty values
                            if optval == '""':
                                optval = ''
                            cursect[optname] = [optval]
                        else:
                            # valueless option handling
                            cursect[optname] = optval
                    else:
                        # a non-fatal parsing error occurred.  set up the
                        # exception but keep going. the exception will be
                        # raised at the end of the file and will contain a
                        # list of all bogus lines
                        if not e:
                            e = ParsingError(fpname)
                        e.append(lineno, repr(line))
        # if any parsing errors occurred, raise an exception
        if e:
            raise e

        # join the multi-line values collected while reading
        all_sections = [self._defaults]
        all_sections.extend(self._sections.values())
        for options in all_sections:
            for name, val in options.items():
                if isinstance(val, list):
                    options[name] = '\n'.join(val)
 def _read(self, fp, fpname):
     cursect = None  # None, or a dictionary
     optname = None
     lineno = 0
     e = None  # None, or an exception
     firstline = True
     while True:
         line = fp.readline()
         if not line:
             break
         lineno = lineno + 1
         if firstline:
             # Skip BOM
             if line[:3] == '\xef\xbb\xbf':
                 line = line[3:]
                 self.encoding = 'utf_8'
             else:
                 self.encoding = sys.getfilesystemencoding()
             firstline = False
         # comment or blank line?
         if line.strip() == '' or line[0] in '#;':
             continue
         if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":
             # no leading whitespace
             continue
         # continuation line?
         if line[0].isspace() and cursect is not None and optname:
             value = line.strip()
             if value:
                 cursect[optname] = "%s\n%s" % (cursect[optname],
                                                value.decode(self.encoding))
         # a section header or option header?
         else:
             # is it a section header?
             mo = self.SECTCRE.match(line)
             if mo:
                 sectname = mo.group('header')
                 if sectname in self._sections:
                     cursect = self._sections[sectname]
                 elif sectname == DEFAULTSECT:
                     cursect = self._defaults
                 else:
                     cursect = {'__name__': sectname}
                     self._sections[sectname] = cursect
                 # So sections can't start with a continuation line
                 optname = None
             # no section header in the file?
             elif cursect is None:
                 raise MissingSectionHeaderError(fpname, lineno, line)
             # an option line?
             else:
                 mo = self.OPTCRE.match(line)
                 if mo:
                     optname, vi, optval = mo.group('option', 'vi', 'value')
                     if vi in ('=', ':') and ';' in optval:
                         # ';' is a comment delimiter only if it follows
                         # a spacing character
                         pos = optval.find(';')
                         if pos != -1 and optval[pos - 1].isspace():
                             optval = optval[:pos]
                     optval = optval.strip()
                     # allow empty values
                     if optval == '""':
                         optval = ''
                     optname = self.optionxform(optname.rstrip())
                     try:
                         _opt = optval.decode(self.encoding)
                     except UnicodeDecodeError:
                         self.encoding = sys.getfilesystemencoding()
                         _opt = optval.decode(self.encoding)
                     cursect[optname] = _opt
                 else:
                     # a non-fatal parsing error occurred.  set up the
                     # exception but keep going. the exception will be
                     # raised at the end of the file and will contain a
                     # list of all bogus lines
                     if not e:
                         e = ParsingError(fpname)
                     e.append(lineno, repr(line))
     # if any parsing errors occurred, raise an exception
     if e:
         raise e
Exemplo n.º 8
0
    def _read(self, fp, fpname):
        """Parse a sectioned setup file.

        The sections in setup file contains a title line at the top,
        indicated by a name in square brackets (`[]'), plus key/value
        options lines, indicated by `name: value' format lines.
        Continuations are represented by an embedded newline then
        leading whitespace.  Blank lines, lines beginning with a '#',
        and just about everything else are ignored.
        """
        if self.main_section in self._sections:
            cursect = self._sections[self.main_section]
        else:
            cursect = self._dict()
            cursect['__name__'] = self.main_section
            self._sections[self.main_section] = cursect

        optname = None
        lineno = 0
        e = None                              # None, or an exception
        while True:
            line = fp.readline()
            if not line:
                break
            lineno = lineno + 1
            # comment or blank line?
            if line.strip() == '' or line[0] in '#;':
                continue
            if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":
                # no leading whitespace
                continue
            # continuation line?
            if line[0].isspace() and optname:
                value = line.strip()
                if value:
                    cursect[optname].append(value)
            # an option line?
            else:
                mo = self._optcre.match(line)
                if mo:
                    optname, vi, optval = mo.group('option', 'vi', 'value')
                    optname = self.optionxform(optname.rstrip())
                    # This check is fine because the OPTCRE cannot
                    # match if it would set optval to None
                    if optval is not None:
                        if vi in ('=', ':') and ';' in optval:
                            # ';' is a comment delimiter only if it follows
                            # a spacing character
                            pos = optval.find(';')
                            if pos != -1 and optval[pos-1].isspace():
                                optval = optval[:pos]
                        optval = optval.strip()
                        # allow empty values
                        if optval == '""':
                            optval = ''
                        self._handle_set_option(cursect, optname, optval)
                    else:
                        # valueless option handling
                        cursect[optname] = optval
                else:
                    # a non-fatal parsing error occurred.  set up the
                    # exception but keep going. the exception will be
                    # raised at the end of the file and will contain a
                    # list of all bogus lines
                    if not e:
                        e = ParsingError(fpname)
                    e.append(lineno, repr(line))
        # if any parsing errors occurred, raise an exception
        if e:
            raise e

        # join the multi-line values collected while reading
        all_sections = [self._defaults]
        all_sections.extend(self._sections.values())
        for options in all_sections:
            for name, val in options.items():
                if isinstance(val, list):
                    options[name] = '\n'.join(val)
Exemplo n.º 9
0
    def _readfp(self, fp):
        cur_section = None
        cur_option = None
        cur_section_name = None
        cur_option_name = None
        pending_lines = []
        pending_empty_lines = False
        try:
            fname = fp.name
        except AttributeError:
            fname = '<???>'
        linecount = 0
        exc = None
        line = None

        for line in readline_iterator(fp):
            # Check for BOM on first line
            if linecount == 0 and isinstance(line, unicode):
                if line[0] == u'\ufeff':
                    line = line[1:]
                    self._bom = True

            lineobj = self._parse(line)
            linecount += 1

            if not cur_section and not isinstance(lineobj,
                                (CommentLine, EmptyLine, SectionLine)):
                if self._parse_exc:
                    raise MissingSectionHeaderError(fname, linecount, line)
                else:
                    lineobj = make_comment(line)

            if lineobj is None:
                if self._parse_exc:
                    if exc is None: exc = ParsingError(fname)
                    exc.append(linecount, line)
                lineobj = make_comment(line)

            if isinstance(lineobj, ContinuationLine):
                if cur_option:
                    if pending_lines:
                        cur_option.extend(pending_lines)
                        pending_lines = []
                        if pending_empty_lines:
                            optobj._compat_skip_empty_lines.add(cur_option_name)
                            pending_empty_lines = False
                    cur_option.add(lineobj)
                else:
                    # illegal continuation line - convert to comment
                    if self._parse_exc:
                        if exc is None: exc = ParsingError(fname)
                        exc.append(linecount, line)
                    lineobj = make_comment(line)

            if isinstance(lineobj, OptionLine):
                if pending_lines:
                    cur_section.extend(pending_lines)
                    pending_lines = []
                    pending_empty_lines = False
                cur_option = LineContainer(lineobj)
                cur_section.add(cur_option)
                if self._optionxform:
                    cur_option_name = self._optionxform(cur_option.name)
                else:
                    cur_option_name = cur_option.name
                if cur_section_name == DEFAULTSECT:
                    optobj = self._defaults
                else:
                    optobj = self._sections[cur_section_name]
                optobj._options[cur_option_name] = cur_option

            if isinstance(lineobj, SectionLine):
                self._data.extend(pending_lines)
                pending_lines = []
                pending_empty_lines = False
                cur_section = LineContainer(lineobj)
                self._data.add(cur_section)
                cur_option = None
                cur_option_name = None
                if cur_section.name == DEFAULTSECT:
                    self._defaults._lines.append(cur_section)
                    cur_section_name = DEFAULTSECT
                else:
                    if self._sectionxform:
                        cur_section_name = self._sectionxform(cur_section.name)
                    else:
                        cur_section_name = cur_section.name
                    if cur_section_name not in self._sections:
                        self._sections[cur_section_name] = \
                                INISection(cur_section, defaults=self._defaults,
                                           optionxformsource=self)
                    else:
                        self._sections[cur_section_name]._lines.append(cur_section)

            if isinstance(lineobj, (CommentLine, EmptyLine)):
                pending_lines.append(lineobj)
                if isinstance(lineobj, EmptyLine):
                    pending_empty_lines = True

        self._data.extend(pending_lines)
        if line and line[-1]=='\n':
            self._data.add(EmptyLine())

        if exc:
            raise exc
Exemplo n.º 10
0
    def _read(self, fp, fpname):
        """Parse a sectioned setup file.

        The sections in setup file contains a title line at the top,
        indicated by a name in square brackets (`[]'), plus key/value
        options lines, indicated by `name: value' format lines.
        Continuations are represented by an embedded newline then
        leading whitespace.  Blank lines, lines beginning with a '#',
        and just about everything else are ignored.
        """
        cursect = None                        # None, or a dictionary
        optname = None
        lineno = 0
        e = None                              # None, or an exception
        while True:
            line = fp.readline()
            if not line:
                break
            lineno += 1
            # comment or blank line?
            if line.strip() == '' or line[0] in '#;':
                continue
            if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":
                # no leading whitespace
                continue
                # continuation line?
            if line[0].isspace() and cursect is not None and optname:
                value = line.strip()
                if value:
                    cursect[optname].append(value)
            # a section header or option header?
            else:
                # is it a section header?
                mo = self.SECTCRE.match(line)
                if mo:
                    sectname = mo.group('header')
                    if sectname in self._sections:
                        cursect = self._sections[sectname]
                    elif sectname == DEFAULTSECT:
                        cursect = self._defaults
                    else:
                        cursect = self._dict()
                        cursect['__name__'] = sectname
                        self._sections[sectname] = cursect
                        # So sections can't start with a continuation line
                    optname = None
                # no section header in the file?
                elif cursect is None:
                    raise MissingSectionHeaderError(fpname, lineno, line)
                # an option line?
                else:
                    if 'env' in cursect['__name__']:
                        mo = self._envcre.match(line)
                    else:
                        mo = self._optcre.match(line)
                    if mo:
                        optname, vi, optval = mo.group('option', 'vi', 'value')
                        optname = self.optionxform(optname.rstrip())
                        # This check is fine because the OPTCRE cannot
                        # match if it would set optval to None
                        if optval is not None:
                            optval = self._envvre.sub(',', optval).strip()
                            # allow empty values
                            if optval == '""':
                                optval = ''
                            cursect[optname] = [optval]
                        else:
                            # valueless option handling
                            cursect[optname] = optval
                    else:
                        # a non-fatal parsing error occurred.  set up the
                        # exception but keep going. the exception will be
                        # raised at the end of the file and will contain a
                        # list of all bogus lines
                        if not e:
                            e = ParsingError(fpname)
                        e.append(lineno, repr(line))
            # if any parsing errors occurred, raise an exception
        if e:
            raise e

        # join the multi-line values collected while reading
        all_sections = [self._defaults]
        all_sections.extend(self._sections.values())
        for options in all_sections:
            for name, val in options.items():
                if isinstance(val, list):
                    options[name] = '\n'.join(val)
    def _readfp(self, fp):
        cur_section = None
        cur_option = None
        cur_section_name = None
        cur_option_name = None
        pending_lines = []
        pending_empty_lines = False
        try:
            fname = fp.name
        except AttributeError:
            fname = '<???>'
        linecount = 0
        exc = None
        line = None

        for line in readline_iterator(fp):
            # Check for BOM on first line
            if linecount == 0 and isinstance(line, unicode):
                if line[0] == u'\ufeff':
                    line = line[1:]
                    self._bom = True

            lineobj = self._parse(line)
            linecount += 1

            if not cur_section and not isinstance(
                    lineobj, (CommentLine, EmptyLine, SectionLine)):
                if self._parse_exc:
                    raise MissingSectionHeaderError(fname, linecount, line)
                else:
                    lineobj = make_comment(line)

            if lineobj is None:
                if self._parse_exc:
                    if exc is None: exc = ParsingError(fname)
                    exc.append(linecount, line)
                lineobj = make_comment(line)

            if isinstance(lineobj, ContinuationLine):
                if cur_option:
                    if pending_lines:
                        cur_option.extend(pending_lines)
                        pending_lines = []
                        if pending_empty_lines:
                            optobj._compat_skip_empty_lines.add(
                                cur_option_name)
                            pending_empty_lines = False
                    cur_option.add(lineobj)
                else:
                    # illegal continuation line - convert to comment
                    if self._parse_exc:
                        if exc is None: exc = ParsingError(fname)
                        exc.append(linecount, line)
                    lineobj = make_comment(line)

            if isinstance(lineobj, OptionLine):
                if pending_lines:
                    cur_section.extend(pending_lines)
                    pending_lines = []
                    pending_empty_lines = False
                cur_option = LineContainer(lineobj)
                cur_section.add(cur_option)
                if self._optionxform:
                    cur_option_name = self._optionxform(cur_option.name)
                else:
                    cur_option_name = cur_option.name
                if cur_section_name == DEFAULTSECT:
                    optobj = self._defaults
                else:
                    optobj = self._sections[cur_section_name]
                optobj._options[cur_option_name] = cur_option

            if isinstance(lineobj, SectionLine):
                self._data.extend(pending_lines)
                pending_lines = []
                pending_empty_lines = False
                cur_section = LineContainer(lineobj)
                self._data.add(cur_section)
                cur_option = None
                cur_option_name = None
                if cur_section.name == DEFAULTSECT:
                    self._defaults._lines.append(cur_section)
                    cur_section_name = DEFAULTSECT
                else:
                    if self._sectionxform:
                        cur_section_name = self._sectionxform(cur_section.name)
                    else:
                        cur_section_name = cur_section.name
                    if cur_section_name not in self._sections:
                        self._sections[cur_section_name] = \
                                INISection(cur_section, defaults=self._defaults,
                                           optionxformsource=self)
                    else:
                        self._sections[cur_section_name]._lines.append(
                            cur_section)

            if isinstance(lineobj, (CommentLine, EmptyLine)):
                pending_lines.append(lineobj)
                if isinstance(lineobj, EmptyLine):
                    pending_empty_lines = True

        self._data.extend(pending_lines)
        if line and line[-1] == '\n':
            self._data.add(EmptyLine())

        if exc:
            raise exc
Exemplo n.º 12
0
    def readfp(self, fp):
        cur_section = None
        cur_option = None
        cur_section_name = None
        cur_option_name = None
        pending_lines = []
        try:
            fname = fp.name
        except AttributeError:
            fname = '<???>'
        linecount = 0
        exc = None
        for line in fp:
            lineobj = self._parse(line)
            linecount += 1

            if not cur_section and not isinstance(lineobj,
                                (comment_line, empty_line, section_line)):
                if self._parse_exc:
                    raise MissingSectionHeaderError(fname, linecount, line)
                else:
                    lineobj = make_comment(line)

            if lineobj is None:
                if self._parse_exc:
                    if exc is None: exc = ParsingError(fname)
                    exc.append(linecount, line)
                lineobj = make_comment(line)

            if isinstance(lineobj, continuation_line):
                if cur_option:
                    cur_option.add(lineobj)
                else:
                    # illegal continuation line - convert to comment
                    if self._parse_exc:
                        if exc is None: exc = ParsingError(fname)
                        exc.append(linecount, line)
                    lineobj = make_comment(line)
            else:
                cur_option = None
                cur_option_name = None

            if isinstance(lineobj, option_line):
                cur_section.extend(pending_lines)
                pending_lines = []
                cur_option = line_container(lineobj)
                cur_section.add(cur_option)
                if self._optionxform:
                    cur_option_name = self._optionxform(cur_option.name)
                else:
                    cur_option_name = cur_option.name
                if cur_section_name == DEFAULTSECT:
                    optobj = self._defaults
                else:
                    optobj = self._sections[cur_section_name]
                optobj._options[cur_option_name] = cur_option

            if isinstance(lineobj, section_line):
                self._data.extend(pending_lines)
                pending_lines = []
                cur_section = line_container(lineobj)
                self._data.add(cur_section)
                if cur_section.name == DEFAULTSECT:
                    self._defaults._lines.append(cur_section)
                    cur_section_name = DEFAULTSECT
                else:
                    if self._sectionxform:
                        cur_section_name = self._sectionxform(cur_section.name)
                    else:
                        cur_section_name = cur_section.name
                    if not self._sections.has_key(cur_section_name):
                        self._sections[cur_section_name] = \
                                section(cur_section, defaults=self._defaults,
                                        optionxform=self._optionxform)
                    else:
                        self._sections[cur_section_name]._lines.append(cur_section)

            if isinstance(lineobj, (comment_line, empty_line)):
                pending_lines.append(lineobj)

        self._data.extend(pending_lines)
        if line and line[-1]=='\n':
            self._data.add(empty_line())

        if exc:
            raise exc
    def _read(self, fp, fpname):
        cursect = None
        optname = None
        lineno = 0
        e = None
        firstline = True
        while True:
            line = fp.readline()
            if not line:
                break
            lineno = lineno + 1
            if firstline:
                if line[:3] == '\xef\xbb\xbf':
                    line = line[3:]
                    self.encoding = 'utf_8'
                else:
                    self.encoding = sys.getfilesystemencoding()
                    if self.encoding is None:
                        self.encoding = 'utf_8'
                firstline = False
            if line.strip() == '' or line[0] in '#;':
                continue
            if line.split(None, 1)[0].lower() == 'rem' and line[0] in 'rR':
                continue
            if line[0].isspace() and cursect is not None and optname:
                value = line.strip()
                if value:
                    cursect[optname] = '%s\n%s' % (cursect[optname], value.decode(self.encoding))
            else:
                mo = self.SECTCRE.match(line)
                if mo:
                    sectname = mo.group('header')
                    if sectname in self._sections:
                        cursect = self._sections[sectname]
                    elif sectname == DEFAULTSECT:
                        cursect = self._defaults
                    else:
                        cursect = {'__name__': sectname}
                        self._sections[sectname] = cursect
                    optname = None
                elif cursect is None:
                    raise MissingSectionHeaderError(fpname, lineno, line)
                else:
                    mo = self.OPTCRE.match(line)
                    if mo:
                        optname, vi, optval = mo.group('option', 'vi', 'value')
                        if vi in ('=', ':') and ';' in optval:
                            pos = optval.find(';')
                            if pos != -1 and optval[pos - 1].isspace():
                                optval = optval[:pos]
                        optval = optval.strip()
                        if optval == '""':
                            optval = ''
                        optname = self.optionxform(optname.rstrip())
                        try:
                            _opt = optval.decode(self.encoding)
                        except UnicodeDecodeError:
                            self.encoding = sys.getfilesystemencoding()
                            if self.encoding is None:
                                self.encoding = 'utf_8'
                            _opt = optval.decode(self.encoding)

                        cursect[optname] = _opt
                    else:
                        if not e:
                            e = ParsingError(fpname)
                        e.append(lineno, repr(line))

        if e:
            raise e
Exemplo n.º 14
0
    def _read(self, fp, fpname):
        cursect = None
        optname = None
        lineno = 0
        e = None
        firstline = True
        while True:
            line = fp.readline()
            if not line:
                break
            lineno = lineno + 1
            if firstline:
                if line[:3] == '\xef\xbb\xbf':
                    line = line[3:]
                    self.encoding = 'utf_8'
                else:
                    self.encoding = sys.getfilesystemencoding()
                    if self.encoding is None:
                        self.encoding = 'utf_8'
                firstline = False
            if line.strip() == '' or line[0] in '#;':
                continue
            if line.split(None, 1)[0].lower() == 'rem' and line[0] in 'rR':
                continue
            if line[0].isspace() and cursect is not None and optname:
                value = line.strip()
                if value:
                    cursect[optname] = '%s\n%s' % (cursect[optname],
                                                   value.decode(self.encoding))
            else:
                mo = self.SECTCRE.match(line)
                if mo:
                    sectname = mo.group('header')
                    if sectname in self._sections:
                        cursect = self._sections[sectname]
                    elif sectname == DEFAULTSECT:
                        cursect = self._defaults
                    else:
                        cursect = {'__name__': sectname}
                        self._sections[sectname] = cursect
                    optname = None
                elif cursect is None:
                    raise MissingSectionHeaderError(fpname, lineno, line)
                else:
                    mo = self.OPTCRE.match(line)
                    if mo:
                        optname, vi, optval = mo.group('option', 'vi', 'value')
                        if vi in ('=', ':') and ';' in optval:
                            pos = optval.find(';')
                            if pos != -1 and optval[pos - 1].isspace():
                                optval = optval[:pos]
                        optval = optval.strip()
                        if optval == '""':
                            optval = ''
                        optname = self.optionxform(optname.rstrip())
                        try:
                            _opt = optval.decode(self.encoding)
                        except UnicodeDecodeError:
                            self.encoding = sys.getfilesystemencoding()
                            if self.encoding is None:
                                self.encoding = 'utf_8'
                            _opt = optval.decode(self.encoding)

                        cursect[optname] = _opt
                    else:
                        if not e:
                            e = ParsingError(fpname)
                        e.append(lineno, repr(line))

        if e:
            raise e
Exemplo n.º 15
0
    def readfp(self, fp):
        cur_section = None
        cur_option = None
        cur_section_name = None
        cur_option_name = None
        line = None
        pending_lines = []
        try:
            fname = fp.name
        except AttributeError:
            fname = '<???>'
        linecount = 0
        exc = None
        for line in fp:
            lineobj = self._parse(line)
            linecount += 1

            if not cur_section and not isinstance(lineobj,
                                (comment_line, empty_line, section_line)):
                if self._parse_exc:
                    raise MissingSectionHeaderError(fname, linecount, line)
                else:
                    lineobj = make_comment(line)

            if lineobj is None:
                if self._parse_exc:
                    if exc is None: exc = ParsingError(fname)
                    exc.append(linecount, line)
                lineobj = make_comment(line)

            if isinstance(lineobj, continuation_line):
                if cur_option:
                    cur_option.add(lineobj)
                else:
                    # illegal continuation line - convert to comment
                    if self._parse_exc:
                        if exc is None: exc = ParsingError(fname)
                        exc.append(linecount, line)
                    lineobj = make_comment(line)
            else:
                cur_option = None
                cur_option_name = None

            if isinstance(lineobj, option_line):
                cur_section.extend(pending_lines)
                pending_lines = []
                cur_option = line_container(lineobj)
                cur_section.add(cur_option)
                if self._optionxform:
                    cur_option_name = self._optionxform(cur_option.name)
                else:
                    cur_option_name = cur_option.name
                if cur_section_name == DEFAULTSECT:
                    optobj = self._defaults
                else:
                    optobj = self._sections[cur_section_name]
                optobj._options[cur_option_name] = cur_option

            if isinstance(lineobj, section_line):
                self._data.extend(pending_lines)
                pending_lines = []
                cur_section = line_container(lineobj)
                self._data.add(cur_section)
                if cur_section.name == DEFAULTSECT:
                    self._defaults._lines.append(cur_section)
                    cur_section_name = DEFAULTSECT
                else:
                    if self._sectionxform:
                        cur_section_name = self._sectionxform(cur_section.name)
                    else:
                        cur_section_name = cur_section.name
                    if not self._sections.has_key(cur_section_name):
                        self._sections[cur_section_name] = \
                                section(cur_section, defaults=self._defaults,
                                        optionxform=self._optionxform)
                    else:
                        self._sections[cur_section_name]._lines.append(cur_section)

            if isinstance(lineobj, (comment_line, empty_line)):
                pending_lines.append(lineobj)

        self._data.extend(pending_lines)
        if line and line[-1]=='\n':
            self._data.add(empty_line())

        if exc:
            raise exc
Exemplo n.º 16
0
    def _read(self, fp, fpname):
        cursect = None  # None, or a dictionary
        optname = None
        lineno = 0
        e = None  # None, or an exception
        while True:
            line = fp.readline()
            if not line:
                break
            lineno = lineno + 1
            # comment or blank line?
            if line.strip() == '' or line[0] in '#;':
                continue
            if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":
                # no leading whitespace
                continue
            # continuation line?
            if line[0].isspace() and cursect is not None and optname:
                value = line.strip()
                if value:
                    cursect[optname].append(value)
            # a section header or option header?
            else:
                # is it a section header?
                mo = self.SECTCRE.match(line)
                if mo:
                    sectname = mo.group('header')
                    if sectname in self._sections:
                        raise ValueError('Duplicate section %r' % sectname)
                    elif sectname == DEFAULTSECT:
                        cursect = self._defaults
                    else:
                        cursect = self._dict()
                        cursect['__name__'] = sectname
                        self._sections[sectname] = cursect
                    # So sections can't start with a continuation line
                    optname = None
                # no section header in the file?
                elif cursect is None:
                    raise MissingSectionHeaderError(fpname, lineno, line)
                # an option line?
                else:
                    try:
                        mo = self._optcre.match(line)  # 2.7
                    except AttributeError:
                        mo = self.OPTCRE.match(line)  # 2.6
                    if mo:
                        optname, vi, optval = mo.group('option', 'vi', 'value')
                        optname = self.optionxform(optname.rstrip())
                        # This check is fine because the OPTCRE cannot
                        # match if it would set optval to None
                        if optval is not None:
                            if vi in ('=', ':') and ';' in optval:
                                # ';' is a comment delimiter only if it follows
                                # a spacing character
                                pos = optval.find(';')
                                if pos != -1 and optval[pos - 1].isspace():
                                    optval = optval[:pos]
                            optval = optval.strip()
                            # allow empty values
                            if optval == '""':
                                optval = ''
                            cursect[optname] = [optval]
                        else:
                            # valueless option handling
                            cursect[optname] = optval
                    else:
                        # a non-fatal parsing error occurred.  set up the
                        # exception but keep going. the exception will be
                        # raised at the end of the file and will contain a
                        # list of all bogus lines
                        if not e:
                            e = ParsingError(fpname)
                        e.append(lineno, repr(line))
        # if any parsing errors occurred, raise an exception
        if e:
            raise e

        # join the multi-line values collected while reading
        all_sections = [self._defaults]
        all_sections.extend(self._sections.values())
        for options in all_sections:
            for name, val in options.items():
                if isinstance(val, list):
                    options[name] = '\n'.join(val)
Exemplo n.º 17
0
    def _read(self, fp, fpname):
        """Parse a sectioned setup file.

        The sections in setup file contains a title line at the top,
        indicated by a name in square brackets (`[]'), plus key/value
        options lines, indicated by `name: value' format lines.
        Continuations are represented by an embedded newline then
        leading whitespace.  Blank lines, lines beginning with a '#',
        and just about everything else are ignored.
        """
        cursect = None  # None, or a dictionary
        optname = None
        lineno = 0
        e = None  # None, or an exception
        while True:
            line = fp.readline()
            if not line:
                break
            lineno = lineno + 1
            # comment or blank line?
            if line.strip() == '' or line[0] in '#;':
                continue
            if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":
                # no leading whitespace
                continue
            # continuation line?
            if line[0].isspace() and cursect is not None and optname:
                value = line.strip()
                if value:
                    cursect[optname] = "%s\n%s" % (cursect[optname], value)
            # a section header or option header?
            else:
                # is it a section header?
                mo = self.SECTCRE.match(line)
                if mo:
                    sectname = mo.group('header')
                    if sectname in self._sections:
                        cursect = self._sections[sectname]
                    elif sectname == DEFAULTSECT:
                        cursect = self._defaults
                    else:
                        cursect = self._dict()
                        cursect['__name__'] = sectname
                        self._sections[sectname] = cursect
                        self.linenos[sectname] = lineno
                    # So sections can't start with a continuation line
                    optname = None
                # no section header in the file?
                elif cursect is None:
                    if not e:
                        e = ParsingError(fpname)
                    e.append(lineno, u'(Line outside section) ' + line)
                    #raise MissingSectionHeaderError(fpname, lineno, line)
                # an option line?
                else:
                    mo = self.OPTCRE.match(line)  # OPTCRE instead of
                    # _optcre so it works
                    # with python 2.6
                    if mo:
                        optname, vi, optval = mo.group('option', 'vi', 'value')
                        # This check is fine because the OPTCRE cannot
                        # match if it would set optval to None
                        if optval is not None:
                            if vi in ('=', ':') and ';' in optval:
                                # ';' is a comment delimiter only if it follows
                                # a spacing character
                                pos = optval.find(';')
                                if pos != -1 and optval[pos - 1].isspace():
                                    optval = optval[:pos]
                            optval = optval.strip()
                        # allow empty values
                        if optval == '""':
                            optval = ''
                        optname = self.optionxform(optname.rstrip())
                        cursect[optname] = optval
                        self.linenos[cursect['__name__'] + ',' +
                                     optname] = lineno
                    else:
                        # a non-fatal parsing error occurred.  set up the
                        # exception but keep going. the exception will be
                        # raised at the end of the file and will contain a
                        # list of all bogus lines
                        if not e:
                            e = ParsingError(fpname)
                        e.append(lineno, line)
        # if any parsing errors occurred, raise an exception
        if e:
            raise e
Exemplo n.º 18
0
 def _read(self, fp, fpname):
     cursect = None                            # None, or a dictionary
     optname = None
     lineno = 0
     e = None                                  # None, or an exception
     firstline = True            
     while True:
         line = fp.readline()
         if not line:
             break
         lineno = lineno + 1
         if firstline:
             # Skip BOM
             if line[:3] == '\xef\xbb\xbf':
                 line = line[3:]
                 self.encoding = 'utf_8'
             else:
                 self.encoding = sys.getfilesystemencoding()
             firstline = False
         # comment or blank line?
         if line.strip() == '' or line[0] in '#;':
             continue
         if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":
             # no leading whitespace
             continue
         # continuation line?
         if line[0].isspace() and cursect is not None and optname:
             value = line.strip()
             if value:
                 cursect[optname] = "%s\n%s" % (cursect[optname], value.decode(self.encoding))
         # a section header or option header?
         else:
             # is it a section header?
             mo = self.SECTCRE.match(line)
             if mo:
                 sectname = mo.group('header')
                 if sectname in self._sections:
                     cursect = self._sections[sectname]
                 elif sectname == DEFAULTSECT:
                     cursect = self._defaults
                 else:
                     cursect = {'__name__': sectname}
                     self._sections[sectname] = cursect
                 # So sections can't start with a continuation line
                 optname = None
             # no section header in the file?
             elif cursect is None:
                 raise MissingSectionHeaderError(fpname, lineno, line)
             # an option line?
             else:
                 mo = self.OPTCRE.match(line)
                 if mo:
                     optname, vi, optval = mo.group('option', 'vi', 'value')
                     if vi in ('=', ':') and ';' in optval:
                         # ';' is a comment delimiter only if it follows
                         # a spacing character
                         pos = optval.find(';')
                         if pos != -1 and optval[pos-1].isspace():
                             optval = optval[:pos]
                     optval = optval.strip()
                     # allow empty values
                     if optval == '""':
                         optval = ''
                     optname = self.optionxform(optname.rstrip())
                     try:
                         _opt = optval.decode(self.encoding)
                     except UnicodeDecodeError:
                         self.encoding = sys.getfilesystemencoding()
                         _opt = optval.decode(self.encoding)
                     cursect[optname] = _opt
                 else:
                     # a non-fatal parsing error occurred.  set up the
                     # exception but keep going. the exception will be
                     # raised at the end of the file and will contain a
                     # list of all bogus lines
                     if not e:
                         e = ParsingError(fpname)
                     e.append(lineno, repr(line))
     # if any parsing errors occurred, raise an exception
     if e:
         raise e