forked from aoaostar/alidrive-uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
68 lines (50 loc) · 1.88 KB
/
common.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
# -*- coding: utf-8 -*-
# +-------------------------------------------------------------------
# | 公共函数类
# +-------------------------------------------------------------------
# | Author: 李小恩 <[email protected]>
# +-------------------------------------------------------------------
import hashlib
import os
import random
import time
def get_hash(filepath):
with open(filepath, 'rb') as f:
sha1 = hashlib.sha1()
while True:
data = f.readline()
if not data:
break
sha1.update(data)
return sha1.hexdigest()
def format_path(path):
return path.replace('/', os.sep).replace('\\\\', os.sep).rstrip(os.sep) + os.sep
def print_info(message):
i = random.randint(34, 37)
log(message)
print('\033[7;30;{i}m{message}\033[0m'.format(message=message, i=i))
def print_warn(message):
log_error(message)
print('\033[7;30;33m{message}\033[0m'.format(message=message))
def print_error(message):
log_error(message)
print('\033[7;30;31m{message}\033[0m'.format(message=message))
def print_success(message):
log(message)
print('\033[7;30;32m{message}\033[0m'.format(message=message))
def date(timestamp):
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp))
def log_error(message):
file = (os.getcwd() + '/log/' + time.strftime("%Y-%m-%d", time.localtime())
+ 'error.log')
if not os.path.exists(os.path.dirname(file)):
os.mkdir(os.path.dirname(file))
with open(file, 'a') as f:
f.write(f'【{date(time.time())}】{message}\n')
def log(message):
file = (os.getcwd() + '/log/' + time.strftime("%Y-%m-%d", time.localtime())
+ 'standard.log')
if not os.path.exists(os.path.dirname(file)):
os.mkdir(os.path.dirname(file))
with open(file, 'a') as f:
f.write(f'【{date(time.time())}】{message}\n')