diff --git a/eboxbw/cli.py b/eboxbw/cli.py index fae48ee..7673985 100644 --- a/eboxbw/cli.py +++ b/eboxbw/cli.py @@ -35,6 +35,8 @@ def _parse_args(): ap.add_argument('-d', '--details', action='store_true', help='display more bandwidth usage details') + ap.add_argument('-m', '--machine', action='store_true', + help='display data in machine-readable format') ap.add_argument('-u', '--unit', action='store', type=str, default='g', metavar='UNIT', help='set display unit to UNIT (\"g\" for GiB or \"m\" for MiB)') @@ -65,6 +67,10 @@ def _print_warning(msg): cprint('warning: {}'.format(msg), 'yellow', attrs=['bold'], file=sys.stderr) +def _print_usage_data_warning(): + _print_warning('no usage data available yet for current month') + + def _bold(t): return colored(t, attrs=['bold']) @@ -80,6 +86,13 @@ def _yes_no(v): }[v] +def _yes_no_machine(v): + return { + True: 'yes', + False: 'no', + }[v] + + def _effective(t): return colored(t, 'yellow') @@ -132,7 +145,7 @@ def print_table_header(): date = cur_month_usage.date if date is None: - _print_warning('no usage data available yet for current month') + _print_usage_data_warning() return if usage_info.has_super_off_peak: @@ -157,6 +170,48 @@ def print_table_header(): print_table_border() +def _print_machine(usage_info, conv_func, punit, details): + def print_row(date, dl, ul, cb, date_cb): + date_txt = date_cb(date) + fmt_row1 = '{},{},{},{}' + print(fmt_row1.format(date_txt, conv_func(dl.real_gb), + conv_func(ul.real_gb), conv_func(cb.real_gb))) + + if dl.effective_gb is not None: + dl_txt = dl.effective_gb + ul_txt = ul.effective_gb + cb_txt = cb.effective_gb + print(fmt_row1.format('effective', conv_func(dl_txt), + conv_func(ul_txt), conv_func(cb_txt))) + + if details: + print('{},{}'.format('plan', usage_info.plan)) + print('{},{}'.format('super-off-peak', + _yes_no_machine(usage_info.has_super_off_peak))) + print('{},{}'.format('extra-blocks', + usage_info.extra_blocks)) + print('{},{}'.format('capacity', + conv_func(usage_info.plan_cap.real_gb))) + print('{},{}'.format('available-usage', + conv_func(usage_info.available_usage.real_gb))) + + cur_month_usage = usage_info.cur_month_usage + date = cur_month_usage.date + + if date is None: + _print_usage_data_warning() + return + + tdl = cur_month_usage.dl_usage + tul = cur_month_usage.ul_usage + tcb = cur_month_usage.combined_usage + print_row(date, tdl, tul, tcb, lambda d: d.strftime('%Y-%m')) + + if details: + for du in cur_month_usage.days_usage: + print_row(du.date, du.dl_usage, du.ul_usage, du.combined_usage, + lambda d: d.strftime('%Y-%m-%d')) + def _main(args): try: @@ -185,7 +240,10 @@ def _main(args): conv_func = lambda x: x * 1024 punit = 'MiB' - _print_human(usage_info, conv_func, punit, args.details) + if args.machine: + _print_machine(usage_info, conv_func, punit, args.details) + else: + _print_human(usage_info, conv_func, punit, args.details) def run():