From 259c78b34fc38fca9f773bb2e8083afa914d307e Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Sat, 19 Sep 2020 15:37:29 +0100 Subject: [PATCH] Allowed custom config --- gri/__main__.py | 37 ++++++++++++++++++++----------------- test/.gertty.yaml | 5 +++++ tox.ini | 2 +- 3 files changed, 26 insertions(+), 18 deletions(-) create mode 100644 test/.gertty.yaml diff --git a/gri/__main__.py b/gri/__main__.py index b59c3cb..94bfc22 100755 --- a/gri/__main__.py +++ b/gri/__main__.py @@ -30,14 +30,15 @@ } ) term = Console(theme=theme, highlighter=rich.highlighter.ReprHighlighter(), record=True) +CFG_FILE = "~/.gertty.yaml" LOG = logging.getLogger(__name__) class Config(dict): - def __init__(self): + def __init__(self, file): super().__init__() - self.update(self.load_config("~/.gertty.yaml")) + self.update(self.load_config(file)) @staticmethod def load_config(config_file): @@ -52,11 +53,12 @@ def load_config(config_file): # pylint: disable=too-few-public-methods class App: - def __init__(self, ctx, server=None, user=None): + def __init__(self, ctx): self.ctx = ctx - self.cfg = Config() + self.cfg = Config(file=ctx.params['config']) self.servers = [] - self.user = user + self.user = ctx.params['user'] + server = ctx.params['server'] for srv in ( self.cfg["servers"] if server is None @@ -146,6 +148,10 @@ def get_command(self, ctx, cmd_name): chain=True, ) @click.option("--user", "-u", default="self", help="Query another user than self") +@click.option( + "--config", + default=CFG_FILE, + help=f"Config file to use, defaults to {CFG_FILE}") @click.option( "--server", "-s", @@ -168,28 +174,28 @@ def get_command(self, ctx, cmd_name): @click.option("--debug", "-d", default=False, help="Debug mode", is_flag=True) @click.pass_context # pylint: disable=unused-argument,too-many-arguments,too-many-locals -def cli(ctx, debug, server, force, user, output): +def cli(ctx, **kwargs): handler = RichHandler(show_time=False, show_path=False) LOG.addHandler(handler) LOG.warning("Called with %s", ctx.params) - if debug: + if ctx.params['debug']: LOG.setLevel(level=logging.DEBUG) - if " " in user: - user = f'"{user}"' + if " " in ctx.params['user']: + ctx.params['user'] = f"\"{ctx.params['user']}\"" # import pdb # pdb.set_trace() - ctx.obj = App(ctx=ctx, server=server, user=user) + ctx.obj = App(ctx=ctx) if ctx.invoked_subcommand is None: LOG.info("I was invoked without subcommand, assuming implicit `owned` command") ctx.invoke(owned) - if output: - term.save_html(path=output, theme=TERMINAL_THEME) + if ctx.params['output']: + term.save_html(path=ctx.params['output'], theme=TERMINAL_THEME) @cli.resultcallback() @@ -230,9 +236,7 @@ def incoming(ctx): ) def merged(ctx, age): """Merged in the last number of days""" - query = f" status:merged -age:{age}d" - query += f" owner:{ctx.obj.user}" - + query = f"status:merged -age:{age}d owner:{ctx.obj.user}" ctx.obj.report(query=query, title=f"Merged Reviews ({age}d)") @@ -271,8 +275,7 @@ def draft(ctx): def abandon(ctx, age): """Abandon changes (delete for drafts) when they are >90 days old " "and with very low score. Requires -f to perform the action.""" - query = f" status:open age:{age}d" - query += f" owner:{ctx.obj.user}" + query = f"status:open age:{age}d owner:{ctx.obj.user}" ctx.obj.report( query=query, diff --git a/test/.gertty.yaml b/test/.gertty.yaml new file mode 100644 index 0000000..2bf3c9f --- /dev/null +++ b/test/.gertty.yaml @@ -0,0 +1,5 @@ +servers: + - name: gerrithub + url: https://review.gerrithub.io/ + username: johndoe + password: CHANGEME diff --git a/tox.ini b/tox.ini index 31f91c1..5b24f3d 100644 --- a/tox.ini +++ b/tox.ini @@ -23,7 +23,7 @@ deps = pip == 19.1.1 whitelist_externals = bash commands = - gri -o report.html owned incoming merged abandon draft watched + gri --config {toxinidir}/test/.gertty.yaml -o report.html owned incoming merged abandon draft watched [testenv:lint] basepython = python3.7