1 | from backuppc_clone.DataLayer import DataLayer |
||
2 | |||
3 | from typing import Optional |
||
4 | |||
5 | import configparser |
||
6 | import os |
||
7 | |||
8 | |||
9 | View Code Duplication | class Config: |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
10 | """ |
||
11 | Singleton class getting and updating parameters |
||
12 | """ |
||
13 | instance = None |
||
14 | """ |
||
15 | The singleton instance of this class. |
||
16 | |||
17 | :type instance: backuppc_clone.Config.Config |
||
18 | """ |
||
19 | |||
20 | # ------------------------------------------------------------------------------------------------------------------ |
||
21 | def __init__(self, config_filename: str): |
||
22 | """ |
||
23 | Object constructor. |
||
24 | |||
25 | @param str config_filename: The path to the configuration file of the clone. |
||
26 | """ |
||
27 | if Config.instance is not None: |
||
28 | raise Exception("This class is a singleton!") |
||
29 | else: |
||
30 | Config.instance = self |
||
31 | |||
32 | self.__config_filename: str = config_filename |
||
33 | """ |
||
34 | The path to the configuration file of the clone. |
||
35 | """ |
||
36 | |||
37 | self.__stats_filename: Optional[str] = None |
||
38 | """ |
||
39 | The path to the stats file. |
||
40 | """ |
||
41 | |||
42 | self.__top_dir_clone: Optional[str] = None |
||
43 | """ |
||
44 | The top dir of the clone. |
||
45 | """ |
||
46 | |||
47 | self.__tmp_dir_clone: Optional[str] = None |
||
48 | """ |
||
49 | The temp dir of the clone. |
||
50 | """ |
||
51 | |||
52 | self.__top_dir_original: Optional[str] = None |
||
53 | """ |
||
54 | The top dir of the original. |
||
55 | """ |
||
56 | |||
57 | self.__pc_dir_clone: Optional[str] = None |
||
58 | """ |
||
59 | The pc dir of the clone. |
||
60 | """ |
||
61 | |||
62 | self.__pc_dir_original: Optional[str] = None |
||
63 | """ |
||
64 | The pc dir of the original. |
||
65 | """ |
||
66 | |||
67 | # ------------------------------------------------------------------------------------------------------------------ |
||
68 | @property |
||
69 | def stats_file(self) -> str: |
||
70 | """ |
||
71 | Returns the path to the stats file. |
||
72 | |||
73 | :rtype: str |
||
74 | """ |
||
75 | if self.__stats_filename is None: |
||
76 | self.__stats_filename = os.path.join(os.path.dirname(self.__config_filename), 'status.json') |
||
77 | |||
78 | return self.__stats_filename |
||
79 | |||
80 | # ------------------------------------------------------------------------------------------------------------------ |
||
81 | @property |
||
82 | def last_pool_scan(self) -> int: |
||
83 | """ |
||
84 | Returns the timestamp of the last original pool scan. |
||
85 | |||
86 | :rtype: int |
||
87 | """ |
||
88 | return int(DataLayer.instance.parameter_get_value('LAST_POOL_SYNC')) |
||
89 | |||
90 | # ------------------------------------------------------------------------------------------------------------------ |
||
91 | @last_pool_scan.setter |
||
92 | def last_pool_scan(self, value: int) -> None: |
||
93 | """ |
||
94 | Saves the timestamp of the last original pool scan. |
||
95 | |||
96 | @param int value: The timestamp. |
||
97 | """ |
||
98 | DataLayer.instance.parameter_update_value('LAST_POOL_SYNC', str(value)) |
||
99 | |||
100 | # ------------------------------------------------------------------------------------------------------------------ |
||
101 | @property |
||
102 | def pc_dir_clone(self) -> str: |
||
103 | """ |
||
104 | Gives the pc dir of the clone. |
||
105 | |||
106 | :rtype: str |
||
107 | """ |
||
108 | if self.__pc_dir_clone is None: |
||
109 | self.__pc_dir_clone = os.path.join(self.top_dir_clone, 'pc') |
||
110 | |||
111 | return self.__pc_dir_clone |
||
112 | |||
113 | # ------------------------------------------------------------------------------------------------------------------ |
||
114 | @property |
||
115 | def pc_dir_original(self) -> str: |
||
116 | """ |
||
117 | Gives the pc dir of the original. |
||
118 | |||
119 | :rtype: str |
||
120 | """ |
||
121 | if self.__pc_dir_original is None: |
||
122 | config_clone = configparser.ConfigParser() |
||
123 | config_clone.read(self.__config_filename) |
||
124 | |||
125 | config_original = configparser.ConfigParser() |
||
126 | config_original.read(config_clone['Original']['config']) |
||
127 | |||
128 | self.__pc_dir_original = os.path.realpath(config_original['Original']['pc_dir']) |
||
129 | |||
130 | return self.__pc_dir_original |
||
131 | |||
132 | # ------------------------------------------------------------------------------------------------------------------ |
||
133 | @property |
||
134 | def tmp_dir_clone(self) -> str: |
||
135 | """ |
||
136 | Gives the temp dir of the clone. |
||
137 | |||
138 | :rtype: str |
||
139 | """ |
||
140 | if self.__tmp_dir_clone is None: |
||
141 | self.__tmp_dir_clone = os.path.join(self.top_dir_clone, 'tmp') |
||
142 | |||
143 | return self.__tmp_dir_clone |
||
144 | |||
145 | # ------------------------------------------------------------------------------------------------------------------ |
||
146 | @property |
||
147 | def top_dir_clone(self) -> str: |
||
148 | """ |
||
149 | Gives the top dir of the clone. |
||
150 | |||
151 | :rtype: str |
||
152 | """ |
||
153 | if self.__top_dir_clone is None: |
||
154 | self.__top_dir_clone = os.path.realpath(os.path.dirname(self.__config_filename)) |
||
155 | |||
156 | return self.__top_dir_clone |
||
157 | |||
158 | # ------------------------------------------------------------------------------------------------------------------ |
||
159 | @property |
||
160 | def top_dir_original(self) -> str: |
||
161 | """ |
||
162 | Gives the top dir of the original. |
||
163 | |||
164 | :rtype: str |
||
165 | """ |
||
166 | if self.__top_dir_original is None: |
||
167 | config_clone = configparser.ConfigParser() |
||
168 | config_clone.read(self.__config_filename) |
||
169 | |||
170 | self.__top_dir_original = os.path.realpath(os.path.dirname(config_clone['Original']['config'])) |
||
171 | |||
172 | return self.__top_dir_original |
||
173 | |||
174 | # ------------------------------------------------------------------------------------------------------------------ |
||
175 | def backup_dir_clone(self, host: str, backup_no: int) -> str: |
||
176 | """ |
||
177 | Returns the path to a host backup of the clone. |
||
178 | |||
179 | @param str host: The name of the host. |
||
180 | @param int backup_no: The backup number. |
||
181 | |||
182 | :rtype: str |
||
183 | """ |
||
184 | return os.path.join(self.top_dir_clone, 'pc', host, str(backup_no)) |
||
185 | |||
186 | # ------------------------------------------------------------------------------------------------------------------ |
||
187 | def backup_dir_original(self, host: str, backup_no: int) -> str: |
||
188 | """ |
||
189 | Returns the path to a host backup of the original. |
||
190 | |||
191 | @param str host: The name of the host. |
||
192 | @param int backup_no: The backup number. |
||
193 | |||
194 | :rtype: str |
||
195 | """ |
||
196 | return os.path.join(self.pc_dir_original, host, str(backup_no)) |
||
197 | |||
198 | # ------------------------------------------------------------------------------------------------------------------ |
||
199 | def host_dir_clone(self, host: str) -> str: |
||
200 | """ |
||
201 | Returns the path to host of the clone. |
||
202 | |||
203 | @param str host: The name of the host. |
||
204 | |||
205 | :rtype: str |
||
206 | """ |
||
207 | return os.path.join(self.top_dir_clone, 'pc', host) |
||
208 | |||
209 | # ------------------------------------------------------------------------------------------------------------------ |
||
210 | def host_dir_original(self, host: str) -> str: |
||
211 | """ |
||
212 | Returns the path to host of the original. |
||
213 | |||
214 | @param str host: The name of the host. |
||
215 | |||
216 | :rtype: str |
||
217 | """ |
||
218 | return os.path.join(self.top_dir_original, 'pc', host) |
||
219 | |||
220 | # ---------------------------------------------------------------------------------------------------------------------- |
||
221 |