VirtualMailManager/cli/__init__.py
author Pascal Volk <neverseen@users.sourceforge.net>
Mon, 26 Jul 2010 03:08:57 +0000
branchv0.6.x
changeset 313 c17c46d9e440
parent 312 6f39a1e56f4a
child 314 0b4a6e9d9f27
permissions -rw-r--r--
VMM/cli/Config: Write warnings to stderr. Renamed method __saveChanges to __save_changes.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
# -*- coding: UTF-8 -*-
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     2
# Copyright (c) 2010, Pascal Volk
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     5
"""
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     6
    VirtualMailManager.cli
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
     7
    ~~~~~~~~~~~~~~~~~~~~~~
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     9
    VirtualMailManager's command line interface.
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
"""
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    11
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 205
diff changeset
    12
import os
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    13
from array import array
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    14
from cStringIO import StringIO
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    15
from fcntl import ioctl
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
from getpass import getpass
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    17
from termios import TIOCGWINSZ
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    18
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 205
diff changeset
    19
from VirtualMailManager import ENCODING
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    20
from VirtualMailManager.constants.ERROR import VMM_TOO_MANY_FAILURES
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    21
from VirtualMailManager.errors import VMMError
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
__all__ = ('get_winsize', 'read_pass', 'string_io', 'w_err', 'w_std')
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    25
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    26
_ = lambda msg: msg
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
_std_write = os.sys.stdout.write
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
_err_write = os.sys.stderr.write
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    29
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    30
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
def w_std(*args):
217
eecd05e31517 VMM/cli: small optimizations in the functions w_std() and w_err()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    32
    """Writes a line for each arg of *args*, encoded in the current
eecd05e31517 VMM/cli: small optimizations in the functions w_std() and w_err()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    33
    ENCODING, to stdout.
eecd05e31517 VMM/cli: small optimizations in the functions w_std() and w_err()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    34
    """
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    35
    _std_write('\n'.join(a.encode(ENCODING, 'replace') for a in args) + '\n')
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    36
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    37
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
def w_err(code, *args):
217
eecd05e31517 VMM/cli: small optimizations in the functions w_std() and w_err()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    39
    """Writes a line for each arg of *args*, encoded in the current
eecd05e31517 VMM/cli: small optimizations in the functions w_std() and w_err()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    40
    ENCODING, to stderr.
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    41
    This function optionally interrupts the program execution if *code*
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    42
    does not equal to 0. *code* will be used as the system exit status.
217
eecd05e31517 VMM/cli: small optimizations in the functions w_std() and w_err()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    43
    """
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    44
    _err_write('\n'.join(a.encode(ENCODING, 'replace') for a in args) + '\n')
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    45
    if code:
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    46
        os.sys.exit(code)
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    47
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
def get_winsize():
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
    """Returns a tuple of integers ``(ws_row, ws_col)`` with the height and
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    51
    width of the terminal."""
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    52
    fd = None
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    53
    for dev in (os.sys.stdout, os.sys.stderr, os.sys.stdin):
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
        if hasattr(dev, 'fileno') and os.isatty(dev.fileno()):
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
            fd = dev.fileno()
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
            break
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    57
    if fd is None:  # everything seems to be redirected
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
        # fall back to environment or assume some common defaults
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
        ws_row, ws_col = 24, 80
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
        try:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
            ws_col = int(os.environ.get('COLUMNS', 80))
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
            ws_row = int(os.environ.get('LINES', 24))
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
        except ValueError:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
            pass
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
        return ws_row, ws_col
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
    #"struct winsize" with the ``unsigned short int``s ws_{row,col,{x,y}pixel}
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
    ws = array('H', (0, 0, 0, 0))
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
    ioctl(fd, TIOCGWINSZ, ws, True)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
    ws_row, ws_col = ws[:2]
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
    return ws_row, ws_col
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    72
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    73
def read_pass():
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    74
    """Interactive 'password chat', returns the password in plain format.
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    76
    Throws a VMMError after the third failure.
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
    """
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    78
    # TP: Please preserve the trailing space.
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    79
    readp_msg0 = _(u'Enter new password: ').encode(ENCODING, 'replace')
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
    # TP: Please preserve the trailing space.
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
    readp_msg1 = _(u'Retype new password: ').encode(ENCODING, 'replace')
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
    mismatched = True
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
    failures = 0
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
    while mismatched:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
        if failures > 2:
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    86
            raise VMMError(_(u'Too many failures - try again later.'),
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    87
                           VMM_TOO_MANY_FAILURES)
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
        clear0 = getpass(prompt=readp_msg0)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
        clear1 = getpass(prompt=readp_msg1)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    90
        if clear0 != clear1:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
            failures += 1
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    92
            w_err(0, _(u'Sorry, passwords do not match'))
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
            continue
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    94
        if not clear0:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    95
            failures += 1
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    96
            w_err(0, _(u'Sorry, empty passwords are not permitted'))
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
            continue
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    98
        mismatched = False
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    99
    return clear0
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   100
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   101
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   102
def string_io():
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   103
    """Returns a new `cStringIO.StringIO` instance."""
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   104
    return StringIO()
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
   105
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
   106
del _