Drop pytomlpp dependency for Python >= 3.11
Rely on tomllib for Python >= 3.11
This commit is contained in:
parent
858b2071df
commit
6ea4f4faf4
@ -16,4 +16,4 @@ redis==5.0.2
|
|||||||
markdown-it-py==3.0.0
|
markdown-it-py==3.0.0
|
||||||
typing_extensions==4.9.0
|
typing_extensions==4.9.0
|
||||||
fasttext-predict==0.9.2.2
|
fasttext-predict==0.9.2.2
|
||||||
pytomlpp==1.0.13
|
pytomlpp==1.0.13; python_version < '3.11'
|
||||||
|
@ -14,7 +14,18 @@ import copy
|
|||||||
import typing
|
import typing
|
||||||
import logging
|
import logging
|
||||||
import pathlib
|
import pathlib
|
||||||
import pytomlpp as toml
|
|
||||||
|
try:
|
||||||
|
import tomllib
|
||||||
|
|
||||||
|
pytomlpp = None
|
||||||
|
USE_TOMLLIB = True
|
||||||
|
except ImportError:
|
||||||
|
import pytomlpp
|
||||||
|
|
||||||
|
tomllib = None
|
||||||
|
USE_TOMLLIB = False
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['Config', 'UNSET', 'SchemaIssue']
|
__all__ = ['Config', 'UNSET', 'SchemaIssue']
|
||||||
|
|
||||||
@ -62,7 +73,7 @@ class Config:
|
|||||||
# init schema
|
# init schema
|
||||||
|
|
||||||
log.debug("load schema file: %s", schema_file)
|
log.debug("load schema file: %s", schema_file)
|
||||||
cfg = cls(cfg_schema=toml.load(schema_file), deprecated=deprecated)
|
cfg = cls(cfg_schema=toml_load(schema_file), deprecated=deprecated)
|
||||||
if not cfg_file.exists():
|
if not cfg_file.exists():
|
||||||
log.warning("missing config file: %s", cfg_file)
|
log.warning("missing config file: %s", cfg_file)
|
||||||
return cfg
|
return cfg
|
||||||
@ -70,12 +81,7 @@ class Config:
|
|||||||
# load configuration
|
# load configuration
|
||||||
|
|
||||||
log.debug("load config file: %s", cfg_file)
|
log.debug("load config file: %s", cfg_file)
|
||||||
try:
|
upd_cfg = toml_load(cfg_file)
|
||||||
upd_cfg = toml.load(cfg_file)
|
|
||||||
except toml.DecodeError as exc:
|
|
||||||
msg = str(exc).replace('\t', '').replace('\n', ' ')
|
|
||||||
log.error("%s: %s", cfg_file, msg)
|
|
||||||
raise
|
|
||||||
|
|
||||||
is_valid, issue_list = cfg.validate(upd_cfg)
|
is_valid, issue_list = cfg.validate(upd_cfg)
|
||||||
for msg in issue_list:
|
for msg in issue_list:
|
||||||
@ -177,6 +183,25 @@ class Config:
|
|||||||
return getattr(m, name)
|
return getattr(m, name)
|
||||||
|
|
||||||
|
|
||||||
|
def toml_load(file_name):
|
||||||
|
if USE_TOMLLIB:
|
||||||
|
# Python >= 3.11
|
||||||
|
try:
|
||||||
|
with open(file_name, "rb") as f:
|
||||||
|
return tomllib.load(f)
|
||||||
|
except tomllib.TOMLDecodeError as exc:
|
||||||
|
msg = str(exc).replace('\t', '').replace('\n', ' ')
|
||||||
|
log.error("%s: %s", file_name, msg)
|
||||||
|
raise
|
||||||
|
# fallback to pytomlpp for Python < 3.11
|
||||||
|
try:
|
||||||
|
return pytomlpp.load(file_name)
|
||||||
|
except pytomlpp.DecodeError as exc:
|
||||||
|
msg = str(exc).replace('\t', '').replace('\n', ' ')
|
||||||
|
log.error("%s: %s", file_name, msg)
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
# working with dictionaries
|
# working with dictionaries
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user