1
0
Fork 0

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
This commit is contained in:
Jan Dittberner 2009-08-01 21:35:43 +02:00
parent 0ac89c5f7b
commit 1435a88a5a
4 changed files with 62 additions and 47 deletions

View file

@ -19,20 +19,19 @@
# #
# Version: $Id$ # Version: $Id$
from gnuviechadmin.exceptions import * from gnuviechadmin.exceptions import CannotDeleteError
from gnuviechadmin.util import passwordutils, getenttools from gnuviechadmin.util import passwordutils, getenttools
from settings import config from gnuviechadmin.backend.BackendTo import Sysuser
from BackendTo import * from gnuviechadmin.backend.BackendEntity import BackendEntity
from BackendEntity import * from gnuviechadmin.backend.BackendEntityHandler import BackendEntityHandler
from BackendEntityHandler import *
import os import os
class SysuserEntity(BackendEntity): class SysuserEntity(BackendEntity):
"""Entity class for system users.""" """Entity class for system users."""
def __init__(self, delegate, verbose = False, **kwargs): def __init__(self, config, delegate, verbose = False, **kwargs):
BackendEntity.__init__(self, delegate, verbose) BackendEntity.__init__(self, config, delegate, verbose)
for (key, value) in kwargs.items(): for (key, value) in kwargs.items():
self.__setattr__(key, value) self.__setattr__(key, value)
if not self.delegateto.username: if not self.delegateto.username:
@ -160,5 +159,6 @@ class SysuserEntity(BackendEntity):
class SysuserHandler(BackendEntityHandler): class SysuserHandler(BackendEntityHandler):
"""BackendEntityHandler for Sysuser entities.""" """BackendEntityHandler for Sysuser entities."""
def __init__(self, verbose = False): def __init__(self, config, verbose = False):
BackendEntityHandler.__init__(self, SysuserEntity, Sysuser, verbose) BackendEntityHandler.__init__(self, SysuserEntity, Sysuser,
config, verbose)

View file

@ -47,7 +47,7 @@ class CommandLineInterface(object):
from gnuviechadmin.cli.sysuser import SysuserCli from gnuviechadmin.cli.sysuser import SysuserCli
from gnuviechadmin.cli.domain import DomainCli from gnuviechadmin.cli.domain import DomainCli
from gnuviechadmin.cli.record import RecordCli from gnuviechadmin.cli.record import RecordCli
for command in [ClientCli]: for command in [ClientCli, SysuserCli]:
command.setup_argparser(subparsers) command.setup_argparser(subparsers)
self.parsedargs = parser.parse_args(args[1:]) self.parsedargs = parser.parse_args(args[1:])

View file

@ -59,7 +59,7 @@ class ClientCli(CliCommand.CliCommand):
help = _("the city of the client's address")) help = _("the city of the client's address"))
cmdparser.add_argument( cmdparser.add_argument(
'--country', '--country',
help = _("the client's country"), default='de') help = _("the client's country"))
cmdparser.add_argument( cmdparser.add_argument(
'-e', '--email', required = True, '-e', '--email', required = True,
help = _("the client's email address")) help = _("the client's email address"))
@ -105,4 +105,3 @@ class ClientCli(CliCommand.CliCommand):
def __init__(self, args, config): def __init__(self, args, config):
CliCommand.CliCommand.__init__(self, args, config) CliCommand.CliCommand.__init__(self, args, config)

View file

@ -30,54 +30,70 @@ class SysuserCli(CliCommand):
This class implements `gnuviechadmin.cli.CliCommand.CliCommand`. This class implements `gnuviechadmin.cli.CliCommand.CliCommand`.
""" """
name = "sysuser" @staticmethod
description = "manage system users" def setup_argparser(subparsers):
_optionmap = { parser = subparsers.add_parser(
"create": ("create a new system user with the given options.", 'sysuser',
[(["-n", "--username"], "username", help = _('manage system users'))
"the system user name", False), parser.set_defaults(commandclass=SysuserCli)
(["-t", "--usertype"], "usertype", cmdsub = parser.add_subparsers(
"the numeric user type", False), title = _('sysuser subcommands'),
(["-h", "--home"], "home", dest = 'subcommand')
"the home directory", False), cmdparser = cmdsub.add_parser(
(["-s", "--shell"], "shell", 'create',
"true if the user should get shell access", False), help = _('create a new system user with the given options.'))
(["-p", "--password"], "clearpass", cmdparser.add_argument(
"the password for the user", False), '-n', '--username',
(["-c", "--clientid"], "clientid", help = _('the system user name'))
"the client id", True)]), cmdparser.add_argument(
"list": ("list existing system users.", []), '-t', '--usertype',
"delete": ("delete a system user.", help = _('the numeric user type'))
[(["-s", "--sysuserid"], "sysuserid", cmdparser.add_argument(
"the system user id", True)])} '--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`. """Executes `subcommand`.
This method implements `CliCommand._execute()`. This method implements `CliCommand._execute()`.
""" """
self.logger.debug("execute %s with data %s", subcommand, self.logger.debug("execute %s", self.args)
str(self._data))
from gnuviechadmin.backend import sysuser from gnuviechadmin.backend import sysuser
from gnuviechadmin import exceptions from gnuviechadmin import exceptions
if subcommand == "create": sh = sysuser.SysuserHandler(self.config, self.args.verbose)
if self.args.subcommand == "create":
try: try:
mysysuser = sysuser.SysuserHandler(self._verbose).create( mysysuser = sh.create(**self.args.__dict__)
**self._data) if self.args.verbose:
if self._verbose:
print mysysuser print mysysuser
except exceptions.CreationFailedError, cfe: except exceptions.CreationFailedError, cfe:
self._usage()
print cfe print cfe
sys.exit(2) sys.exit(2)
elif subcommand == "list": elif self.args.subcommand == "list":
sysusers = sysuser.SysuserHandler(self._verbose).fetchall() sysusers = sh.fetchall()
for su in sysusers: for su in sysusers:
print su print su
elif subcommand == "delete": elif self.args.subcommand == "delete":
sysuser.SysuserHandler(self._verbose).delete( sh.delete(self.args.sysuserid)
self._data["sysuserid"])
def __init__(self, argv): def __init__(self, args, config):
"""Constructor for the sysuser command.""" """Constructor for the sysuser command."""
CliCommand.CliCommand.__init__(self, argv) CliCommand.__init__(self, args, config)