-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
71 lines (51 loc) · 1.33 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
# -*- coding: utf-8 -*-
from contextlib import contextmanager
import logging
import logging.config
import os
import sqlite3
logger = logging.getLogger()
LOGGER_CONFIG = {
'version': 1,
'formatters': {
'main': {
'format': '%(asctime)-15s %(message)s',
}
},
'handlers': {
'file': {
'class': 'logging.FileHandler',
'filename': 'pankreator_app.log',
'formatter': 'main'
}
},
'root': {
'handlers': ['file', ],
'level': 'INFO',
'propagate': 'yes'
}
}
def initialize_logging():
logging.config.dictConfig(LOGGER_CONFIG)
@contextmanager
def db_connection(db_path):
try:
connection = sqlite3.connect(db_path,
detect_types=sqlite3.PARSE_DECLTYPES)
yield connection.cursor()
finally:
connection.commit()
connection.close()
def cleanup(config):
def del_files(directory):
[os.remove(os.path.join(directory, f)) for f in os.listdir(directory)]
try:
os.remove(config['files']['zipfile'])
del_files(config['files']['zipdir'])
del_files(config['files']['imagesdir'])
except Exception as e:
logger.error(e)
class APIException(Exception):
pass
class ConverterException(Exception):
pass