Passed
Push — master ( ba6342...71f198 )
by P.R.
07:12
created

backuppc_clone.Config   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 203
Duplicated Lines 94.58 %

Importance

Changes 0
Metric Value
eloc 62
dl 192
loc 203
rs 10
c 0
b 0
f 0
wmc 18

11 Methods

Rating   Name   Duplication   Size   Complexity  
A Config.pc_dir_clone() 11 11 2
A Config.last_pool_scan() 8 8 1
A Config.host_dir_original() 9 9 1
A Config.__init__() 38 38 2
A Config.tmp_dir_clone() 11 11 2
A Config.host_dir_clone() 9 9 1
A Config.backup_dir_clone() 10 10 1
A Config.backup_dir_original() 10 10 1
A Config.top_dir_clone() 11 11 2
A Config.top_dir_original() 14 14 2
A Config.pc_dir_original() 17 17 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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