-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13dcccd
commit f0f86f1
Showing
5 changed files
with
194 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,25 @@ | ||
""" | ||
pint.delegates | ||
~~~~~~~~~~~~~~ | ||
pint.delegates | ||
~~~~~~~~~~~~~~ | ||
Defines methods and classes to handle autonomous tasks. | ||
Defines methods and classes to handle autonomous tasks. | ||
:copyright: 2022 by Pint Authors, see AUTHORS for more details. | ||
:license: BSD, see LICENSE for more details. | ||
:copyright: 2022 by Pint Authors, see AUTHORS for more details. | ||
:license: BSD, see LICENSE for more details. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from . import txt_defparser | ||
from .base_defparser import ParserConfig, build_disk_cache_class | ||
from .formatter import Formatter | ||
from .toml_parser import toml_parser | ||
from .toml_parser import toml_defparser, write_definitions | ||
|
||
__all__ = [ | ||
"txt_defparser", | ||
"ParserConfig", | ||
"build_disk_cache_class", | ||
"Formatter", | ||
"toml_parser", | ||
"toml_defparser", | ||
"write_definitions", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
""" | ||
pint.delegates.toml_parser | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
pint.delegates.toml_parser | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
Parser for the toml Pint Definition file. | ||
Parser for the toml Pint Definition file. | ||
:copyright: 2025 by Pint Authors, see AUTHORS for more details. | ||
:license: BSD, see LICENSE for more details. | ||
:copyright: 2025 by Pint Authors, see AUTHORS for more details. | ||
:license: BSD, see LICENSE for more details. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from .toml_parser import TomlParser | ||
from .toml_defparser import TomlParser | ||
from .toml_writer import write_definitions | ||
|
||
__all__ = [ | ||
"TomlParser", | ||
"write_definitions", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from __future__ import annotations | ||
|
||
import copy | ||
import pathlib | ||
|
||
import flexcache as fc | ||
|
||
from ...compat import tomllib | ||
from ..base_defparser import ParserConfig | ||
from . import plain | ||
|
||
|
||
class TomlParser: | ||
def __init__(self, default_config: ParserConfig, diskcache: fc.DiskCache): | ||
self._default_config = default_config | ||
self._diskcache = diskcache | ||
|
||
def iter_parsed_project(self, parsed_project: dict): | ||
stmts = { | ||
"unit": plain.UnitDefinition, | ||
"prefix": plain.PrefixDefinition, | ||
"dimension": plain.DerivedDimensionDefinition, | ||
"system": plain.SystemDefinition, | ||
"context": plain.ContextDefinition, | ||
"group": plain.GroupDefinition, | ||
} | ||
for definition_type in parsed_project.keys(): | ||
for key, value in parsed_project[definition_type].items(): | ||
d = copy.copy(value) | ||
d["name"] = key | ||
stmt = stmts[definition_type].from_dict_and_config( | ||
d, self._default_config | ||
) | ||
yield stmt | ||
|
||
def parse_file( | ||
self, filename: pathlib.Path | str, cfg: ParserConfig | None = None | ||
) -> dict: | ||
with open(filename, "rb") as f: | ||
data = tomllib.load(f) | ||
return data | ||
|
||
# def parse_string(self, content: str, cfg: ParserConfig | None = None): | ||
# return fp.parse_bytes( | ||
# content.encode("utf-8"), | ||
# _PintParser, | ||
# cfg or self._default_config, | ||
# diskcache=self._diskcache, | ||
# strip_spaces=True, | ||
# delimiters=_PintParser._delimiters, | ||
# ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import fields | ||
|
||
import tomli_w | ||
|
||
from ...facets.plain import GenericPlainRegistry | ||
|
||
keep_fields = [ | ||
"value", | ||
"defined_symbol", | ||
"aliases", | ||
] | ||
|
||
|
||
def add_key_if_not_empty(dat, key, value): | ||
if value == () or value is None: | ||
return dat | ||
else: | ||
dat[key] = value | ||
return dat | ||
|
||
|
||
def parse_simple_definition(definition, definition_type): | ||
attrs = [field.name for field in fields(definition) if field.name in keep_fields] | ||
dat = {} | ||
for attr in attrs: | ||
dat = add_key_if_not_empty(dat, attr, getattr(definition, attr)) | ||
if definition_type in ["units", "dimensions", "prefixes"] and hasattr( | ||
definition, "raw" | ||
): | ||
dat["value"] = definition.raw.split("=")[1].strip() | ||
return dat | ||
|
||
|
||
def prefixes_units_dimensions(ureg): | ||
data = { | ||
"prefix": {}, | ||
"unit": {}, | ||
"dimension": {}, | ||
} | ||
for definition_type, ureg_attr in zip( | ||
["unit", "prefix", "dimension"], | ||
["_units", "_prefixes", "_dimensions"], | ||
): | ||
definitions = getattr(ureg, ureg_attr).values() | ||
for definition in definitions: | ||
for name, definition in getattr(ureg, ureg_attr).items(): | ||
if hasattr(definition, "raw"): | ||
data[definition_type][definition.name] = parse_simple_definition( | ||
definition, definition_type | ||
) | ||
return data | ||
|
||
|
||
def groups(ureg): | ||
group_data = {} | ||
for group in ureg._group_definitions: | ||
dat = {} | ||
for attr in ["using_group_names"]: | ||
dat = add_key_if_not_empty(dat, attr, getattr(group, attr)) | ||
dat["definitions"] = {} | ||
for definition in group.definitions: | ||
dat["definitions"][definition.name] = parse_simple_definition( | ||
definition, "_units" | ||
) | ||
group_data[group.name] = dat | ||
return group_data | ||
|
||
|
||
def systems(ureg): | ||
system_data = {} | ||
for group in ureg._system_definitions: | ||
dat = {} | ||
for attr in ["using_group_names"]: | ||
dat = add_key_if_not_empty(dat, attr, getattr(group, attr)) | ||
dat["rules"] = [] | ||
for rule in group.rules: | ||
dat["rules"].append(rule.raw) | ||
system_data[group.name] = dat | ||
return system_data | ||
|
||
|
||
def contexts(ureg): | ||
context_data = {} | ||
for group in ureg._context_definitions: | ||
dat = {} | ||
for attr in ["aliases", "defaults", "redefinitions"]: | ||
dat = add_key_if_not_empty(dat, attr, getattr(group, attr)) | ||
dat["relations"] = [] | ||
for rule in group.relations: | ||
dat["relations"].append(rule.raw) | ||
context_data[group.name] = dat | ||
return context_data | ||
|
||
|
||
def write_definitions(filename: str, ureg: GenericPlainRegistry): | ||
data = prefixes_units_dimensions(ureg) | ||
data["group"] = groups(ureg) | ||
data["system"] = systems(ureg) | ||
data["context"] = contexts(ureg) | ||
with open("test.toml", "wb") as f: | ||
tomli_w.dump(data, f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters