start argparse transition (addresses #33)
* use argparse instead of old home grown solution for argument parsing * change ClientCli class to use argparse * disable SysuserCli, DomainCli and RecordCli * mark strings as translatable (addresses #34)
This commit is contained in:
		
							parent
							
								
									076621a0be
								
							
						
					
					
						commit
						b8139e91f2
					
				
					 3 changed files with 91 additions and 93 deletions
				
			
		|  | @ -18,13 +18,13 @@ | |||
| # USA. | ||||
| # | ||||
| # Version: $Id$ | ||||
| import getopt | ||||
| import argparse | ||||
| import sys | ||||
| import logging | ||||
| from gnuviechadmin.exceptions import GnuviechadminError | ||||
| 
 | ||||
| 
 | ||||
| class CliCommand: | ||||
| class CliCommand(object): | ||||
|     """Base class for command line interface. | ||||
| 
 | ||||
|     A specific implementation class must define the fields name, | ||||
|  | @ -50,7 +50,7 @@ class CliCommand: | |||
|         """This method shows usage information. The implementation | ||||
|         relies on the information in the fields name, description and | ||||
|         _optionmap in the implementation classes.""" | ||||
|         print """GNUViechAdmin command line interface | ||||
|         return """GNUViechAdmin command line interface | ||||
| 
 | ||||
| Subcommand: %(command)s | ||||
| 
 | ||||
|  | @ -201,26 +201,7 @@ Common options: | |||
|         """This initializes the command with the given command line | ||||
|         arguments and executes it.""" | ||||
|         self.config = config | ||||
|         self.args = args | ||||
|         self.logger = logging.getLogger("%s.%s" % ( | ||||
|             self.__class__.__module__, self.__class__.__name__)) | ||||
|         self._data = {} | ||||
|         if len(args) > 0: | ||||
|             if args[0] in self._subcommands(): | ||||
|                 self._parseopts(args[0], args[1:]) | ||||
|                 reqcheck = self._checkrequired(args[0]) | ||||
|                 if reqcheck[0]: | ||||
|                     try: | ||||
|                         self._execute(args[0]) | ||||
|                     except GnuviechadminError, e: | ||||
|                         print e | ||||
|                 else: | ||||
|                     self._usage() | ||||
|                     print """ | ||||
| the following required arguments are missing: | ||||
| """ | ||||
|                     print "\n".join(reqcheck[1]) | ||||
|             else: | ||||
|                 self._usage() | ||||
|                 print "invalid sub command" | ||||
|         else: | ||||
|             self._usage() | ||||
|         self._execute() | ||||
|  |  | |||
|  | @ -27,36 +27,28 @@ __all__ = ["client", "sysuser", "domain", "record"] | |||
| 
 | ||||
| from logging import getLogger | ||||
| from sys import exit | ||||
| from argparse import ArgumentParser | ||||
| from gettext import gettext as _ | ||||
| 
 | ||||
| 
 | ||||
| class CommandLineInterface(object): | ||||
| 
 | ||||
|     def __init__(self, config, args): | ||||
|         self.log = getLogger(__name__) | ||||
|         self.config = config | ||||
|         if len(args) < 2: | ||||
|             self._usage(args[0]) | ||||
|             exit(1) | ||||
|         self.commands = [command for command in self._get_commands() \ | ||||
|                              if command.name == args[1]] | ||||
|         self.cmdargs = args[2:] | ||||
|          | ||||
| 
 | ||||
|     def _usage(self, callee): | ||||
|         print """%s <command> [commandargs] | ||||
| 
 | ||||
| where command is one of | ||||
| """ % callee | ||||
|         for command in self._get_commands(): | ||||
|             print "%10s - %s" % (command.name, command.description) | ||||
| 
 | ||||
|     def run(self): | ||||
|         for cmd in self.commands: | ||||
|             cmd(self.cmdargs, self.config) | ||||
| 
 | ||||
|     def _get_commands(self): | ||||
|         parser = ArgumentParser(prog=args[0]) | ||||
|         parser.add_argument( | ||||
|             '-v', '--verbose', | ||||
|             help = _('enable verbose output'), | ||||
|             action = 'store_true') | ||||
|         subparsers = parser.add_subparsers( | ||||
|             title = _('subcommands'), dest  = 'command') | ||||
|         from gnuviechadmin.cli.client import ClientCli | ||||
|         from gnuviechadmin.cli.sysuser import SysuserCli | ||||
|         from gnuviechadmin.cli.domain import DomainCli | ||||
|         from gnuviechadmin.cli.record import RecordCli | ||||
|         return [ClientCli, SysuserCli, DomainCli, RecordCli] | ||||
|         for command in [ClientCli]: | ||||
|             command.setup_argparser(subparsers) | ||||
|         self.parsedargs = parser.parse_args(args[1:]) | ||||
|          | ||||
|     def run(self): | ||||
|         self.parsedargs.commandclass(self.parsedargs, self.config) | ||||
|  |  | |||
|  | @ -20,66 +20,91 @@ | |||
| # Version: $Id$ | ||||
| import CliCommand | ||||
| import sys | ||||
| from gettext import gettext as _ | ||||
| 
 | ||||
| 
 | ||||
| class ClientCli(CliCommand.CliCommand): | ||||
|     """Command line interface command for client management.""" | ||||
| 
 | ||||
|     name = "client" | ||||
|     description = "manage clients" | ||||
|     _optionmap = { | ||||
|         'create': ("creates a new client", | ||||
|                    [(["-f", "--firstname"], "firstname", | ||||
|                      "the client's first name", True), | ||||
|                     (["-l", "--lastname"], "lastname", | ||||
|                      "the client's last name", True), | ||||
|                     (["-t", "--title"], "title", | ||||
|                      "the client's title", False), | ||||
|                     (["-a", "--address"], "address1", | ||||
|                      "the address of the client", True), | ||||
|                     (["--address2"], "address2", | ||||
|                      "second line of the client's address", False), | ||||
|                     (["-z", "--zip"], "zip", | ||||
|                      "the zipcode of the client's address", True), | ||||
|                     (["-c", "--city"], "city", | ||||
|                      "the city of the client's address", True), | ||||
|                     (["--country"], "country", | ||||
|                      "the client's country", False), | ||||
|                     (["-e", "--email"], "email", | ||||
|                      "the client's email address", True), | ||||
|                     (["-p", "--phone"], "phone", | ||||
|                      "the client's phone number", True), | ||||
|                     (["-m", "--mobile"], "mobile", | ||||
|                      "the client's mobile phone number", False), | ||||
|                     (["-x", "--fax"], "fax", | ||||
|                      "the client's fax number", False)]), | ||||
|         'list': ("lists existing clients", []), | ||||
|         'delete': ("deletes the specified client if it has no dependent data", | ||||
|                    [(["-c", "--clientid"], "clientid", | ||||
|                      "the client id", True)])} | ||||
|     @staticmethod | ||||
|     def setup_argparser(subparsers): | ||||
|         parser = subparsers.add_parser( | ||||
|             'client', | ||||
|             help = _('manage clients')) | ||||
|         parser.set_defaults(commandclass=ClientCli) | ||||
|         cmdsub = parser.add_subparsers( | ||||
|             title = _('client subcommands'), dest = 'subcommand') | ||||
|         cmdparser = cmdsub.add_parser( | ||||
|             'create', | ||||
|             help = _('creates a new client')) | ||||
|         cmdparser.add_argument( | ||||
|             '-f', '--firstname', required = True, | ||||
|             help = _("the client's first name")) | ||||
|         cmdparser.add_argument( | ||||
|             '-l', '--lastname', required = True, | ||||
|             help = _("the client's last name")) | ||||
|         cmdparser.add_argument( | ||||
|             '-t', '--title', | ||||
|             help = _("the client's title")) | ||||
|         cmdparser.add_argument( | ||||
|             '-a', '--address', dest = 'address1', required = True, | ||||
|             help = _("the address of the client")) | ||||
|         cmdparser.add_argument( | ||||
|             '--address2', | ||||
|             help = _("second line of the client's address")) | ||||
|         cmdparser.add_argument( | ||||
|             '-z', '--zip', required = True, | ||||
|             help = _("the zipcode of the client's address")) | ||||
|         cmdparser.add_argument( | ||||
|             '-c', '--city', required = True, | ||||
|             help = _("the city of the client's address")) | ||||
|         cmdparser.add_argument( | ||||
|             '--country', | ||||
|             help = _("the client's country")) | ||||
|         cmdparser.add_argument( | ||||
|             '-e', '--email', required = True, | ||||
|             help = _("the client's email address")) | ||||
|         cmdparser.add_argument( | ||||
|             '-p', '--phone', required = True, | ||||
|             help = _("the client's phone number")) | ||||
|         cmdparser.add_argument( | ||||
|             '-m', '--mobile', | ||||
|             help = _("the client's mobile phone number")) | ||||
|         cmdparser.add_argument( | ||||
|             '-x', '--fax', | ||||
|             help = _("the client's fax number")) | ||||
|         cmdparser = cmdsub.add_parser( | ||||
|             'list', | ||||
|             help = _('lists existing clients')) | ||||
|         cmdparser = cmdsub.add_parser( | ||||
|             'delete', | ||||
|             help = _( | ||||
|                 'deletes the specified client if it has no dependent data')) | ||||
|         cmdparser.add_argument( | ||||
|             '-c', '--clientid', required = True, | ||||
|             help = _("the client id")) | ||||
| 
 | ||||
|     def _execute(self, subcommand): | ||||
|         self.logger.debug("execute %s with data %s", subcommand, | ||||
|                           str(self._data)) | ||||
|     def _execute(self): | ||||
|         self.logger.debug("execute %s", self.args) | ||||
|         from gnuviechadmin.backend.client import ClientHandler | ||||
|         from gnuviechadmin.exceptions import CreationFailedError | ||||
|         if subcommand == "create": | ||||
|         ch = ClientHandler(self.config, self.args.verbose) | ||||
|         if self.args.subcommand == "create": | ||||
|             try: | ||||
|                 myclient = ClientHandler(self.config, | ||||
|                                          self._verbose).create(**self._data) | ||||
|                 if self._verbose: | ||||
|                 myclient = ch.create(**self.args) | ||||
|                 if self.args.verbose: | ||||
|                     print myclient | ||||
|             except CreationFailedError, cfe: | ||||
|                 self._usage() | ||||
|                 print cfe | ||||
|                 sys.exit(2) | ||||
|         elif subcommand == "list": | ||||
|             clients = ClientHandler(self.config, self._verbose).fetchall() | ||||
|         elif self.args.subcommand == "list": | ||||
|             clients = ch.fetchall() | ||||
|             for client in clients: | ||||
|                 print client | ||||
|         elif subcommand == "delete": | ||||
|             ClientHandler(self.config, | ||||
|                           self._verbose).delete(self._data["clientid"]) | ||||
|         elif self.args.subcommand == "delete": | ||||
|             ch.delete(self.args.clientid) | ||||
| 
 | ||||
|     def __init__(self, args, config): | ||||
|         CliCommand.CliCommand.__init__(self, args, config) | ||||
| 
 | ||||
|     def __init__(self, argv, config): | ||||
|         CliCommand.CliCommand.__init__(self, argv, config) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue