Skip to content

Commit 659869b

Browse files
committed
Support click
1 parent 0b6125c commit 659869b

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

branchmgr/main.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import functools
12
import json
23
import os
34
import pprint
45
import sys
56

7+
import click
68
import gidgethub
79

810
from twisted.internet import reactor, defer, task
@@ -13,6 +15,20 @@
1315
USER_AGENT = "Lukasa"
1416

1517

18+
def synchronize(f):
19+
"""
20+
A function decorator that takes an async function and wraps it in a
21+
function that can invoke it synchronously using twisted's task.react. This
22+
is done to bridge between Twisted and click.
23+
"""
24+
@functools.wraps(f)
25+
def inner(*args, **kwargs):
26+
d = defer.ensureDeferred(f(*args, **kwargs))
27+
task.react(lambda *args: d)
28+
29+
return inner
30+
31+
1632
def protection_data():
1733
"""
1834
Get a dictionary representing the JSON required to protect a branch.
@@ -83,23 +99,23 @@ async def protect_branch(self, owner, reponame, branch):
8399
self._set_branch_protection(owner, reponame, branch, protection_status)
84100

85101

86-
async def async_main():
102+
@click.group()
103+
def cli():
104+
pass
105+
106+
107+
@cli.command()
108+
@click.argument('repo')
109+
@click.argument('branch')
110+
@synchronize
111+
async def protection(repo, branch):
112+
"""
113+
Query the protection status of a branch.
114+
"""
87115
client = APIClient()
88-
organisation, reponame = sys.argv[1].split('/', 1)
89-
try:
90-
branch = sys.argv[2]
91-
except IndexError:
92-
branch = sys.argv[2]
116+
organisation, reponame = repo.split('/', 1)
93117

94118
if await client.branch_requires_review(organisation, reponame, branch):
95119
print(f"{organisation}/{reponame}@{branch} requires review")
96120
else:
97121
print(f"{organisation}/{reponame}@{branch} does not require review")
98-
99-
100-
def sync_main(reactor, *args):
101-
return defer.ensureDeferred(async_main(*args))
102-
103-
104-
def main():
105-
task.react(sync_main)

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
'Programming Language :: Python :: 3.6',
3939
'Programming Language :: Python :: Implementation :: CPython',
4040
],
41-
install_requires=['twisted[tls]', 'gidgethub', 'treq'],
41+
install_requires=['twisted[tls]', 'gidgethub', 'treq', 'click'],
4242
entry_points={
4343
'console_scripts': [
44-
'branchmgr = branchmgr.main:main',
44+
'branchmgr = branchmgr.main:cli',
4545
],
4646
}
4747
)

0 commit comments

Comments
 (0)