From 1435a88a5afafb3d1d35de39b6d6c2e15c2eb591 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Sat, 1 Aug 2009 21:35:43 +0200 Subject: [PATCH] port sysuser to argparse and new config (addresses #33, #34) * enable sysuser command * argument parsing via argparse * mark strings as translatable * pass parsed configuration where appropriate --- gnuviechadmin/backend/sysuser.py | 18 +++---- gnuviechadmin/cli/__init__.py | 2 +- gnuviechadmin/cli/client.py | 3 +- gnuviechadmin/cli/sysuser.py | 86 +++++++++++++++++++------------- 4 files changed, 62 insertions(+), 47 deletions(-) diff --git a/gnuviechadmin/backend/sysuser.py b/gnuviechadmin/backend/sysuser.py index 3077e93..0e635cd 100644 --- a/gnuviechadmin/backend/sysuser.py +++ b/gnuviechadmin/backend/sysuser.py @@ -19,20 +19,19 @@ # # Version: $Id$ -from gnuviechadmin.exceptions import * +from gnuviechadmin.exceptions import CannotDeleteError from gnuviechadmin.util import passwordutils, getenttools -from settings import config -from BackendTo import * -from BackendEntity import * -from BackendEntityHandler import * +from gnuviechadmin.backend.BackendTo import Sysuser +from gnuviechadmin.backend.BackendEntity import BackendEntity +from gnuviechadmin.backend.BackendEntityHandler import BackendEntityHandler import os class SysuserEntity(BackendEntity): """Entity class for system users.""" - def __init__(self, delegate, verbose = False, **kwargs): - BackendEntity.__init__(self, delegate, verbose) + def __init__(self, config, delegate, verbose = False, **kwargs): + BackendEntity.__init__(self, config, delegate, verbose) for (key, value) in kwargs.items(): self.__setattr__(key, value) if not self.delegateto.username: @@ -160,5 +159,6 @@ class SysuserEntity(BackendEntity): class SysuserHandler(BackendEntityHandler): """BackendEntityHandler for Sysuser entities.""" - def __init__(self, verbose = False): - BackendEntityHandler.__init__(self, SysuserEntity, Sysuser, verbose) + def __init__(self, config, verbose = False): + BackendEntityHandler.__init__(self, SysuserEntity, Sysuser, + config, verbose) diff --git a/gnuviechadmin/cli/__init__.py b/gnuviechadmin/cli/__init__.py index 36eafbb..24dc6df 100644 --- a/gnuviechadmin/cli/__init__.py +++ b/gnuviechadmin/cli/__init__.py @@ -47,7 +47,7 @@ class CommandLineInterface(object): from gnuviechadmin.cli.sysuser import SysuserCli from gnuviechadmin.cli.domain import DomainCli from gnuviechadmin.cli.record import RecordCli - for command in [ClientCli]: + for command in [ClientCli, SysuserCli]: command.setup_argparser(subparsers) self.parsedargs = parser.parse_args(args[1:]) diff --git a/gnuviechadmin/cli/client.py b/gnuviechadmin/cli/client.py index 86a88ec..41d64d3 100644 --- a/gnuviechadmin/cli/client.py +++ b/gnuviechadmin/cli/client.py @@ -59,7 +59,7 @@ class ClientCli(CliCommand.CliCommand): help = _("the city of the client's address")) cmdparser.add_argument( '--country', - help = _("the client's country"), default='de') + help = _("the client's country")) cmdparser.add_argument( '-e', '--email', required = True, help = _("the client's email address")) @@ -105,4 +105,3 @@ class ClientCli(CliCommand.CliCommand): def __init__(self, args, config): CliCommand.CliCommand.__init__(self, args, config) - diff --git a/gnuviechadmin/cli/sysuser.py b/gnuviechadmin/cli/sysuser.py index 55390e9..27a06fd 100644 --- a/gnuviechadmin/cli/sysuser.py +++ b/gnuviechadmin/cli/sysuser.py @@ -30,54 +30,70 @@ class SysuserCli(CliCommand): This class implements `gnuviechadmin.cli.CliCommand.CliCommand`. """ - name = "sysuser" - description = "manage system users" - _optionmap = { - "create": ("create a new system user with the given options.", - [(["-n", "--username"], "username", - "the system user name", False), - (["-t", "--usertype"], "usertype", - "the numeric user type", False), - (["-h", "--home"], "home", - "the home directory", False), - (["-s", "--shell"], "shell", - "true if the user should get shell access", False), - (["-p", "--password"], "clearpass", - "the password for the user", False), - (["-c", "--clientid"], "clientid", - "the client id", True)]), - "list": ("list existing system users.", []), - "delete": ("delete a system user.", - [(["-s", "--sysuserid"], "sysuserid", - "the system user id", True)])} + @staticmethod + def setup_argparser(subparsers): + parser = subparsers.add_parser( + 'sysuser', + help = _('manage system users')) + parser.set_defaults(commandclass=SysuserCli) + cmdsub = parser.add_subparsers( + title = _('sysuser subcommands'), + dest = 'subcommand') + cmdparser = cmdsub.add_parser( + 'create', + help = _('create a new system user with the given options.')) + cmdparser.add_argument( + '-n', '--username', + help = _('the system user name')) + cmdparser.add_argument( + '-t', '--usertype', + help = _('the numeric user type')) + cmdparser.add_argument( + '--home', + help = _('the home directory')) + cmdparser.add_argument( + '-s', '--shell', + help = _('true if the user should get shell access')) + cmdparser.add_argument( + '-p', '--password', dest = 'clearpass', + help = _('the password for the user')) + cmdparser.add_argument( + '-c', '--clientid', required = True, + help = _('the client id')) + cmdparser = cmdsub.add_parser( + 'list', + help = _('list existing system users.')) + cmdparser = cmdsub.add_parser( + 'delete', + help = _('delete a system user.')) + cmdparser.add_argument( + '-s', '--sysuserid', required = True, + help = _('the system user id')) - def _execute(self, subcommand): + def _execute(self): """Executes `subcommand`. This method implements `CliCommand._execute()`. """ - self.logger.debug("execute %s with data %s", subcommand, - str(self._data)) + self.logger.debug("execute %s", self.args) from gnuviechadmin.backend import sysuser from gnuviechadmin import exceptions - if subcommand == "create": + sh = sysuser.SysuserHandler(self.config, self.args.verbose) + if self.args.subcommand == "create": try: - mysysuser = sysuser.SysuserHandler(self._verbose).create( - **self._data) - if self._verbose: + mysysuser = sh.create(**self.args.__dict__) + if self.args.verbose: print mysysuser except exceptions.CreationFailedError, cfe: - self._usage() print cfe sys.exit(2) - elif subcommand == "list": - sysusers = sysuser.SysuserHandler(self._verbose).fetchall() + elif self.args.subcommand == "list": + sysusers = sh.fetchall() for su in sysusers: print su - elif subcommand == "delete": - sysuser.SysuserHandler(self._verbose).delete( - self._data["sysuserid"]) + elif self.args.subcommand == "delete": + sh.delete(self.args.sysuserid) - def __init__(self, argv): + def __init__(self, args, config): """Constructor for the sysuser command.""" - CliCommand.CliCommand.__init__(self, argv) + CliCommand.__init__(self, args, config)