VirtualMailManager/cli/__init__.py
author Pascal Volk <user@localhost.localdomain.org>
Wed, 21 Nov 2012 12:53:56 +0000
changeset 646 86c2ed36aba8
parent 568 14abdd04ddf5
child 643 df1e3b67882a
child 675 d24f094d1cb5
permissions -rw-r--r--
po: Updated Finnish translation (translated by Jorma Karvonen).
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 -*-
568
14abdd04ddf5 Updated copyright notices to include the year 2012.
Pascal Volk <user@localhost.localdomain.org>
parents: 417
diff changeset
     2
# Copyright (c) 2010 - 2012, 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
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    22
__all__ = ('prog', '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
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    27
prog = os.path.basename(os.sys.argv[0])
205
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
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    30
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
    31
    """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
    32
    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
    33
    """
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    34
    _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
    35
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
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
    38
    """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
    39
    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
    40
    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
    41
    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
    42
    """
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    43
    _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
    44
    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
    45
        os.sys.exit(code)
205
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
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
def get_winsize():
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
    """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
    50
    width of the terminal."""
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    51
    fd = None
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    52
    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
    53
        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
    54
            fd = dev.fileno()
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
            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
    56
    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
    57
        # 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
    58
        ws_row, ws_col = 24, 80
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
        try:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
            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
    61
            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
    62
        except ValueError:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
            pass
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
        return ws_row, ws_col
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
    #"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
    66
    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
    67
    ioctl(fd, TIOCGWINSZ, ws, True)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
    ws_row, ws_col = ws[:2]
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
    return ws_row, ws_col
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
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    72
def read_pass():
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    73
    """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
    74
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
    75
    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
    76
    """
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
    # TP: Please preserve the trailing space.
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    78
    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
    79
    # TP: Please preserve the trailing space.
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
    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
    81
    mismatched = True
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
    failures = 0
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
    while mismatched:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
        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
    85
            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
    86
                           VMM_TOO_MANY_FAILURES)
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
        clear0 = getpass(prompt=readp_msg0)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
        clear1 = getpass(prompt=readp_msg1)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
        if clear0 != clear1:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    90
            failures += 1
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    91
            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
    92
            continue
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
        if not clear0:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    94
            failures += 1
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    95
            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
    96
            continue
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
        mismatched = False
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    98
    return clear0
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    99
312
6f39a1e56f4a VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 217
diff changeset
   100
del _