ssb-pubmd 0.0.3__tar.gz → 0.0.5__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.3
3
+ Version: 0.0.5
4
4
  Summary: SSB Pubmd
5
5
  License: MIT
6
6
  Author: Olav Landsverk
@@ -14,6 +14,9 @@ Classifier: Programming Language :: Python :: 3.11
14
14
  Classifier: Programming Language :: Python :: 3.12
15
15
  Classifier: Programming Language :: Python :: 3.13
16
16
  Requires-Dist: click (>=8.0.1)
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)
17
20
  Project-URL: Changelog, https://github.com/statisticsnorway/ssb-pubmd/releases
18
21
  Project-URL: Documentation, https://statisticsnorway.github.io/ssb-pubmd
19
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.3"
3
+ version = "0.0.5"
4
4
  description = "SSB Pubmd"
5
5
  authors = ["Olav Landsverk <stud-oll@ssb.no>"]
6
6
  license = "MIT"
@@ -16,6 +16,9 @@ Changelog = "https://github.com/statisticsnorway/ssb-pubmd/releases"
16
16
  [tool.poetry.dependencies]
17
17
  python = "^3.10"
18
18
  click = ">=8.0.1"
19
+ nbformat = "^5.10.4"
20
+ requests = "^2.32.3"
21
+ types-requests = "^2.32.0.20250306"
19
22
 
20
23
  [tool.poetry.group.dev.dependencies]
21
24
  pygments = ">=2.10.0"
@@ -0,0 +1,5 @@
1
+ """SSB Pubmd."""
2
+
3
+ from .functions import notebook_to_cms
4
+
5
+ __all__ = ["notebook_to_cms"]
@@ -0,0 +1,79 @@
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 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
+ return json.dumps(response, indent=4)
@@ -1 +0,0 @@
1
- """SSB Pubmd."""
@@ -1,37 +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
-
9
- def example_function(number1: int, number2: int) -> str:
10
- """Compare two integers.
11
-
12
- This is merely an example function can be deleted. It is used to show and test generating
13
- documentation from code, type hinting, testing, and testing examples
14
- in the code.
15
-
16
-
17
- Args:
18
- number1: The first number.
19
- number2: The second number, which will be compared to number1.
20
-
21
- Returns:
22
- A string describing which number is the greatest.
23
-
24
- Examples:
25
- Examples should be written in doctest format, and should illustrate how
26
- to use the function.
27
-
28
- >>> example_function(1, 2)
29
- 1 is less than 2 by 1
30
-
31
- """
32
- if number1 < number2:
33
- return f"{number1} is less than {number2} by {number2 - number1}"
34
- elif number1 > number2:
35
- return f"{number1} is greater than {number2} by {number1 - number2}"
36
- else:
37
- return f"{number1} is equal to {number2}"
File without changes
File without changes