forked from tdryer/hangups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
81 lines (69 loc) · 2.63 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Common framework used to run hangups examples."""
import argparse
import asyncio
import logging
import os
import hangups
import appdirs
def run_example(example_coroutine, *extra_args):
"""Run a hangups example coroutine.
Args:
example_coroutine (coroutine): Coroutine to run with a connected
hangups client and arguments namespace as arguments.
extra_args (str): Any extra command line arguments required by the
example.
"""
args = _get_parser(extra_args).parse_args()
logging.basicConfig(level=logging.DEBUG if args.debug else logging.WARNING)
# Obtain hangups authentication cookies, prompting for credentials from
# standard input if necessary.
cookies = hangups.auth.get_auth_stdin(args.token_path)
client = hangups.Client(cookies)
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(_async_main(example_coroutine, client, args),
loop=loop)
try:
loop.run_until_complete(task)
except KeyboardInterrupt:
task.cancel()
loop.run_until_complete(task)
finally:
loop.close()
def _get_parser(extra_args):
"""Return ArgumentParser with any extra arguments."""
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
dirs = appdirs.AppDirs('hangups', 'hangups')
default_token_path = os.path.join(dirs.user_cache_dir, 'refresh_token.txt')
parser.add_argument(
'--token-path', default=default_token_path,
help='path used to store OAuth refresh token'
)
parser.add_argument(
'-d', '--debug', action='store_true',
help='log detailed debugging messages'
)
for extra_arg in extra_args:
parser.add_argument(extra_arg, required=True)
return parser
async def _async_main(example_coroutine, client, args):
"""Run the example coroutine."""
# Spawn a task for hangups to run in parallel with the example coroutine.
task = asyncio.ensure_future(client.connect())
# Wait for hangups to either finish connecting or raise an exception.
on_connect = asyncio.Future()
client.on_connect.add_observer(lambda: on_connect.set_result(None))
done, _ = await asyncio.wait(
(on_connect, task), return_when=asyncio.FIRST_COMPLETED
)
await asyncio.gather(*done)
# Run the example coroutine. Afterwards, disconnect hangups gracefully and
# yield the hangups task to handle any exceptions.
try:
await example_coroutine(client, args)
except asyncio.CancelledError:
pass
finally:
await client.disconnect()
await task