Пример #1
0
    def parse(self, stream):
        # see ConfigFileParser.parse docstring
        yaml = self._load_yaml()

        try:
            parsed_obj = yaml.load(stream, Loader=self.loader)
        except Exception as e:
            raise cap.ConfigFileParserException(
                "Couldn't parse config file: %s" % e)

        if not isinstance(parsed_obj, dict):
            raise cap.ConfigFileParserException(
                "The config file doesn't appear to "
                "contain 'key: value' pairs (aka. a YAML mapping). "
                "yaml.load('%s') returned type '%s' instead of 'dict'." %
                (getattr(stream, 'name', 'stream'), type(parsed_obj).__name__))

        result = OrderedDict()
        for key, value in parsed_obj.items():
            if isinstance(value, list):
                result[key] = value
            elif value is None:
                pass
            else:
                result[key] = str(value)

        return result
Пример #2
0
    def _parse_section(self, parsed_obj, section, stream):
        try:
            if not isinstance(
                    parsed_obj[section],
                (
                    collections.abc.Mapping,
                    collections.abc.MutableMapping,
                ),
            ):

                raise configargparse.ConfigFileParserException(
                    "The config file doesn't appear to "
                    "contain 'key: value' pairs (aka. a YAML mapping). "
                    "yaml.load('%s') returned type '%s' instead of "
                    "type 'dict' in section %r." % (
                        getattr(stream, "name", "stream"),
                        type(parsed_obj).__name__,
                        section,
                    ))

            # interpolate environment variables
            interpolated = interpolate_environment_variables(
                parsed_obj, os.environ, section)

        except KeyError:
            return {}
        else:
            return interpolated[section]
Пример #3
0
    def parse(self, stream):
        try:
            parsed_obj = yaml.safe_load(stream)
        except Exception as e:
            raise configargparse.ConfigFileParserException("Couldn't parse config file: %s" % e)

        config = parsed_obj.get('configuration', {})

        return super().parse(yaml.safe_dump(config))
Пример #4
0
    def parse(self, stream):
        yaml = self._load_yaml()

        try:
            parsed_obj = yaml.safe_load(stream)
        except Exception as e:
            raise configargparse.ConfigFileParserException(
                "Couldn't parse config file: %s" % e)

        if not isinstance(parsed_obj, dict):
            raise configargparse.ConfigFileParserException(
                "The config file doesn't appear to "
                "contain 'key: value' pairs (aka. a YAML mapping). "
                "yaml.load('%s') returned type '%s' instead of type 'dict'." %
                (
                    getattr(stream, "name", "stream"),
                    type(parsed_obj).__name__,
                ))

        if isinstance(self._sections, str):
            parsed_obj = self._parse_section(parsed_obj, stream,
                                             self._sections)
        elif isinstance(self._sections, (list, tuple)):
            _maps = [
                self._parse_section(parsed_obj, stream, s)
                for s in reversed(self._sections)
            ]

            parsed_obj = collections.ChainMap(*_maps)
        else:
            parsed_obj = interpolate_environment_variables(
                parsed_obj, os.environ)

        result = collections.OrderedDict()
        for key, value in parsed_obj.items():
            if isinstance(value, list):
                result[key] = value
            else:
                result[key] = str(value)

        return result
Пример #5
0
    def parse(self, stream):
        """Parses the keys + values from a config file."""

        items = dict()
        numbers = dict()
        comments = dict()
        inline_comments = dict()
        for ii, line in enumerate(stream):
            line = line.strip()
            if not line:
                continue
            if line[0] in ["#", ";", "["] or line.startswith("---"):
                comments[ii] = line
                continue
            if len(line.split("#")) > 1:
                inline_comments[ii] = "  #" + "#".join(line.split("#")[1:])
                line = line.split("#")[0]
            white_space = "\\s*"
            key = r"(?P<key>[^:=;#\s]+?)"
            value = white_space + r"[:=\s]" + white_space + "(?P<value>.+?)"
            comment = white_space + "(?P<comment>\\s[;#].*)?"

            key_only_match = re.match("^" + key + comment + "$", line)
            if key_only_match:
                key = HyphenStr(key_only_match.group("key"))
                items[key] = "true"
                numbers[key] = ii
                continue

            key_value_match = re.match("^" + key + value + comment + "$", line)
            if key_value_match:
                key = HyphenStr(key_value_match.group("key"))
                value = key_value_match.group("value")

                if value.startswith("[") and value.endswith("]"):
                    # handle special case of lists
                    value = [elem.strip() for elem in value[1:-1].split(",")]

                items[key] = value
                numbers[key] = ii
                continue

            raise configargparse.ConfigFileParserException(
                f"Unexpected line {ii} in {getattr(stream, 'name', 'stream')}: {line}"
            )

        items = self.reconstruct_multiline_dictionary(items)
        return items, numbers, comments, inline_comments
Пример #6
0
    def parse(self, stream):
        """Parses the keys + values from a config file."""

        # Pre-process lines to put multi-line dicts on single lines
        lines = list(stream)  # Turn into a list
        lines = [line.strip(" ")
                 for line in lines]  # Strip trailing white space
        first_chars = [line[0]
                       for line in lines]  # Pull out the first character

        ii = 0
        lines_repacked = []
        while ii < len(lines):
            if first_chars[ii] == "#":
                lines_repacked.append(lines[ii])
                ii += 1
            else:
                # Find the next comment
                jj = ii + 1
                while jj < len(first_chars) and first_chars[jj] != "#":
                    jj += 1

                int_lines = "".join(lines[ii:jj])  # Form single string
                int_lines = int_lines.replace(",\n#", ", \n#")  #
                int_lines = int_lines.replace(
                    ",\n", ", ")  # Multiline args on single lines
                int_lines = int_lines.replace(
                    "\n}\n", "}\n")  # Trailing } on single lines
                int_lines = int_lines.split("\n")
                lines_repacked += int_lines
                ii = jj

        # items is where we store the key-value pairs read in from the config
        # we use a DuplicateErrorDict so that an error is raised on duplicate
        # entries (e.g., if the user has two identical keys)
        items = DuplicateErrorDict()
        numbers = dict()
        comments = dict()
        inline_comments = dict()
        for ii, line in enumerate(lines_repacked):
            line = line.strip()
            if not line:
                continue
            if line[0] in ["#", ";", "["] or line.startswith("---"):
                comments[ii] = line
                continue
            if len(line.split("#")) > 1:
                inline_comments[ii] = "  #" + "#".join(line.split("#")[1:])
                line = line.split("#")[0]
            white_space = "\\s*"
            key = r"(?P<key>[^:=;#\s]+?)"
            value = white_space + r"[:=\s]" + white_space + "(?P<value>.+?)"
            comment = white_space + "(?P<comment>\\s[;#].*)?"

            key_only_match = re.match("^" + key + comment + "$", line)
            if key_only_match:
                key = HyphenStr(key_only_match.group("key"))
                items[key] = "true"
                numbers[key] = ii
                continue

            key_value_match = re.match("^" + key + value + comment + "$", line)
            if key_value_match:
                key = HyphenStr(key_value_match.group("key"))
                value = key_value_match.group("value")

                if value.startswith("[") and value.endswith("]"):
                    # handle special case of lists
                    value = [elem.strip() for elem in value[1:-1].split(",")]

                items[key] = value
                numbers[key] = ii
                continue

            raise configargparse.ConfigFileParserException(
                f"Unexpected line {ii} in {getattr(stream, 'name', 'stream')}: {line}"
            )

        items = self.reconstruct_multiline_dictionary(items)
        return items, numbers, comments, inline_comments