VirtualMailManager/cli/__init__.py
author Pascal Volk <neverseen@users.sourceforge.net>
Thu, 15 Apr 2010 23:25:24 +0000
branchv0.6.x
changeset 252 af555e6967c8
parent 217 eecd05e31517
child 312 6f39a1e56f4a
permissions -rw-r--r--
VMM/Account: renamed function getAccountByID -> get_account_by_uid. Fixed AttributeError (tid) in Account._prepare(). Removed unused imports.
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
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
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    12
from cStringIO import StringIO
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    13
from getpass import getpass
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    14
from textwrap import TextWrapper
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    15
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 205
diff changeset
    16
from VirtualMailManager import ENCODING
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    17
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    18
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    19
__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
    20
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
_std_write = os.sys.stdout.write
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
_err_write = os.sys.stderr.write
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
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    25
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
    26
    """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
    27
    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
    28
eecd05e31517 VMM/cli: small optimizations in the functions w_std() and w_err()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    29
    """
eecd05e31517 VMM/cli: small optimizations in the functions w_std() and w_err()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    30
    _std_write('\n'.join(arg.encode(ENCODING, 'replace') for arg in args))
eecd05e31517 VMM/cli: small optimizations in the functions w_std() and w_err()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    31
    _std_write('\n')
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    32
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    33
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
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
    35
    """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
    36
    ENCODING, to stderr.
205
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
    This function additional interrupts the program execution and uses
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
    *code* as the system exit status.
eecd05e31517 VMM/cli: small optimizations in the functions w_std() and w_err()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    40
eecd05e31517 VMM/cli: small optimizations in the functions w_std() and w_err()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    41
    """
eecd05e31517 VMM/cli: small optimizations in the functions w_std() and w_err()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    42
    _err_write('\n'.join(arg.encode(ENCODING, 'replace') for arg in args))
eecd05e31517 VMM/cli: small optimizations in the functions w_std() and w_err()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    43
    _err_write('\n')
205
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    44
    os.sys.exit(code)
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
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
    if fd is None:# everything seems to be redirected
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
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
    from array import array
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
    from fcntl import ioctl
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
    from termios import TIOCGWINSZ
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
    #"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
    70
    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
    71
    ioctl(fd, TIOCGWINSZ, ws, True)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    72
    ws_row, ws_col = ws[:2]
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    73
    return ws_row, ws_col
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
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
def read_pass():
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
    """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
    78
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    79
    Throws a VMMException after the third failure.
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
    """
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
    # TP: Please preserve the trailing space.
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
    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
    83
    # TP: Please preserve the trailing space.
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
    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
    85
    mismatched = True
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
    failures = 0
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
    while mismatched:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
        if failures > 2:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
            raise VMMException(_(u'Too many failures - try again later.'),
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    90
                               ERR.VMM_TOO_MANY_FAILURES)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
        clear0 = getpass(prompt=readp_msg0)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
        clear1 = getpass(prompt=readp_msg1)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
        if clear0 != clear1:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    94
            failures += 1
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    95
            w_std(_(u'Sorry, passwords do not match'))
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
        if not clear0:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    98
            failures += 1
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    99
            w_std(_(u'Sorry, empty passwords are not permitted'))
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   100
            continue
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   101
        mismatched = False
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   102
    return clear0
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   103
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   104
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   105
def string_io():
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   106
    """Returns a new `cStringIO.StringIO` instance."""
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   107
    return StringIO()