-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
62 lines (49 loc) · 1.84 KB
/
utils.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
######################################################################################################
# Created by Leonardo Viana Teixeira at 17/10/2018 #
######################################################################################################
import os
import re
# Varibles of the printd Debug function.
DEBUG = True
DEBUG_lvl = 1
def printd(str, lvl=1):
"""
Function to facilitate the debug.
It only shows the message if the message lvl is below the DEBUG lvl activated on that moment.
"""
if DEBUG and DEBUG_lvl >= lvl:
print("{}".format(str))
def folder_exists(path_save):
"""
Function that verifies if a folder exist if not it creates the folder.
:param path_save: str (path)
Path to be verified/created
:return: nothing
"""
if not (os.path.exists(path_save)):
os.mkdir(path_save)
def read_cfg(file):
appended_text = []
with open(file) as f:
for line in f:
content = line.strip().replace(" ", "")
if len(content) and content[0] != "#" and content[0] != ";":
if content[0] != "+":
key, value = content.split("=", 1)
appended_text.append("--{}".format(key))
readPath(value)
appended_text.append(value)
else:
appended_text[-1] = "{}{}".format(appended_text[-1], content[1:])
return appended_text
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise Exception('Boolean value expected.')
def readPath(path):
if path.lower()[:2] == "..":
path = os.path.join(os.path.dirname(__file__), path[3:])
return path