-
Notifications
You must be signed in to change notification settings - Fork 22
/
__main__.py
executable file
·70 lines (56 loc) · 1.91 KB
/
__main__.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
#!/usr/bin/env python3
from prompt_toolkit.patch_stdout import patch_stdout
from loguru import logger
from mutil import safeLoop # type: ignore
import asyncio
import icli.cli as cli
import sys
from dotenv import dotenv_values
import os
# Use more efficient coroutine logic if available
# https://docs.python.org/3.12/library/asyncio-task.html#asyncio.eager_task_factory
if sys.version_info >= (3, 12):
asyncio.get_event_loop().set_task_factory(asyncio.eager_task_factory)
CONFIG_DEFAULT = dict(
ICLI_IBKR_HOST="127.0.0.1", ICLI_IBKR_PORT=4001, ICLI_REFRESH=3.33
)
CONFIG = {**CONFIG_DEFAULT, **dotenv_values(".env.icli"), **os.environ}
try:
ACCOUNT_ID: str = CONFIG["ICLI_IBKR_ACCOUNT_ID"]
except:
logger.error(
"Sorry, please provide your IBKR Account ID [U...] in ICLI_IBKR_ACCOUNT_ID"
)
sys.exit(0)
HOST: str = CONFIG["ICLI_IBKR_HOST"]
PORT = int(CONFIG["ICLI_IBKR_PORT"]) # type: ignore
REFRESH = float(CONFIG["ICLI_REFRESH"]) # type: ignore
async def initcli():
app = cli.IBKRCmdlineApp(
accountId=ACCOUNT_ID, toolbarUpdateInterval=REFRESH, host=HOST, port=PORT
)
await app.setup()
if sys.stdin.isatty():
# patch entire application with prompt-toolkit-compatible stdout
with patch_stdout(raw=True):
try:
await app.runall()
except (SystemExit, EOFError):
# known-good exit condition
pass
except:
logger.exception("Major uncaught exception?")
else:
logger.error("Attached input isn't a console, so we can't do anything!")
app.stop()
def runit():
"""Entry point for icli script and __main__ for entire package."""
try:
asyncio.run(initcli())
except (KeyboardInterrupt, SystemExit):
# known-good exit condition
...
except:
logger.exception("bad bad so bad bad")
if __name__ == "__main__":
runit()