VirtualMailManager/cli/__init__.py
author Pascal Volk <neverseen@users.sourceforge.net>
Mon, 22 Feb 2010 10:20:46 +0000
branchv0.6.x
changeset 209 c705a9e38962
parent 205 bc9726c9ad85
child 215 33f727efa7c4
permissions -rw-r--r--
VMM{/Config}: reduced docstrings. Added doc to the repository.
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
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    11
from cStringIO import StringIO
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    12
from getpass import getpass
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    13
from textwrap import TextWrapper
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    14
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    15
import VirtualMailManager
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
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
__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
    19
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
os = VirtualMailManager.os
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):
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    26
    """Writes each arg of `args`, encoded in the current ENCODING, to stdout
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
    and appends a newline."""
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
    for arg in args:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    29
        _std_write(arg.encode(VirtualMailManager.ENCODING, 'replace'))
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    30
        _std_write('\n')
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
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
def w_err(code, *args):
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
    """Writes each arg of `args`, encoded in the current ENCODING, to stderr
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
    and appends a newline.
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
    This function additional interrupts the program execution and uses
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
    `code` system exit status."""
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
    for arg in args:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    40
        _err_write(arg.encode(VirtualMailManager.ENCODING, 'replace'))
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    41
        _err_write('\n')
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    42
    os.sys.exit(code)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    43
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    44
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    45
def get_winsize():
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    46
    """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
    47
    width of the terminal."""
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
    fd = None
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
    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
    50
        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
    51
            fd = dev.fileno()
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    52
            break
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    53
    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
    54
        # 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
    55
        ws_row, ws_col = 24, 80
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
        try:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
            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
    58
            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
    59
        except ValueError:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
            pass
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
        return ws_row, ws_col
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
    from array import array
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
    from fcntl import ioctl
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
    from termios import TIOCGWINSZ
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
    #"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
    68
    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
    69
    ioctl(fd, TIOCGWINSZ, ws, True)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
    ws_row, ws_col = ws[:2]
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
    return ws_row, ws_col
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
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    74
def read_pass():
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
    """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
    76
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
    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
    78
    """
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_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
    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_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
    83
    mismatched = True
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
    failures = 0
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
    while mismatched:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
        if failures > 2:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
            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
    88
                               ERR.VMM_TOO_MANY_FAILURES)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
        clear0 = getpass(prompt=readp_msg0)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    90
        clear1 = getpass(prompt=readp_msg1)
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
        if clear0 != clear1:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
            failures += 1
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
            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
    94
            continue
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    95
        if not clear0:
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    96
            failures += 1
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
            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
    98
            continue
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    99
        mismatched = False
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   100
    return clear0
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
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   103
def string_io():
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   104
    """Returns a new `cStringIO.StringIO` instance."""
bc9726c9ad85 VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   105
    return StringIO()