-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.py
104 lines (84 loc) · 3.45 KB
/
settings.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
# -*- coding: utf-8 -*-
import os
import re
import logging
import operator
from datetime import datetime
class MissingFileSettings(Exception):
pass
class MissingValueRequired(Exception):
pass
class Settings(object):
FILE_SETTINGS = "setencas.settings"
def __init__(self, file_path=None):
self.file_path = file_path if file_path is not None else self.find_settings_file()
self.settings_values = {"path": "required", 'mongo':' required'}
self.extract_settings()
def find_settings_file(self):
search_paths = [
os.path.join(os.path.dirname(os.path.realpath(__file__)), self.FILE_SETTINGS),
os.path.join(os.path.expanduser("~"), self.FILE_SETTINGS),
os.path.join(os.path.expanduser("~"), "..", self.FILE_SETTINGS),
os.path.join(os.getcwd(), self.FILE_SETTINGS),
os.path.join(os.getcwd(), "..", self.FILE_SETTINGS)
]
settings_path = None
for file_path in search_paths:
if os.path.exists(file_path):
settings_path = file_path
return settings_path
def get_setting(self, setting, line):
regex_text = setting + ": (.*)"
m = re.search(regex_text, line)
if m is not None:
return m.group(1)
return None
def read_settings(self):
if self.file_path is None or not os.path.isfile(self.file_path):
raise MissingFileSettings("O arquivo de configuracao nao foi encontrado")
with open(self.file_path, "r") as handle:
text = handle.read()
return text
def extract_settings(self):
text = self.read_settings()
for setting, value in self.settings_values.items():
found = self.get_setting(setting, text)
if value == "required" and found == None:
raise MissingValueRequired("Setting %s not found on file %s" % (setting, self.file_path))
self.__dict__[setting] = found
def join(self, path, *args):
path_all = os.path.join(self.path, path)
for p in list(args):
path_all = os.path.join(path_all, p)
return path_all
def createLogFile(self, name_):
name = name_ + datetime.now().strftime("%d%m%Y_%H_%M_%S")
log_file = name + ".txt"
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
log_file = os.path.join(self.path, "log", log_file)
print("log file", log_file)
handler = logging.FileHandler(log_file)
handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(logging.INFO)
handlers = logger.handlers[:]
for h in handlers:
h.close()
logger.removeHandler(h)
logger.addHandler(handler)
return logger
def getLastFile(self, dir_="log"):
dir_path = os.path.join(self.path, dir_)
dict_dir = {}
for filename in os.listdir(dir_path):
dict_dir[filename] = os.stat(os.path.join(dir_path, filename)).st_mtime
return sorted(dict_dir.items(), key=operator.itemgetter(1), reverse=True)[0][0]
def replace_settings(self, setting, value):
setting = "(" + setting + ")" + " (\d+)"
print(setting)
text = ""
with open(self.file_path, "r") as handle:
text = handle.read()
text = re.sub(setting, r"\1 " + value, text)
with open(self.file_path, "w") as handle:
handle.write(text)