Пример #1
0
    def load_from_file(self, config_file):
        """Load a config object from a file
        """
        merger = self.merger
        parser = self.parser

        datas = parser.load_from_file(config_file)
        self.validator.validate_config(datas)

        if datas is None or datas == {}:
            config = ConfigObject()
        else:
            config = merger.merge_configs(ConfigObject(), [datas])

        self.fix_all_path(config, os.path.dirname(config_file))
        return config
Пример #2
0
    def test_create_from_config(self, mock_extender, mock_merger, mock_parser_directory, mock_parser_file):
        config = ConfigObject()
        config["input"]["directories"] = ["directory1", "directory2"]
        config["input"]["files"] = ["file1", "file2"]
        config["input"]["arguments"] = {"var": "value"}

        response = self.source.create_from_config(config)

        self.assertIsInstance(response, RootDto)

        mock_extender.assert_called_once_with({"i": "j"}, paths=('categories/?', 'versions/?', 'versions/?/methods/?', 'versions/?/types/?', 'versions/?/references/?'))
        mock_merger.assert_called_once_with([{"a": "b"}, {"c": "d"}, {"z": "y"}, {"e": "f"}, {"g": "h"}])
        mock_parser_directory.assert_has_calls([call('directory1'), call('directory2')])
        mock_parser_file.assert_has_calls([call('file1'), call('file2')])
Пример #3
0
    def test_hide_filtered_elements__category_exclude(self):
        root = Root()

        category1 = Category("c1")
        category2 = Category("c2")
        category3 = Category("c3")

        root.categories = {"c1": category1, "c2": category2, "c3": category3}

        config = ConfigObject()
        config["filter"]["categories"]["excludes"] = ["c1", "c3"]
        self.source.hide_filtered_elements(root, config["filter"])

        self.assertFalse(category1.display)
        self.assertTrue(category2.display)
        self.assertFalse(category3.display)
Пример #4
0
    def test_hide_filtered_elements__version(self):
        root = Root()
        version1 = Version()
        version2 = Version()
        version3 = Version()
        version1.name = "v1"
        version2.name = "v2"
        version3.name = "v3"

        root.versions = {"v1": version1, "v2": version2, "v3": version3}

        config = ConfigObject()
        self.source.hide_filtered_elements(root, config["filter"])

        self.assertTrue(version1.display)
        self.assertTrue(version2.display)
        self.assertTrue(version3.display)
Пример #5
0
    def test_create_from_config(self, mock_extender, mock_merger, mock_parser_directory, mock_parser_file):
        config = ConfigObject()
        init = os.path.isdir
        try:
            os.path.isdir = lambda x: x[0] == "d"
            config["input"]["locations"] = ["d1", "d2", "f1", "f2"]
            config["input"]["arguments"] = {"var": "value"}

            response = self.source.create_from_config(config)

            self.assertIsInstance(response, RootDto)

            mock_extender.assert_called_once_with({"i": "j"}, paths=('categories/?', 'versions/?', 'versions/?/methods/?', 'versions/?/types/?', 'versions/?/references/?'))
            mock_merger.assert_called_once_with([{"a": "b"}, {"c": "d"}, {"z": "y"}, {"e": "f"}, {"g": "h"}])
            mock_parser_directory.assert_has_calls([call("d1"), call("d2")])
            mock_parser_file.assert_has_calls([call("f1"), call("f2")])
        finally:
            os.path.isdir = init
Пример #6
0
    def test_hide_filtered_elements__category_include(self):
        root = Root()

        category1 = Category("c")
        category2 = Category("c")
        category3 = Category("c")
        category1.name = "v1"
        category2.name = "v2"
        category3.name = "v3"

        root.categories = {"s1": category1, "s2": category2, "s3": category3}

        config = ConfigObject()
        config["filter"]["categories"]["includes"] = ["v1", "v3"]
        self.source.hide_filtered_elements(root, config["filter"])

        self.assertTrue(category1.display)
        self.assertFalse(category2.display)
        self.assertTrue(category3.display)
Пример #7
0
    def _init_config(self):
        """return command's configuration from call's arguments
        """
        options = self.parser.parse_args()
        if options.config is None and options.input is None:
            self.parser.print_help()
            sys.exit(2)

        if options.config is not None:
            configFactory = ConfigFactory()
            config = configFactory.load_from_file(options.config)
        else:
            config = ConfigObject()

        if options.input is not None:
            config["input"]["locations"] = [str(x) for x in options.input]
        if options.arguments is not None:
            config["input"]["arguments"] = dict(
                (x.partition("=")[0], x.partition("=")[2])
                for x in options.arguments)

        if options.output is not None:
            config["output"]["location"] = options.output

        if options.no_validate is not None:
            config["input"]["validate"] = not options.no_validate

        if options.dry_run is not None:
            self.dry_run = options.dry_run
        if options.watch is not None:
            self.watch = options.watch
        if options.traceback is not None:
            self.traceback = options.traceback

        if options.quiet is not None:
            self.logger.setLevel(logging.WARNING)
        if options.silence is not None:
            logging.disable(logging.CRITICAL)

        configService = ConfigService()
        configService.validate(config)
        self.config = config
Пример #8
0
    def test_hide_filtered_elements__category(self):
        root = Root()
        version1 = Version()

        category1 = Category("c")
        category2 = Category("c")
        category3 = Category("c")
        category1.name = "v1"
        category2.name = "v2"
        category3.name = "v3"

        root.versions = {"v1": version1}
        version1.categories = {"s1": category1, "s2": category2, "s3": category3}

        config = ConfigObject()
        self.source.hide_filtered_elements(root, config["filter"])

        self.assertTrue(category1.display)
        self.assertTrue(category2.display)
        self.assertTrue(category3.display)
Пример #9
0
    def get_config(self):
        """return command's configuration from call's arguments
        """
        options = self.parser.parse_args()
        if options.config is None and options.directories is None and options.files is None:
            self.parser.print_help()
            sys.exit(2)

        if options.config is not None:
            configFactory = ConfigFactory()
            config = configFactory.load_from_file(options.config)
        else:
            config = ConfigObject()

        if options.directories is not None:
            config["input"]["directories"] = [
                str(x) for x in options.directories
            ]

        if options.files is not None:
            config["input"]["files"] = [str(x) for x in options.files]

        if options.arguments is not None:
            config["input"]["arguments"] = dict(
                (x.partition("=")[0], x.partition("=")[2])
                for x in options.arguments)

        if options.output is not None:
            config["output"]["location"] = options.output

        if options.no_validate is not None:
            config["input"]["validate"] = not options.no_validate

        configService = ConfigService()
        configService.validate(config)
        return config
Пример #10
0
    def test_get_sources_from_config(self):
        config = ConfigObject()

        response = self.source.get_sources_from_config(config)

        self.assertEqual([], response)