Skip to content

Commit

Permalink
try new zenodo metadat class
Browse files Browse the repository at this point in the history
  • Loading branch information
drifter089 committed Nov 28, 2024
1 parent 1400164 commit 67fcd0e
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 36 deletions.
78 changes: 42 additions & 36 deletions src/zenodopy/zenodopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import zipfile
from datetime import datetime
import time

from dataclasses import dataclass, field
from typing import Optional, List

def validate_url(url):
"""validates if URL is formatted correctly
Expand Down Expand Up @@ -49,7 +50,19 @@ def make_zipfile(path, ziph):
ziph.write(os.path.join(root, file),
os.path.relpath(os.path.join(root, file),
os.path.join(path, '..')))


@dataclass
class ZenodoMetadata:
title: str
upload_type: str = "other"
description: Optional[str] = None
publication_date: str = field(default_factory=lambda: datetime.now().strftime("%Y-%m-%d"))
version: str = "0.1.0"
access_right: str = "open"
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"}])


class BearerAuth(requests.auth.AuthBase):
"""Bearer Authentication"""
Expand Down Expand Up @@ -363,7 +376,7 @@ def list_files(self):
# warnings.warn("The object is not pointing to a project. Either create a project or explicity set the project'", UserWarning)

def create_project(
self, title=None, metadata_json=None,
self, metadata:ZenodoMetadata,
):
"""Creates a new project
Expand Down Expand Up @@ -391,9 +404,9 @@ def create_project(
self.deposition_id = r.json()["id"]
self.bucket = r.json()["links"]["bucket"]
self.title = title

self.change_metadata(
json_file_path=metadata_json,
metadata=metadata,
)

else:
Expand Down Expand Up @@ -425,38 +438,31 @@ def _check_parent_doi(self, dep_id, project_obj):
return int(dep_id) == int(concept_doi.split(".")[-1])
return False

def change_metadata(self, json_file_path=None):
"""change projects metadata
def change_metadata(self, metadata: ZenodoMetadata):
# """Change project's metadata.
# Args:
# metadata (ZenodoMetadata): The metadata to update.
# Returns:
# dict: Dictionary with the updated metadata.
# """
metadata.publication_date = datetime.now().strftime("%Y-%m-%d")

** warning **
This changes everything. If nothing is supplied then
uses default values are used.
data = {
"metadata": metadata.__dict__
}

For example. If you do not supply an upload_type
then it will default to "other"

Args:
dep_id (str): deposition to change
title (str): new title of project
upload_type (str): new upload type
description (str): new description
**kwargs: dictionary to update default metadata
# if json_file_path is None:
# raise ValueError("You need to supply a path")

Returns:
dict: dictionary with new metadata
"""

if json_file_path is None:
raise ValueError("You need to supply a path")

if not Path(os.path.expanduser(json_file_path)).exists():
raise ValueError(
f"{json_file_path} does not exist. Please check you entered the correct path"
)
# if not Path(os.path.expanduser(json_file_path)).exists():
# raise ValueError(
# f"{json_file_path} does not exist. Please check you entered the correct path"
# )

if json_file_path:
with open(json_file_path, "rb") as json_file:
file_data = json.load(json_file)
# if json_file_path:
# with open(json_file_path, "rb") as json_file:
# file_data = json.load(json_file)

# if upload_type is None:
# upload_types = self._get_upload_types()
Expand All @@ -466,12 +472,12 @@ def change_metadata(self, json_file_path=None):
# )
# upload_type = "other"

file_data["metadata"]["publication_date"] = datetime.now().strftime("%Y-%m-%d")
# file_data["metadata"]["publication_date"] = datetime.now().strftime("%Y-%m-%d")

r = requests.put(
f"{self._endpoint}/deposit/depositions/{self.deposition_id}",
auth=self._bearer_auth,
data=json.dumps(file_data),
data=json.dumps(data),
headers={"Content-Type": "application/json"},
)

Expand Down Expand Up @@ -628,7 +634,7 @@ def upload_tar(self, source_dir=None, output_file=None, publish=False):
# remove tar file after uploading it
os.remove(output_file)

def update(self, source=None, output_file=None, metadata_json=None, publish=False):
def update(self, metadata:ZenodoMetadata, source=None, output_file=None, publish=False):
"""update an existed record
Args:
Expand All @@ -652,7 +658,7 @@ def update(self, source=None, output_file=None, metadata_json=None, publish=Fals

time.sleep(5)

self.change_metadata(json_file_path=metadata_json)
self.change_metadata(metadata=metadata)
# invoke upload funcions
if not source:
print("You need to supply a path")
Expand Down
90 changes: 90 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import os
import time
import json
from pathlib import Path
import argparse
import zenodopy
from zenodopy import ZenodoMetadata
import parse

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 ZenodoMetadata(**metadata_dict)

def main():
# Set up argument parsing
parser = argparse.ArgumentParser(description='Update Zenodo deposition with new version and files.')
parser.add_argument('--version_tag', required=True, help='The version tag for the new release.')
parser.add_argument('--zenodo_token', required=True, help='The Zenodo API token.')
parser.add_argument('--dep_id', required=True, type=int, help='The Zenodo deposition ID.')
parser.add_argument('--base_dir', required=True, help='The base directory path.')
parser.add_argument('--metadata_file', required=True, help='The metadata JSON file path.')
parser.add_argument('--upload_dir', required=True, help='The directory containing files to upload.')

args = parser.parse_args()

version_tag = args.version_tag
zenodo_token = args.zenodo_token
dep_id = args.dep_id
base_dir = Path(args.base_dir)
zenodo_metadata_file = Path(args.metadata_file)
upload_dir = Path(args.upload_dir)

print("Version Tag:", version_tag)

def prepare_metadata(file_path, new_version):
with open(file_path, "r") as file:
zenodo_data = json.load(file)

zenodo_data["metadata"]["version"] = new_version

with open(file_path, "w") as file:
json.dump(zenodo_data, file, indent=2)

# Prepare the metadata file with the new version tag
prepare_metadata(zenodo_metadata_file, version_tag)

max_retries = 5

for attempt in range(1, max_retries + 1):
try:
zeno = zenodopy.Client(
sandbox=True,
token=zenodo_token,
)

zeno.set_project(dep_id=dep_id)

zeno.update(
source=str(upload_dir),
publish=True,
metadata_json=str(zenodo_metadata_file),
)
print("Update succeeded.")
break

except Exception as e:
print(f"Attempt {attempt} failed with error: {e}")

time.sleep(2) # Optional: Wait before retrying

zeno._delete_project(dep_id=dep_id)

if attempt == max_retries:
print("Max retries reached. Exiting.")
raise
else:
time.sleep(2)

if __name__ == "__main__":
main()
16 changes: 16 additions & 0 deletions tests/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@
from pathlib import Path
import argparse
import zenodopy
from zenodopy import ZenodoMetadata
import parse

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 ZenodoMetadata(**metadata_dict)

def main():
# Set up argument parsing
Expand Down

0 comments on commit 67fcd0e

Please sign in to comment.