yfrake.config.config   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A ConfigSingleton.__init__() 0 4 2
A ConfigSingleton.file() 0 3 1
A ConfigSingleton.settings() 0 3 1
A ConfigSingleton.__new__() 0 4 2
A ConfigSingleton.is_locked() 0 3 1
1
# ==================================================================================== #
2
#    config.py - This file is part of the YFrake package.                              #
3
# ------------------------------------------------------------------------------------ #
4
#                                                                                      #
5
#    MIT License                                                                       #
6
#                                                                                      #
7
#    Copyright (c) 2022 Mattias Aabmets                                                #
8
#                                                                                      #
9
#    Permission is hereby granted, free of charge, to any person obtaining a copy      #
10
#    of this software and associated documentation files (the "Software"), to deal     #
11
#    in the Software without restriction, including without limitation the rights      #
12
#    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell         #
13
#    copies of the Software, and to permit persons to whom the Software is             #
14
#    furnished to do so, subject to the following conditions:                          #
15
#                                                                                      #
16
#    The above copyright notice and this permission notice shall be included in all    #
17
#    copies or substantial portions of the Software.                                   #
18
#                                                                                      #
19
#    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR        #
20
#    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,          #
21
#    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE       #
22
#    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER            #
23
#    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,     #
24
#    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE     #
25
#    SOFTWARE.                                                                         #
26
#                                                                                      #
27
# ==================================================================================== #
28
from ..cache.cache import CacheSingleton as Cache
29
from . import helpers, utils
30
from ..client import session
31
from pathlib import Path
32
import copy
33
34
35
# ==================================================================================== #
36
class ConfigSingleton:
37
    _err_not_available = 'Operation not available on config object attributes! (YFrake)'
38
    _err_no_file = 'Config file does not exist at provided path! (YFrake)'
39
    _err_locked = 'Cannot modify config while session is open! (YFrake)'
40
    _path = Path()
41
    _config = dict()
42
    __instance__ = None
43
44
    # Singleton pattern
45
    # ------------------------------------------------------------------------------------ #
46
    def __new__(cls):
47
        if not cls.__instance__:
48
            cls.__instance__ = super(ConfigSingleton, cls).__new__(cls)
49
        return cls.__instance__
50
51
    # ------------------------------------------------------------------------------------ #
52
    def __init__(self):
53
        if not getattr(self, '_config', None):
54
            self.file = utils.get_default_config_path()
55
            self.HERE = utils.get_cwd_config_path()
56
57
    # ------------------------------------------------------------------------------------ #
58
    @staticmethod
59
    def is_locked() -> bool:
60
        return session.is_locked()
61
62
    # ------------------------------------------------------------------------------------ #
63
    @property
64
    def file(self) -> Path:
65
        return self._path
66
67
    @file.setter
68
    def file(self, path: Path | str) -> None:
69
        if self.is_locked():
70
            raise RuntimeError(self._err_locked)
71
        path = Path(path).resolve()
72
        if not path.exists():
73
            if path != self.HERE:
74
                raise RuntimeError(self._err_no_file)
75
            utils.copy_default_config_to(path)
76
        self._config = helpers.read_config_from(path)
77
        Cache.populate_settings(self.settings)
78
        self._path = path
79
80
    @file.deleter
81
    def file(self) -> None:
82
        raise TypeError(self._err_not_available)
83
84
    # ------------------------------------------------------------------------------------ #
85
    @property
86
    def settings(self) -> dict:
87
        return copy.deepcopy(self._config)
88
89
    @settings.setter
90
    def settings(self, _) -> None:
91
        raise TypeError(self._err_not_available)
92
93
    @settings.deleter
94
    def settings(self) -> None:
95
        raise TypeError(self._err_not_available)
96