From d179e8f138c02bd6802d23ea9b8a131c822896a4 Mon Sep 17 00:00:00 2001 From: Alexander Shpilkin Date: Fri, 3 May 2019 23:06:33 +0300 Subject: [PATCH] Rearrange commands alphabetically --- dvrip/__main__.py | 122 +++++++++++++++++++++++----------------------- 1 file changed, 60 insertions(+), 62 deletions(-) diff --git a/dvrip/__main__.py b/dvrip/__main__.py index 3c855de..d9b5cc4 100755 --- a/dvrip/__main__.py +++ b/dvrip/__main__.py @@ -28,13 +28,6 @@ # pylint: disable=too-many-branches,too-many-locals,too-many-statements -def prog() -> str: - name = basename(argv[0]) - return ('{} -m dvrip'.format(executable) - if name in {'__main__.py', '-c'} - else name) - - def ioerr(e: OSError, code: int = EX_IOERR) -> NoReturn: message = ('{}: {}'.format(e.filename, e.strerror) \ if e.filename is not None else e.strerror) @@ -72,6 +65,41 @@ def connect(address: Tuple[str, int], user: str, password: str) -> DVRIPClient: return conn +def cat_usage() -> NoReturn: + print('Usage: {} cat {{NAME|CHANNEL}}'.format(prog_connected()), + file=stderr) + exit(EX_USAGE) + +def run_cat(conn: DVRIPClient, sock: Socket, args: List[str]) -> None: + if len(args) != 1: + cat_usage() + + name, = args + if name.startswith('/'): + reader = lambda: conn.download(sock, name) + elif name.startswith('monitor:'): + if ';' not in name: + name += ';hd' + chanstr, strstr = name[len('monitor:'):].split(';', 1) + try: + channel = int(chanstr, base=0) + except ValueError: + cat_usage() + try: + stream = Stream[strstr.upper()] + except KeyError: + cat_usage() + reader = lambda: conn.monitor(sock, channel, stream) + try: + file = reader() + try: + copyfileobj(file, stdout.buffer, length=256) + except (BrokenPipeError, KeyboardInterrupt): + pass + finally: + sock.close() + + def info_usage() -> NoReturn: print('Usage: {} info'.format(prog_connected()), file=stderr) exit(EX_USAGE) @@ -130,20 +158,6 @@ def run_info(conn: DVRIPClient, args: List[str]) -> None: print(' '.join(line)) # status line -def time_usage() -> NoReturn: - print('Usage: {} time [TIME]'.format(prog_connected()), file=stderr) - exit(EX_USAGE) - -def run_time(conn: DVRIPClient, args: List[str]) -> None: - from dateparser import parse as dateparse # type: ignore - - time = dateparse(args[0] if args else '1970-01-01') - if len(args) > 2 or time is None or time.tzinfo is not None: - time_usage() - - print((conn.time(time) if args else conn.time()).isoformat()) - - def find_usage() -> NoReturn: print('Usage: {} find -{{i|v}} [-l] [-s START] [-e END] -c CHANNEL' .format(prog_connected()), @@ -212,41 +226,6 @@ def run_find(conn: DVRIPClient, args: List[str]) -> None: print(file.name) -def cat_usage() -> NoReturn: - print('Usage: {} cat {{NAME|CHANNEL}}'.format(prog_connected()), - file=stderr) - exit(EX_USAGE) - -def run_cat(conn: DVRIPClient, sock: Socket, args: List[str]) -> None: - if len(args) != 1: - cat_usage() - - name, = args - if name.startswith('/'): - reader = lambda: conn.download(sock, name) - elif name.startswith('monitor:'): - if ';' not in name: - name += ';hd' - chanstr, strstr = name[len('monitor:'):].split(';', 1) - try: - channel = int(chanstr, base=0) - except ValueError: - cat_usage() - try: - stream = Stream[strstr.upper()] - except KeyError: - cat_usage() - reader = lambda: conn.monitor(sock, channel, stream) - try: - file = reader() - try: - copyfileobj(file, stdout.buffer, length=256) - except (BrokenPipeError, KeyboardInterrupt): - pass - finally: - sock.close() - - def neigh_usage() -> NoReturn: print('Usage: {} neigh'.format(prog()), file=stderr) exit(EX_USAGE) @@ -283,13 +262,32 @@ def run_neigh(args: List[str]) -> None: ioerr(e) +def time_usage() -> NoReturn: + print('Usage: {} time [TIME]'.format(prog_connected()), file=stderr) + exit(EX_USAGE) + +def run_time(conn: DVRIPClient, args: List[str]) -> None: + from dateparser import parse as dateparse # type: ignore + + time = dateparse(args[0] if args else '1970-01-01') + if len(args) > 2 or time is None or time.tzinfo is not None: + time_usage() + + print((conn.time(time) if args else conn.time()).isoformat()) + + +def prog() -> str: + name = basename(argv[0]) + return ('{} -m dvrip'.format(executable) + if name in {'__main__.py', '-c'} + else name) + def prog_connected() -> str: return '{} -h HOST [-p PORT] [-u USERNAME]'.format(prog()) - def usage() -> NoReturn: print('Usage: {} [-h HOST] [-p PORT] [-u USERNAME] COMMAND ...\n' - 'where COMMAND is one of cat, find, info, neigh, reboot, or time' + ' COMMAND is one of cat, find, info, neigh, reboot, or time' .format(prog()), file=stderr) exit(EX_USAGE) @@ -339,10 +337,6 @@ def run(args: List[str] = argv[1:]) -> None: # pylint: disable=dangerous-defaul run_cat(conn, sock, args) finally: conn.logout() - elif command == 'neigh': - if host is not None: - neigh_usage() - run_neigh(args) elif command == 'info': if host is None: usage() @@ -361,6 +355,10 @@ def run(args: List[str] = argv[1:]) -> None: # pylint: disable=dangerous-defaul run_find(conn, args) finally: conn.logout() + elif command == 'neigh': + if host is not None: + neigh_usage() + run_neigh(args) elif command == 'reboot': if host is None or args: print('Usage: {} reboot'.format(prog_connected()),