Exemplo n.º 1
0
class TestPublicKey(unittest.TestCase):
    def setUp(self):
        test_config = {'_this_is_private_key': 1, 'this_is_public_key': 1}
        self.config = JsonCfg(test_config)

    def test_parse_from_args(self):
        self.config.print_config()
        self.assertTrue(True)
Exemplo n.º 2
0
    def test_validate_tuple(self):
        cfg = JsonCfg({'a': (0, 'this is desc,', lambda x: x < 10 and x >= 0)})

        try:
            cfg.a = 11
            self.assertTrue(False)
        except JCfgValidateFailError:
            pass
Exemplo n.º 3
0
 def test_parse_with_config_meta(self):
     test_config = {
         'foo': {
             '_default': 2,
             '_desc': 'this is a test description!'
         }
     }
     self.config = JsonCfg(test_config)
     self.config.print_config()
     self.assertTrue(True)
Exemplo n.º 4
0
    def test_validate(self):
        cfg = JsonCfg(
            {'a': {
                '_default': 0,
                '_validate': lambda x: x >= 0 and x < 10
            }})

        try:
            cfg.a = 11
            self.assertTrue(False)
        except JCfgValidateFailError:
            pass
Exemplo n.º 5
0
def main():
    test_config = {
        'a': 1,
        'b': 1.0,
        'c': 'val',
        'd': [1, 2, 3, 4],
        'e': {
            '_default': True,
            '_custom_attr': 't',
        },
        'f': {
            'f_a': 1,
            'f_b': 2,
            'f_c': {
                '_default': 1,
                '_custom_attr': 't',
            },
            'f_d': {
                'f_d_a': 's',
                'f_d_b': {
                    '_default': ['a', 'b', 'c'],
                    '_desc': 'test_description'
                }
            }
        },
        'g':
        (3, 'this is a description str')  # define an option with 2-size tuple
    }

    jcfg = JsonCfg(test_config)
    jcfg.print_config()
    jcfg.parse_args(description='test jcfg')
    jcfg.print_config()
Exemplo n.º 6
0
    def test_save_to_file(self):
        test_config = {
            'a': 1,
            'b': 1.0,
            'c': 'val',
            'd': [1, 2, 3, 4],
            'e': {
                '_default': True,
                '_custom_attr': 't',
            },
            'f': {
                'f_a': 1,
                'f_b': 2,
                'f_c': {
                    '_default': 1,
                    '_custom_attr': 't',
                },
                'f_d': {
                    'f_d_a': 's',
                    'f_d_b': {
                        '_default': ['a', 'b', 'c']
                    }
                }
            }
        }

        cfg = JsonCfg(test_config)
        tmp_dst_file = 'test_tmp_config.json'
        cfg.save_to_file(tmp_dst_file)
        cfg.update_from_file(tmp_dst_file)
        cfg.print_config()
Exemplo n.º 7
0
 def setUp(self):
     test_config = {
         'a': 1,
         'b': 1.0,
         'c': 'val',
         'd': [1, 2, 3, 4],
         'e': {
             '_default': True,
             '_custom_attr': 't',
         },
         'f': {
             'f_a': 1,
             'f_b': 2,
             'f_c': {
                 '_default': 1,
                 '_custom_attr': 't',
             },
             'f_d': {
                 'f_d_a': 's',
                 'f_d_b': {
                     '_default': ['a', 'b', 'c']
                 }
             }
         }
     }
     self.config = JsonCfg(test_config)
Exemplo n.º 8
0
def main():
    cfg = JsonCfg({
        'input_path': '',
        'input_int': 0,
        'input_float': 0.0,
        'test_default': {
            '_default': 3,
            '_desc': 'description'
        }
    })
    cfg.parse_args()

    # do any work you want below
    # you can get the input_path, input_int, or input_float like this:

    res = cfg.input_int + cfg.input_float
    # `cfg.input_int` and `cfg.input_float` will get the actual value depending on the cli input. jcfg have done the type check work for you. If the input option cannot be parsed as an integer, it will raise error
    print(res)
Exemplo n.º 9
0
    def test_load_dump(self):
        output_dir = Path('example_dump')
        output_dir.mkdir(parents=True, exist_ok=True)

        cfg = JsonCfg({'a': {'a': 0, 'b': []}, 'b': {'a': ''}})
        cfg.update_from_file('example/config.yaml')
        cfg.save_to_file('example_dump/config_output.yaml')
Exemplo n.º 10
0
 def test_failed_loading(self):
     try:
         JsonCfg({'2363': 1})
     except JCfgInvalidKeyError:
         self.assertTrue(True)
Exemplo n.º 11
0
 def test_load_meta(self):
     JsonCfg(test_config)
     self.assertTrue(True)
Exemplo n.º 12
0
 def test_load_from_2size_tuple(self):
     cfg = JsonCfg({'a': (1, 'this is a option defined by 2-size tuple')})
     cfg.print_config()
Exemplo n.º 13
0
 def setUp(self):
     test_config = {'_this_is_private_key': 1, 'this_is_public_key': 1}
     self.config = JsonCfg(test_config)