ssb-pubmd 0.0.6__tar.gz → 0.0.7__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.7}/PKG-INFO +1 -1
- {ssb_pubmd-0.0.6 → ssb_pubmd-0.0.7}/pyproject.toml +2 -1
- {ssb_pubmd-0.0.6 → ssb_pubmd-0.0.7}/src/ssb_pubmd/__init__.py +1 -1
- ssb_pubmd-0.0.7/src/ssb_pubmd/exporter.py +130 -0
- ssb_pubmd-0.0.6/src/ssb_pubmd/functions.py +0 -85
- {ssb_pubmd-0.0.6 → ssb_pubmd-0.0.7}/LICENSE +0 -0
- {ssb_pubmd-0.0.6 → ssb_pubmd-0.0.7}/README.md +0 -0
- {ssb_pubmd-0.0.6 → ssb_pubmd-0.0.7}/src/ssb_pubmd/__main__.py +0 -0
- {ssb_pubmd-0.0.6 → ssb_pubmd-0.0.7}/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.7"
|
|
4
4
|
description = "SSB Pubmd"
|
|
5
5
|
authors = ["Olav Landsverk <stud-oll@ssb.no>"]
|
|
6
6
|
license = "MIT"
|
|
@@ -38,6 +38,7 @@ 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"
|
|
41
42
|
|
|
42
43
|
[tool.pytest.ini_options]
|
|
43
44
|
pythonpath = ["src"]
|
|
@@ -0,0 +1,130 @@
|
|
|
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 markdown-based content."""
|
|
11
|
+
|
|
12
|
+
def __init__(self, endpoint: str) -> None:
|
|
13
|
+
self.endpoint: str = endpoint
|
|
14
|
+
self.notebook_folder: str = ""
|
|
15
|
+
self.notebook_filename: str = ""
|
|
16
|
+
self.content_id: str = ""
|
|
17
|
+
|
|
18
|
+
def _set_working_directory(self) -> None:
|
|
19
|
+
"""Set the working directory to the notebook folder."""
|
|
20
|
+
if self.notebook_folder:
|
|
21
|
+
os.chdir(self.notebook_folder)
|
|
22
|
+
else:
|
|
23
|
+
os.chdir(os.getcwd())
|
|
24
|
+
|
|
25
|
+
def _get_basename(self) -> str:
|
|
26
|
+
"""Returns the name of the notebook file without extension."""
|
|
27
|
+
return os.path.splitext(self.notebook_filename)[0]
|
|
28
|
+
|
|
29
|
+
def _get_display_name(self) -> str:
|
|
30
|
+
"""Generate a display name to send to the CMS endpoint."""
|
|
31
|
+
return self._get_basename().replace("_", " ").title()
|
|
32
|
+
|
|
33
|
+
def _get_data_filename(self) -> str:
|
|
34
|
+
"""Returns the data filename to store the content id returned from the CMS."""
|
|
35
|
+
return self._get_basename() + ".json"
|
|
36
|
+
|
|
37
|
+
def _get_content_id(self) -> str:
|
|
38
|
+
"""Get the content id from the JSON file if it exists."""
|
|
39
|
+
content_id = ""
|
|
40
|
+
data_filename = self._get_data_filename()
|
|
41
|
+
if os.path.exists(data_filename):
|
|
42
|
+
with open(data_filename) as file:
|
|
43
|
+
content_id = json.load(file)["_id"]
|
|
44
|
+
return content_id
|
|
45
|
+
|
|
46
|
+
def _read_notebook(self) -> NotebookNode:
|
|
47
|
+
"""Reads the notebook file and returns its content."""
|
|
48
|
+
return nbformat.read(self.notebook_filename, as_version=nbformat.NO_CONVERT) # type: ignore
|
|
49
|
+
|
|
50
|
+
def _get_content_from_notebook(self) -> str:
|
|
51
|
+
"""Extracts all markdown cells from the notebook and returns it as a merged string."""
|
|
52
|
+
notebook = self._read_notebook()
|
|
53
|
+
markdown_content = ""
|
|
54
|
+
for cell in notebook.cells:
|
|
55
|
+
if cell.cell_type == "markdown":
|
|
56
|
+
markdown_content += cell.source + "\n\n"
|
|
57
|
+
return markdown_content
|
|
58
|
+
|
|
59
|
+
def _prepare_request_data(self) -> dict[str, str]:
|
|
60
|
+
"""Prepares the request data to be sent to the CMS endpoint."""
|
|
61
|
+
return {
|
|
62
|
+
"_id": self._get_content_id(),
|
|
63
|
+
"displayName": self._get_display_name(),
|
|
64
|
+
"markdown": self._get_content_from_notebook(),
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
def _prepare_headers(self) -> dict[str, str]:
|
|
68
|
+
"""Prepares the headers for the request."""
|
|
69
|
+
return {"Content-Type": "application/json"}
|
|
70
|
+
|
|
71
|
+
def _send_request(self) -> None:
|
|
72
|
+
"""Sends the request to the CMS endpoint and returns the content id."""
|
|
73
|
+
data = self._prepare_request_data()
|
|
74
|
+
response = requests.post(
|
|
75
|
+
self.endpoint,
|
|
76
|
+
data=json.dumps(data),
|
|
77
|
+
headers=self._prepare_headers(),
|
|
78
|
+
)
|
|
79
|
+
self.content_id = response.json()["_id"]
|
|
80
|
+
|
|
81
|
+
def _save_content_id(self) -> None:
|
|
82
|
+
"""Saves the content id to a JSON file."""
|
|
83
|
+
with open(self._get_data_filename(), "w") as file:
|
|
84
|
+
json.dump({"_id": self.content_id}, file)
|
|
85
|
+
|
|
86
|
+
def set_notebook(self, notebook_filename: str, notebook_folder: str) -> None:
|
|
87
|
+
"""Uses the notebook_filename and working directory."""
|
|
88
|
+
self.notebook_filename = notebook_filename
|
|
89
|
+
self.notebook_folder = notebook_folder
|
|
90
|
+
self._set_working_directory()
|
|
91
|
+
|
|
92
|
+
def export(self) -> str:
|
|
93
|
+
"""Main method to export the notebook content to the CMS endpoint."""
|
|
94
|
+
self._send_request()
|
|
95
|
+
self._save_content_id()
|
|
96
|
+
return self.content_id
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def notebook_to_cms(
|
|
100
|
+
notebook_filename: str,
|
|
101
|
+
endpoint: str,
|
|
102
|
+
notebook_folder: str = "",
|
|
103
|
+
) -> str:
|
|
104
|
+
r"""Sends all the markdown content of a notebook to a CMS endpoint.
|
|
105
|
+
|
|
106
|
+
The CMS endpoint must satisfy two constraints:
|
|
107
|
+
|
|
108
|
+
- It must accept a post request with fields *id*, *displayName* and *markdown*.
|
|
109
|
+
- The response body must have a key *_id* whose value should be
|
|
110
|
+
a unique string identifier of the content.
|
|
111
|
+
|
|
112
|
+
Creating and updating content is handled in the following way:
|
|
113
|
+
|
|
114
|
+
- On the first request, an empty string is sent as *id*.
|
|
115
|
+
- If the request succeeds, the value of *_id* (in the response) is stored in a JSON file
|
|
116
|
+
(created in the same directory as the notebook file).
|
|
117
|
+
- On subsequent requests, the stored value is sent as *id*.
|
|
118
|
+
|
|
119
|
+
Args:
|
|
120
|
+
notebook_filename (str): The name of the notebook file, e.g. `"my_notebook.ipynb"`.
|
|
121
|
+
endpoint (str): The URL of the CMS endpoint.
|
|
122
|
+
notebook_folder (str): Sets a custom notebook folder (as absolute path) containing the notebook file.
|
|
123
|
+
If not set, the current folder is used.
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
str: The identifier of the content returned by the CMS endpoint.
|
|
127
|
+
"""
|
|
128
|
+
exporter = _Exporter(endpoint)
|
|
129
|
+
exporter.set_notebook(notebook_filename, notebook_folder)
|
|
130
|
+
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
|