VirtualMailManager/cli/handler.py
author Pascal Volk <neverseen@users.sourceforge.net>
Thu, 05 Aug 2010 05:08:02 +0000
branchv0.6.x
changeset 340 4515afec62e5
parent 339 abff2de9eed0
child 341 6709d0faf2f5
permissions -rw-r--r--
vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands out to VirtualMailManager/cli/subcommands.py. vmm: New created with minimal code.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
# -*- coding: UTF-8 -*-
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     2
# Copyright (c) 2010, Pascal Volk
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
"""
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 319
diff changeset
     5
    VirtualMailManager.cli.handler
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 319
diff changeset
     6
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
    A derived Handler class with a few changes/additions for cli use.
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     9
"""
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    11
import os
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    12
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 190
diff changeset
    13
from VirtualMailManager.errors import VMMError
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 319
diff changeset
    14
from VirtualMailManager.handler import Handler
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    15
from VirtualMailManager.cli import read_pass
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 319
diff changeset
    16
from VirtualMailManager.cli.config import CliConfig as Cfg
339
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    17
from VirtualMailManager.constants import ACCOUNT_EXISTS, INVALID_SECTION, \
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    18
     NO_SUCH_ACCOUNT
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    19
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    20
_ = lambda msg: msg
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    21
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
class CliHandler(Handler):
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
    """This class uses a `CliConfig` for configuration stuff, instead of
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    25
    the non-interactive `Config` class.
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    26
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
    It provides the additional methods cfgSet() and configure().
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    29
    Additionally it uses `VirtualMailManager.cli.read_pass()` for for the
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    30
    interactive password dialog.
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
    """
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    32
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    33
    __slots__ = ()  # nothing additional, also no __dict__/__weakref__
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
    def __init__(self):
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    36
        """Creates a new CliHandler instance.
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    37
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 190
diff changeset
    38
        Throws a NotRootError if your uid is greater 0.
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
        """
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    40
        # Overwrite the parent CTor partly, we use the CliConfig class
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    41
        # and add some command line checks.
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    42
        skip_some_checks = os.sys.argv[1] in ('cf', 'configure', 'h', 'help',
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    43
                                              'v', 'version')
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    44
        super(CliHandler, self).__init__(skip_some_checks)
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    45
318
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    46
        self._cfg = Cfg(self._cfg_fname)
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    47
        self._cfg.load()
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
        if not skip_some_checks:
318
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    49
            self._cfg.check()
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
            self._chkenv()
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    51
319
f4956b4ceba1 VMM//{,cli/}Handler: PEP-8-ified the Handler classes:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 318
diff changeset
    52
    def cfg_set(self, option, value):
f4956b4ceba1 VMM//{,cli/}Handler: PEP-8-ified the Handler classes:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 318
diff changeset
    53
        """Set a new value for the given option."""
318
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    54
        return self._cfg.set(option, value)
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
    def configure(self, section=None):
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
        """Starts the interactive configuration.
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
        Configures in interactive mode options in the given ``section``.
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
        If no section is given (default) all options from all sections
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
        will be prompted.
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
        """
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
        if section is None:
318
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    64
            self._cfg.configure(self._cfg.sections())
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    65
        elif self._cfg.has_section(section):
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    66
            self._cfg.configure([section])
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
        else:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 190
diff changeset
    68
            raise VMMError(_(u'Invalid section: “%s”') % section,
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 190
diff changeset
    69
                           INVALID_SECTION)
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    71
    def user_add(self, emailaddress, password=None):
337
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    72
        """Override the parent user_add() - add the interactive password
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    73
        dialog."""
337
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    74
        acc = self._get_account(emailaddress)
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    75
        if acc:
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    76
            raise VMMError(_(u"The account '%s' already exists.") %
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    77
                           acc.address, ACCOUNT_EXISTS)
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    78
        if password is None:
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    79
            password = read_pass()
337
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    80
        acc.set_password(password)
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    81
        acc.save()
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    82
        self._make_account_dirs(acc)
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    84
    def user_password(self, emailaddress, password=None):
339
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    85
        """Override the parent user_password() - add the interactive
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    86
        password dialog."""
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    87
        acc = self._get_account(emailaddress)
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    88
        if not acc:
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    89
            raise VMMError(_(u"The account '%s' doesn't exist.") %
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    90
                           acc.address, NO_SUCH_ACCOUNT)
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    91
        if not isinstance(password, basestring) or not password:
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
            password = read_pass()
339
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    93
        acc.modify('password', password)
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    94
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    95
del _