Skip to content

Commit

Permalink
refactoring of db-related stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
leonid-shevtsov committed Jun 11, 2011
1 parent 9ce5d2d commit 91042c0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
10 changes: 5 additions & 5 deletions modules/cronograph/cron.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from db import *
import db
import sys

def handle():
from subprocess import Popen, PIPE
Expand All @@ -8,9 +9,8 @@ def handle():
out = None
err = None
exit_code = None


db = ensure_db_structure(spawn_db(get_database_name()))
database = db.ensure_structure(db.spawn())

args = sys.argv
args.pop(0)
Expand Down Expand Up @@ -38,8 +38,8 @@ def handle():

data = [' '.join(args), start_time, end_time, out, err, exit_code]

db.execute('insert into cronjobs (command_line, start_time, end_time, stdout, stderr, exit_code) values(?, ?, ?, ?, ?, ?)', data)
db.commit()
database.execute('insert into cronjobs (command_line, start_time, end_time, stdout, stderr, exit_code) values(?, ?, ?, ?, ?, ?)', data)
database.commit()

if should_notify_by_email(*data):
print_email_notification(*data)
Expand Down
14 changes: 9 additions & 5 deletions modules/cronograph/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
import sys
import sqlite3

def get_database_name():
return sys.argv.pop(1) if len(sys.argv)>1 and sys.argv[1].endswith('.sqlite3') else os.path.expanduser('~/.cronograph.sqlite3')
_database_name = None

def ensure_db_structure(db):
def database_name():
global _database_name
_database_name = _database_name or (sys.argv.pop(1) if len(sys.argv)>1 and sys.argv[1].endswith('.sqlite3') else os.path.expanduser('~/.cronograph.sqlite3'))
return _database_name

def ensure_structure(db):
db.execute('''
CREATE TABLE IF NOT EXISTS cronjobs (
start_time INTEGER NOT NULL,
Expand All @@ -18,7 +22,7 @@ def ensure_db_structure(db):
''')
return db

def spawn_db(database_name):
db = sqlite3.connect(database_name)
def spawn():
db = sqlite3.connect(database_name())
db.row_factory = sqlite3.Row
return db
31 changes: 19 additions & 12 deletions modules/cronograph/server.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
from db import *
import db
import sys

def serve():
import web

sys.argv.pop(1)

urls = (
'/', 'index'
)

database_name = get_database_name()
ensure_db_structure(spawn_db(database_name))

class index:
def GET(self):
result = ""
for row in spawn_db(database_name).execute('SELECT * FROM cronjobs'):
result += row['stdout']
return result
db.ensure_structure(db.spawn())

app = web.application(urls, {'index': index})
app = web.application(urls, globals())
app.run()

class Request:
def __init__(self):
self._db = None

def db(self):
self._db = self._db or db.spawn()
return self._db

class index(Request):
def GET(self):
result = ""
for row in self.db().execute('SELECT * FROM cronjobs'):
result += row['stdout']
return result

0 comments on commit 91042c0

Please sign in to comment.