VirtualMailManager/cli/Handler.py
branchv0.6.x
changeset 190 1903d4ce97d7
child 216 0c8c053b451c
equal deleted inserted replaced
189:e63853509ad0 190:1903d4ce97d7
       
     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.Exceptions import VMMException
       
    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.ERROR import INVALID_SECTION
       
    18 from VirtualMailManager.ext.Postconf import Postconf
       
    19 
       
    20 
       
    21 class CliHandler(Handler):
       
    22     """This class uses a `CliConfig` for configuration stuff, instead of
       
    23     the non-interactive `Config` class.
       
    24 
       
    25     It provides the additional methods cfgSet() and configure().
       
    26 
       
    27     Additionally it uses `VirtualMailManager.cli.read_pass()` for for the
       
    28     interactive password dialog.
       
    29     """
       
    30 
       
    31     __slots__ = ()# nothing additional, also no __dict__/__weakref__
       
    32 
       
    33     def __init__(self):
       
    34         """Creates a new CliHandler instance.
       
    35 
       
    36         Throws a VMMNotRootException if your uid is greater 0.
       
    37         """
       
    38         # Overwrite the parent CTor partly, we use the CliConfig class
       
    39         # and add some command line checks.
       
    40         skip_some_checks = os.sys.argv[1] in ('cf', 'configure', 'h', 'help',
       
    41                                               'v', 'version')
       
    42         super(CliHandler, self).__init__(skip_some_checks)
       
    43 
       
    44         self._Cfg = Cfg(self._cfgFileName)
       
    45         self._Cfg.load()
       
    46         if not skip_some_checks:
       
    47             self._Cfg.check()
       
    48             self._chkenv()
       
    49             self._scheme = self._Cfg.dget('misc.password_scheme')
       
    50             self._postconf = Postconf(self._Cfg.dget('bin.postconf'))
       
    51 
       
    52     def cfgSet(self, option, value):
       
    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 VMMException(_(u'Invalid section: ā€œ%sā€') % section,
       
    68                                INVALID_SECTION)
       
    69 
       
    70     def userAdd(self, emailaddress, password):
       
    71         if password is None:
       
    72             password = read_pass()
       
    73         super(CliHandler, self).userAdd(emailaddress, password)
       
    74 
       
    75     def userPassword(self, emailaddress, password):
       
    76         if password is None:
       
    77             password = read_pass()
       
    78         super(CliHandler, self).userPassword(emailaddress, password)