VirtualMailManager/cli/handler.py
changeset 571 a4aead244f75
parent 568 14abdd04ddf5
child 643 df1e3b67882a
child 675 d24f094d1cb5
equal deleted inserted replaced
465:c0e1fb1b0145 571:a4aead244f75
       
     1 # -*- coding: UTF-8 -*-
       
     2 # Copyright (c) 2010 - 2012, Pascal Volk
       
     3 # See COPYING for distribution information.
       
     4 """
       
     5     VirtualMailManager.cli.handler
       
     6     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       
     7 
       
     8     A derived Handler class with a few changes/additions for cli use.
       
     9 """
       
    10 
       
    11 import os
       
    12 
       
    13 from VirtualMailManager.errors import VMMError
       
    14 from VirtualMailManager.handler import Handler
       
    15 from VirtualMailManager.cli import read_pass
       
    16 from VirtualMailManager.cli.config import CliConfig as Cfg
       
    17 from VirtualMailManager.constants import ACCOUNT_EXISTS, INVALID_SECTION, \
       
    18      NO_SUCH_ACCOUNT, TYPE_ACCOUNT
       
    19 from VirtualMailManager.password import randompw
       
    20 
       
    21 _ = lambda msg: msg
       
    22 
       
    23 
       
    24 class CliHandler(Handler):
       
    25     """This class uses a `CliConfig` for configuration stuff, instead of
       
    26     the non-interactive `Config` class.
       
    27 
       
    28     It provides the additional methods cfgSet() and configure().
       
    29 
       
    30     Additionally it uses `VirtualMailManager.cli.read_pass()` for for the
       
    31     interactive password dialog.
       
    32     """
       
    33 
       
    34     __slots__ = ()  # nothing additional, also no __dict__/__weakref__
       
    35 
       
    36     def __init__(self):
       
    37         """Creates a new CliHandler instance.
       
    38 
       
    39         Throws a NotRootError if your uid is greater 0.
       
    40         """
       
    41         # Overwrite the parent CTor partly, we use the CliConfig class
       
    42         # and add some command line checks.
       
    43         skip_some_checks = os.sys.argv[1] in ('cf', 'configure', 'h', 'help',
       
    44                                               'v', 'version')
       
    45         super(CliHandler, self).__init__(skip_some_checks)
       
    46 
       
    47         self._cfg = Cfg(self._cfg_fname)
       
    48         self._cfg.load()
       
    49 
       
    50     def cfg_set(self, option, value):
       
    51         """Set a new value for the given option."""
       
    52         return self._cfg.set(option, value)
       
    53 
       
    54     def configure(self, section=None):
       
    55         """Starts the interactive configuration.
       
    56 
       
    57         Configures in interactive mode options in the given ``section``.
       
    58         If no section is given (default) all options from all sections
       
    59         will be prompted.
       
    60         """
       
    61         if section is None:
       
    62             self._cfg.configure(self._cfg.sections())
       
    63         elif self._cfg.has_section(section):
       
    64             self._cfg.configure([section])
       
    65         else:
       
    66             raise VMMError(_(u"Invalid section: '%s'") % section,
       
    67                            INVALID_SECTION)
       
    68 
       
    69     def user_add(self, emailaddress, password=None):
       
    70         """Override the parent user_add() - add the interactive password
       
    71         dialog.
       
    72 
       
    73         Returns the generated password, if account.random_password == True.
       
    74         """
       
    75         acc = self._get_account(emailaddress)
       
    76         if acc:
       
    77             raise VMMError(_(u"The account '%s' already exists.") %
       
    78                            acc.address, ACCOUNT_EXISTS)
       
    79         self._is_other_address(acc.address, TYPE_ACCOUNT)
       
    80         rand_pass = self._cfg.dget('account.random_password')
       
    81         if password is None:
       
    82             password = (read_pass, randompw)[rand_pass]()
       
    83         acc.set_password(password)
       
    84         acc.save()
       
    85         self._make_account_dirs(acc)
       
    86         return (None, password)[rand_pass]
       
    87 
       
    88     def user_password(self, emailaddress, password=None):
       
    89         """Override the parent user_password() - add the interactive
       
    90         password dialog."""
       
    91         acc = self._get_account(emailaddress)
       
    92         if not acc:
       
    93             raise VMMError(_(u"The account '%s' does not exist.") %
       
    94                            acc.address, NO_SUCH_ACCOUNT)
       
    95         if not isinstance(password, basestring) or not password:
       
    96             password = read_pass()
       
    97         acc.modify('password', password)
       
    98 
       
    99 del _