Skip to content

Commit

Permalink
allow token to be string itself or filename
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Jun 25, 2018
1 parent a71e7cb commit 4f95ca2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.key
*.json
*.ini

Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ zen = pyzenodo3.Zenodo()

### Upload file to Zenodo

0. Get a [Zenodo API Token](https://zenodo.org/account/settings/applications/tokens/new/).
0. Get a Zenodo `deposit:write` [API Token](https://zenodo.org/account/settings/applications/tokens/new/).
This token must remain private, NOT uploaded to GitHub, etc.!
1. create a simple text file `mymeta.ini` containing title, author etc. (see the example `meta.ini` in this repo)
2. upload file to Zenodo (myApiToken is the cut-n-pasted Zenodo API text token)
Expand All @@ -54,3 +54,6 @@ Recs = zen.search('scivision')
Recs is a `list` of Zenodo Records for the GitHub username queried, as in the example above.


## Notes

* We don't use `deposit:publish` API token to keep a human-in-the-loop in case of hacking of sensor nodes.
16 changes: 11 additions & 5 deletions pyzenodo3/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,28 @@ def meta(inifn: Path) -> Path:
return outfn


def upload(metafn: Path, datafn: Path, token: str, live: bool):
def upload(metafn: Path, datafn: Path, token: Union[str, Path], live: bool):
"""takes metadata and file and uploads to Zenodo"""

if not metafn:
raise ValueError('must specify API token or file containing the token')

metafn = Path(metafn).expanduser()
datafn = Path(datafn).expanduser()
assert datafn.is_file(), "for now, upload a file only"

if not metafn.is_file():
raise FileNotFoundError('meta JSON file is required')

assert datafn.is_file(), "for now, upload a file only"

meta = metafn.read_text()

base_url = BASE_URL if live else SANDBOX_URL

if not token or not isinstance(token, str):
# %% token
if Path(token).expanduser().is_file():
token = Path(token).expanduser().read_text().strip() # in case \n or spaces sneak in
elif isinstance(token, str) and 100 > len(token) > 10:
pass
else:
raise ValueError('API Token must be specified to upload to Zenodo')
# %% Create new paper submission
url = f"{base_url}/deposit/depositions/?access_token={token}"
Expand Down

0 comments on commit 4f95ca2

Please sign in to comment.