Skip to content

Commit

Permalink
add json file parse logic as data class function
Browse files Browse the repository at this point in the history
  • Loading branch information
drifter089 committed Dec 4, 2024
1 parent 65eef94 commit b10ed9c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ jobs:
- name: Run pytest
env:
ZENODO_TOKEN: ${{ secrets.ZENODO_TOKEN }}
DEPOSITION_ID: ${{ secrets.DEPOSITION_ID }}
run: pytest tests/test_zenodopy.py -v
14 changes: 14 additions & 0 deletions src/zenodopy/zenodopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ class ZenodoMetadata:
license: str = "Apache-2.0"
keywords: List[str] = field(default_factory=lambda: ["zenodo", "github", "git"])
creators: List[dict] = field(default_factory=lambda: [{"name": "Jhon, Doe", "orcid": "0000-0003-2584-3576"}])
@classmethod
def parse_metadata_from_json(cls, json_file_path: Path) -> 'ZenodoMetadata':
"""Parse metadata from a JSON file into a ZenodoMetadata object."""
json_file_path = Path(json_file_path).expanduser()
if not json_file_path.exists():
raise ValueError(
f"{json_file_path} does not exist. Please check you entered the correct path."
)

with json_file_path.open("r") as json_file:
data = json.load(json_file)

metadata_dict = data.get("metadata", {})
return cls(**metadata_dict)


class BearerAuth(requests.auth.AuthBase):
Expand Down
17 changes: 1 addition & 16 deletions tests/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,6 @@
import argparse
import zenodopy

def parse_metadata_from_json(json_file_path: Path) -> zenodopy.ZenodoMetadata:
"""Parse metadata from a JSON file into a ZenodoMetadata object."""
json_file_path = json_file_path.expanduser()
if not json_file_path.exists():
raise ValueError(
f"{json_file_path} does not exist. Please check you entered the correct path."
)

with json_file_path.open("r") as json_file:
data = json.load(json_file)

metadata_dict = data.get("metadata", {})
return zenodopy.ZenodoMetadata(**metadata_dict)


def main():
# Set up argument parsing
parser = argparse.ArgumentParser(description='Update Zenodo deposition with new version and files.')
Expand All @@ -42,7 +27,7 @@ def main():
print("Version Tag:", version_tag)

# Parse and update metadata with new version tag
metadata = parse_metadata_from_json(zenodo_metadata_file)
metadata = zenodopy.ZenodoMetadata.parse_metadata_from_json(zenodo_metadata_file)
metadata.version = version_tag

max_retries = 5
Expand Down
7 changes: 4 additions & 3 deletions tests/test_zenodopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# use this when using pytest
import os
ACCESS_TOKEN = os.getenv('ZENODO_TOKEN')
DEPOSITION_ID = os.getenv('DEPOSITION_ID')

# can also hardcode sandbox token using tox locally
# ACCESS_TOKEN = ''
Expand Down Expand Up @@ -67,7 +68,7 @@ def test_get_baseurl():

def test_get_depositions():
zeno = zen.Client(sandbox=True,token=ACCESS_TOKEN)
dep_id=106299
dep_id=DEPOSITION_ID
zeno.set_project(dep_id=dep_id)
depositions = zeno._get_depositions()
deposition_by_id = zeno._get_depositions_by_id()
Expand All @@ -83,7 +84,7 @@ def test_get_depositions():

def test_get_bucket():
zeno = zen.Client(sandbox=True,token=ACCESS_TOKEN)
dep_id=106299
dep_id=DEPOSITION_ID
zeno.set_project(dep_id=dep_id)
bucket_link = zeno._get_bucket_by_id()
assert bucket_link.startswith('https://sandbox.zenodo.org/api/files/')
Expand All @@ -93,7 +94,7 @@ def test_get_bucket():

def test_get_projects_and_files():
zeno = zen.Client(sandbox=True,token=ACCESS_TOKEN)
dep_id=106299
dep_id=DEPOSITION_ID
zeno.set_project(dep_id=dep_id)
_ = zeno.list_projects
_ = zeno.list_files
Expand Down

0 comments on commit b10ed9c

Please sign in to comment.