Konfigurasjonsfil; mulighet for å gjøre programmet uavsluttbart.

This commit is contained in:
Øystein Ingmar Skartsæterhagen 2010-05-11 15:04:40 +00:00
parent 102f6b91cc
commit dcb249991b
3 changed files with 27 additions and 7 deletions

2
conf.py Normal file
View File

@ -0,0 +1,2 @@
db_url = 'sqlite:///data'
quit_allowed = True

3
db.py
View File

@ -2,8 +2,9 @@ from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, cre
from sqlalchemy.orm import sessionmaker, relationship, backref from sqlalchemy.orm import sessionmaker, relationship, backref
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
import datetime import datetime
import conf
engine = create_engine('sqlite:////var/local/pvvvv/dibbler/data') engine = create_engine(conf.db_url)
Base = declarative_base() Base = declarative_base()
Session = sessionmaker(bind=engine) Session = sessionmaker(bind=engine)

View File

@ -1,3 +1,5 @@
#!/usr/bin/python
import sqlalchemy import sqlalchemy
import re import re
from helpers import * from helpers import *
@ -13,7 +15,7 @@ class ExitMenu(Exception):
class Menu(): class Menu():
def __init__(self, name, items=[], prompt='> ', def __init__(self, name, items=[], prompt='> ',
return_index=True, return_index=True,
exit_msg=None, exit_confirm_msg=None, exit_msg=None, exit_confirm_msg=None, exit_disallowed_msg=None,
help_text=None): help_text=None):
self.name = name self.name = name
self.items = items self.items = items
@ -21,11 +23,15 @@ class Menu():
self.return_index = return_index self.return_index = return_index
self.exit_msg = exit_msg self.exit_msg = exit_msg
self.exit_confirm_msg = exit_confirm_msg self.exit_confirm_msg = exit_confirm_msg
self.exit_disallowed_msg = exit_disallowed_msg
self.help_text = help_text self.help_text = help_text
self.context = None self.context = None
self.header_format = '[%s]' self.header_format = '[%s]'
def exit_menu(self): def exit_menu(self):
if self.exit_disallowed_msg != None:
print self.exit_disallowed_msg
return
if self.exit_confirm_msg != None: if self.exit_confirm_msg != None:
if not confirm(self.exit_confirm_msg): if not confirm(self.exit_confirm_msg):
return return
@ -212,8 +218,8 @@ product name or barcode.
if self.help_text == None: if self.help_text == None:
print 'no help here' print 'no help here'
else: else:
print 'Help for %s' % (self.header_format%self.name)
print print
print 'Help for %s:' % (self.header_format%self.name)
print self.help_text print self.help_text
def execute(self): def execute(self):
@ -255,8 +261,8 @@ class Selector(Menu):
print 'This is a selection menu. Enter one of the listed numbers, or' print 'This is a selection menu. Enter one of the listed numbers, or'
print '\'exit\' to go out and do something else.' print '\'exit\' to go out and do something else.'
else: else:
print 'Help for selector (%s)' % self.name
print print
print 'Help for selector (%s):' % self.name
print self.help_text print self.help_text
@ -627,6 +633,17 @@ main = Menu('Dibbler main menu',
], ],
exit_msg='happy happy joy joy', exit_msg='happy happy joy joy',
exit_confirm_msg='Really quit Dibbler? (y/n) ') exit_confirm_msg='Really quit Dibbler? (y/n) ')
if not conf.quit_allowed:
main.exit_disallowed_msg = 'You can check out any time you like, but you can never leave.'
while True:
try:
main.execute() main.execute()
except KeyboardInterrupt:
print
print 'Interrupted.'
except:
print 'Something went wrong.'
print '%s: %s' % sys.exc_info()[0:1]
else:
break
print 'Restarting main menu.'