author | Pascal Volk <neverseen@users.sourceforge.net> |
Mon, 26 Jul 2010 16:02:58 +0000 | |
branch | v0.6.x |
changeset 314 | 0b4a6e9d9f27 |
parent 312 | 6f39a1e56f4a |
child 315 | 81bccfd14355 |
permissions | -rw-r--r-- |
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 |
312
6f39a1e56f4a
VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
217
diff
changeset
|
7 |
~~~~~~~~~~~~~~~~~~~~~~ |
205
bc9726c9ad85
VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
8 |
|
bc9726c9ad85
VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
9 |
VirtualMailManager's command line interface. |
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 |
|
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
205
diff
changeset
|
12 |
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
|
13 |
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
|
14 |
from fcntl import ioctl |
205
bc9726c9ad85
VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
15 |
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
|
16 |
from termios import TIOCGWINSZ |
205
bc9726c9ad85
VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
17 |
|
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
205
diff
changeset
|
18 |
from VirtualMailManager import ENCODING |
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.constants.ERROR import VMM_TOO_MANY_FAILURES |
6f39a1e56f4a
VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
217
diff
changeset
|
20 |
from VirtualMailManager.errors import VMMError |
205
bc9726c9ad85
VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
21 |
|
bc9726c9ad85
VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
22 |
|
bc9726c9ad85
VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
23 |
__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
|
24 |
|
312
6f39a1e56f4a
VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
217
diff
changeset
|
25 |
_ = lambda msg: msg |
205
bc9726c9ad85
VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
26 |
_std_write = os.sys.stdout.write |
bc9726c9ad85
VMM/cli: added __init__.py to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
27 |
_err_write = os.sys.stderr.write |
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 |
312
6f39a1e56f4a
VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
217
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 |
312
6f39a1e56f4a
VMM/cli: w_err() call os.sys.exit only if code != 0.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
217
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 _ |