def multi_config(monkeypatch):
    description = None

    file_1 = ("{\n"
              "    \"section_A\": [\n"
              "       {\n"
              "           \"prop_a\": \"val_a\",\n"
              "           \"prop_b\": \"val_b\"\n"
              "       },\n"
              "       {\n"
              "           \"prop_c\": \"val_c\",\n"
              "           \"prop_d\": \"val_d\"\n"
              "       }\n"
              "    ],\n"
              "    \"section_B\": [\n"
              "       {\n"
              "           \"prop_e\": \"val_e\",\n"
              "           \"prop_f\": \"val_f\"\n"
              "       },\n"
              "       {\n"
              "           \"prop_g\": \"val_g\",\n"
              "           \"prop_h\": \"val_h\"\n"
              "       }\n"
              "    ]\n"
              "}\n")
    file_2 = ("{\n"
              "    \"section_B\": [\n"
              "       {\n"
              "           \"prop_i\": \"val_i\",\n"
              "           \"prop_j\": \"val_j\"\n"
              "       }\n"
              "    ],\n"
              "    \"section_C\": [\n"
              "       {\n"
              "           \"prop_k\": \"val_k\",\n"
              "           \"prop_l\": \"val_l\"\n"
              "       }\n"
              "    ]\n"
              "}\n")

    monkeypatch.setattr(__builtin__, 'open', mock_open(read_data=file_1))
    parser = DescriptionParser()
    description = parser.read("mock_1.json")
    monkeypatch.undo()

    monkeypatch.setattr(__builtin__, 'open', mock_open(read_data=file_2))
    parser = DescriptionParser()
    parser.read("mock_2.json", description)

    return description
Пример #2
0
def pyconfig():
    file_1 = ("{\n"
              "    \"section_A\": [\n"
              "       {\n"
              "           \"prop_a\": \"val_a\",\n"
              "           \"prop_b\": \"val_b\"\n"
              "       },\n"
              "       {\n"
              "           \"prop_c\": \"val_c\",\n"
              "           \"prop_d\": \"val_d\"\n"
              "       }\n"
              "    ],\n"
              "    \"section_B\": [\n"
              "       {\n"
              "           \"prop_e\": \"val_e\",\n"
              "           \"prop_f\": \"val_f\"\n"
              "       },\n"
              "       {\n"
              "           \"prop_g\": \"val_g\",\n"
              "           \"prop_h\": \"val_h\"\n"
              "       }\n"
              "    ]\n"
              "}\n")
    file_2 = ("{\n"
              "    \"section_B\": [\n"
              "       {\n"
              "           \"prop_i\": \"val_i\",\n"
              "           \"prop_j\": \"val_j\"\n"
              "       }\n"
              "    ],\n"
              "    \"section_C\": [\n"
              "       {\n"
              "           \"prop_k\": \"val_k\",\n"
              "           \"prop_l\": \"val_l\"\n"
              "       }\n"
              "    ]\n"
              "}\n")

    with patch(builtins.__name__ + ".open", mock_open(read_data=file_1)):
        parser = DescriptionParser()
        description = parser.read("mock_1.pycfg")

    with patch(builtins.__name__ + ".open", mock_open(read_data=file_2)):
        parser = DescriptionParser()
        parser.read("mock_2.pycfg", description)

    return description
Пример #3
0
    def read_model_description(self):
        '''parse the model_file field of the application configuration
        and read the files.

        The model_file field of the application configuration is
        first split at commas, since it may list more than one file.

        The files may be uris of the form :samp:`file:filename?section=name`,
        in which case a bare configuration object is read from filename
        into the configuration section with key 'name'.

        A simple filename without a section option
        is treated as a standard multi-section configuration file.

        Returns
        -------
        description : Description
            Configuration object.
        '''
        reader = DescriptionParser()
        description = Description()

        Config._log.info("model file: %s" % self.model_file)

        # TODO: make space aware w/ regex
        for model_file in self.model_file.split(','):
            if not model_file.startswith("file:"):
                model_file = 'file:' + model_file

            file_regex = re.compile(r"^file:([^?]*)(\?(.*)?)?")
            m = file_regex.match(model_file)
            model_file = m.group(1)
            file_url_params = {}
            if m.group(3):
                file_url_params.update(
                    ((x[0], x[1])
                     for x in (y.split('=') for y in m.group(3).split('&'))))
            if 'section' in file_url_params:
                section = file_url_params['section']
            else:
                section = None
            Config._log.info("reading model file %s" % (model_file))
            reader.read(model_file, description, section)

        return description