Use non-deprecated importlib_resources API.

This commit is contained in:
Chris Berkhout 2023-06-10 13:19:35 +02:00
parent 7f4ed2f8b5
commit 34c503f6cb
2 changed files with 26 additions and 13 deletions

View file

@ -24,7 +24,7 @@ Functions:
""" """
from dataclasses import dataclass, field from dataclasses import dataclass, field
from importlib.resources import read_binary from importlib.resources import files
from typing import List from typing import List
from lxml import etree from lxml import etree
@ -43,20 +43,28 @@ class ISOCurrency:
def current_data_date(): def current_data_date():
one = etree.fromstring(read_binary("pricehist.resources", "list-one.xml")) one = etree.fromstring(
files("pricehist.resources").joinpath("list-one.xml").read_bytes()
)
return one.cssselect("ISO_4217")[0].attrib["Pblshd"] return one.cssselect("ISO_4217")[0].attrib["Pblshd"]
def historical_data_date(): def historical_data_date():
three = etree.fromstring(read_binary("pricehist.resources", "list-three.xml")) three = etree.fromstring(
files("pricehist.resources").joinpath("list-three.xml").read_bytes()
)
return three.cssselect("ISO_4217")[0].attrib["Pblshd"] return three.cssselect("ISO_4217")[0].attrib["Pblshd"]
def by_code(): def by_code():
result = {} result = {}
one = etree.fromstring(read_binary("pricehist.resources", "list-one.xml")) one = etree.fromstring(
three = etree.fromstring(read_binary("pricehist.resources", "list-three.xml")) files("pricehist.resources").joinpath("list-one.xml").read_bytes()
)
three = etree.fromstring(
files("pricehist.resources").joinpath("list-three.xml").read_bytes()
)
for entry in three.cssselect("HstrcCcyNtry") + one.cssselect("CcyNtry"): for entry in three.cssselect("HstrcCcyNtry") + one.cssselect("CcyNtry"):
if currency := _parse(entry): if currency := _parse(entry):

View file

@ -42,7 +42,7 @@ import hashlib
import logging import logging
from datetime import datetime from datetime import datetime
from decimal import Decimal from decimal import Decimal
from importlib.resources import read_text from importlib.resources import files
from pricehist import __version__ from pricehist import __version__
from pricehist.format import Format from pricehist.format import Format
@ -119,7 +119,11 @@ class GnuCashSQL(BaseOutput):
"well." "well."
) )
sql = read_text("pricehist.resources", "gnucash.sql").format( sql = (
files("pricehist.resources")
.joinpath("gnucash.sql")
.read_text()
.format(
version=__version__, version=__version__,
timestamp=datetime.utcnow().isoformat() + "Z", timestamp=datetime.utcnow().isoformat() + "Z",
base=self._sql_str(base), base=self._sql_str(base),
@ -127,6 +131,7 @@ class GnuCashSQL(BaseOutput):
values_comment=values_comment, values_comment=values_comment,
values=values, values=values,
) )
)
return sql return sql