Esempio n. 1
0
    def test_translating_key_dot_dict_with_acquisition(self):
        HyphenUnderscoreDictWithAcquisition = create_key_translating_dot_dict(
            "HyphenUnderscoreDictWithAcquisition",
            (('-', '_'),),
            base_class=DotDictWithAcquisition
        )
        d = HyphenUnderscoreDictWithAcquisition()
        d['a-a.b-b.c-c'] = 17
        d['a-a.b_b.d-d'] = 8
        d['a_a.x-x'] = 99
        d['b-b'] = 21
        self.assertTrue(isinstance(d._key_order, OrderedSet))
        # the keys should be in order of insertion within each level of the
        # nested dicts
        keys_in_breadth_first_order = [
            'a_a', 'b_b', 'a_a.b_b', 'a_a.x_x', 'a_a.b_b.c_c', 'a_a.b_b.d_d'
        ]
        self.assertEqual(
            keys_in_breadth_first_order,
            [k for k in d.keys_breadth_first(include_dicts=True)]
        )
        self.assertEqual(d.a_a.b_b.c_c, 17)
        self.assertEqual(d.a_a.b_b['c-c'], 17)
        self.assertEqual(d.a_a['b-b'].c_c, 17)
        self.assertEqual(d['a-a'].b_b.c_c, 17)
        self.assertEqual(d['a-a.b-b.c-c'], 17)
        self.assertEqual(d['a-a.b-b.c_c'], 17)
        self.assertEqual(d['a-a.b_b.c_c'], 17)
        self.assertEqual(d['a_a.b_b.c_c'], 17)

        del d['a-a.b-b.c-c']

        self.assertTrue('a-a.b-b.c-c' not in d)
        self.assertTrue('a-a.b-b.c_c' not in d)
        self.assertTrue('a-a.b_b.c_c' not in d)
        self.assertTrue('a_a.b_b.c_c' not in d)
        self.assertTrue('c-c' not in d['a_a']['b_b']._key_order)
        self.assertTrue('c_c' not in d['a_a']['b_b']._key_order)

        self.assertTrue(isinstance(d, HyphenUnderscoreDictWithAcquisition))
        self.assertTrue(
            isinstance(d['a-a'], HyphenUnderscoreDictWithAcquisition)
        )
        self.assertTrue(
            isinstance(d.a_a, HyphenUnderscoreDictWithAcquisition)
        )
        self.assertTrue(
            isinstance(d.a_a.b_b, HyphenUnderscoreDictWithAcquisition)
        )

        self.assertTrue(isinstance(d, HyphenUnderscoreDictWithAcquisition))
        self.assertTrue(isinstance(d.a_a, HyphenUnderscoreDictWithAcquisition))
        self.assertTrue(
            isinstance(d.a_a.b_b, HyphenUnderscoreDictWithAcquisition)
        )
        self.assertEqual(d.a_a.b_b['x_x'], 99)
        self.assertEqual(d.a_a.b_b.x_x, 99)
        self.assertEqual(d.a_a['b-b']['a-a'].x_x, 99)
        self.assertEqual(d.a_a['b-b']['a-a']['b-b']['a-a'].x_x, 99)
Esempio n. 2
0
    def test_translating_key_namespace(self):
        HyphenUnderscoreNamespace = create_key_translating_dot_dict(
            "HyphenUnderscoreNamespace",
            (('-', '_'),),
            base_class=Namespace
        )
        d = HyphenUnderscoreNamespace()
        d.namespace('a-a')
        d.a_a.namespace('b-b')
        d.a_a['b-b'].add_option('c-c')
        d['a-a'].b_b.add_aggregation('d-d', lambda x, y, z: True)
        d['a_a'].add_option('x-x')
        d.add_option('b-b')
        self.assertTrue(isinstance(d._key_order, OrderedSet))
        # the keys should be in order of insertion within each level of the
        # nested dicts
        keys_in_breadth_first_order = [
            'a_a', 'b_b', 'a_a.b_b', 'a_a.x_x', 'a_a.b_b.c_c', 'a_a.b_b.d_d'
        ]
        self.assertEqual(
            keys_in_breadth_first_order,
            [k for k in d.keys_breadth_first(include_dicts=True)]
        )
        self.assertEqual(d.a_a.b_b.c_c.name, 'c-c')
        self.assertEqual(d.a_a.b_b['c-c'].name, 'c-c')
        self.assertEqual(d.a_a['b-b'].c_c.name, 'c-c')
        self.assertEqual(d['a-a'].b_b.c_c.name, 'c-c')
        self.assertEqual(d['a-a.b-b.c-c'].name, 'c-c')
        self.assertEqual(d['a-a.b-b.c_c'].name, 'c-c')
        self.assertEqual(d['a-a.b_b.c_c'].name, 'c-c')
        self.assertEqual(d['a_a.b_b.c_c'].name, 'c-c')

        del d['a-a.b-b.c-c']

        self.assertTrue('a-a.b-b.c-c' not in d)
        self.assertTrue('a-a.b-b.c_c' not in d)
        self.assertTrue('a-a.b_b.c_c' not in d)
        self.assertTrue('a_a.b_b.c_c' not in d)
        self.assertTrue('c-c' not in d['a_a']['b_b']._key_order)
        self.assertTrue('c_c' not in d['a_a']['b_b']._key_order)

        self.assertTrue(isinstance(d, HyphenUnderscoreNamespace))
        self.assertTrue(
            isinstance(d['a-a'], HyphenUnderscoreNamespace)
        )
        self.assertTrue(
            isinstance(d.a_a, HyphenUnderscoreNamespace)
        )
        self.assertTrue(
            isinstance(d.a_a.b_b, HyphenUnderscoreNamespace)
        )

        self.assertTrue(isinstance(d, HyphenUnderscoreNamespace))
        self.assertTrue(isinstance(d.a_a, HyphenUnderscoreNamespace))
        self.assertTrue(
            isinstance(d.a_a.b_b, HyphenUnderscoreNamespace)
        )
Esempio n. 3
0
    def test_translating_key_dot_dict_with_acquisition(self):
        HyphenUnderscoreDictWithAcquisition = create_key_translating_dot_dict(
            "HyphenUnderscoreDictWithAcquisition", (('-', '_'), ),
            base_class=DotDictWithAcquisition)
        d = HyphenUnderscoreDictWithAcquisition()
        d['a-a.b-b.c-c'] = 17
        d['a-a.b_b.d-d'] = 8
        d['a_a.x-x'] = 99
        d['b-b'] = 21
        self.assertTrue(isinstance(d._key_order, OrderedSet))
        # the keys should be in order of insertion within each level of the
        # nested dicts
        keys_in_breadth_first_order = [
            'a_a', 'b_b', 'a_a.b_b', 'a_a.x_x', 'a_a.b_b.c_c', 'a_a.b_b.d_d'
        ]
        self.assertEqual(keys_in_breadth_first_order,
                         [k for k in d.keys_breadth_first(include_dicts=True)])
        self.assertEqual(d.a_a.b_b.c_c, 17)
        self.assertEqual(d.a_a.b_b['c-c'], 17)
        self.assertEqual(d.a_a['b-b'].c_c, 17)
        self.assertEqual(d['a-a'].b_b.c_c, 17)
        self.assertEqual(d['a-a.b-b.c-c'], 17)
        self.assertEqual(d['a-a.b-b.c_c'], 17)
        self.assertEqual(d['a-a.b_b.c_c'], 17)
        self.assertEqual(d['a_a.b_b.c_c'], 17)

        del d['a-a.b-b.c-c']

        self.assertTrue('a-a.b-b.c-c' not in d)
        self.assertTrue('a-a.b-b.c_c' not in d)
        self.assertTrue('a-a.b_b.c_c' not in d)
        self.assertTrue('a_a.b_b.c_c' not in d)
        self.assertTrue('c-c' not in d['a_a']['b_b']._key_order)
        self.assertTrue('c_c' not in d['a_a']['b_b']._key_order)

        self.assertTrue(isinstance(d, HyphenUnderscoreDictWithAcquisition))
        self.assertTrue(
            isinstance(d['a-a'], HyphenUnderscoreDictWithAcquisition))
        self.assertTrue(isinstance(d.a_a, HyphenUnderscoreDictWithAcquisition))
        self.assertTrue(
            isinstance(d.a_a.b_b, HyphenUnderscoreDictWithAcquisition))

        self.assertTrue(isinstance(d, HyphenUnderscoreDictWithAcquisition))
        self.assertTrue(isinstance(d.a_a, HyphenUnderscoreDictWithAcquisition))
        self.assertTrue(
            isinstance(d.a_a.b_b, HyphenUnderscoreDictWithAcquisition))
        self.assertEqual(d.a_a.b_b['x_x'], 99)
        self.assertEqual(d.a_a.b_b.x_x, 99)
        self.assertEqual(d.a_a['b-b']['a-a'].x_x, 99)
        self.assertEqual(d.a_a['b-b']['a-a']['b-b']['a-a'].x_x, 99)
Esempio n. 4
0
    def parse_args(self, args=None, namespace=None):
        """this method hijacks the normal argparse Namespace generation,
        shimming configman into the process. The return value will be a
        configman DotDict rather than an argparse Namespace."""
        # load the config_manager within the scope of the method that uses it
        # so that we avoid circular references in the outer scope
        from configman.config_manager import ConfigurationManager

        configuration_manager = ConfigurationManager(
            definition_source=[self.get_required_config()],
            values_source_list=self.value_source_list,
            argv_source=args,
            app_name=self.prog,
            app_version=self.version,
            app_description=self.description,
            use_auto_help=False,
        )

        # it is apparent a common idiom that commandline options may have
        # embedded '-' characters in them.  Configman requires that option
        # follow the Python Identifier rules.  Fortunately, Configman has a
        # class that will perform dynamic translation of keys.  In this
        # code fragment, we fetch the final configuration from configman
        # using a Mapping that will translate keys with '-' into keys with
        # '_' instead.
        conf = configuration_manager.get_config(
            mapping_class=create_key_translating_dot_dict(
                "HyphenUnderscoreDict",
                (('-', '_'),)
            )
        )

        # here is where we add the values given to "set_defaults" method
        # of argparse.
        if self.configman_subparsers_option:
            subparser_name = conf[self.configman_subparsers_option.name]
            try:
                conf.update(
                    self.configman_subparsers_option.foreign_data.argparse
                    .subparsers[subparser_name].subparser
                    .extra_defaults
                )
            except (AttributeError, KeyError):
                # no extra_defaults skip on
                pass

        if hasattr(self, 'extra_defaults'):
            conf.update(self.extra_defaults)

        return conf
Esempio n. 5
0
    def parse_args(self, args=None, namespace=None):
        """this method hijacks the normal argparse Namespace generation,
        shimming configman into the process. The return value will be a
        configman DotDict rather than an argparse Namespace."""
        # load the config_manager within the scope of the method that uses it
        # so that we avoid circular references in the outer scope
        from configman.config_manager import ConfigurationManager

        configuration_manager = ConfigurationManager(
            definition_source=[self.get_required_config()],
            values_source_list=self.value_source_list,
            argv_source=args,
            app_name=self.prog,
            app_version=self.version,
            app_description=self.description,
            use_auto_help=False,
        )

        # it is apparent a common idiom that commandline options may have
        # embedded '-' characters in them.  Configman requires that option
        # follow the Python Identifier rules.  Fortunately, Configman has a
        # class that will perform dynamic translation of keys.  In this
        # code fragment, we fetch the final configuration from configman
        # using a Mapping that will translate keys with '-' into keys with
        # '_' instead.
        conf = configuration_manager.get_config(
            mapping_class=create_key_translating_dot_dict(
                "HyphenUnderscoreDict", (('-', '_'), )))

        # here is where we add the values given to "set_defaults" method
        # of argparse.
        if self.configman_subparsers_option:
            subparser_name = conf[self.configman_subparsers_option.name]
            try:
                conf.update(
                    self.configman_subparsers_option.foreign_data.argparse.
                    subparsers[subparser_name].subparser.extra_defaults)
            except (AttributeError, KeyError):
                # no extra_defaults skip on
                pass

        if hasattr(self, 'extra_defaults'):
            conf.update(self.extra_defaults)

        return conf
Esempio n. 6
0
    def test_translating_key_namespace(self):
        HyphenUnderscoreNamespace = create_key_translating_dot_dict(
            "HyphenUnderscoreNamespace", (('-', '_'), ), base_class=Namespace)
        d = HyphenUnderscoreNamespace()
        d.namespace('a-a')
        d.a_a.namespace('b-b')
        d.a_a['b-b'].add_option('c-c')
        d['a-a'].b_b.add_aggregation('d-d', lambda x, y, z: True)
        d['a_a'].add_option('x-x')
        d.add_option('b-b')
        self.assertTrue(isinstance(d._key_order, OrderedSet))
        # the keys should be in order of insertion within each level of the
        # nested dicts
        keys_in_breadth_first_order = [
            'a_a', 'b_b', 'a_a.b_b', 'a_a.x_x', 'a_a.b_b.c_c', 'a_a.b_b.d_d'
        ]
        self.assertEqual(keys_in_breadth_first_order,
                         [k for k in d.keys_breadth_first(include_dicts=True)])
        self.assertEqual(d.a_a.b_b.c_c.name, 'c-c')
        self.assertEqual(d.a_a.b_b['c-c'].name, 'c-c')
        self.assertEqual(d.a_a['b-b'].c_c.name, 'c-c')
        self.assertEqual(d['a-a'].b_b.c_c.name, 'c-c')
        self.assertEqual(d['a-a.b-b.c-c'].name, 'c-c')
        self.assertEqual(d['a-a.b-b.c_c'].name, 'c-c')
        self.assertEqual(d['a-a.b_b.c_c'].name, 'c-c')
        self.assertEqual(d['a_a.b_b.c_c'].name, 'c-c')

        del d['a-a.b-b.c-c']

        self.assertTrue('a-a.b-b.c-c' not in d)
        self.assertTrue('a-a.b-b.c_c' not in d)
        self.assertTrue('a-a.b_b.c_c' not in d)
        self.assertTrue('a_a.b_b.c_c' not in d)
        self.assertTrue('c-c' not in d['a_a']['b_b']._key_order)
        self.assertTrue('c_c' not in d['a_a']['b_b']._key_order)

        self.assertTrue(isinstance(d, HyphenUnderscoreNamespace))
        self.assertTrue(isinstance(d['a-a'], HyphenUnderscoreNamespace))
        self.assertTrue(isinstance(d.a_a, HyphenUnderscoreNamespace))
        self.assertTrue(isinstance(d.a_a.b_b, HyphenUnderscoreNamespace))

        self.assertTrue(isinstance(d, HyphenUnderscoreNamespace))
        self.assertTrue(isinstance(d.a_a, HyphenUnderscoreNamespace))
        self.assertTrue(isinstance(d.a_a.b_b, HyphenUnderscoreNamespace))
Esempio n. 7
0
 def parse_known_args(self, args=None, namespace=None):
     """this method hijacks the normal argparse Namespace generation,
     shimming configman into the process. The return value will be a
     configman DotDict rather than an argparse Namespace."""
     # load the config_manager within the scope of the method that uses it
     # so that we avoid circular references in the outer scope
     from configman.config_manager import ConfigurationManager
     configuration_manager = ConfigurationManager(
         definition_source=[self.get_required_config()],
         values_source_list=self.value_source_list,
         argv_source=args,
         app_name=self.prog,
         app_version=self.version,
         app_description=self.description,
         use_auto_help=False,
     )
     conf = configuration_manager.get_config(
         mapping_class=create_key_translating_dot_dict(
             "HyphenUnderscoreDict", (('-', '_'), )))
     return conf
Esempio n. 8
0
 def parse_known_args(self, args=None, namespace=None):
     """this method hijacks the normal argparse Namespace generation,
     shimming configman into the process. The return value will be a
     configman DotDict rather than an argparse Namespace."""
     # load the config_manager within the scope of the method that uses it
     # so that we avoid circular references in the outer scope
     from configman.config_manager import ConfigurationManager
     configuration_manager = ConfigurationManager(
         definition_source=[self.get_required_config()],
         values_source_list=self.value_source_list,
         argv_source=args,
         app_name=self.prog,
         app_version=self.version,
         app_description=self.description,
         use_auto_help=False,
     )
     conf = configuration_manager.get_config(
         mapping_class=create_key_translating_dot_dict(
             "HyphenUnderscoreDict",
             (('-', '_'),)
         )
     )
     return conf