- CliCommand base class for cli commands
- client command for creating clients - gva dispatcher git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/gnuviech.info/gnuviechadmin/trunk@224 a67ec6bc-e5d5-0310-a910-815c51eb3124
This commit is contained in:
parent
3f099c72ff
commit
ee36146629
7 changed files with 271 additions and 31 deletions
50
bin/gva
Executable file
50
bin/gva
Executable file
|
@ -0,0 +1,50 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
# -*- 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$
|
||||||
|
|
||||||
|
import gnuviechadmin.cli.client
|
||||||
|
import gnuviechadmin.cli.sysuser
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def usage():
|
||||||
|
print """%s <command> [commandargs]
|
||||||
|
|
||||||
|
where command is one of
|
||||||
|
|
||||||
|
client - for creating clients
|
||||||
|
sysuser - for creating system users
|
||||||
|
""" % sys.argv[0]
|
||||||
|
|
||||||
|
def main():
|
||||||
|
if (sys.argv.__len__() < 2):
|
||||||
|
usage()
|
||||||
|
sys.exit()
|
||||||
|
command = sys.argv[1]
|
||||||
|
commargs = sys.argv[2:]
|
||||||
|
if command == "client":
|
||||||
|
gnuviechadmin.cli.client.ClientCli(commargs)
|
||||||
|
elif command == "sysuser":
|
||||||
|
gnuviechadmin.cli.sysuser.SysuserCli(commargs)
|
||||||
|
else:
|
||||||
|
usage()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
81
gnuviechadmin/cli/CliCommand.py
Normal file
81
gnuviechadmin/cli/CliCommand.py
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
# -*- 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$
|
||||||
|
|
||||||
|
import getopt, sys
|
||||||
|
|
||||||
|
class CliCommand:
|
||||||
|
"""Base class for command line interface."""
|
||||||
|
def usage(self):
|
||||||
|
"""This method should print usage information for the command."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def shortopts(self):
|
||||||
|
"""This method should return an option string for the short
|
||||||
|
options for getopt.gnu_getopt(...)."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def longopts(self):
|
||||||
|
"""This method should return a list of long options for
|
||||||
|
getopt.gnu_getopt(...)."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def handleoption(self, option, argument):
|
||||||
|
"""This method should handle each option known to the command."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def execute(self):
|
||||||
|
"""This method is called when the command is executed."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def checkrequired(self):
|
||||||
|
"""This methode is called after handling command line options
|
||||||
|
and should check whether all required values were set."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def __parseopts(self, args):
|
||||||
|
"""This method parses the options given on the command line."""
|
||||||
|
longopts = ["help", "verbose"]
|
||||||
|
longopts.extend(self.longopts())
|
||||||
|
try:
|
||||||
|
opts, args = getopt.gnu_getopt(
|
||||||
|
args,
|
||||||
|
"hv" + self.shortopts(),
|
||||||
|
longopts)
|
||||||
|
except getopt.GetoptError:
|
||||||
|
self.usage()
|
||||||
|
sys.exit(2)
|
||||||
|
self.verbose = False
|
||||||
|
for o, a in opts:
|
||||||
|
if o in ("-v", "--verbose"):
|
||||||
|
self.verbose = True
|
||||||
|
if o in ("-h", "--help"):
|
||||||
|
self.usage()
|
||||||
|
sys.exit()
|
||||||
|
self.handleoption(o, a)
|
||||||
|
|
||||||
|
def __init__(self, args):
|
||||||
|
"""This initializes the command with the given command line
|
||||||
|
arguments and executes it."""
|
||||||
|
self.__parseopts(args)
|
||||||
|
if (self.checkrequired()):
|
||||||
|
self.execute()
|
||||||
|
else:
|
||||||
|
self.usage()
|
27
gnuviechadmin/cli/__init__.py
Normal file
27
gnuviechadmin/cli/__init__.py
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
# -*- 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 is the gnuviechadmin.cli package.
|
||||||
|
|
||||||
|
This package provides modules for the command line interface of the
|
||||||
|
gnuviechadmin server administration suite."""
|
||||||
|
|
||||||
|
__all__ = ["client", "sysuser"]
|
110
gnuviechadmin/cli/client.py
Normal file
110
gnuviechadmin/cli/client.py
Normal file
|
@ -0,0 +1,110 @@
|
||||||
|
# -*- 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$
|
||||||
|
|
||||||
|
import CliCommand, sys
|
||||||
|
from gnuviechadmin import client
|
||||||
|
|
||||||
|
class ClientCli(CliCommand.CliCommand):
|
||||||
|
"""Command line interface command for client creation."""
|
||||||
|
def shortopts(self):
|
||||||
|
return "f:l:a:z:c:e:p:o:t:m:x:"
|
||||||
|
|
||||||
|
def longopts(self):
|
||||||
|
return ["firstname=", "lastname=", "address=", "zip=",
|
||||||
|
"city=", "email=", "phone=", "address2=", "organisation=",
|
||||||
|
"country=", "title=", "mobile=", "fax="]
|
||||||
|
|
||||||
|
def usage(self):
|
||||||
|
print """Usage: %s client [-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 handleoption(self, o, a):
|
||||||
|
if o in ("-f", "--firstname"):
|
||||||
|
self.data["firstname"] = a
|
||||||
|
elif o in ("-l", "--lastname"):
|
||||||
|
self.data["lastname"] = a
|
||||||
|
elif o in ("-a", "--address"):
|
||||||
|
self.data["address1"] = a
|
||||||
|
elif o in ("-z", "--zip"):
|
||||||
|
self.data["zip"] = a
|
||||||
|
elif o in ("-c", "--city"):
|
||||||
|
self.data["city"] = a
|
||||||
|
elif o == "--country":
|
||||||
|
self.data["country"] = a
|
||||||
|
elif o in ("-t", "--title"):
|
||||||
|
self.data["title"] = a
|
||||||
|
elif o in ("-m", "--mobile"):
|
||||||
|
self.data["mobile"] = a
|
||||||
|
elif o in ("-e", "--email"):
|
||||||
|
self.data["email"] = a
|
||||||
|
elif o in ("-o", "--organisation"):
|
||||||
|
self.data["organisation"] = a
|
||||||
|
elif o in ("-x", "--fax"):
|
||||||
|
self.data["fax"] = a
|
||||||
|
elif o in ("-p", "--phone"):
|
||||||
|
self.data["phone"] = a
|
||||||
|
|
||||||
|
def checkrequired(self):
|
||||||
|
required = ['firstname', 'lastname', 'address1', 'zip', 'city',
|
||||||
|
'phone', 'email']
|
||||||
|
if self.verbose:
|
||||||
|
print self.data
|
||||||
|
for req in required:
|
||||||
|
if not req in self.data:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def execute(self):
|
||||||
|
myclient = client.create(**self.data)
|
||||||
|
if self.verbose:
|
||||||
|
print myclient
|
||||||
|
|
||||||
|
def __init__(self, argv):
|
||||||
|
self.data = {}
|
||||||
|
CliCommand.CliCommand.__init__(self, argv)
|
1
gnuviechadmin/cli/sysuser.py
Normal file
1
gnuviechadmin/cli/sysuser.py
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pass
|
|
@ -49,28 +49,6 @@ class Client(object):
|
||||||
if missingfields:
|
if missingfields:
|
||||||
raise MissingFieldsError(missingfields)
|
raise MissingFieldsError(missingfields)
|
||||||
|
|
||||||
# def __repr__(self):
|
|
||||||
# return """Client (Id %(clientid)d):
|
|
||||||
# Name: %(title)s %(firstname)s %(lastname)s
|
|
||||||
# Address: %(address1)s
|
|
||||||
# %(address2)s
|
|
||||||
# %(zip)s %(city)s (%(country)s)
|
|
||||||
# Phone: %(phone)s
|
|
||||||
# Mobile: %(mobile)s
|
|
||||||
# Fax: %(fax)s
|
|
||||||
# Email: %(email)s""" % ({'clientid' : self.clientid,
|
|
||||||
# 'title' : self.title,
|
|
||||||
# 'firstname' : self.firstname,
|
|
||||||
# 'lastname' : self.lastname,
|
|
||||||
# 'address1' : self.address1,
|
|
||||||
# 'address2' : self.address2,
|
|
||||||
# 'zip' : self.zip,
|
|
||||||
# 'city' : self.city,
|
|
||||||
# 'country' : self.country,
|
|
||||||
# 'phone' : self.phone,
|
|
||||||
# 'mobile' : self.mobile,
|
|
||||||
# 'fax' : self.fax,
|
|
||||||
# 'email' : self.email})
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "%(clientid)d,%(firstname)s,%(lastname)s,%(email)s" % (
|
return "%(clientid)d,%(firstname)s,%(lastname)s,%(email)s" % (
|
||||||
{'clientid' : self.clientid, 'firstname' : self.firstname,
|
{'clientid' : self.clientid, 'firstname' : self.firstname,
|
||||||
|
|
|
@ -20,17 +20,11 @@
|
||||||
# Version: $Id$
|
# Version: $Id$
|
||||||
|
|
||||||
from sqlalchemy import *
|
from sqlalchemy import *
|
||||||
from pkg_resources import Requirement, resource_filename
|
|
||||||
import ConfigParser, os
|
import ConfigParser, os
|
||||||
|
|
||||||
config = ConfigParser.ConfigParser()
|
config = ConfigParser.ConfigParser()
|
||||||
config.readfp(
|
config.readfp(open('gnuviechadmin/defaults.cfg'))
|
||||||
open(resource_filename(Requirement.parse('gnuviechadmin'),
|
config.read(['gnuviechadmin/gva.cfg', os.path.expanduser('~/.gva.cfg')])
|
||||||
'gnuviechadmin/defaults.cfg')))
|
|
||||||
config.read([
|
|
||||||
resource_filename(Requirement.parse('gnuviechadmin'),
|
|
||||||
'gnuviechadmin/gva.cfg'),
|
|
||||||
os.path.expanduser('~/.gva.cfg')])
|
|
||||||
|
|
||||||
meta = BoundMetaData(config.get('database', 'uri'))
|
meta = BoundMetaData(config.get('database', 'uri'))
|
||||||
client_table = Table(
|
client_table = Table(
|
||||||
|
@ -64,4 +58,3 @@ sysuser_table = Table(
|
||||||
Column('lastchange', DateTime, default=func.now())
|
Column('lastchange', DateTime, default=func.now())
|
||||||
)
|
)
|
||||||
sysuser_table.create(checkfirst=True)
|
sysuser_table.create(checkfirst=True)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue