ssb-pubmd 0.0.3__py3-none-any.whl → 0.0.5__py3-none-any.whl

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/__init__.py CHANGED
@@ -1 +1,5 @@
1
1
  """SSB Pubmd."""
2
+
3
+ from .functions import notebook_to_cms
4
+
5
+ __all__ = ["notebook_to_cms"]
ssb_pubmd/functions.py CHANGED
@@ -5,33 +5,75 @@ https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
5
5
 
6
6
  """
7
7
 
8
+ import json
9
+ import os
8
10
 
9
- def example_function(number1: int, number2: int) -> str:
10
- """Compare two integers.
11
+ import nbformat
12
+ import requests
13
+ from nbformat import NotebookNode
11
14
 
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
15
 
16
+ def _read_notebook(fp: str) -> NotebookNode:
17
+ return nbformat.read(fp, as_version=nbformat.NO_CONVERT) # type: ignore
16
18
 
17
- Args:
18
- number1: The first number.
19
- number2: The second number, which will be compared to number1.
20
19
 
21
- Returns:
22
- A string describing which number is the greatest.
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.
23
33
 
24
- Examples:
25
- Examples should be written in doctest format, and should illustrate how
26
- to use the function.
34
+ Creating and updating content is handled in the following way:
27
35
 
28
- >>> example_function(1, 2)
29
- 1 is less than 2 by 1
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*.
30
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.
31
52
  """
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}"
53
+ if notebook_folder:
54
+ os.chdir(notebook_folder)
36
55
  else:
37
- return f"{number1} is equal to {number2}"
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,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
@@ -0,0 +1,9 @@
1
+ ssb_pubmd/__init__.py,sha256=GGdFjb7Gcazu5sC97C-Je8JY-8f0dZ6mKjL-G-4cJLo,88
2
+ ssb_pubmd/__main__.py,sha256=8D0yedPhnV_2L7nj0s0KUKxNQqPxoussMHGDNM-vyjg,209
3
+ ssb_pubmd/functions.py,sha256=Qb07rblTiSDytfVKXZvnaRazphD27yPi_fsgCq-0Xbs,2717
4
+ ssb_pubmd/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ ssb_pubmd-0.0.5.dist-info/LICENSE,sha256=tF5bnYv09fgH5ph9t1EpH1MGrVOGTQeswL4dzVeZ_ak,1073
6
+ ssb_pubmd-0.0.5.dist-info/METADATA,sha256=-iURLTkViBNpxXw4-CdCbiWwh69-FhsgDddfe6HhjI4,3883
7
+ ssb_pubmd-0.0.5.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
8
+ ssb_pubmd-0.0.5.dist-info/entry_points.txt,sha256=o4oU99zbZNIBKGYWdgdEG6ev-62ZRWEJOe7EOjJaajk,53
9
+ ssb_pubmd-0.0.5.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- ssb_pubmd/__init__.py,sha256=kCA0PilWSQNDZh4rUo5jRghGfLkmQkZDcWMNRJSQVaA,17
2
- ssb_pubmd/__main__.py,sha256=8D0yedPhnV_2L7nj0s0KUKxNQqPxoussMHGDNM-vyjg,209
3
- ssb_pubmd/functions.py,sha256=liG4j8_NaA1NGGZGqqDcexaOSUQmR9Ksbk7xa253O94,1122
4
- ssb_pubmd/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
- ssb_pubmd-0.0.3.dist-info/LICENSE,sha256=tF5bnYv09fgH5ph9t1EpH1MGrVOGTQeswL4dzVeZ_ak,1073
6
- ssb_pubmd-0.0.3.dist-info/METADATA,sha256=F33kVdLp4H2Jt4Oj4cMzCWhkydj01L47d0XnCnHf4wU,3740
7
- ssb_pubmd-0.0.3.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
8
- ssb_pubmd-0.0.3.dist-info/entry_points.txt,sha256=o4oU99zbZNIBKGYWdgdEG6ev-62ZRWEJOe7EOjJaajk,53
9
- ssb_pubmd-0.0.3.dist-info/RECORD,,