Пример #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
Пример #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)
Пример #3
0
def test_check_file_attribute_single_file():
    cfg = Config(["foo.json"], auto_create=False)
    assert not cfg.foo
Пример #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"]
Пример #5
0
def test_getfiles_duplicate_names():
    cfg = Config(["foo.json", "foo.yaml"], auto_create=False)
    assert cfg.files == ["foo"]
Пример #6
0
def test_getfiles_some_files():
    cfg = Config(["foo.json", "baz.yaml"], auto_create=False)
    assert sorted(cfg.files) == ["baz", "foo"]
Пример #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"
Пример #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
Пример #9
0
def test_init_invalid_args_dir_Case3():
    with pytest.raises(AssertionError):
        Config(dir="invalid_dir")
Пример #10
0
def test_init_invalid_args_dir_Case1():
    with pytest.raises(ValueError):
        Config(dir=None)
Пример #11
0
def test_init_invalid_args_file_Case4():
    with pytest.raises(AssertionError):
        Config(4)
Пример #12
0
def test_init_invalid_args_file_Case3():
    with pytest.raises(ValueError):
        Config(None)
Пример #13
0
def test_init_invalid_args_file_Case2():
    with pytest.raises(AssertionError):
        Config(["usr/test", None])
Пример #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"]
Пример #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
Пример #16
0
def test_check_file_attribute_duplicate_names():
    cfg = Config(["foo.json", "foo.yaml"], auto_create=False)
    assert not cfg.foo
Пример #17
0
def test_getfiles_single_file():
    cfg = Config("foo.json", auto_create=False)
    assert cfg.files[0] == "foo"
Пример #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"
Пример #19
0
def test_init_no_args():
    with pytest.raises(ValueError):
        Config()
Пример #20
0
def test_getfiles_single_file_in_list():
    cfg = Config(["foo.json"], auto_create=False)
    assert cfg.files[0] == "foo"
Пример #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"