This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Globals.py
79 lines (66 loc) · 2.17 KB
/
Globals.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
import Config
import multiprocessing
import logging
import sqlite3
import os
Log = None
db = None
class Database:
def __init__(self, dbFile):
self.dbFile = os.path.abspath(dbFile)
def init_db(self):
try:
conn = sqlite3.connect(self.dbFile)
conn.execute("SELECT * FROM job")
except sqlite3.OperationalError:
Log.debug("Initalizing database file at " + self.dbFile)
qry = open('schema.sql', 'r').read()
conn = sqlite3.connect(self.dbFile)
conn.executescript(qry)
conn.commit()
conn.close()
else:
self.__fix_interrupted()
def __fix_interrupted(self):
self.update("UPDATE job SET status='Interrupted' WHERE status LIKE 'Encoding%'")
def query(self, query, args=(), one=False):
conn = sqlite3.connect(self.dbFile)
cur = conn.execute(query, args)
rv = [dict((cur.description[idx][0], value) for idx, value in enumerate(row)) for row in cur.fetchall()]
return (rv[0] if rv else None) if one else rv
def insert(self, query, args=()):
conn = self.conn()
cur = conn.execute(query, args)
ret = cur.lastrowid
conn.commit()
conn.close()
return ret
def update(self, query, args=()):
conn = self.conn()
cur = conn.execute(query, args)
conn.commit()
conn.close()
return
def conn(self):
return sqlite3.connect(self.dbFile)
def init():
global Config
global Log
global db
Config.loadSettings()
Log = logging.getLogger("WebRake")
Log.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
format = logging.Formatter ( "[%(asctime)s] %(levelname)-8s %(name)s %(message)s", "%Y-%m-%d %H:%M:%S" )
ch.setFormatter( format )
ch.setLevel(logging.DEBUG)
logFile = 'messages.log'
fh = logging.FileHandler(logFile)
fh.setFormatter( format )
ch.setLevel(logging.DEBUG)
Log.addHandler(ch)
Log.debug("Stdout log handler initialized")
Log.addHandler(fh)
Log.debug("Log file %s initialized" % logFile)
db = Database(Config.Database)
db.init_db()