-
Notifications
You must be signed in to change notification settings - Fork 1
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
cb90324
commit c1987eb
Showing
7 changed files
with
208 additions
and
638 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from collections import defaultdict | ||
from collections.abc import Mapping, Sequence | ||
|
||
|
||
def format_value(format_str: str, value): | ||
if isinstance(value, Mapping): | ||
return format_str.format(**value) | ||
if isinstance(value, Sequence) and not isinstance(value, str): | ||
return [ | ||
format_str.format_map(defaultdict(str, v)) if isinstance(v, Mapping) else format_str.format(v) | ||
for v in value | ||
] | ||
|
||
return format_str.format(value) | ||
|
||
def map_source_record(source_record: dict, mapping: dict) -> dict: | ||
formatted_record = {} | ||
|
||
for key, entries in mapping.items(): | ||
if isinstance(entries, tuple): | ||
record_key, format_str = entries | ||
value = source_record.get(record_key) | ||
|
||
if value is not None: | ||
formatted_record[key] = format_value(format_str, value) | ||
elif isinstance(entries, list): | ||
mapped_values = [] | ||
|
||
for entry in entries: | ||
record_key, format_str = entry | ||
value = source_record.get(record_key) | ||
|
||
if value is not None: | ||
formatted_value = format_value(format_str, value) | ||
|
||
if isinstance(formatted_value, list): | ||
mapped_values.extend(formatted_value) | ||
else: | ||
mapped_values.append(formatted_value) | ||
|
||
if mapped_values: | ||
formatted_record[key] = mapped_values | ||
|
||
return formatted_record |
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
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,16 @@ | ||
from datetime import datetime, timezone | ||
from uuid import uuid4 | ||
|
||
from model import Record, FRBRStatus, Source | ||
|
||
|
||
def map_to_record(source_record: dict, source: Source) -> Record: | ||
return Record( | ||
uuid=uuid4(), | ||
date_created=datetime.now(timezone.utc).replace(tzinfo=None), | ||
date_modified=datetime.now(timezone.utc).replace(tzinfo=None), | ||
frbr_status=FRBRStatus.TODO.value, | ||
cluster_status=False, | ||
source=source.value, | ||
**source_record | ||
) |
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
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
|
||
class Source(Enum): | ||
CHICACO_ISAC = 'isac' | ||
NYPL = 'nypl' |
Oops, something went wrong.