codekasten/in-gen/in-gen.py

161 lines
4.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python
import os
import sys
try:
import pygtk
pygtk.require("2.0") #wir wollen gtkv2 nutzen
#print "pygtk importiert"
except:
print "couldn't import 'pygtk'! Try 'apt-get install python-gtk2'."
sys.exit(1)
try:
import gtk
import gtk.glade
#print "gtk.glade importiert"
except:
print "Mit GTKv2 kannst du den Schlafenden wecken!"
sys.exit(1)
try:
import configobj ## needed for reading and writing of the config file
except:
print "couldn't import 'configobj'! Try 'apt-get install python-configobj'."
sys.exit(1)
############################ CONFIG FILE ##########################
class SettingsClass(configobj.ConfigObj):
""" a class for handling the config file(s) using configobj"""
CONF_LOCATIONS = [
"./in-gen.conf",
"~/.in-gen.conf",
]
def __init__(self, config_file=None):
config_file = self.__getConfigFileName(config_file)
self.prefs = self.__getPreferences(config_file)
#TODO: a proper ConfigObj instance knows these variables, read them automatically!
def __getitem__(self, key):
"""redirect all requests to the 'prefs' attribute"""
return self.prefs[key]
def __getPreferences(self, config_file):
"""read the config file and return a configobj object."""
try:
prefs = configobj.ConfigObj(config_file)
if prefs:
pass
else:
raise "failed to load the config file: %s" % config_file
except IOError:
raise "unable to open the config file: %s" % config_file
return prefs
def __getConfigFileName(self, config_file):
# search for the configuration file
import types
if config_file is None:
# no config file was specified - we will look for it in the ususal locations
conf_file_list = [os.path.expanduser(f)
for f in self.CONF_LOCATIONS
if os.path.exists(os.path.expanduser(f))]
if not conf_file_list:
# no possible config file found in the usual locations
raise "could not find a config file"
config_file = conf_file_list[0]
else:
# a config file was specified (e.g. via command line)
if type(config_file) != types.StringType:
raise "invalid config file specified: %s" % config_file
if not os.path.exists(config_file):
raise "could not find the specified configuration file (%s)" % config_file
return config_file
def safe(self):
"""safe the current values of the config to file"""
return self.prefs.write()
def createsection(self,newsection):
"""adds a new empty section to config object"""
self.prefs[newsection] = {}
################################# GUI ######################
class gtkgui:
def __init__(self,gtkext):
# gtk is given, it has to be known in the whole class
self.gtk = gtkext
# define which gladefile to use for frontend
gladefile = "in-gen-glade/in-gen-glade.glade"
self.gui = gtk.glade.XML(gladefile)
# set button handlers
actions = { "on_OkButton_clicked": self.clicked_ok,
"on_mainwindow_destroy": self.exit }
# connect the actions to the events
self.gui.signal_autoconnect (actions)
#load config
self.config = SettingsClass()
#display an unused invoice number
self.display_new_number()
#### functions which get executed on startup
def display_new_number(self):
#get relevant variables
prefix = self.config[section]["prefix"]
lastnumber = self.config[section]["lastnumber"]
suffix = self.config[section]["suffix"]
newnumber = create_number(prefix, lastnumber, suffix)
#display new number in GUI
self.outputfield = self.gui.get_widget("NumberDisplay")
self.textbuffer = self.outputfield.get_buffer()
self.textbuffer.set_text(newnumber)
#### implementation of actions
def clicked_ok(self, widget):
"""save last generated number in config file"""
#update "lastnumber" entry in config
self.config[section]["lastnumber"] = int(self.config[section]["lastnumber"]) + 1
#write config to filesystem
self.config.safe()
#notify user
self.outputfield = self.gui.get_widget("NumberDisplay")
self.textbuffer = self.outputfield.get_buffer()
self.textbuffer.set_text("Saved number has been updated.")
def exit(self, widget):
gtk.main_quit()
############################# INVOICE NUMBER HANDLING ###################
def create_number(prefix, lastnumber, suffix):
"""gets prefix string, last used number and suffix string.
returns new invoice number."""
return str(prefix) + str(int(lastnumber)+1) + str(suffix)
############################ MAIN #################################
#section = str(sys.argv[1])
section = "Handrechnung"
app = gtkgui(gtk)
gtk.main()