ssb-pubmd 0.0.4__tar.gz → 0.0.6__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: ssb-pubmd
3
- Version: 0.0.4
3
+ Version: 0.0.6
4
4
  Summary: SSB Pubmd
5
5
  License: MIT
6
6
  Author: Olav Landsverk
@@ -15,6 +15,8 @@ Classifier: Programming Language :: Python :: 3.12
15
15
  Classifier: Programming Language :: Python :: 3.13
16
16
  Requires-Dist: click (>=8.0.1)
17
17
  Requires-Dist: nbformat (>=5.10.4,<6.0.0)
18
+ Requires-Dist: requests (>=2.32.3,<3.0.0)
19
+ Requires-Dist: types-requests (>=2.32.0.20250306,<3.0.0.0)
18
20
  Project-URL: Changelog, https://github.com/statisticsnorway/ssb-pubmd/releases
19
21
  Project-URL: Documentation, https://statisticsnorway.github.io/ssb-pubmd
20
22
  Project-URL: Homepage, https://github.com/statisticsnorway/ssb-pubmd
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "ssb-pubmd"
3
- version = "0.0.4"
3
+ version = "0.0.6"
4
4
  description = "SSB Pubmd"
5
5
  authors = ["Olav Landsverk <stud-oll@ssb.no>"]
6
6
  license = "MIT"
@@ -17,6 +17,8 @@ Changelog = "https://github.com/statisticsnorway/ssb-pubmd/releases"
17
17
  python = "^3.10"
18
18
  click = ">=8.0.1"
19
19
  nbformat = "^5.10.4"
20
+ requests = "^2.32.3"
21
+ types-requests = "^2.32.0.20250306"
20
22
 
21
23
  [tool.poetry.group.dev.dependencies]
22
24
  pygments = ">=2.10.0"
@@ -0,0 +1,85 @@
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)
@@ -1,78 +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
- from nbformat import NotebookNode
13
-
14
-
15
- def _read_notebook(fp: str) -> NotebookNode:
16
- return nbformat.read(fp, as_version=nbformat.NO_CONVERT) # type: ignore
17
-
18
-
19
- def notebook_to_cms(
20
- notebook_filename: str,
21
- endpoint: str,
22
- notebook_folder: str = "",
23
- display_name: str = "",
24
- ) -> dict[str, str]:
25
- r"""Sends all the markdown content of a notebook to a CMS endpoint.
26
-
27
- This function can be executed within the notebook it gets the markdown content from, \
28
- but the notebook filename always has to be explicitly passed.
29
-
30
- The CMS endpoint has to satisfy two constraints:
31
- * It must accept post requests with fields *id*, *displayName* and *markdown*.
32
- * The response body must have a key *_id* whose value should be \
33
- the unique identifier of the created content in the CMS.
34
-
35
- On the first successfull request, an empty string is sent as *id*,
36
- and the *_id* in the response is stored in a JSON file \
37
- (created in the same directory as the notebook file). \
38
- On subsequent requests, the stored value is sent as *id*.
39
-
40
- Args:
41
- notebook_filename (str): The name of the notebook file.
42
- endpoint (str): The URL of the CMS endpoint.
43
- notebook_folder (str): Ignore this parameter when executing the function from \
44
- the notebook containing the markdown content. \
45
- Sets a custom base directory (absolute path) containing the notebook file.
46
- display_name (str): Send a custom *displayName* value to the CMS endpoint.
47
-
48
- Returns:
49
- (dict): The response from the CMS endpoint.
50
- """
51
- if notebook_folder:
52
- os.chdir(notebook_folder)
53
- else:
54
- os.chdir(os.getcwd())
55
-
56
- basename = os.path.splitext(notebook_filename)[0]
57
- json_filename = basename + ".json"
58
-
59
- _id = ""
60
- if os.path.exists(json_filename):
61
- with open(json_filename) as file:
62
- _id = json.load(file)["_id"]
63
-
64
- if not display_name:
65
- display_name = basename.replace("_", " ").title()
66
-
67
- markdown = ""
68
- if os.path.exists(notebook_filename):
69
- notebook = _read_notebook(notebook_filename)
70
- markdown = "\n\n".join(
71
- cell.source for cell in notebook.cells if cell.cell_type == "markdown"
72
- )
73
-
74
- endpoint = endpoint
75
-
76
- request_data = {"id": _id, "displayName": display_name, "markdown": markdown}
77
-
78
- return request_data
File without changes
File without changes