2007-02-10 23:23:17 +01:00
|
|
|
#!/usr/bin/python
|
2007-02-08 22:06:58 +01:00
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
#
|
2007-02-10 14:52:59 +01:00
|
|
|
# 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.
|
2007-02-08 22:06:58 +01:00
|
|
|
#
|
|
|
|
# Version: $Id$
|
|
|
|
|
2007-02-10 14:52:59 +01:00
|
|
|
import getopt, sys, sqlalchemy
|
2007-02-08 22:06:58 +01:00
|
|
|
|
2007-02-10 14:52:59 +01:00
|
|
|
from gnuviechadmin import client, exceptions
|
2007-02-08 22:06:58 +01:00
|
|
|
|
|
|
|
def usage():
|
|
|
|
print """Usage: %s [-h|--help] [-v|--verbose]
|
|
|
|
[-t <title>|--title=<title>]
|
|
|
|
-f <firstname>|--firstname=<firstname> -l <lastname>|--lastname=<lastname>
|
|
|
|
-a <address1>|--address=<address1> [--address2=<address2>]
|
|
|
|
-z <zip>|--zip=<zip> -c <city>|--city=<city> [--country=<isocode>]
|
|
|
|
[-o <organisation>|--organisation=<organisation>]
|
|
|
|
-e <email>|--email=<email> -p <phone>|--phone=<phone>
|
|
|
|
[-m <mobile>|--mobile=<mobile>] [-x <fax>|--fax=<fax>]
|
|
|
|
|
|
|
|
General options:
|
|
|
|
-h, --help show this usage message and exit
|
|
|
|
-v, --verbose verbose operation
|
|
|
|
|
|
|
|
Mandatory client data options:
|
|
|
|
-f, --firstname firstname
|
|
|
|
-l, --lastname lastname
|
|
|
|
-a, --address street address
|
|
|
|
-z, --zip zip or postal code
|
|
|
|
-c, --city city or location
|
|
|
|
-e, --email contact email address
|
|
|
|
-p, --phone telephone number
|
|
|
|
|
|
|
|
Optional client data options:
|
|
|
|
--address2 optional second line of the street address
|
|
|
|
-o, --organisation option organisation
|
|
|
|
--country country (defaults to de)
|
|
|
|
-t, --title optional title
|
|
|
|
-m, --mobile optional mobile number
|
|
|
|
-x, --fax optional fax number
|
|
|
|
""" % (sys.argv[0])
|
|
|
|
|
|
|
|
def main():
|
|
|
|
try:
|
|
|
|
opts, args = getopt.gnu_getopt(sys.argv[1:],
|
|
|
|
"hvf:l:a:z:c:e:p:o:t:m:x:",
|
|
|
|
["help", "verbose", "firstname=",
|
|
|
|
"lastname=", "address=", "zip=",
|
|
|
|
"city=", "email=", "phone=",
|
|
|
|
"address2=", "organisation=",
|
|
|
|
"country=", "title=", "mobile=",
|
|
|
|
"fax="])
|
|
|
|
except getopt.GetoptError:
|
|
|
|
usage()
|
|
|
|
sys.exit(2)
|
|
|
|
clientdata = {}
|
|
|
|
verbose = False
|
|
|
|
for o, a in opts:
|
|
|
|
if o in ("-v", "--verbose"):
|
|
|
|
verbose = True
|
|
|
|
if o in ("-h", "--help"):
|
|
|
|
usage()
|
|
|
|
sys.exit()
|
|
|
|
if o in ("-f", "--firstname"):
|
|
|
|
clientdata["firstname"] = a
|
|
|
|
if o in ("-l", "--lastname"):
|
|
|
|
clientdata["lastname"] = a
|
|
|
|
if o in ("-a", "--address"):
|
|
|
|
clientdata["address1"] = a
|
|
|
|
if o in ("-z", "--zip"):
|
|
|
|
clientdata["zip"] = a
|
|
|
|
if o in ("-c", "--city"):
|
|
|
|
clientdata["city"] = a
|
|
|
|
if o == "--country":
|
|
|
|
clientdata["country"] = a
|
|
|
|
if o in ("-t", "--title"):
|
|
|
|
clientdata["title"] = a
|
|
|
|
if o in ("-m", "--mobile"):
|
|
|
|
clientdata["mobile"] = a
|
|
|
|
if o in ("-e", "--email"):
|
|
|
|
clientdata["email"] = a
|
|
|
|
if o in ("-o", "--organisation"):
|
|
|
|
clientdata["organisation"] = a
|
|
|
|
if o in ("-x", "--fax"):
|
|
|
|
clientdata["fax"] = a
|
|
|
|
if o in ("-p", "--phone"):
|
|
|
|
clientdata["phone"] = a
|
|
|
|
if verbose:
|
|
|
|
print "parsed client data is ", clientdata
|
2007-02-10 14:52:59 +01:00
|
|
|
try:
|
|
|
|
myclient = client.Client(**clientdata)
|
|
|
|
except exceptions.MissingFieldsError, mfe:
|
|
|
|
print mfe
|
2007-02-08 22:06:58 +01:00
|
|
|
usage()
|
|
|
|
sys.exit(2)
|
2007-02-10 14:52:59 +01:00
|
|
|
try:
|
|
|
|
sess = sqlalchemy.create_session()
|
|
|
|
sess.save(myclient)
|
|
|
|
sess.flush()
|
|
|
|
except sqlalchemy.exceptions.SQLError, e:
|
|
|
|
print "saving client failed: ", e
|
|
|
|
sys.exit(2)
|
2007-02-08 22:06:58 +01:00
|
|
|
if verbose:
|
|
|
|
print myclient
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|