-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdio.py
205 lines (181 loc) · 5.25 KB
/
stdio.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import datetime
import webbrowser
from random import randrange
from operator import itemgetter
def read_config_file(file_name):
lines = []
try:
f = open(file_name, "r")
try:
lines = f.readlines()
finally:
f.close()
conf_ini = ini_file_to_dic(lines)
return conf_ini
except IOError:
print ('erro ao ler ini')
return {}
def ini_file_to_dic(lines):
dic = {}
if not lines == []:
for n in lines:
dum = n.split('=')
if len(dum) > 1:
dic[dum[0]] = dum[1].strip('\n')
else:
dic[dum[0].strip('\n')] = None
dic['error'] = False
else:
dic['error'] = True
return dic
def read_file(file_name, mode=1):
try:
f = open(file_name, "r")
try:
if mode == 1:
# lines into a list.
toto = f.readlines()
elif mode == 2:
# Read the entire contents of a file at once.
toto = f.read()
elif mode == 3:
# OR read one line at a time.
toto = f.readline()
finally:
f.close()
except IOError:
toto = 'error ao carregar ficheiro:' + file_name
return toto
def log_write(file_name, content):
d = datetime.datetime.now().strftime("%Y.%b.%d %H:%M:?")
with open(file_name, "a") as myfile:
myfile.write(d + ': ' + content + '\n')
def wipe(path):
with open(path, 'wb') as fout:
fout.write(os.urandom(randrange(1309, 7483)))
def int_format(number, sep=' '):
s = '%d' % number
groups = []
while s and s[-1].isdigit():
groups.append(s[-3:])
s = s[:-3]
return s + sep.join(reversed(groups))
def float_format(number, sepi=' ', sepf=','):
""" o dois é a precisão do numero flutuante """
try:
dum = str(number).split('.')
# print dum
toto = int_format(int(dum[0]), sepi) + sepf + dum[1][:2]
except:
return '0' + sepf + '00'
return toto
def sort_files_by_last_modified(files):
""" Given a list of files, return them sorted by the last
modified times. """
fileData = {}
for fname in files:
fileData[fname] = os.stat(fname).st_mtime
fileData = sorted(list(fileData.items()), key=itemgetter(1))
return fileData
def internet_on():
try:
response = urllib.request.urlopen('http://google.com', timeout=1)
return True
except urllib.error.URLError as err: pass
return False
def delete_file(path, echo=False):
if os.path.isfile(path):
if echo:
os.remove(path)
def file_ok(file):
if os.path.exists(file):
return True
else:
return False
def dir_ok(path, create=True):
if os.path.isdir(path):
pass
else:
if create:
os.makedirs(path)
def last_file(path):
files_by_date = sort_files_by_last_modified(path)
if not files_by_date:
toto = -1
else:
toto = files_by_date[0][0]
return toto
def hashfile(afile, hasher, blocksize=65536):
buf = afile.read(blocksize)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(blocksize)
return hasher.digest()
def zip_file(filename):
import gzip
dum = filename + '.gz'
dum = dum[:dum.rfind('.xml')]
f_in = open(filename, 'rb')
f_out = gzip.open(filename + '.gz', 'wb')
f_out.writelines(f_in)
f_out.close()
f_in.close()
if not file_ok(dum + '.gz'):
os.rename(filename + '.gz', dum + '.gz', )
return dum + '.gz'
def make_file_name(db_name, drive):
tdate = datetime.datetime.now().strftime("%Y-%m-%d-%Hh-%Mm")
return drive + db_name + '_' + tdate + '.backup'
def clean_isbn(a):
a = a.strip()
a = a.strip(',')
a = a.strip('-')
try:
a = int(a)
except ValueError:
a = -1
return a
def authors_process(hl):
hl = hl.replace('&', ',')
hl = hl.replace(';', ',')
hl = hl.replace(', ', ',')
hl = hl.split(',')
xl = ''
for n in hl:
a = n.title()
a = a.strip()
xl += a + ','
xl = xl.rstrip(',')
return xl
def postgresql(data_params):
print ('--------Debug---------')
print ('postgresql')
print (data_params)
print ('------end debug-------')
backup_file = make_file_name(data_params['db_database'], data_params['backup_dir'] )
print('Fazendo backup do Postgresql')
print('Dest:',)
exec_string = 'PGPASSWORD=\"' + data_params['db_password'] + '\" pg_dump -h ' + data_params['db_host'] + ' -p 5432 -U root -Fc -f ' \
+ backup_file + ' ' + data_params['db_database']
os.system(exec_string)
def remove_duplicates(values):
output = []
seen = set()
for value in values:
# If value has not been encountered yet,
# ... add it to both list and set.
if value not in seen:
output.append(value)
seen.add(value)
return output
def search_internet(txt=None):
if txt is None:
webbrowser.open('''https://www.google.com''')
else:
url = "https://www.google.com.tr/search?q={}".format(txt)
webbrowser.open(url)
if __name__ == '__main__':
pass