-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
613 additions
and
18 deletions.
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
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,68 @@ | ||
import json | ||
from pathlib import Path | ||
from typing import Any, Dict, List, Optional | ||
import nbformat | ||
|
||
AuthorInfo = Dict[str, str] | ||
|
||
DEFAULT_AUTHOR = { | ||
"name": "Unknown", | ||
"orcid": "https://orcid.org/0000-0000-0000-0000", | ||
} | ||
CREATORS_KEY = "creators" | ||
|
||
LISTIFY = ["author", "object", "input"] | ||
|
||
def extract_metadata(metadata): | ||
with open(metadata) as file: | ||
data = json.load(file) | ||
return data | ||
|
||
def extract_default_authors(metadata: Path) -> List[AuthorInfo]: | ||
"""Attempts to extract author information from the metadata.json file within | ||
the repository. If none are found, returns a dummy value. | ||
Parameters: | ||
metadata: The path to the metadata file, commonly metadata.json | ||
""" | ||
with open(metadata) as file: | ||
data = json.load(file) | ||
|
||
return data.get(CREATORS_KEY, [DEFAULT_AUTHOR]) | ||
|
||
def listify(value): | ||
if not isinstance(value, list): | ||
return [value] | ||
return value | ||
|
||
def extract_notebook_metadata(notebook: Path, keys: Dict[str, Any]) -> Dict[str, Any]: | ||
"""Attempts to extract metadata from the notebook. | ||
Parameters: | ||
notebook: The path to the jupyter notebook | ||
keys: A dictionary of keys to look for in the notebook, and their | ||
corresponding defaults if the key is not found. | ||
Returns: | ||
A dictionary containing the retrieved metadata for each key. | ||
""" | ||
""" | ||
with open(notebook) as file: | ||
data = json.load(file) | ||
metadata = data["metadata"] | ||
result = {} | ||
for key, default in keys.items(): | ||
result[key] = metadata.get(key, default) | ||
""" | ||
result = {} | ||
nb = nbformat.read(notebook, nbformat.NO_CONVERT) | ||
metadata = nb.metadata.rocrate | ||
for key, default in keys.items(): | ||
if key in LISTIFY: | ||
result[key] = listify(metadata.get(key, default)) | ||
else: | ||
result[key] = metadata.get(key, default) | ||
return result | ||
|
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,41 @@ | ||
from pathlib import Path | ||
import json | ||
import re | ||
import importlib.util | ||
import os.path | ||
import sys | ||
|
||
external_imports = ['jupyterlab', 'voila', 'voila-material @ git+https://github.com/GLAM-Workbench/voila-material.git'] | ||
|
||
python_path = os.path.dirname(sys.executable).replace('bin', 'lib') | ||
#print(python_path) | ||
|
||
imports = [] | ||
for nb in Path(__file__).resolve().parent.parent.glob('*.ipynb'): | ||
if not nb.name.startswith('.') and not nb.name.startswith('Untitled'): | ||
nb_json = json.loads(nb.read_bytes()) | ||
for cell in nb_json['cells']: | ||
for line in cell['source']: | ||
if match := re.search(r'^\s*import ([a-zA-Z_]+)(?! from)', line): | ||
imports.append(match.group(1)) | ||
elif match := re.search(r'^\s*from ([a-zA-Z_]+)\.?[a-zA-Z_]* import [a-zA-Z_]+', line): | ||
imports.append(match.group(1)) | ||
|
||
# print(list(set(imports))) | ||
|
||
for imported_mod in list(set(imports)): | ||
try: | ||
module_path = importlib.util.find_spec(imported_mod).origin | ||
except AttributeError: | ||
pass | ||
else: | ||
if module_path: | ||
# print(imported_mod) | ||
# print(module_path) | ||
if 'site-packages' in module_path or python_path in module_path: | ||
external_imports.append(imported_mod) | ||
#print(external_imports) | ||
|
||
with Path(Path(__file__).resolve().parent.parent, 'requirements-tocheck.in').open('w') as req_file: | ||
for mod in external_imports: | ||
req_file.write(mod + '\n') |
File renamed without changes.
Oops, something went wrong.