-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_params.py
97 lines (83 loc) · 3.23 KB
/
config_params.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
import json
from os import mkdir
from os import getcwd
from os.path import join
from os.path import exists
from time import time
import logging.config
from logger import logger_config
logging.config.dictConfig(logger_config)
logger = logging.getLogger('app_logger')
class ConfigParams(object):
''' class singelton to manage configuration '''
# write default information into fields
data = []
data.append({
"version": 0.1,
"user_name": "user",
"game": "Click-Click",
"delay": 1.1,
"increase_latancy": 1,
"keep_log_file_days": 3,
"path_to_log_file": "log/"
})
def __new__(cls):
''' ensure just one instance of this class '''
if not hasattr(cls, 'instance'):
cls.instance = super(ConfigParams, cls).__new__(cls)
cls.instance.config_file(file='config.json')
return cls.instance
def config_save(self, file='config.json'):
''' save configratoion info'''
try:
with open(file, 'w') as f:
ConfigParams.data[0]['HELP game'] = 'Value in [Twins, Click-Click, Count color, Count colors, Find number, Find object]'
ConfigParams.data[0]['HELP delay'] = 'Value in [0:0.1:5]'
ConfigParams.data[0]['HELP increase_latancy'] = 'Value in [0 1]'
ConfigParams.data[0]['HELP keep_log_file_days'] = 'Value in [1 .. 365]'
ConfigParams.data[0]['HELP path_to_log_file'] = 'path to log file'
json.dump(ConfigParams.data[0], f, indent=4)
except:
logger.info('Could not save configuration file.')
def config_file(self, file='config.json'):
''' format configratoion info'''
try:
with open(file, 'r') as f:
ConfigParams.data.append(json.load(f))
except:
logger.info('Could not load configuration file. Using default parameters.')
return
# parse all keys
if ConfigParams.data[1]['game'] in ['Twins', 'Click-Click', 'Count color', 'Count colors', 'Find number', 'Find object']:
ConfigParams.data[0]['game'] = ConfigParams.data[1]['game']
if ConfigParams.data[1]['delay'] >= 0 and ConfigParams.data[1]['delay'] <= 5:
ConfigParams.data[0]['delay'] = ConfigParams.data[1]['delay']
if ConfigParams.data[1]['increase_latancy'] in [0, 1]:
ConfigParams.data[0]['increase_latancy'] = ConfigParams.data[1]['increase_latancy']
if ConfigParams.data[1]['keep_log_file_days'] >= 1 and ConfigParams.data[1]['keep_log_file_days'] <= 365:
ConfigParams.data[0]['keep_log_file_days'] = ConfigParams.data[1]['keep_log_file_days']
self.make_dir(ConfigParams, 'path_to_log_file')
def make_dir(self, ConfigParams, dir):
''' creates drectory <dir> if not exist and write it to configuration '''
path = join(getcwd(), ConfigParams.data[1][dir])
if exists(path):
ConfigParams.data[0][dir] = path
else:
try:
mkdir(path)
ConfigParams.data[0][dir] = path
except OSError:
logger.info('Creation of the directory {} failed. Try use name with timestamp.'.format(path))
path = join(path, '_', str(int(time())))
mkdir(path)
ConfigParams.data[0][dir] = path
except:
logger.info('Creation of the directory {} failed. Exiting.'.format(path))
# can be tested to convince it Singelton
# usage: python config_params.py
if __name__ == "__main__":
s = ConfigParams()
s1 = ConfigParams()
s.data[0]['user_name'] = '100'
print(s.data[0]['user_name'])
print(s1.data[0]['user_name'])