VirtualMailManager/cli/__init__.py
author Pascal Volk <user@localhost.localdomain.org>
Sun, 02 Feb 2014 14:36:01 +0000
branchv0.7.x
changeset 711 2a75058fc064
parent 676 2bc11dada296
parent 703 58815c004a61
permissions -rw-r--r--
Merged changes from default(5716bf441ceb).
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 -*-
703
58815c004a61 Updated copyright notices to include the year 2014.
Pascal Volk <user@localhost.localdomain.org>
parents: 675
diff changeset
     2
# Copyright (c) 2010 - 2014, Pascal Volk
205
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
668
b4396a78c996 VMM/cli: Small cleanup.
Pascal Volk <user@localhost.localdomain.org>
parents: 663
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
    """
654
f2463a64e1d7 VMM/cli: Avoid UnicodeDecodeErrors with some „obsolete“ encodings.
Pascal Volk <user@localhost.localdomain.org>
parents: 653
diff changeset
    33
    _std_write('\n'.join(arg.encode(ENCODING, 'replace').decode(ENCODING,
f2463a64e1d7 VMM/cli: Avoid UnicodeDecodeErrors with some „obsolete“ encodings.
Pascal Volk <user@localhost.localdomain.org>
parents: 653
diff changeset
    34
                                                                'replace')
653
cf07e4468934 VMM: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents: 643
diff changeset
    35
               for arg 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
    """
654
f2463a64e1d7 VMM/cli: Avoid UnicodeDecodeErrors with some „obsolete“ encodings.
Pascal Volk <user@localhost.localdomain.org>
parents: 653
diff changeset
    44
    _err_write('\n'.join(arg.encode(ENCODING, 'replace').decode(ENCODING,
f2463a64e1d7 VMM/cli: Avoid UnicodeDecodeErrors with some „obsolete“ encodings.
Pascal Volk <user@localhost.localdomain.org>
parents: 653
diff changeset
    45
                                                                'replace')
653
cf07e4468934 VMM: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents: 643
diff changeset
    46
               for arg in args) + '\n')
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    47
    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
    48
        os.sys.exit(code)
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    51
def get_winsize():
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    52
    """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
    53
    width of the terminal."""
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
    fd = None
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
    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
    56
        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
    57
            fd = dev.fileno()
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
            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
    59
    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
    60
        # 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
    61
        ws_row, ws_col = 24, 80
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
        try:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
            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
    64
            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
    65
        except ValueError:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
            pass
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
        return ws_row, ws_col
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
    #"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
    69
    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
    70
    ioctl(fd, TIOCGWINSZ, ws, True)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
    ws_row, ws_col = ws[:2]
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    72
    return ws_row, ws_col
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    73
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    74
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
def read_pass():
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
    """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
    77
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    78
    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
    79
    """
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.
663
de435d1aa1c0 VMM/cli: Deleted unnecessary encode()s from read_pass.
Pascal Volk <user@localhost.localdomain.org>
parents: 654
diff changeset
    81
    readp_msg0 = _('Enter new password: ')
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
    # TP: Please preserve the trailing space.
663
de435d1aa1c0 VMM/cli: Deleted unnecessary encode()s from read_pass.
Pascal Volk <user@localhost.localdomain.org>
parents: 654
diff changeset
    83
    readp_msg1 = _('Retype new password: ')
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
    mismatched = True
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
    failures = 0
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
    while mismatched:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
        if failures > 2:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    88
            raise VMMError(_('Too many failures - try again later.'),
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    89
                           VMM_TOO_MANY_FAILURES)
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    90
        clear0 = getpass(prompt=readp_msg0)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
        clear1 = getpass(prompt=readp_msg1)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
        if clear0 != clear1:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
            failures += 1
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    94
            w_err(0, _('Sorry, passwords do not match.'))
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
        if not clear0:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
            failures += 1
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    98
            w_err(0, _('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
    99
            continue
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   100
        mismatched = False
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   101
    return clear0
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   102
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
   103
del _