This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
117 lines (89 loc) · 3.05 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import json
import logging
import os
from dnsProvider import ProviderType
class Configuration:
DEFAULT_CONFIG = "config.json.default"
def __init__(self, config: str = DEFAULT_CONFIG):
self._file_path = config
self._settings = {}
def __isValid(self, setting: list) -> bool:
if 'domain' not in setting or not setting['domain']:
return False
if 'key' not in setting or not setting['key']:
return False
if 'secret' not in setting or not setting['secret']:
return False
if 'interface' not in setting or not setting['interface']:
return False
if 'name' not in setting or not setting['name']:
return False
if 'ipv6' not in setting or not setting['ipv6']:
return False
if 'provider' not in setting or not setting['provider']:
return False
return True
def __load(self, path: str):
with open(path, 'r') as json_file:
temp_settings = json.load(json_file)
if self.__isValid(temp_settings) == False:
raise ValueError('Invalid configuration file')
self._settings = temp_settings
def load(self):
return self.__load(self._file_path)
@property
def settings(self):
return self._settings
@property
def file(self) -> str:
return self._file_path
@file.setter
def file(self, new_file: str):
if os.path.isfile(new_file):
try:
self.__load(new_file)
except Exception as e:
raise Exception from e
self._file_path = new_file
logging.info('configuration is updated from {0}'.format(self._file_path))
else:
raise FileNotFoundError('file: ' + new_file + ' is not found')
@property
def domain(self) -> str:
return self._settings['domain']
@property
def key(self) -> str:
return self._settings['key']
@property
def secret(self) -> str:
return self._settings['secret']
@property
def interface(self) -> str:
return self._settings['interface']
@property
def name(self) -> str:
return self._settings['name']
@property
def fulldomain(self) -> str:
if self.name == '@':
return self.domain
else:
return self.name + '.' + self.domain
@property
def log(self) -> str:
if 'log' not in self._settings or not self._settings['log']:
return None
return self._settings['log']
@property
def ipv6(self) -> bool:
if self._settings['ipv6'].upper() == 'TRUE':
return True
return False
@property
def provider(self) -> ProviderType:
if self._settings['provider'].upper() == 'GODADDY':
return ProviderType.PROVIDER_GODADDY
return ProviderType.PROVIDER_GOOGLE
def dump(self):
logging.info('Configuration file: ' + self._file_path)
logging.info('Configuration: ' + json.dumps(self._settings))