diff options
Diffstat (limited to 'python')
-rw-r--r-- | python/fatcat_tools/transforms/__init__.py | 2 | ||||
-rw-r--r-- | python/fatcat_tools/transforms/entities.py | 22 | ||||
-rw-r--r-- | python/tests/transform_toml.py | 22 |
3 files changed, 42 insertions, 4 deletions
diff --git a/python/fatcat_tools/transforms/__init__.py b/python/fatcat_tools/transforms/__init__.py index 3f4700ff..7cc8f699 100644 --- a/python/fatcat_tools/transforms/__init__.py +++ b/python/fatcat_tools/transforms/__init__.py @@ -1,5 +1,5 @@ -from .entities import entity_to_dict, entity_from_json, entity_from_dict +from .entities import entity_to_dict, entity_from_json, entity_from_dict, entity_from_toml, entity_to_toml from .elasticsearch import release_to_elasticsearch, container_to_elasticsearch, changelog_to_elasticsearch, file_to_elasticsearch from .csl import release_to_csl, citeproc_csl from .ingest import release_ingest_request diff --git a/python/fatcat_tools/transforms/entities.py b/python/fatcat_tools/transforms/entities.py index 53455e85..3892d54a 100644 --- a/python/fatcat_tools/transforms/entities.py +++ b/python/fatcat_tools/transforms/entities.py @@ -1,9 +1,10 @@ import json +import toml import collections from fatcat_openapi_client import ApiClient -def entity_to_dict(entity, api_client=None): +def entity_to_dict(entity, api_client=None) -> dict: """ Hack to take advantage of the code-generated serialization code. @@ -17,7 +18,7 @@ def entity_to_dict(entity, api_client=None): api_client = ApiClient() return api_client.sanitize_for_serialization(entity) -def entity_from_json(json_str, entity_type, api_client=None): +def entity_from_json(json_str: str, entity_type, api_client=None): """ Hack to take advantage of the code-generated deserialization code @@ -29,6 +30,21 @@ def entity_from_json(json_str, entity_type, api_client=None): thing.data = json_str return api_client.deserialize(thing, entity_type) -def entity_from_dict(obj, entity_type, api_client=None): +def entity_from_dict(obj: dict, entity_type, api_client=None): json_str = json.dumps(obj) return entity_from_json(json_str, entity_type, api_client=api_client) + +def entity_to_toml(entity, api_client=None, pop_fields=None) -> str: + """ + pop_fields parameter can be used to strip out some fields from the resulting + TOML. Eg, for fields which should not be edited, like the ident. + """ + obj = entity_to_dict(entity, api_client=api_client) + pop_fields = pop_fields or [] + for k in pop_fields: + obj.pop(k, None) + return toml.dumps(obj) + +def entity_from_toml(toml_str: str, entity_type, api_client=None): + obj = toml.loads(toml_str) + return entity_from_dict(obj, entity_type, api_client=api_client) diff --git a/python/tests/transform_toml.py b/python/tests/transform_toml.py new file mode 100644 index 00000000..d12ba027 --- /dev/null +++ b/python/tests/transform_toml.py @@ -0,0 +1,22 @@ + +import json + +from fatcat_tools import * +from fatcat_openapi_client import * +from import_crossref import crossref_importer +from fixtures import * + + +def test_basic_toml(crossref_importer): + with open('tests/files/crossref-works.single.json', 'r') as f: + # not a single line + raw = json.loads(f.read()) + r = crossref_importer.parse_record(raw) + r.state = 'active' + toml_str = entity_to_toml(r) + r2 = entity_from_toml(toml_str, ReleaseEntity) + assert r == r2 + + toml_str = entity_to_toml(r, pop_fields=['ident', 'revision', 'blah', 'extra']) + r3 = entity_from_toml(toml_str, ReleaseEntity) + assert r != r3 |