From 2d9b562e2d22053cdbdc37d6dd801b0d67859818 Mon Sep 17 00:00:00 2001 From: Niklas Sombert Date: Mon, 11 Sep 2017 11:51:44 +0200 Subject: [PATCH] utils.with_connection --- metecli/account.py | 2 +- metecli/audits.py | 11 +++++------ metecli/drinks.py | 15 +++++++-------- metecli/utils.py | 8 ++++++++ 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/metecli/account.py b/metecli/account.py index 4c69237..329a323 100644 --- a/metecli/account.py +++ b/metecli/account.py @@ -70,7 +70,7 @@ def modify(self, args): def logs(self, args): """The same as `audits --user `.""" - audits.show(self._conf, user=str(self._uid)) + audits.show(self._conf, self._conn, user=str(self._uid)) def buy(self, args): drink_found = fuzzy_search(self._conn().drinks(), args.drink) diff --git a/metecli/audits.py b/metecli/audits.py index 689d8f0..9b14297 100644 --- a/metecli/audits.py +++ b/metecli/audits.py @@ -1,5 +1,4 @@ -from .connection import Connection -from .utils import fuzzy_search, find_by_id, print_table +from .utils import fuzzy_search, find_by_id, print_table, with_connection from datetime import datetime from argparse import ArgumentTypeError @@ -21,8 +20,9 @@ def setup_cmdline(global_subparsers): parser.add_argument("--to_date", type=valid_date, help="show only audits that were created before this date") parser.set_defaults(func=do) -def do(args, config): - show(config, user=args.user, from_date=args.from_date, to_date=args.to_date) +@with_connection +def do(args, config, conn): + show(config, conn, user=args.user, from_date=args.from_date, to_date=args.to_date) def _create_table(audits, drinks): for audit in audits["audits"]: @@ -33,8 +33,7 @@ def _create_table(audits, drinks): drink = {"name": "n/a"} yield [audit["created_at"], drink["name"], audit["difference"]] -def show(config, user=None, from_date=None, to_date=None): - conn = Connection(base_url=config.settings["connection"]["base_url"]) +def show(config, conn, user=None, from_date=None, to_date=None): params = dict() if user: user_found = fuzzy_search(conn.users(), user) diff --git a/metecli/drinks.py b/metecli/drinks.py index 074ae84..3d88a4a 100644 --- a/metecli/drinks.py +++ b/metecli/drinks.py @@ -1,5 +1,4 @@ -from .connection import Connection -from .utils import true_false_to_yes_no, fuzzy_search, print_table, show_edit +from .utils import true_false_to_yes_no, fuzzy_search, print_table, show_edit, with_connection import logging log = logging.getLogger(__name__) @@ -17,8 +16,8 @@ def setup_cmdline(global_subparsers): parser_modify.set_defaults(func=modify) parser.set_defaults(func=list_drinks) -def list_drinks(args, config): - conn = Connection(base_url=config.settings["connection"]["base_url"]) +@with_connection +def list_drinks(args, config, conn): drinks = conn.drinks() print("All drinks:") print_table(config, @@ -40,8 +39,8 @@ def list_drinks(args, config): ], ) -def show(args, config): - conn = Connection(base_url=config.settings["connection"]["base_url"]) +@with_connection +def show(args, config, conn): drink = fuzzy_search(conn.drinks(), args.drink) if not drink: return @@ -54,8 +53,8 @@ def show(args, config): ["active?", true_false_to_yes_no(drink["active"])], ]) -def modify(args, config): - conn = Connection(base_url=config.settings["connection"]["base_url"]) +@with_connection +def modify(args, config, conn): data = fuzzy_search(conn.drinks(), args.drink) if not data: return diff --git a/metecli/utils.py b/metecli/utils.py index 7d94337..fde161c 100644 --- a/metecli/utils.py +++ b/metecli/utils.py @@ -1,9 +1,17 @@ +from .connection import Connection + from tabulate import tabulate import logging log = logging.getLogger(__name__) +def with_connection(func): + def new_func(args, config): + conn = Connection(base_url=config.settings["connection"]["base_url"]) + return func(args, config, conn) + return new_func + def print_table(config, data, headers=[]): print(tabulate( data,