Пример #1
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
Пример #2
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
Пример #3
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
Пример #4
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
Пример #5
0
 def setUp(self):
     self.config = ConfigFactory()
Пример #6
0
class TestConfig(unittest.TestCase):
    def setUp(self):
        self.config = ConfigFactory()

    def test_parser(self):
        self.assertIsInstance(self.config.parser, Parser)

        self.config.parser = "foo"
        self.assertEqual("foo", self.config.parser)

        self.config.parser = None
        self.assertIsInstance(self.config.parser, Parser)

    def test_merger(self):
        self.assertIsInstance(self.config.merger, Merger)

        self.config.merger = "foo"
        self.assertEqual("foo", self.config.merger)

        self.config.merger = None
        self.assertIsInstance(self.config.merger, Merger)

    @patch.object(Parser,
                  "load_from_file",
                  side_effect=({
                      "output": {
                          "location": "file2"
                      }
                  }, {}))
    def test_load_from_file(self, mock_parser):
        response = self.config.load_from_file("yaml_file")

        self.assertIsInstance(response, ConfigObject)
        self.assertEqual(
            os.path.realpath("") + "/file2", response["output"]["location"])

    @patch.object(Parser, "load_from_file", side_effect=({}, {}))
    def test_load_from_file__empty(self, mock_parser):
        response = self.config.load_from_file("yaml_file")

        self.assertIsInstance(response, ConfigObject)
        self.assertEqual("stdout", response["output"]["location"])

    def test_fix_all_path(self):
        sample = {
            "input": {
                "locations": [
                    "directory", "directory2/subdirectory", "file",
                    "directory/file"
                ]
            },
            "output": {
                "location": "file2",
                "template": "file3"
            }
        }
        self.config.fix_all_path(sample, "/root/path")

        self.assertEqual("/root/path/directory",
                         sample["input"]["locations"][0])
        self.assertEqual("/root/path/directory2/subdirectory",
                         sample["input"]["locations"][1])
        self.assertEqual("/root/path/file", sample["input"]["locations"][2])
        self.assertEqual("/root/path/directory/file",
                         sample["input"]["locations"][3])
        self.assertEqual("/root/path/file2", sample["output"]["location"])
        self.assertEqual("/root/path/file3", sample["output"]["template"])

    def test_fix_all_path__empty_datas(self):
        sample = {
            "input": {
                "locations": None,
            },
            "output": {
                "location": "file2",
                "template": "file3"
            }
        }
        self.config.fix_all_path(sample, "/root/path")
        self.assertEqual(None, sample["input"]["locations"])

    def test_fix_all_path__empty_output(self):
        sample = {
            "input": {
                "locations": None,
            },
            "output": {
                "location": "stdout",
                "template": "default"
            }
        }
        self.config.fix_all_path(sample, "/root/path")
        self.assertEqual("stdout", sample["output"]["location"])
        self.assertEqual("default", sample["output"]["template"])

    def test_fix_path__None(self):
        self.assertEqual(None, self.config.fix_path(None, "root"))

    def test_fix_path__file_missing(self):
        self.assertEqual("/root/sub/path/file",
                         self.config.fix_path("path/file", "/root/sub"))

    def test_fix_path__file_exists(self):
        self.assertEqual(__file__, self.config.fix_path(__file__, "root"))
Пример #7
0
def when_factory_config(context):
    factory = ConfigFactory()
    response = factory.load_from_file(context.conf_file)
    context.config_object = response
Пример #8
0
 def setUp(self):
     self.config = ConfigFactory()
Пример #9
0
class TestConfig(unittest.TestCase):

    def setUp(self):
        self.config = ConfigFactory()

    def test_parser(self):
        self.assertIsInstance(self.config.parser, Parser)

        self.config.parser = "foo"
        self.assertEqual("foo", self.config.parser)

        self.config.parser = None
        self.assertIsInstance(self.config.parser, Parser)

    def test_merger(self):
        self.assertIsInstance(self.config.merger, Merger)

        self.config.merger = "foo"
        self.assertEqual("foo", self.config.merger)

        self.config.merger = None
        self.assertIsInstance(self.config.merger, Merger)

    @patch.object(Parser, "load_from_file", side_effect=({"output": {"location": "file2"}}, {}))
    def test_load_from_file(self, mock_parser):
        response = self.config.load_from_file("yaml_file")

        self.assertIsInstance(response, ConfigObject)
        self.assertEqual(os.path.realpath("") + "/file2", response["output"]["location"])

    @patch.object(Parser, "load_from_file", side_effect=({}, {}))
    def test_load_from_file__empty(self, mock_parser):
        response = self.config.load_from_file("yaml_file")

        self.assertIsInstance(response, ConfigObject)
        self.assertEqual("stdout", response["output"]["location"])

    def test_fix_all_path(self):
        sample = {
            "input": {
                "locations": [
                    "directory",
                    "directory2/subdirectory",
                    "file",
                    "directory/file"
                ]
            },
            "output": {
                "location": "file2",
                "template": "file3"
            }
        }
        self.config.fix_all_path(sample, "/root/path")

        self.assertEqual("/root/path/directory", sample["input"]["locations"][0])
        self.assertEqual("/root/path/directory2/subdirectory", sample["input"]["locations"][1])
        self.assertEqual("/root/path/file", sample["input"]["locations"][2])
        self.assertEqual("/root/path/directory/file", sample["input"]["locations"][3])
        self.assertEqual("/root/path/file2", sample["output"]["location"])
        self.assertEqual("/root/path/file3", sample["output"]["template"])

    def test_fix_all_path__empty_datas(self):
        sample = {
            "input": {
                "locations": None,
            },
            "output": {
                "location": "file2",
                "template": "file3"
            }
        }
        self.config.fix_all_path(sample, "/root/path")
        self.assertEqual(None, sample["input"]["locations"])

    def test_fix_all_path__empty_output(self):
        sample = {
            "input": {
                "locations": None,
            },
            "output": {
                "location": "stdout",
                "template": "default"
            }
        }
        self.config.fix_all_path(sample, "/root/path")
        self.assertEqual("stdout", sample["output"]["location"])
        self.assertEqual("default", sample["output"]["template"])

    def test_fix_path__None(self):
        self.assertEqual(None, self.config.fix_path(None, "root"))

    def test_fix_path__file_missing(self):
        self.assertEqual("/root/sub/path/file", self.config.fix_path("path/file", "/root/sub"))

    def test_fix_path__file_exists(self):
        self.assertEqual(__file__, self.config.fix_path(__file__, "root"))
Пример #10
0
def when_factory_config(context):
    factory = ConfigFactory()
    response = factory.load_from_file(context.conf_file)
    context.config_object = response