-
Notifications
You must be signed in to change notification settings - Fork 0
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
Get record #6
Merged
Merged
Get record #6
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f54f594
error for no data
kyokukou ee053ee
create record headers
kyokukou 63cf3b7
create record objects to hold and process data
kyokukou c9471df
implement get record
kyokukou 220fd48
pytest bug fix
kyokukou 4efd021
bugfix for blasphemous use of the kilo- prefix
kyokukou 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 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
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,23 @@ | ||
from typing import Optional, List | ||
|
||
from arxiv.db import Session | ||
from arxiv.db.models import Metadata | ||
from arxiv.identifier import Identifier | ||
|
||
|
||
def get_record_data_current(arxiv_id: Identifier )-> Optional[Metadata]: | ||
"""fetch latest metadata for a specific paper""" | ||
data=(Session.query(Metadata) | ||
.filter(Metadata.paper_id == arxiv_id.id) | ||
.filter(Metadata.is_current==1) | ||
.first() | ||
) | ||
return data | ||
|
||
def get_record_data_all(arxiv_id: Identifier)-> Optional[List[Metadata]]: | ||
"""fetch all metadata for a specific paper""" | ||
data=(Session.query(Metadata) | ||
.filter(Metadata.paper_id == arxiv_id.id) | ||
.all() | ||
) | ||
return data |
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 typing import Dict | ||
from datetime import datetime, timezone | ||
|
||
from flask import render_template | ||
|
||
from arxiv.identifier import Identifier | ||
|
||
from oaipmh.processors.db import get_record_data_all, get_record_data_current | ||
from oaipmh.data.oai_errors import OAINonexistentID | ||
from oaipmh.data.oai_properties import OAIParams, MetadataFormat | ||
from oaipmh.serializers.create_records import arXivOldRecord, arXivRawRecord, arXivRecord, dcRecord | ||
from oaipmh.serializers.output_formats import Response | ||
|
||
def do_get_record(arxiv_id: Identifier, format: MetadataFormat, query_data: Dict[OAIParams, str])-> Response: | ||
"""fetches the required data for a record for a specific format | ||
converts data into specif format and renders record template | ||
""" | ||
if format.all_versions: | ||
data=get_record_data_all(arxiv_id) | ||
if not data: | ||
raise OAINonexistentID("Nothing found for this ID",query_params=query_data) | ||
|
||
if format.prefix=="oai_dc": | ||
record=dcRecord(data) | ||
else: #arXivRaw | ||
record=arXivRawRecord(data) | ||
else: | ||
data=get_record_data_current(arxiv_id) | ||
if data is None: | ||
raise OAINonexistentID("Nothing found for this ID",query_params=query_data) | ||
if format.prefix=="arXivOld": | ||
record= arXivOldRecord(data) | ||
else: #arXiv | ||
record= arXivRecord(data) | ||
|
||
response=render_template("get_record.xml", | ||
response_date=datetime.now(timezone.utc), | ||
query_params=query_data, | ||
record=record, | ||
format=format.prefix | ||
) | ||
headers={"Content-Type":"application/xml"} | ||
return response, 200, headers | ||
|
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
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
Empty file.
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,107 @@ | ||
from typing import List, Optional | ||
from datetime import datetime | ||
|
||
from arxiv.authors import parse_author_affil | ||
from arxiv.db.models import Metadata | ||
from arxiv.document.version import VersionEntry | ||
from arxiv.taxonomy.category import Category | ||
from arxiv.taxonomy.definitions import CATEGORIES | ||
|
||
from oaipmh.processors.create_set_list import make_set_str | ||
|
||
class Header: | ||
def __init__(self, id:str, date:datetime, cats:List[Category]) -> None: | ||
self.id=f"oai:arXiv.org:{id}" | ||
self.date=date | ||
self.sets=[] | ||
for cat in cats: | ||
self.sets.append(make_set_str(cat)) | ||
|
||
def __eq__(self, other: object) -> bool: | ||
if not isinstance(other, Header): | ||
return False | ||
return ( | ||
self.id == other.id and | ||
self.date == other.date and | ||
self.sets == other.sets | ||
) | ||
|
||
class Record: #base record class | ||
def __init__(self, current_meta: Metadata): | ||
self.categories: List[Category]=[] | ||
if current_meta.abs_categories: | ||
for cat in current_meta.abs_categories.split(): | ||
self.categories.append(CATEGORIES[cat]) | ||
|
||
date= current_meta.updated if current_meta.updated else current_meta.created | ||
self.header = Header(current_meta.paper_id, date, self.categories) | ||
self.current_meta = current_meta | ||
|
||
#specialized record classes for the different supported metadata types | ||
class arXivRecord(Record): | ||
def __init__(self, current_meta: Metadata): | ||
super().__init__(current_meta) | ||
self.authors= parse_author_affil(current_meta.authors) | ||
|
||
class arXivRawRecord(Record): | ||
def __init__(self, metadata: List[Metadata]): | ||
self.versions: List[VersionEntry]=[] | ||
for version in metadata: | ||
entry= VersionEntry( | ||
version=version.version, | ||
raw='', | ||
submitted_date=version.created, | ||
size_kilobytes = version.source_size // 1000 if version.source_size else 0, | ||
source_flag=self._process_source_format(version.source_format, version.source_flags), | ||
is_current=version.is_current, | ||
source_format=version.source_format | ||
) | ||
self.versions.append(entry) | ||
if version.is_current: | ||
super().__init__(version) | ||
|
||
@staticmethod | ||
def _process_source_format(format: Optional[str], source_flags: Optional[str]) -> Optional[str]: | ||
"""oai excepts the source information to be in the form of flags for both our flag data and source type data""" | ||
format_map={ | ||
'pdftex' :'D', | ||
'tex':'', | ||
'pdf':'', | ||
'withdrawn': 'I', | ||
'html': 'H', | ||
'ps': 'P', | ||
'docx': 'X' | ||
} | ||
shown_flags=['A', 'S'] #not shown: 1, D (duplicates pdftex format sometimes) | ||
|
||
result="" | ||
if source_flags: | ||
for flag in shown_flags: | ||
if flag in source_flags: | ||
result+=flag | ||
result+=format_map.get(format,"") | ||
|
||
return result or None | ||
|
||
class dcRecord(Record): | ||
def __init__(self, metadata: List[Metadata]): | ||
for version in metadata: | ||
if version.is_current: | ||
super().__init__(version) | ||
self.current_version_date=version.created | ||
self.authors= parse_author_affil(version.authors) | ||
|
||
if version.version==1: | ||
self.initial_date=version.created | ||
|
||
def deduplicate_cat_names(self)-> List[str]: | ||
result=[] | ||
for cat in self.categories: | ||
if cat.full_name not in result: | ||
result.append(cat.full_name) | ||
return result | ||
|
||
class arXivOldRecord(Record): | ||
#no extra data | ||
def __init__(self, current_meta: Metadata): | ||
super().__init__(current_meta) |
Empty file.
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,12 @@ | ||
{% extends "base.xml" %} | ||
{% import 'record_formats.xml' as formats %} | ||
|
||
{% block request_element %} | ||
{{ macros.request_element(query_params) }} | ||
{% endblock %} | ||
|
||
{% block interior_xml %} | ||
<GetRecord> | ||
{{formats.create_record(record, format)}} | ||
</GetRecord> | ||
{% endblock %} |
Empty file.
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,3 +1,13 @@ | ||
{% macro request_element( attributes={}) %} | ||
<request {% for key, value in attributes.items() %}{{ key }}="{{ value }}" {% endfor %}>{{ url_for("general.oai", _external=True) }}</request> | ||
<request{% for key, value in attributes.items() %} {{ key }}="{{ value }}"{% endfor %}>{{ url_for("general.oai", _external=True) }}</request> | ||
{% endmacro %} | ||
|
||
{% macro header(header) %} | ||
<header> | ||
<identifier>{{header.id}}</identifier> | ||
<datestamp>{{ header.date.strftime('%Y-%m-%d') }}</datestamp> | ||
{% for set in header.sets %} | ||
<setSpec>{{set}}</setSpec> | ||
{% endfor %} | ||
</header> | ||
{% endmacro %} |
Oops, something went wrong.
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.
A kilobyte is not 1000 bytes. It is 1024.