ssb-pubmd 0.0.6__tar.gz → 0.0.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {ssb_pubmd-0.0.6 → ssb_pubmd-0.0.8}/PKG-INFO +1 -1
- {ssb_pubmd-0.0.6 → ssb_pubmd-0.0.8}/pyproject.toml +3 -1
- {ssb_pubmd-0.0.6 → ssb_pubmd-0.0.8}/src/ssb_pubmd/__init__.py +1 -1
- ssb_pubmd-0.0.8/src/ssb_pubmd/exporter.py +140 -0
- ssb_pubmd-0.0.6/src/ssb_pubmd/functions.py +0 -85
- {ssb_pubmd-0.0.6 → ssb_pubmd-0.0.8}/LICENSE +0 -0
- {ssb_pubmd-0.0.6 → ssb_pubmd-0.0.8}/README.md +0 -0
- {ssb_pubmd-0.0.6 → ssb_pubmd-0.0.8}/src/ssb_pubmd/__main__.py +0 -0
- {ssb_pubmd-0.0.6 → ssb_pubmd-0.0.8}/src/ssb_pubmd/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "ssb-pubmd"
|
|
3
|
-
version = "0.0.
|
|
3
|
+
version = "0.0.8"
|
|
4
4
|
description = "SSB Pubmd"
|
|
5
5
|
authors = ["Olav Landsverk <stud-oll@ssb.no>"]
|
|
6
6
|
license = "MIT"
|
|
@@ -38,6 +38,8 @@ sphinx-click = ">=3.0.2"
|
|
|
38
38
|
typeguard = ">=2.13.3"
|
|
39
39
|
xdoctest = { extras = ["colors"], version = ">=0.15.10" }
|
|
40
40
|
myst-parser = { version = ">=0.16.1" }
|
|
41
|
+
uuid = "^1.30"
|
|
42
|
+
ipykernel = "^6.29.5"
|
|
41
43
|
|
|
42
44
|
[tool.pytest.ini_options]
|
|
43
45
|
pythonpath = ["src"]
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
import nbformat
|
|
5
|
+
import requests
|
|
6
|
+
from nbformat import NotebookNode
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class _Exporter:
|
|
10
|
+
"""Helper class for exporting notebook content."""
|
|
11
|
+
|
|
12
|
+
ID_KEY = "_id"
|
|
13
|
+
|
|
14
|
+
def __init__(self, post_url: str) -> None:
|
|
15
|
+
self.post_url: str = post_url
|
|
16
|
+
self.notebook_folder: str = ""
|
|
17
|
+
self.notebook_filename: str = ""
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def parent_folder(self) -> str:
|
|
21
|
+
"""The parent folder path, defaults to current working directory."""
|
|
22
|
+
if self.notebook_folder:
|
|
23
|
+
return self.notebook_folder
|
|
24
|
+
else:
|
|
25
|
+
return os.getcwd()
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def notebook_path(self) -> str:
|
|
29
|
+
"""The absolute path of the notebook file."""
|
|
30
|
+
return os.path.join(self.parent_folder, self.notebook_filename)
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def basename(self) -> str:
|
|
34
|
+
"""The name of the notebook file without extension."""
|
|
35
|
+
return os.path.splitext(self.notebook_filename)[0]
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def data_path(self) -> str:
|
|
39
|
+
"""The absolute path of the file to store data returned from the CMS."""
|
|
40
|
+
return os.path.join(self.parent_folder, self.basename + ".json")
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def display_name(self) -> str:
|
|
44
|
+
"""Generate a display name for the content."""
|
|
45
|
+
return self.basename.replace("_", " ").title()
|
|
46
|
+
|
|
47
|
+
def _save_content_id(self, content_id: str) -> None:
|
|
48
|
+
"""Saves the content id to the data file."""
|
|
49
|
+
filename = self.data_path
|
|
50
|
+
with open(filename, "w") as file:
|
|
51
|
+
json.dump({self.ID_KEY: content_id}, file)
|
|
52
|
+
|
|
53
|
+
def _get_content_id(self) -> str:
|
|
54
|
+
"""Returns the content id from the data file if it exists, otherwise an empty string."""
|
|
55
|
+
content_id = ""
|
|
56
|
+
|
|
57
|
+
filename = self.data_path
|
|
58
|
+
if os.path.exists(filename):
|
|
59
|
+
with open(filename) as file:
|
|
60
|
+
content_id = json.load(file)[self.ID_KEY]
|
|
61
|
+
return content_id
|
|
62
|
+
|
|
63
|
+
def _read_notebook(self) -> NotebookNode:
|
|
64
|
+
"""Reads the notebook file and returns its content."""
|
|
65
|
+
return nbformat.read(self.notebook_path, as_version=nbformat.NO_CONVERT) # type: ignore
|
|
66
|
+
|
|
67
|
+
def _get_content_from_notebook(self) -> str:
|
|
68
|
+
"""Extracts all markdown cells from the notebook and returns it as a merged string."""
|
|
69
|
+
notebook = self._read_notebook()
|
|
70
|
+
|
|
71
|
+
markdown_cells = []
|
|
72
|
+
for cell in notebook.cells:
|
|
73
|
+
if cell.cell_type == "markdown":
|
|
74
|
+
markdown_cells.append(cell.source)
|
|
75
|
+
|
|
76
|
+
markdown_content = "\n\n".join(markdown_cells)
|
|
77
|
+
|
|
78
|
+
return markdown_content
|
|
79
|
+
|
|
80
|
+
def _request_data(self) -> dict[str, str]:
|
|
81
|
+
"""Prepares the request data to be sent to the CMS post_url."""
|
|
82
|
+
return {
|
|
83
|
+
"_id": self._get_content_id(),
|
|
84
|
+
"displayName": self.display_name,
|
|
85
|
+
"markdown": self._get_content_from_notebook(),
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
def _send_request(self) -> str:
|
|
89
|
+
"""Sends the request to the CMS endpoint and returns the content id from the response."""
|
|
90
|
+
response = requests.post(
|
|
91
|
+
self.post_url,
|
|
92
|
+
data=self._request_data(),
|
|
93
|
+
)
|
|
94
|
+
content_id = response.json()[self.ID_KEY]
|
|
95
|
+
return content_id # type: ignore
|
|
96
|
+
|
|
97
|
+
def set_notebook(self, notebook_filename: str, notebook_folder: str) -> None:
|
|
98
|
+
"""Sets the notebook filename and notebook folder."""
|
|
99
|
+
self.notebook_filename = notebook_filename
|
|
100
|
+
self.notebook_folder = notebook_folder
|
|
101
|
+
|
|
102
|
+
def export(self) -> str:
|
|
103
|
+
"""Main method to export the notebook content to the CMS post_url."""
|
|
104
|
+
content_id = self._send_request()
|
|
105
|
+
self._save_content_id(content_id)
|
|
106
|
+
return content_id
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def notebook_to_cms(
|
|
110
|
+
post_url: str,
|
|
111
|
+
notebook_filename: str,
|
|
112
|
+
notebook_folder: str = "",
|
|
113
|
+
) -> str:
|
|
114
|
+
r"""Sends all the markdown content of a notebook to a CMS endpoint.
|
|
115
|
+
|
|
116
|
+
The CMS endpoint must satisfy two constraints:
|
|
117
|
+
|
|
118
|
+
- It must accept a post request with fields *_id*, *displayName* and *markdown*.
|
|
119
|
+
- The response body must have a key *_id* whose value should be
|
|
120
|
+
a unique string identifier of the content.
|
|
121
|
+
|
|
122
|
+
Creating and updating content is handled in the following way:
|
|
123
|
+
|
|
124
|
+
- On the first request, an empty string is sent as *id*.
|
|
125
|
+
- If the request succeeds, the value of *_id* (in the response) is stored in a JSON file
|
|
126
|
+
(created in the same directory as the notebook file).
|
|
127
|
+
- On subsequent requests, the stored value is sent as *id*.
|
|
128
|
+
|
|
129
|
+
Args:
|
|
130
|
+
post_url (str): The URL of the CMS endpoint.
|
|
131
|
+
notebook_filename (str): The name of the notebook file, e.g. `"my_notebook.ipynb"`.
|
|
132
|
+
notebook_folder (str): Sets a custom notebook folder (as absolute path) containing the notebook file.
|
|
133
|
+
If not set, the current folder is used.
|
|
134
|
+
|
|
135
|
+
Returns:
|
|
136
|
+
str: The identifier of the content returned by the CMS endpoint.
|
|
137
|
+
"""
|
|
138
|
+
exporter = _Exporter(post_url)
|
|
139
|
+
exporter.set_notebook(notebook_filename, notebook_folder)
|
|
140
|
+
return exporter.export()
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
"""A collection of useful functions.
|
|
2
|
-
|
|
3
|
-
The template and this example uses Google style docstrings as described at:
|
|
4
|
-
https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
|
|
5
|
-
|
|
6
|
-
"""
|
|
7
|
-
|
|
8
|
-
import json
|
|
9
|
-
import os
|
|
10
|
-
|
|
11
|
-
import nbformat
|
|
12
|
-
import requests
|
|
13
|
-
from nbformat import NotebookNode
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
def _read_notebook(fp: str) -> NotebookNode:
|
|
17
|
-
return nbformat.read(fp, as_version=nbformat.NO_CONVERT) # type: ignore
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
def notebook_to_cms(
|
|
21
|
-
notebook_filename: str,
|
|
22
|
-
endpoint: str,
|
|
23
|
-
notebook_folder: str = "",
|
|
24
|
-
display_name: str = "",
|
|
25
|
-
) -> str:
|
|
26
|
-
r"""Sends all the markdown content of a notebook to a CMS endpoint.
|
|
27
|
-
|
|
28
|
-
The CMS endpoint must satisfy two constraints:
|
|
29
|
-
|
|
30
|
-
- It must accept a post request with fields *id*, *displayName* and *markdown*.
|
|
31
|
-
- The response body must have a key *_id* whose value should be
|
|
32
|
-
a unique string identifier of the content.
|
|
33
|
-
|
|
34
|
-
Creating and updating content is handled in the following way:
|
|
35
|
-
|
|
36
|
-
- On the first request, an empty string is sent as *id*.
|
|
37
|
-
- If the request succeeds, the value of *_id* (in the response) is stored in a JSON file
|
|
38
|
-
(created in the same directory as the notebook file).
|
|
39
|
-
- On subsequent requests, the stored value is sent as *id*.
|
|
40
|
-
|
|
41
|
-
Args:
|
|
42
|
-
notebook_filename (str): The name of the notebook file, e.g. `"my_notebook.ipynb"`.
|
|
43
|
-
endpoint (str): The URL of the CMS endpoint.
|
|
44
|
-
notebook_folder (str): Sets a custom notebook folder (as absolute path) containing the notebook file.
|
|
45
|
-
If not set, the current folder is used.
|
|
46
|
-
display_name (str): Send a custom *displayName* value to the CMS endpoint.
|
|
47
|
-
If not set, the notebook filename is used (with the file extension removed,
|
|
48
|
-
underscores replaced with spaces, and words capitalized).
|
|
49
|
-
|
|
50
|
-
Returns:
|
|
51
|
-
str: The body of the response from the CMS endpoint, string-formatted.
|
|
52
|
-
"""
|
|
53
|
-
if notebook_folder:
|
|
54
|
-
os.chdir(notebook_folder)
|
|
55
|
-
else:
|
|
56
|
-
os.chdir(os.getcwd())
|
|
57
|
-
|
|
58
|
-
basename = os.path.splitext(notebook_filename)[0]
|
|
59
|
-
json_filename = basename + ".json"
|
|
60
|
-
|
|
61
|
-
_id = ""
|
|
62
|
-
if os.path.exists(json_filename):
|
|
63
|
-
with open(json_filename) as file:
|
|
64
|
-
_id = json.load(file)["_id"]
|
|
65
|
-
|
|
66
|
-
if not display_name:
|
|
67
|
-
display_name = basename.replace("_", " ").title()
|
|
68
|
-
|
|
69
|
-
markdown = ""
|
|
70
|
-
if os.path.exists(notebook_filename):
|
|
71
|
-
notebook = _read_notebook(notebook_filename)
|
|
72
|
-
markdown = "\n\n".join(
|
|
73
|
-
cell.source for cell in notebook.cells if cell.cell_type == "markdown"
|
|
74
|
-
)
|
|
75
|
-
|
|
76
|
-
request_data = {"_id": _id, "displayName": display_name, "markdown": markdown}
|
|
77
|
-
response = requests.post(endpoint, data=request_data)
|
|
78
|
-
|
|
79
|
-
body = response.json()
|
|
80
|
-
node_id = body.get("_id")
|
|
81
|
-
|
|
82
|
-
with open(json_filename, "w") as file:
|
|
83
|
-
json.dump({"_id": node_id}, file)
|
|
84
|
-
|
|
85
|
-
return json.dumps(body, indent=4)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|