-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdt_util.py
91 lines (75 loc) · 2.77 KB
/
dt_util.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
import webbrowser
import sys
import pymssql
import datetime
def str2bool(v):
if v.lower().strip() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower().strip() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise Error('Boolean value expected.')
def ask_yes_no_question(question):
answer = input('%s (y/n) ' %question)
return str2bool(answer)
def check_if_json(str):
if "<html>" in str:
print(str)
return False
if "<title>" in str:
print(str)
return False
return True
def logged_in(response, a_config):
a_config.logger.info("Checking if you're logged into DT.")
if "<title>DT - Login</title>" in response:
strMessage = "You need to login into DzjinTonik first... Be sure to set the correct variables on top of this file!"
print(strMessage)
a_config.logger.error(strMessage)
webbrowser.open(a_config.domain)
sys.exit()
return False
elif "<title>" in response:
print(response)
print("An other error occured... Please read the lines above! SUGGESTION: Is your model correct? Are you sending an ID?")
return False
elif "<html>" in response:
print(response)
print("Your output is html, which is not nice!")
return False
return True
def get_sql_list_from_file(config, sql_file):
conn = pymssql.connect(server = config.sql_server, database = config.sql_database)
cursor = conn.cursor(as_dict=True)
message = "Trying to connect to %s/%s" %(config.sql_server, config.sql_database)
print(message)
strQry = None
try:
with open(sql_file, 'r') as myfile:
strQry = myfile.read().replace('\n', ' ')
#strQry = myfile.readlines()
except Exception as e:
print("Sorry, I cannot read your SQL file: %s!" %file)
print(strQry)
if strQry is not None:
cursor.execute(strQry)
return cursor.fetchall()
return None
def get_date_from_string(datestring):
if datestring is not None:
return datetime.datetime.strptime(datestring, '%Y-%m-%dT%H:%M:%S')
return datestring
def get_list_from_file(file, separator = None):
my_list_to_return = []
with open(file) as f:
content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
content = [x.strip() for x in content]
if separator is not None:
print("Your lines will be separated with a: '%s'" %separator)
for current_line in content:
my_new_list = current_line.split(separator)
my_list_to_return.append(my_new_list)
else:
my_list_to_return = content[:]
return my_list_to_return