Skip to content

Commit

Permalink
feat(router): add meta_data methods to DataProvider for issue #12
Browse files Browse the repository at this point in the history
  • Loading branch information
spool committed Jul 31, 2023
1 parent 777bf3f commit 7640333
Showing 1 changed file with 67 additions and 7 deletions.
74 changes: 67 additions & 7 deletions alto2txt2fixture/router.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import uuid
import zipfile
from pathlib import Path
Expand Down Expand Up @@ -690,27 +689,88 @@ class DataProvider(Cache):
Attributes:
collection: A string representing publication collection
kind: Indication of object type, defaults to `data-provider`
providers_meta_data: structured dict of metadata for known collection sources
collection_type: related data sources and potential linkage source
Examples:
```pycon
>>> hmd = DataProvider("hmd")
>>> hmd.pk
2
>>> pprint(hmd.as_dict())
{'code': 'bl-hmd',
'collection': 'newspapers',
'legacy_code': 'hmd',
'name': 'Heritage Made Digital',
'source_note': 'British Library-funded digitised newspapers provided by the '
'British Newspaper Archive'}
```
"""

kind: str = "data-provider"
meta_data: list[FixtureDict] = NEWSPAPER_COLLECTION_METADATA
providers_meta_data: list[FixtureDict] = NEWSPAPER_COLLECTION_METADATA
collection_type: str = "newspapers"

def __init__(self, collection: str):
"""Constructor method."""
self.collection: str = collection

@property
def providers_legacy_code_id_dict(self) -> dict[str, FixtureDict]:
"""Return all `legacy_code` values from `providers_meta_data`."""
return {
provider["fields"]["legacy_code"]: provider
for provider in self.providers_meta_data
}

@property
def meta_data(self) -> FixtureDict | dict:
"""Return ``self.providers_meta_data[self.collection]`` or `{}`."""
if self.collection in self.providers_legacy_code_id_dict:
return self.providers_legacy_code_id_dict[self.collection]
else:
return {}

@property
def meta_data_fields(self) -> FixtureDict | dict:
"""Return ``self.providers_meta_data[self.collection]`` or `{}`."""
if self.meta_data:
return self.meta_data["fields"]
else:
return {}

@property
def pk(self) -> int | None:
"""Return ``pk`` if provided via ``providers_meta_data``, else `None`."""
if self.meta_data:
return self.meta_data["pk"]
else:
return None

def as_dict(self) -> dict:
"""
Return a `dict` of the data provider object.
Returns:
Dictionary representation of the DataProvider object
"""
return {
"name": self.collection,
"collection": "newspapers",
"source_note": "",
}
if self.meta_data:
return {
"name": self.meta_data_fields["name"],
"code": self.meta_data_fields["code"],
"legacy_code": self.collection,
"source_note": self.meta_data_fields["source_note"],
"collection": self.collection_type,
}
else:
return {
"name": self.collection,
"collection": self.collection_type,
"source_note": "",
}

@property
def id(self) -> str:
Expand Down

0 comments on commit 7640333

Please sign in to comment.