VirtualMailManager/cli/handler.py
author Pascal Volk <neverseen@users.sourceforge.net>
Wed, 28 Jul 2010 02:08:03 +0000
branchv0.6.x
changeset 320 011066435e6f
parent 319 VirtualMailManager/cli/Handler.py@f4956b4ceba1
child 337 150ddcc8b315
permissions -rw-r--r--
VMM/*: Made all modules names lowercase, adjusted imports.
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
316
31d8931dc535 VMM/constants: Replaced the constants subpackage by a module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 281
diff changeset
    17
from VirtualMailManager.constants import 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
    18
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    19
_ = 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
    20
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
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
    23
    """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
    24
    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
    25
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    26
    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
    27
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
    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
    29
    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
    30
    """
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    32
    __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
    33
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
    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
    35
        """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
    36
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 190
diff changeset
    37
        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
    38
        """
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
        # 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
    40
        # 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
    41
        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
    42
                                              'v', 'version')
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    43
        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
    44
318
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    45
        self._cfg = Cfg(self._cfg_fname)
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    46
        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
    47
        if not skip_some_checks:
318
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    48
            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
    49
            self._chkenv()
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
319
f4956b4ceba1 VMM//{,cli/}Handler: PEP-8-ified the Handler classes:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 318
diff changeset
    51
    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
    52
        """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
    53
        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
    54
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
    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
    56
        """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
    57
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
        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
    59
        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
    60
        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
    61
        """
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
        if section is None:
318
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    63
            self._cfg.configure(self._cfg.sections())
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    64
        elif self._cfg.has_section(section):
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    65
            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
    66
        else:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 190
diff changeset
    67
            raise VMMError(_(u'Invalid section: “%s”') % section,
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 190
diff changeset
    68
                           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
    69
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    70
    def user_add(self, emailaddress, password=None):
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    71
        """Prefix the parent user_add() with the interactive password
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    72
        dialog."""
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    73
        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
    74
            password = read_pass()
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    75
        super(CliHandler, self).user_add(emailaddress, password)
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    77
    def user_password(self, emailaddress, password=None):
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    78
        """Prefix the parent user_password() with the interactive password
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    79
        dialog."""
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
        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
    81
            password = read_pass()
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    82
        super(CliHandler, self).user_password(emailaddress, password)
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    83
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    84
del _