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 importlib.resources import read_binary
from importlib.resources import files
from typing import List
from lxml import etree
@ -43,20 +43,28 @@ class ISOCurrency:
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"]
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"]
def by_code():
result = {}
one = etree.fromstring(read_binary("pricehist.resources", "list-one.xml"))
three = etree.fromstring(read_binary("pricehist.resources", "list-three.xml"))
one = etree.fromstring(
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"):
if currency := _parse(entry):

View file

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