`piker.config`: use `tomlkit` for accounting files

We still need to get some patches landed in order to resolve:
- https://github.com/sdispater/tomlkit/issues/288
- https://github.com/sdispater/tomlkit/issues/289
- https://github.com/sdispater/tomlkit/issues/290

But, this does work for style preservation and the inline-table style we
were previously hacking into the `toml` lib in `.accounting._toml`,
which we can pretty much just drop now B)

Relates to #496 (pretty much solves it near-term i think?)
master
Tyler Goodlet 2023-05-12 16:05:45 -04:00
parent 5f79434b23
commit 2865f0efe9
2 changed files with 40 additions and 19 deletions

View File

@ -23,13 +23,19 @@ import sys
import os import os
import shutil import shutil
import time import time
from typing import Optional from typing import (
Callable,
MutableMapping,
)
from pathlib import Path from pathlib import Path
from bidict import bidict from bidict import bidict
import toml import tomlkit
import tomli try:
# import tomlkit # TODO! import tomllib
except ModuleNotFoundError:
import tomli as tomllib
from .log import get_logger from .log import get_logger
@ -220,6 +226,11 @@ def load(
conf_name: str = 'brokers', conf_name: str = 'brokers',
path: Path | None = None, path: Path | None = None,
decode: Callable[
[str | bytes,],
MutableMapping,
] = tomllib.loads,
**tomlkws, **tomlkws,
) -> tuple[dict, Path]: ) -> tuple[dict, Path]:
@ -250,11 +261,7 @@ def load(
pass pass
with path.open(mode='r') as fp: with path.open(mode='r') as fp:
# TODO: move to tomlkit: config: dict = decode(
# - needs to be fixed to support bidict?
# - we need to use or fork's fix to do multiline array
# indenting.
config: dict = toml.loads(
fp.read(), fp.read(),
**tomlkws, **tomlkws,
) )
@ -277,7 +284,10 @@ def load_account(
fn: str = f'account.{brokername}.{acctid}.toml' fn: str = f'account.{brokername}.{acctid}.toml'
dirpath: Path = _config_dir / 'accounting' dirpath: Path = _config_dir / 'accounting'
config, path = load(path=dirpath / fn) config, path = load(
path=dirpath / fn,
decode=tomlkit.parse,
)
if not config: if not config:
legacypath = dirpath / legacy_fn legacypath = dirpath / legacy_fn
@ -287,7 +297,16 @@ def load_account(
f'Rewriting contents to new name -> {path}\n' f'Rewriting contents to new name -> {path}\n'
'Please delete the old file!\n' 'Please delete the old file!\n'
) )
legacy_config, _ = load(path=legacypath) legacy_config, _ = load(
path=legacypath,
# TODO: move to tomlkit:
# - needs to be fixed to support bidict?
# https://github.com/sdispater/tomlkit/issues/289
# - we need to use or fork's fix to do multiline array
# indenting.
decode=tomlkit.parse,
)
config.update(legacy_config) config.update(legacy_config)
# XXX: override the presumably previously non-existant # XXX: override the presumably previously non-existant
@ -295,6 +314,7 @@ def load_account(
write( write(
config, config,
path=path, path=path,
fail_empty=False,
) )
return config, path return config, path
@ -321,7 +341,7 @@ def load_ledger(
with fpath.open(mode='rb') as cf: with fpath.open(mode='rb') as cf:
start = time.time() start = time.time()
ledger_dict = tomli.load(cf) ledger_dict = tomlkit.parse(cf.read())
log.debug(f'Ledger load took {time.time() - start}s') log.debug(f'Ledger load took {time.time() - start}s')
return ledger_dict, fpath return ledger_dict, fpath
@ -362,10 +382,10 @@ def write(
f"Writing config `{name}` file to:\n" f"Writing config `{name}` file to:\n"
f"{path}" f"{path}"
) )
with path.open(mode='w') as cf: with path.open(mode='w') as fp:
return toml.dump( return tomlkit.dump( # preserve style on write B)
config, config,
cf, fp,
**toml_kwargs, **toml_kwargs,
) )
@ -373,7 +393,7 @@ def write(
def load_accounts( def load_accounts(
providers: list[str] | None = None providers: list[str] | None = None
) -> bidict[str, Optional[str]]: ) -> bidict[str, str | None]:
conf, path = load() conf, path = load()
accounts = bidict() accounts = bidict()

View File

@ -44,15 +44,16 @@ setup(
] ]
}, },
install_requires=[ install_requires=[
'toml', 'tomlkit', # fork & fix for now:
'tomli', # fastest pure py reader 'tomli', # for pre-3.11
'click',
'colorlog', 'colorlog',
'attrs', 'attrs',
'pygments', 'pygments',
'colorama', # numba traceback coloring 'colorama', # numba traceback coloring
'msgspec', # performant IPC messaging and structs 'msgspec', # performant IPC messaging and structs
'protobuf', 'protobuf',
'typer',
'rich',
# async # async
'trio', 'trio',