From 9e041fa933b0b7e9cde65b13bce0e993ead0e95f Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 19 Jul 2016 05:36:19 +0300 Subject: track_status.py: interface update --- bin/track_status.py | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) (limited to 'bin') diff --git a/bin/track_status.py b/bin/track_status.py index 6f1b0b5..19fae07 100644 --- a/bin/track_status.py +++ b/bin/track_status.py @@ -8,17 +8,23 @@ from vk.api import API, Language from vk.tracking import StatusTracker from vk.tracking.db import Format as DatabaseFormat -def _parse_natural_number(s): - x = int(s) +DEFAULT_TIMEOUT = StatusTracker.DEFAULT_TIMEOUT +DEFAULT_DB_FORMAT = DatabaseFormat.CSV + +def _parse_positive_integer(s): + try: + x = int(s) + except ValueError: + raise argparse.ArgumentTypeError('must be a positive integer: ' + s) if x < 1: - raise argparse.ArgumentTypeError('must be positive: ' + x) + raise argparse.ArgumentTypeError('must be a positive integer: ' + s) return x -def _parse_output_format(s): +def _parse_database_format(s): try: return DatabaseFormat(s) except ValueError: - raise argparse.ArgumentTypeError('invalid database format: ' + str(s)) + raise argparse.ArgumentTypeError('invalid database format: ' + s) def _parse_args(args=sys.argv): parser = argparse.ArgumentParser( @@ -27,36 +33,38 @@ def _parse_args(args=sys.argv): parser.add_argument('uids', metavar='UID', nargs='+', help='user IDs or "screen names"') parser.add_argument('-t', '--timeout', metavar='SECONDS', - type=_parse_natural_number, - default=StatusTracker.DEFAULT_TIMEOUT, + type=_parse_positive_integer, + default=DEFAULT_TIMEOUT, help='set refresh interval') parser.add_argument('-l', '--log', metavar='PATH', dest='log_fd', type=argparse.FileType('w', encoding='utf-8'), default=sys.stdout, help='set log file path (standard output by default)') - parser.add_argument('--output-format', dest='fmt', - type=_parse_output_format, default=DatabaseFormat.CSV, + parser.add_argument('-f', '--format', dest='db_fmt', + type=_parse_database_format, choices=DatabaseFormat, - help='set database format') - parser.add_argument('-o', '--output', metavar='PATH', dest='fd', + default=DEFAULT_DB_FORMAT, + help='specify database format') + parser.add_argument('-o', '--output', metavar='PATH', dest='db_fd', type=argparse.FileType('w', encoding='utf-8'), default=None, help='set database file path') return parser.parse_args(args[1:]) -def track_status(uids, timeout=StatusTracker.DEFAULT_TIMEOUT, - log_fd=sys.stdout, fmt=DatabaseFormat.CSV, fd=None): +def track_status(uids, timeout=DEFAULT_TIMEOUT, + log_fd=sys.stdout, + db_fd=None, db_fmt=DEFAULT_DB_FORMAT): api = API(Language.EN, deactivated_users=False) tracker = StatusTracker(api, timeout) - if fmt is DatabaseFormat.LOG or fd is None: - fmt = DatabaseFormat.NULL + if db_fmt is DatabaseFormat.LOG or db_fd is None: + db_fmt = DatabaseFormat.NULL with DatabaseFormat.LOG.create_writer(log_fd) as log_writer: tracker.add_database_writer(log_writer) - with fmt.create_writer(fd) as db_writer: + with db_fmt.create_writer(db_fd) as db_writer: tracker.add_database_writer(db_writer) tracker.loop(uids) -- cgit v1.2.3