Fixup missing ib section handling; drop `.api` subsection

brokers_config
Tyler Goodlet 2021-07-27 08:28:44 -04:00
parent d5394ac677
commit 89b2089562
2 changed files with 17 additions and 10 deletions

View File

@ -11,15 +11,15 @@ key_descr = "api_0"
public_key = "" public_key = ""
private_key = "" private_key = ""
[ib.api] [ib]
ipaddr = "127.0.0.1" host = "127.0.0.1"
[ib.accounts] [ib.accounts]
margin = "" margin = ""
registered = "" registered = ""
paper = "" paper = ""
[ib.api.ports] [ib.ports]
gw = 4002 gw = 4002
tws = 7497 tws = 7497
order = [ "gw", "tws",] order = [ "gw", "tws",]

View File

@ -656,25 +656,28 @@ def get_config() -> dict[str, Any]:
section = conf.get('ib') section = conf.get('ib')
if not section: if section is None:
log.warning(f'No config section found for ib in {path}') log.warning(f'No config section found for ib in {path}')
return return {}
return section return section
@asynccontextmanager @asynccontextmanager
async def _aio_get_client( async def _aio_get_client(
host: str = '127.0.0.1', host: str = '127.0.0.1',
port: int = None, port: int = None,
client_id: Optional[int] = None, client_id: Optional[int] = None,
) -> Client: ) -> Client:
"""Return an ``ib_insync.IB`` instance wrapped in our client API. '''Return an ``ib_insync.IB`` instance wrapped in our client API.
Client instances are cached for later use. Client instances are cached for later use.
TODO: consider doing this with a ctx mngr eventually? TODO: consider doing this with a ctx mngr eventually?
""" '''
conf = get_config() conf = get_config()
# first check cache for existing client # first check cache for existing client
@ -699,17 +702,21 @@ async def _aio_get_client(
ib = NonShittyIB() ib = NonShittyIB()
# attempt to get connection info from config # attempt to get connection info from config; if no .toml entry
ports = conf['api'].get( # exists, we try to load from a default localhost connection.
host = conf.get('host', '127.0.0.1')
ports = conf.get(
'ports', 'ports',
{
# default order is to check for gw first # default order is to check for gw first
{
'gw': 4002, 'gw': 4002,
'tws': 7497, 'tws': 7497,
'order': ['gw', 'tws'] 'order': ['gw', 'tws']
} }
) )
order = ports['order'] order = ports['order']
try_ports = [ports[key] for key in order] try_ports = [ports[key] for key in order]
ports = try_ports if port is None else [port] ports = try_ports if port is None else [port]