VirtualMailManager/cli/handler.py
author Pascal Volk <neverseen@users.sourceforge.net>
Mon, 09 Aug 2010 08:25:11 +0000
branchv0.6.x
changeset 358 6fabb9cd212d
parent 346 95d45e4ec1a6
child 361 971577b89d26
permissions -rw-r--r--
update_config: Set pyPgSQL, maybe there is no psycopg2 installed.
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
319
f4956b4ceba1 VMM//{,cli/}Handler: PEP-8-ified the Handler classes:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 318
diff changeset
    49
    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
    50
        """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
    51
        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
    52
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    53
    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
    54
        """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
    55
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
        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
    57
        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
    58
        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
    59
        """
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
        if section is None:
318
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    61
            self._cfg.configure(self._cfg.sections())
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    62
        elif self._cfg.has_section(section):
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    63
            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
    64
        else:
341
6709d0faf2f5 VMM/…: Unified messages and removed non-ASCII quotes.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 339
diff changeset
    65
            raise VMMError(_(u"Invalid section: '%s'") % section,
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 190
diff changeset
    66
                           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
    67
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    68
    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
    69
        """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
    70
        dialog."""
337
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    71
        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
    72
        if acc:
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    73
            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
    74
                           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
    75
        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
    76
            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
    77
        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
    78
        acc.save()
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    79
        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
    80
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    81
    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
    82
        """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
    83
        password dialog."""
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    84
        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
    85
        if not acc:
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    86
            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
    87
                           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
    88
        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
    89
            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
    90
        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
    91
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    92
del _