-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.py
executable file
·179 lines (153 loc) · 4.85 KB
/
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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import re
from datetime import datetime
import time
try:
# For Python 3.0 and later
from urllib import parse as urlparse
except ImportError:
# Fall back to Python 2
from urlparse import urlparse
import base64
def command_encode(command):
"""
Encode the command array in a command-string
"""
return "\n".join(_command_encode(command))
def _command_encode(command):
r = []
if type(command) == int:
command = str(command)
if type(command) == str:
return ["=" + command]
elif type(command) == dict:
for k,v in command.items():
for v2 in _command_encode(v):
r.append(k.upper() + v2)
elif type(command) == list:
for (i, v) in enumerate(command):
for v2 in _command_encode(v):
r.append(str(i) + v2)
else:
pass
return r
def response_to_hash(response):
"""
Convert the response string as a hash
"""
r = {'PROPERTY':{}}
re1 = re.compile(r'^([^\=]*[^\t\= ])[\t ]*=[\t ]*(.*)')
re2 = re.compile(r'^property\[([^\]]*)\]', re.IGNORECASE)
for line in response.split("\n"):
m1 = re1.match(line)
if not m1: continue
attr = m1.group(1)
value = m1.group(2)
value = re.sub(r'[\t ]*$', '', value)
m2 = re2.match(attr)
if m2:
prop = m2.group(1).upper()
prop = re.sub(r'\s', '', prop)
if prop not in r['PROPERTY']:
r['PROPERTY'][prop] = []
r['PROPERTY'][prop].append(value)
else:
r[attr.upper()] = value
if 'CODE' in r:
r['CODE'] = int(r['CODE'])
if 'RUNTIME' in r:
r['RUNTIME'] = float(r['RUNTIME'])
if 'QUEUETIME' in r:
r['QUEUETIME'] = float(r['QUEUETIME'])
return r
def response_to_list_hash(response):
"""
Convert the response string as a list hash
"""
list_hash = {
'CODE': "",
'DESCRIPTION': "",
'RUNTIME': "",
'QUEUETIME': "",
'ITEMS': []
}
if 'CODE' in response: list_hash['CODE'] = response['CODE']
if 'DESCRIPTION' in response: list_hash['DESCRIPTION'] = response['DESCRIPTION']
if 'RUNTIME' in response: list_hash['RUNTIME'] = response['RUNTIME']
if 'QUEUETIME' in response: list_hash['QUEUETIME'] = response['QUEUETIME']
count = 0
if 'PROPERTY' in response:
columns = None
if 'COLUMN' in response['PROPERTY']:
list_hash['COLUMNS'] = response['PROPERTY']['COLUMN']
columns = {}
for col in list_hash['COLUMNS']:
columns[col] = 1
else:
list_hash['COLUMNS'] = []
for prop,values in response['PROPERTY'].items():
if prop == "COLUMN": continue
if prop in ['FIRST', 'LAST', 'LIMIT', 'COUNT', 'TOTAL']:
list_hash[prop] = int(response['PROPERTY'][prop][0])
else:
if columns and not columns[prop]: continue
list_hash['COLUMNS'].append(prop)
index = 0
for v in values:
if index >= len(list_hash['ITEMS']):
list_hash['ITEMS'].append({})
list_hash['ITEMS'][index][prop] = v
index = index + 1
if index > count: count = index
if not 'COUNT' in list_hash: list_hash['COUNT'] = count
if not 'FIRST' in list_hash: list_hash['FIRST'] = 0
if not 'TOTAL' in list_hash: list_hash['TOTAL'] = list_hash['COUNT']
if 'FIRST' in list_hash and 'LIMIT' in list_hash and list_hash['LIMIT'] > 0:
list_hash['PAGE'] = int(list_hash['FIRST'] / list_hash['LIMIT']) + 1
if list_hash['PAGE'] > 1:
list_hash['PREVPAGE'] = list_hash['PAGE'] - 1
list_hash['PREVPAGEFIRST'] = (list_hash['PREVPAGE'] - 1) * list_hash['LIMIT']
list_hash['NEXTPAGE'] = list_hash['PAGE'] + 1
list_hash['NEXTPAGEFIRST'] = (list_hash['NEXTPAGE'] - 1) * list_hash['LIMIT']
if 'TOTAL' in list_hash and 'LIMIT' in list_hash and list_hash['LIMIT'] > 0:
list_hash['PAGES'] = int(list_hash['TOTAL'] + list_hash['LIMIT'] - 1) / list_hash['LIMIT']
list_hash['LASTPAGEFIRST'] = (list_hash['PAGES'] - 1) * list_hash['LIMIT']
if 'NEXTPAGE' in list_hash and (list_hash['NEXTPAGE'] > list_hash['PAGES']):
list_hash['NEXTPAGE'] = None
list_hash['NEXTPAGEFIRST'] = None
return list_hash
def sqltime(timestamp = None):
"""
Convert the Unix-Timestamp to a SQL datetime
If no timestamp given, returns the current datetime
"""
if timestamp:
return datetime.fromtimestamp(int(timestamp)).strftime('%Y-%m-%d %H:%M:%S')
else:
return datetime.now().strftime('%Y-%m-%d %H:%M:%S')
def timesql(sqldatetime):
"""
Convert the SQL datetime to Unix-Timestamp
"""
return int(time.mktime(datetime.strptime(sqldatetime, '%Y-%m-%d %H:%M:%S').timetuple()))
def url_encode(string):
"""
URL-encodes string
This function is convenient when encoding a string to be used in a query part of a URL
"""
return urlparse.quote(string)
def url_decode(string):
"""
Decodes URL-encoded string Decodes any %## encoding in the given string.
"""
return urlparse.unquote(string)
def base64_encode(string):
"""
Encodes data with MIME base64
This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies.
"""
return base64.b64encode(string)
def base64_decode(string):
"""
Decodes data encoded with MIME base64
"""
return base64.b64decode(string)