VirtualMailManager/cli/Handler.py
branchv0.6.x
changeset 320 011066435e6f
parent 319 f4956b4ceba1
child 321 883d5cd66498
equal deleted inserted replaced
319:f4956b4ceba1 320:011066435e6f
     1 # -*- coding: UTF-8 -*-
       
     2 # Copyright (c) 2010, Pascal Volk
       
     3 # See COPYING for distribution information.
       
     4 
       
     5 """
       
     6     VirtualMailManager.cli.Handler
       
     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 INVALID_SECTION
       
    18 
       
    19 _ = lambda msg: msg
       
    20 
       
    21 
       
    22 class CliHandler(Handler):
       
    23     """This class uses a `CliConfig` for configuration stuff, instead of
       
    24     the non-interactive `Config` class.
       
    25 
       
    26     It provides the additional methods cfgSet() and configure().
       
    27 
       
    28     Additionally it uses `VirtualMailManager.cli.read_pass()` for for the
       
    29     interactive password dialog.
       
    30     """
       
    31 
       
    32     __slots__ = ()  # nothing additional, also no __dict__/__weakref__
       
    33 
       
    34     def __init__(self):
       
    35         """Creates a new CliHandler instance.
       
    36 
       
    37         Throws a NotRootError if your uid is greater 0.
       
    38         """
       
    39         # Overwrite the parent CTor partly, we use the CliConfig class
       
    40         # and add some command line checks.
       
    41         skip_some_checks = os.sys.argv[1] in ('cf', 'configure', 'h', 'help',
       
    42                                               'v', 'version')
       
    43         super(CliHandler, self).__init__(skip_some_checks)
       
    44 
       
    45         self._cfg = Cfg(self._cfg_fname)
       
    46         self._cfg.load()
       
    47         if not skip_some_checks:
       
    48             self._cfg.check()
       
    49             self._chkenv()
       
    50 
       
    51     def cfg_set(self, option, value):
       
    52         """Set a new value for the given option."""
       
    53         return self._cfg.set(option, value)
       
    54 
       
    55     def configure(self, section=None):
       
    56         """Starts the interactive configuration.
       
    57 
       
    58         Configures in interactive mode options in the given ``section``.
       
    59         If no section is given (default) all options from all sections
       
    60         will be prompted.
       
    61         """
       
    62         if section is None:
       
    63             self._cfg.configure(self._cfg.sections())
       
    64         elif self._cfg.has_section(section):
       
    65             self._cfg.configure([section])
       
    66         else:
       
    67             raise VMMError(_(u'Invalid section: ā€œ%sā€') % section,
       
    68                            INVALID_SECTION)
       
    69 
       
    70     def user_add(self, emailaddress, password=None):
       
    71         """Prefix the parent user_add() with the interactive password
       
    72         dialog."""
       
    73         if password is None:
       
    74             password = read_pass()
       
    75         super(CliHandler, self).user_add(emailaddress, password)
       
    76 
       
    77     def user_password(self, emailaddress, password=None):
       
    78         """Prefix the parent user_password() with the interactive password
       
    79         dialog."""
       
    80         if password is None:
       
    81             password = read_pass()
       
    82         super(CliHandler, self).user_password(emailaddress, password)
       
    83 
       
    84 del _