Beispiel #1
0
class FileStorage(StorageBase):
    def __init__(self,
                 path, 
                 default_type=None, 
                 initializer=None,
                 schema=None,
                 auto_create=False):
        
        self._path = path

        file_exists = os.path.isfile(path)
        
        self._filestore = Config(path, 
                                 default=default_type, 
                                 auto_create=auto_create)
        
        self.name = self._filestore.files[0]

        if not file_exists and initializer:
            self._initialize(initializer)
            self._filestore.save_all()

        super().__init__(self._filestore[self.name], schema)

    def _on_object_change(self, sender, action=None):
        self._filestore.save_all()
        super()._on_object_change(sender, action=action)

    def _initialize(self, initializer):
        for object_name, value in viewitems(initializer):
            if not object_name in self._filestore[self.name]:
                self._filestore[self.name][object_name] = value
Beispiel #2
0
    def __init__(self,
                 path, 
                 default_type=None, 
                 initializer=None,
                 schema=None,
                 auto_create=False):
        
        self._path = path

        file_exists = os.path.isfile(path)
        
        self._filestore = Config(path, 
                                 default=default_type, 
                                 auto_create=auto_create)
        
        self.name = self._filestore.files[0]

        if not file_exists and initializer:
            self._initialize(initializer)
            self._filestore.save_all()

        super().__init__(self._filestore[self.name], schema)
Beispiel #3
0
def test_check_file_attribute_single_file():
    cfg = Config(["foo.json"], auto_create=False)
    assert not cfg.foo
Beispiel #4
0
def test_getfiles_invalid_chars_in_filename():
    invalid_filename = "File()`~!@#$%^&*-+=|{}[]:;\"'<>,.?name.json"
    cfg = Config([invalid_filename], auto_create=False)
    assert cfg.files == ["File_____________________________name"]
Beispiel #5
0
def test_getfiles_duplicate_names():
    cfg = Config(["foo.json", "foo.yaml"], auto_create=False)
    assert cfg.files == ["foo"]
Beispiel #6
0
def test_getfiles_some_files():
    cfg = Config(["foo.json", "baz.yaml"], auto_create=False)
    assert sorted(cfg.files) == ["baz", "foo"]
Beispiel #7
0
def test_set_attribute_more_complex_type():
    cfg = Config(["foo.json"], auto_create=False)
    cfg.foo.bar = {"baz": {"eggs": {"price": 7, "currency": "pounds"}}}
    assert "{} {}".format(cfg.foo.bar.baz.eggs.price,
                          cfg.foo.bar.baz.eggs.currency) == "7 pounds"
Beispiel #8
0
def test_check_file_attribute_invalid_chars_in_filename():
    invalid_filename = "File()`~!@#$%^&*-+=|{}[]:;\"'<>,.?name.json"
    cfg = Config([invalid_filename], auto_create=False)
    assert not cfg.File_____________________________name
Beispiel #9
0
def test_init_invalid_args_dir_Case3():
    with pytest.raises(AssertionError):
        Config(dir="invalid_dir")
Beispiel #10
0
def test_init_invalid_args_dir_Case1():
    with pytest.raises(ValueError):
        Config(dir=None)
Beispiel #11
0
def test_init_invalid_args_file_Case4():
    with pytest.raises(AssertionError):
        Config(4)
Beispiel #12
0
def test_init_invalid_args_file_Case3():
    with pytest.raises(ValueError):
        Config(None)
Beispiel #13
0
def test_init_invalid_args_file_Case2():
    with pytest.raises(AssertionError):
        Config(["usr/test", None])
Beispiel #14
0
def test_get_config_attribute_single_file():
    cfg = Config(["foo.json"], auto_create=False)
    cfg.foo.bar = {"baz": {"eggs": {"price": 7, "currency": "pounds"}}}
    assert cfg["foo"]
Beispiel #15
0
def test_check_file_attribute_some_files():
    cfg = Config(["foo.json", "baz.yaml"], auto_create=False)
    assert not cfg.foo
    assert not cfg.baz
Beispiel #16
0
def test_check_file_attribute_duplicate_names():
    cfg = Config(["foo.json", "foo.yaml"], auto_create=False)
    assert not cfg.foo
Beispiel #17
0
def test_getfiles_single_file():
    cfg = Config("foo.json", auto_create=False)
    assert cfg.files[0] == "foo"
Beispiel #18
0
def test_set_attribute_simple_type():
    cfg = Config(["foo.json", "baz.yaml"], auto_create=False)
    cfg.foo.bar = "bar"
    cfg.baz.ham = "ham"
    assert cfg.foo.bar == "bar"
    assert cfg.baz.ham == "ham"
Beispiel #19
0
def test_init_no_args():
    with pytest.raises(ValueError):
        Config()
Beispiel #20
0
def test_getfiles_single_file_in_list():
    cfg = Config(["foo.json"], auto_create=False)
    assert cfg.files[0] == "foo"
Beispiel #21
0
def test_set_attribute_complex_type():
    cfg = Config(["foo.json"], auto_create=False)
    cfg.foo.bar = {"baz": "ham"}
    assert cfg.foo.bar.baz == "ham"