From a0778661c65af89f1c7bf93c5675698907bcea08 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Sat, 10 Feb 2007 13:52:59 +0000 Subject: [PATCH] - configuration information file (like mailman) - GNU GPL information in each file - more pythonic way to define attributes - exception class git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/gnuviech.info/gnuviechadmin/trunk@215 a67ec6bc-e5d5-0310-a910-815c51eb3124 --- createclient.py | 40 ++++++++++++++------ gnuviechadmin/Defaults.py | 34 +++++++++++++++++ gnuviechadmin/__init__.py | 25 +++++++++---- gnuviechadmin/client.py | 70 ++++++++++++++++++++--------------- gnuviechadmin/exceptions.py | 30 +++++++++++++++ gnuviechadmin/gva_cfg.py.tmpl | 48 ++++++++++++++++++++++++ 6 files changed, 200 insertions(+), 47 deletions(-) create mode 100644 gnuviechadmin/Defaults.py create mode 100644 gnuviechadmin/exceptions.py create mode 100644 gnuviechadmin/gva_cfg.py.tmpl diff --git a/createclient.py b/createclient.py index 83c2b5c..4eedf8c 100755 --- a/createclient.py +++ b/createclient.py @@ -1,16 +1,28 @@ #!/usr/bin/env python # -*- coding: UTF-8 -*- # -# This file is part of gnuviechadmin. +# Copyright (C) 2007 by Jan Dittberner. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +# USA. # -# Author: Jan Dittberner -# Copyright (c) 2007 Jan Dittberner # Version: $Id$ -import getopt, sys -from sqlalchemy import * +import getopt, sys, sqlalchemy -from gnuviechadmin import client +from gnuviechadmin import client, exceptions def usage(): print """Usage: %s [-h|--help] [-v|--verbose] @@ -91,13 +103,19 @@ def main(): clientdata["phone"] = a if verbose: print "parsed client data is ", clientdata - myclient = client.Client(clientdata) - if not myclient: + try: + myclient = client.Client(**clientdata) + except exceptions.MissingFieldsError, mfe: + print mfe usage() sys.exit(2) - sess = create_session() - sess.save(myclient) - sess.flush() + try: + sess = sqlalchemy.create_session() + sess.save(myclient) + sess.flush() + except sqlalchemy.exceptions.SQLError, e: + print "saving client failed: ", e + sys.exit(2) if verbose: print myclient diff --git a/gnuviechadmin/Defaults.py b/gnuviechadmin/Defaults.py new file mode 100644 index 0000000..45dea90 --- /dev/null +++ b/gnuviechadmin/Defaults.py @@ -0,0 +1,34 @@ +# -*- python -*- +# +# Copyright (C) 2007 by Jan Dittberner. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +# USA. +# +# Version: $Id$ + +"""Distributed default settings for significant GnuviechAdmin config +variables.""" + +# NEVER make site configuration changes to this file. ALWAYS make +# them in gva_cfg.py instead, in the designated area. See the +# comments in that file for details. + +#---------------------------------------------------------------------- +# The database connection string in a format usable for +# sqlalchemy. The default is an sqlite in memory database which is not +# very usable for a real installation. +# +GVA_DBE = 'sqlite:///:memory:' diff --git a/gnuviechadmin/__init__.py b/gnuviechadmin/__init__.py index de653d6..970bc7c 100644 --- a/gnuviechadmin/__init__.py +++ b/gnuviechadmin/__init__.py @@ -1,11 +1,22 @@ -# This file is part of gnuviechadmin. # -*- coding: UTF-8 -*- # -# Author: Jan Dittberner -# Copyright (c) 2007, Jan Dittberner +# Copyright (C) 2007 by Jan Dittberner. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +# USA. +# # Version: $Id$ -""" -This is the gnuviechadmin package. - -""" +"""This is the gnuviechadmin package.""" diff --git a/gnuviechadmin/client.py b/gnuviechadmin/client.py index d29b371..469fc83 100644 --- a/gnuviechadmin/client.py +++ b/gnuviechadmin/client.py @@ -1,13 +1,28 @@ -# This file is part of gnuviechadmin. # -*- coding: UTF-8 -*- # -# Author: Jan Dittberner -# Copyright (c) 2007 Jan Dittberner +# Copyright (C) 2007 by Jan Dittberner. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +# USA. +# # Version: $Id$ from sqlalchemy import * +from gnuviechadmin import gva_cfg, exceptions -meta = BoundMetaData('sqlite:///database.txt') +meta = BoundMetaData(gva_cfg.GVA_DBE) client_table = Table('clients', meta, Column('clientid', Integer, primary_key=True), Column('title', String(10)), @@ -25,32 +40,29 @@ client_table = Table('clients', meta, client_table.create(checkfirst=True) class Client(object): + mandatory = ('firstname', 'lastname', 'address1', 'zip', 'city', + 'country', 'phone', 'email') + """This class provides a client representation""" - def __init__(self, clientdata): - mandatory = ('firstname', 'lastname', 'address1', 'zip', 'city', - 'email', 'phone', 'country') - data = {"country": "de", "title": None, "address2": None, - "mobile": None, "fax": None, "organisation": None} - for key in clientdata.keys(): - data[key] = clientdata[key] - for key in mandatory: - if not key in data: - print "mandatory client field %s is missing" % (key) - self = None - return - self.title = data["title"] - self.firstname = data["firstname"] - self.lastname = data["lastname"] - self.address1 = data["address1"] - self.address2 = data["address2"] - self.organisation = data["organisation"] - self.zip = data["zip"] - self.city = data["city"] - self.country = data["country"] - self.phone = data["phone"] - self.mobile = data["mobile"] - self.fax = data["fax"] - self.email = data["email"] + def __init__(self, **kwargs): + self.clientid = None + self.country = 'de' + self.title = None + self.address2 = None + self.mobile = None + self.fax = None + self.organisation = None + for key in kwargs.keys(): + self.__setattr__(key, kwargs[key]) + self.validate() + + def validate(self): + missingfields = [] + for key in self.mandatory: + if self.__getattribute__(key) is None: + missingfields.append(key) + if missingfields: + raise exceptions.MissingFieldsError(missingfields) def __repr__(self): return """Client (Id %(clientid)d): diff --git a/gnuviechadmin/exceptions.py b/gnuviechadmin/exceptions.py new file mode 100644 index 0000000..961bade --- /dev/null +++ b/gnuviechadmin/exceptions.py @@ -0,0 +1,30 @@ +# -*- coding: UTF-8 -*- +# +# Copyright (C) 2007 by Jan Dittberner. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +# USA. +# +# Version: $Id$ + +"""This file defines the gnuviechadmin specific exception types.""" +class MissingFieldsError(Exception): + """This exception should be raised when a required field of a data +class is missing.""" + def __init__(self, missingfields): + self.missing = missingfields + + def __str__(self): + return "the fields %s are missing." % (repr(self.missing)) diff --git a/gnuviechadmin/gva_cfg.py.tmpl b/gnuviechadmin/gva_cfg.py.tmpl new file mode 100644 index 0000000..17b6338 --- /dev/null +++ b/gnuviechadmin/gva_cfg.py.tmpl @@ -0,0 +1,48 @@ +# -*- python -*- +# +# Copyright (C) 2007 by Jan Dittberner. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +# USA. +# +# Version: $Id$ + +"""This is the module which takes your site-specific settings. + +From a raw distribution it should be copied to gva_cfg.py. If you +already have a gva_cfg.py, be careful to add in only the new settings +you want. The complete set of distributed defaults, with annotation, +are in ./Defaults. In mm_cfg, override only those you want to change, +after the + + from Defaults import * + +line (see below).""" + +############################################################### +# Here's where we get the distributed defaults. # + +from Defaults import * + +############################################################### +# Put YOUR site-specific configuration below, in gva_cfg.py . # +# See Defaults.py for explanations of the values. # + +#-------------------------------------------------------------- +# The database connection string in a format usable for +# sqlalchemy. For examples see +# http://www.sqlalchemy.org/docs/dbengine.myt +# +GVA_DBE = 'sqlite:///database.txt'