This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathutils.py
124 lines (95 loc) · 3.7 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import sys
import configparser
import os
import datetime
import pwdutil #local util for database password
import pymysql # http://pymysql.readthedocs.io/en/latest/
# https://github.com/PyMySQL/PyMySQL
class ConfigFileAccessError(Exception):
pass
def fileexists(CONFIGFILE):
return(os.path.isfile(CONFIGFILE) )
def get_config():
""" Load parameter and configuration values from the CONFIGFILE
A nested dictionary is passed back in following format
{"ConfigClass" : { param1 : value, param2 : value ... }
The config file is in standard Python .ini fmt, EG:
[MySQL]
server: 192.168.56.100
dbuser: simplebot
dbname: simplebot
dbcharset: utf8mb4
The above example can then be ref'd:
config = utils.get_config()
username = config["MySQL"]["dbuser"]
"""
CONFIGFILE = "./config/config.ini"
Config = configparser.ConfigParser()
config = {} # Dictionary of "section" keys. Each value is a sub-dict of key-vals
if fileexists(CONFIGFILE):
Config.read(CONFIGFILE)
for section in Config.sections():
subdict = {}
options = Config.options(section)
for option in options:
key = option
val = Config.get(section,option)
subdict[option] = Config.get(section,option)
config[section] = subdict
else:
raise ConfigFileAccessError(CONFIGFILE)
return config
def query_yes_no(question, default="yes"):
"""Ask a yes/no question via raw_input() and return their answer.
The "answer" return value is True for "yes" or False for "no".
- a Cut-and-Paste piece of code from Stack Overflow
"""
valid = {"yes": True, "y": True, "ye": True,
"no": False, "n": False}
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")
# Flatten out a list of lists (taken from SO: http://stackoverflow.com/questions/10823877/what-is-the-fastest-way-to-flatten-arbitrarily-nested-lists-in-python
def flatten(container):
for i in container:
if isinstance(i, (list,tuple)):
for j in flatten(i):
yield j
else:
yield i
def db_connection(host, user, dbname, charset = "utf8mb4"):
"""
Connect to a MySQL Database Server
"""
key = pwdutil.get_key()
encoded = pwdutil.get_pwd()
password = pwdutil.decode(key,encoded)
connection = pymysql.connect(host = host
, user = user
, password = password
, db = dbname
, charset = charset
, cursorclass=pymysql.cursors.DictCursor)
return connection
def db_connectionID(cursor):
cursor.execute('SELECT connection_id()', (None))
value = cursor.fetchone()["connection_id()"]
return(value)
def timestamp_string():
timestamp_string = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
return(timestamp_string)