Skip to content

Commit

Permalink
Allowed custom config
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Sep 20, 2020
1 parent a1576d7 commit 259c78b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
37 changes: 20 additions & 17 deletions gri/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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()
Expand Down Expand Up @@ -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)")


Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions test/.gertty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
servers:
- name: gerrithub
url: https://review.gerrithub.io/
username: johndoe
password: CHANGEME
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 259c78b

Please sign in to comment.