-
Notifications
You must be signed in to change notification settings - Fork 11
feat(mapper): Set publication date based on resource type #595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sakshamarora1
wants to merge
1
commit into
CERNDocumentServer:master
Choose a base branch
from
sakshamarora1:fix/dates_mapping
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -60,15 +60,77 @@ def map_value(self, ctx): | |
| return title | ||
|
|
||
|
|
||
| def _date_precision(date_str): | ||
| """Return how granular a normalized date string is (year=1, month=2, day=3).""" | ||
| if not date_str: | ||
| return 0 | ||
| return len(date_str.split("-")) | ||
|
|
||
|
|
||
| def _is_more_accurate(candidate, current): | ||
| """Return True if `candidate` has finer granularity than `current`.""" | ||
| return _date_precision(candidate) > _date_precision(current) | ||
|
|
||
|
|
||
| class PublicationDateMapper(FieldMapper): | ||
| """Maps publication_date, falling back to status week or file creation date.""" | ||
| """Maps publication_date, preferring 260 (article) or 269 (preprint). | ||
|
|
||
| - resource_type == "publication-article": publication_date (260) | ||
| always wins; preprint_date (269), if present, becomes a secondary | ||
| "submitted"/"preprint" entry in `dates`. | ||
| - any other resource_type: preprint_date (269) wins when present, | ||
| unless publication_date (260) is also present and at least as | ||
| accurate (day > month > year) - in that case publication_date wins | ||
| instead. Whichever one loses, if present, becomes a secondary entry | ||
| in `dates` ("available"/"published" for publication_date, | ||
| "submitted"/"preprint" for preprint_date). | ||
|
|
||
| Falls back to status week or file creation date when neither applies. | ||
| """ | ||
|
|
||
| id = "publication_date" | ||
|
|
||
| def map_value(self, ctx): | ||
| """Return publication_date, requiring at least one date source.""" | ||
| dojson_entry = ctx.dojson_entry | ||
| pub_date = dojson_entry.get("publication_date") | ||
| # `preprint_date` is bookkeeping produced by the 269 rules (see | ||
| # xml_processing/rules/base.py) - drop it before it reaches the | ||
| # final record. | ||
| preprint_date = dojson_entry.pop("preprint_date", None) | ||
| resource_type = (dojson_entry.get("resource_type") or {}).get("id") | ||
|
|
||
| if resource_type == "publication-article": | ||
| if preprint_date: | ||
| dojson_entry.setdefault("dates", []).append( | ||
| { | ||
| "date": preprint_date, | ||
| "type": {"id": "submitted"}, | ||
| "description": "preprint", | ||
| } | ||
| ) | ||
|
Comment on lines
+103
to
+111
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it possible there’s only 269 (no 260)? In this case if there is no 260 preprint is moved to secondary dates and publication_date falls back to status week / file creation |
||
| elif preprint_date: | ||
| if pub_date and not _is_more_accurate(preprint_date, pub_date): | ||
| # publication_date is present and at least as accurate - | ||
| # keep it, preprint_date becomes the secondary entry. | ||
| dojson_entry.setdefault("dates", []).append( | ||
| { | ||
| "date": preprint_date, | ||
| "type": {"id": "submitted"}, | ||
| "description": "preprint", | ||
| } | ||
| ) | ||
| else: | ||
| if pub_date: | ||
| dojson_entry.setdefault("dates", []).append( | ||
| { | ||
| "date": pub_date, | ||
| "type": {"id": "available"}, | ||
| "description": "published", | ||
| } | ||
| ) | ||
| pub_date = preprint_date | ||
|
|
||
| created = dojson_entry.get("status_week_date") | ||
| files = ctx.raw_dump_entry["files"] | ||
| if not (pub_date or created or files): | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we're not always return in the format with "-" so maybe we should change that? or we can split with "-" and "/". See the date normalize method
example: