-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.py
102 lines (73 loc) · 2.53 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
from configparser import ConfigParser
from inn_requests import get_auth_cookie
class Config:
config = None
def __init__(self):
self.config = ConfigParser()
self.config.read("config.ini")
self.validate_properties()
@property
def get_config(self):
return self.config
def set_cookies(self):
self.config['COOKIES'] = \
{
'upd_inn': get_auth_cookie()
}
self.write_data_to_file()
def set_timeouts(self, timeout=0, interval=1):
self.config['TIMEOUTS'] = \
{
'timeout': timeout,
'interval': interval
}
self.write_data_to_file()
def set_paths(self, inner_path=None, outer_path=None, proxy_path=None):
if inner_path is None and self.is_property_exist('PATHS'):
inner_path = self.config['PATHS']['innerpath']
if outer_path is None and self.is_property_exist('PATHS'):
outer_path = self.config['PATHS']['outerpath']
if proxy_path is None and self.is_property_exist('PATHS'):
proxy_path = self.config['PATHS']['proxypath']
if outer_path[-13:] != 'completed.txt':
outer_path += '\\completed.txt'
self.config['PATHS'] = \
{
'innerPath': inner_path,
'outerPath': outer_path,
'proxypath': proxy_path,
}
self.write_data_to_file()
def set_proxies(self, proxy):
self.config['PROXIES'] = \
{
'https': proxy
}
self.write_data_to_file()
def write_data_to_file(self):
with open('config.ini', 'w') as configfile:
self.config.write(configfile)
def is_property_exist(self, property_name):
"""
Check is property exist
:param property_name: Name of property what you want to check
:return: boolean
"""
try:
self.config[property_name]
except KeyError:
return False
return True
def validate_properties(self):
"""
Check is properties exist, set default values in it if not
:return: None
"""
if not self.is_property_exist('COOKIES'):
self.set_cookies()
if not self.is_property_exist('TIMEOUTS'):
self.set_timeouts(0, 1)
if not self.is_property_exist('PATHS'):
self.set_paths('', '.', '')
if not self.is_property_exist('PROXIES'):
self.set_proxies('')