Esempio n. 1
0
 def test_basic_namespace(self):
     a = reusables.Namespace(one=1, two=2, three=3)
     b = reusables.Namespace({'one': 1, 'two': 2, 'three': 3})
     c = reusables.Namespace((zip(['one', 'two', 'three'], [1, 2, 3])))
     d = reusables.Namespace(([('two', 2), ('one', 1), ('three', 3)]))
     e = reusables.Namespace(({'three': 3, 'one': 1, 'two': 2}))
     assert a == b == c == d == e
Esempio n. 2
0
 def test_namespace_from_bad_dict(self):
     try:
         ns = reusables.Namespace('{"k1": "v1", ' '"k2": {"k3": "v2"}}')
     except ValueError:
         assert True
     else:
         assert False, "Should have raised type error"
Esempio n. 3
0
    def test_namespace_modifiy_at_depth(self):
        test_dict = {
            'key1': 'value1',
            "Key 2": {
                "Key 3": "Value 3",
                "Key4": {
                    "Key5": "Value5"
                }
            }
        }

        namespace = reusables.Namespace(**test_dict)
        assert 'key1' in namespace
        assert 'key2' not in namespace
        namespace['Key 2'].new_thing = "test"
        assert namespace['Key 2'].new_thing == "test"
        namespace['Key 2'].new_thing += "2"
        assert namespace['Key 2'].new_thing == "test2"
        assert namespace['Key 2'].to_dict(
        )['new_thing'] == "test2", namespace['Key 2'].to_dict()
        assert namespace.to_dict()['Key 2']['new_thing'] == "test2"
        namespace.__setattr__('key1', 1)
        assert namespace['key1'] == 1
        namespace.__delattr__('key1')
        assert 'key1' not in namespace
Esempio n. 4
0
 def test_namespace_tree(self):
     test_dict = {
         'key1': 'value1',
         "Key 2": {
             "Key 3": "Value 3",
             "Key4": {
                 "Key5": "Value5"
             }
         }
     }
     namespace = reusables.Namespace(**test_dict)
     result = namespace.tree_view(sep="    ")
     assert result.startswith("key1\n") or result.startswith("Key 2\n")
     assert "Key4" in result and "    Value5\n" not in result
Esempio n. 5
0
    def test_error_namespace(self):
        test_dict = {
            'key1': 'value1',
            "Key 2": {
                "Key 3": "Value 3",
                "Key4": {
                    "Key5": "Value5"
                }
            }
        }

        namespace = reusables.Namespace(**test_dict)
        try:
            getattr(namespace, 'hello')
        except AttributeError:
            pass
        else:
            raise AssertionError("Should not find 'hello' in the test dict")
Esempio n. 6
0
def get_config(config_file="config.yaml"):
    config = default_config.copy()

    if os.path.exists(config_file):
        with open(config_file) as f:
            config.update(yaml.load(f))
    logger.info("Loaded config")
    logger.debug("Config - {0}".format(config))

    config['video_dir'] = (
        config['storage_directory'] if not config.get('video_sub_dir') else
        os.path.join(config['storage_directory'], config['video_sub_dir']))

    config['image_dir'] = (
        config['storage_directory'] if not config.get('image_sub_dir') else
        os.path.join(config['storage_directory'], config['image_sub_dir']))

    return reusables.Namespace(**config)
Esempio n. 7
0
 def test_namespace(self):
     test_dict = {
         'key1': 'value1',
         "Key 2": {
             "Key 3": "Value 3",
             "Key4": {
                 "Key5": "Value5"
             }
         }
     }
     namespace = reusables.Namespace(**test_dict)
     assert namespace.key1 == test_dict['key1']
     assert dict(getattr(namespace, 'Key 2')) == test_dict['Key 2']
     setattr(namespace, 'TEST_KEY', 'VALUE')
     assert namespace.TEST_KEY == 'VALUE'
     delattr(namespace, 'TEST_KEY')
     assert 'TEST_KEY' not in namespace.to_dict(), namespace.to_dict()
     assert isinstance(namespace['Key 2'].Key4, reusables.Namespace)
     assert "'key1': 'value1'" in str(namespace)
     assert repr(namespace).startswith("<Namespace:")
Esempio n. 8
0
 def test_namespace_from_dict(self):
     ns = reusables.Namespace({"k1": "v1", "k2": {"k3": "v2"}})
     assert ns.k2.k3 == "v2"