Drop final use of `toml` 3rd party lib

We moved to `tomlkit` as per #496 and this lets us drop the mess that
was the inline-table encoder in `.accounting._toml` XD

Relates to #496
master
Tyler Goodlet 2023-05-12 16:15:12 -04:00
parent 2865f0efe9
commit a62283bae2
2 changed files with 10 additions and 174 deletions

View File

@ -36,11 +36,8 @@ from typing import (
import pendulum import pendulum
from pendulum import datetime, now from pendulum import datetime, now
import tomlkit
from ._toml import (
toml,
PpsEncoder,
)
from ._ledger import ( from ._ledger import (
Transaction, Transaction,
iter_by_dt, iter_by_dt,
@ -180,11 +177,16 @@ class Position(Struct):
elif expiry: elif expiry:
d['expiry'] = str(expiry) d['expiry'] = str(expiry)
toml_clears_list: list[dict[str, Any]] = [] clears_table: tomlkit.Array = tomlkit.array()
clears_table.multiline(
multiline=True,
indent='',
)
# reverse sort so latest clears are at top of section? # reverse sort so latest clears are at top of section?
for tid, data in iter_by_dt(clears): for tid, data in iter_by_dt(clears):
inline_table = toml.TomlDecoder().get_empty_inline_table()
inline_table = tomlkit.inline_table()
# serialize datetime to parsable `str` # serialize datetime to parsable `str`
dtstr = inline_table['dt'] = data['dt'].isoformat('T') dtstr = inline_table['dt'] = data['dt'].isoformat('T')
@ -201,9 +203,9 @@ class Position(Struct):
inline_table[k] = data[k] inline_table[k] = data[k]
inline_table['tid'] = tid inline_table['tid'] = tid
toml_clears_list.append(inline_table) clears_table.append(inline_table)
d['clears'] = toml_clears_list d['clears'] = clears_table
return fqme, d return fqme, d
@ -732,19 +734,9 @@ class PpTable(Struct):
for entry in list(self.conf): for entry in list(self.conf):
del self.conf[entry] del self.conf[entry]
# TODO: why tf haven't they already done this for inline
# tables smh..
enc = PpsEncoder(preserve=True)
# table_bs_type = type(toml.TomlDecoder().get_empty_inline_table())
enc.dump_funcs[
toml.decoder.InlineTableDict
] = enc.dump_inline_table
config.write( config.write(
config=self.conf, config=self.conf,
path=self.conf_path, path=self.conf_path,
encoder=enc,
fail_empty=False
) )

View File

@ -1,156 +0,0 @@
# piker: trading gear for hackers
# Copyright (C) Tyler Goodlet (in stewardship for pikers)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
TOML codec hacks to make position tables look decent.
(looking at you "`toml`-lib"..)
'''
import re
import toml
# TODO: instead see if we can hack tomli and tomli-w to do the same:
# - https://github.com/hukkin/tomli
# - https://github.com/hukkin/tomli-w
class PpsEncoder(toml.TomlEncoder):
'''
Special "styled" encoder that makes a ``pps.toml`` redable and
compact by putting `.clears` tables inline and everything else
flat-ish.
'''
separator = ','
def dump_list(self, v):
'''
Dump an inline list with a newline after every element and
with consideration for denoted inline table types.
'''
retval = "[\n"
for u in v:
if isinstance(u, toml.decoder.InlineTableDict):
out = self.dump_inline_table(u)
else:
out = str(self.dump_value(u))
retval += " " + out + "," + "\n"
retval += "]"
return retval
def dump_inline_table(self, section):
"""Preserve inline table in its compact syntax instead of expanding
into subsection.
https://github.com/toml-lang/toml#user-content-inline-table
"""
val_list = []
for k, v in section.items():
# if isinstance(v, toml.decoder.InlineTableDict):
if isinstance(v, dict):
val = self.dump_inline_table(v)
else:
val = str(self.dump_value(v))
val_list.append(k + " = " + val)
retval = "{ " + ", ".join(val_list) + " }"
return retval
def dump_sections(self, o, sup):
retstr = ""
if sup != "" and sup[-1] != ".":
sup += '.'
retdict = self._dict()
arraystr = ""
for section in o:
qsection = str(section)
value = o[section]
if not re.match(r'^[A-Za-z0-9_-]+$', section):
qsection = toml.encoder._dump_str(section)
# arrayoftables = False
if (
self.preserve
and isinstance(value, toml.decoder.InlineTableDict)
):
retstr += (
qsection
+
" = "
+
self.dump_inline_table(o[section])
+
'\n' # only on the final terminating left brace
)
# XXX: this code i'm pretty sure is just blatantly bad
# and/or wrong..
# if isinstance(o[section], list):
# for a in o[section]:
# if isinstance(a, dict):
# arrayoftables = True
# if arrayoftables:
# for a in o[section]:
# arraytabstr = "\n"
# arraystr += "[[" + sup + qsection + "]]\n"
# s, d = self.dump_sections(a, sup + qsection)
# if s:
# if s[0] == "[":
# arraytabstr += s
# else:
# arraystr += s
# while d:
# newd = self._dict()
# for dsec in d:
# s1, d1 = self.dump_sections(d[dsec], sup +
# qsection + "." +
# dsec)
# if s1:
# arraytabstr += ("[" + sup + qsection +
# "." + dsec + "]\n")
# arraytabstr += s1
# for s1 in d1:
# newd[dsec + "." + s1] = d1[s1]
# d = newd
# arraystr += arraytabstr
elif isinstance(value, dict):
retdict[qsection] = o[section]
elif o[section] is not None:
retstr += (
qsection
+
" = "
+
str(self.dump_value(o[section]))
)
# if not isinstance(value, dict):
if not isinstance(value, toml.decoder.InlineTableDict):
# inline tables should not contain newlines:
# https://toml.io/en/v1.0.0#inline-table
retstr += '\n'
else:
raise ValueError(value)
retstr += arraystr
return (retstr, retdict)