VirtualMailManager/cli/__init__.py
author Pascal Volk <neverseen@users.sourceforge.net>
Wed, 28 Jul 2010 03:43:59 +0000
branchv0.6.x
changeset 322 94bd10e237e5
parent 320 011066435e6f
child 340 4515afec62e5
permissions -rw-r--r--
VMM/…: More PEP-8 fixes; eliminated __names. VMM/emailaddress: Fixed™ methods __eq__ and __ne__. (I'm not pylint's nanny.)
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
    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
     6
    ~~~~~~~~~~~~~~~~~~~~~~
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
    VirtualMailManager's command line interface.
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     9
"""
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 205
diff changeset
    11
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
    12
from array import array
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 fcntl import ioctl
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    14
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
    15
from termios import TIOCGWINSZ
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 205
diff changeset
    17
from VirtualMailManager import ENCODING
316
31d8931dc535 VMM/constants: Replaced the constants subpackage by a module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 315
diff changeset
    18
from VirtualMailManager.constants import VMM_TOO_MANY_FAILURES
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    19
from VirtualMailManager.errors import VMMError
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
315
81bccfd14355 VMM/cli: Forgot to remove string_io from __all__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 314
diff changeset
    22
__all__ = ('get_winsize', 'read_pass', 'w_err', 'w_std')
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    24
_ = lambda msg: msg
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    25
_std_write = os.sys.stdout.write
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    26
_err_write = os.sys.stderr.write
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    29
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
    30
    """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
    31
    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
    32
    """
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    33
    _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
    34
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    36
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
    37
    """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
    38
    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
    39
    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
    40
    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
    41
    """
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    42
    _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
    43
    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
    44
        os.sys.exit(code)
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    45
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    46
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    47
def get_winsize():
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
    """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
    49
    width of the terminal."""
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
    fd = None
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    51
    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
    52
        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
    53
            fd = dev.fileno()
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
            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
    55
    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
    56
        # 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
    57
        ws_row, ws_col = 24, 80
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
        try:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
            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
    60
            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
    61
        except ValueError:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
            pass
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
        return ws_row, ws_col
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
    #"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
    65
    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
    66
    ioctl(fd, TIOCGWINSZ, ws, True)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
    ws_row, ws_col = ws[:2]
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
    return ws_row, ws_col
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
def read_pass():
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    72
    """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
    73
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    74
    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
    75
    """
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
    # TP: Please preserve the trailing space.
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
    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
    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_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
    80
    mismatched = True
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
    failures = 0
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
    while mismatched:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
        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
    84
            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
    85
                           VMM_TOO_MANY_FAILURES)
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
        clear0 = getpass(prompt=readp_msg0)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
        clear1 = getpass(prompt=readp_msg1)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
        if clear0 != clear1:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
            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
    90
            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
    91
            continue
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
        if not clear0:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
            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
    94
            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
    95
            continue
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    96
        mismatched = False
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
    return clear0
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    98
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    99
del _