nbsync 0.3.9__py3-none-any.whl → 0.3.11__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.
nbsync/cell.py
CHANGED
@@ -13,6 +13,23 @@ if TYPE_CHECKING:
|
|
13
13
|
from nbstore.markdown import Image
|
14
14
|
|
15
15
|
|
16
|
+
@dataclass
|
17
|
+
class Attributes:
|
18
|
+
source: str
|
19
|
+
tabs: str
|
20
|
+
identifier: str
|
21
|
+
result: str
|
22
|
+
|
23
|
+
@classmethod
|
24
|
+
def pop(cls, attrs: dict[str, str]) -> Attributes:
|
25
|
+
return cls(
|
26
|
+
source=attrs.pop("source", ""),
|
27
|
+
tabs=attrs.pop("tabs", ""),
|
28
|
+
identifier=attrs.pop("identifier", ""),
|
29
|
+
result=attrs.pop("result", ""),
|
30
|
+
)
|
31
|
+
|
32
|
+
|
16
33
|
@dataclass
|
17
34
|
class Cell:
|
18
35
|
image: Image
|
@@ -28,64 +45,77 @@ class Cell:
|
|
28
45
|
"""The content of the image."""
|
29
46
|
|
30
47
|
def convert(self, *, escape: bool = False) -> str:
|
31
|
-
|
32
|
-
tabs = self.image.attributes.pop("tabs", "")
|
33
|
-
identifier = self.image.attributes.pop("identifier", "")
|
34
|
-
result = self.image.attributes.pop("result", "")
|
35
|
-
|
36
|
-
if "/" not in self.mime or not self.content or kind == "only":
|
37
|
-
if self.image.source:
|
38
|
-
source = get_source(
|
39
|
-
self,
|
40
|
-
include_attrs=True,
|
41
|
-
include_identifier=bool(identifier),
|
42
|
-
)
|
43
|
-
kind = "only"
|
44
|
-
else:
|
45
|
-
source = ""
|
46
|
-
result = self.image.url = ""
|
48
|
+
attrs = Attributes.pop(self.image.attributes)
|
47
49
|
|
48
|
-
|
49
|
-
source = get_source(
|
50
|
-
self,
|
51
|
-
include_attrs=True,
|
52
|
-
include_identifier=bool(identifier),
|
53
|
-
)
|
50
|
+
if include_attrs := self._include_attributes():
|
54
51
|
self.image.url = ""
|
55
|
-
|
52
|
+
|
53
|
+
source = get_source(
|
54
|
+
self,
|
55
|
+
console=attrs.source == "console",
|
56
|
+
include_attrs=include_attrs,
|
57
|
+
include_identifier=bool(attrs.identifier),
|
58
|
+
)
|
59
|
+
|
60
|
+
if not self.content or attrs.source in ["console", "only"]:
|
61
|
+
attrs.source = "only" if self.image.source else ""
|
62
|
+
result = ""
|
63
|
+
|
64
|
+
elif self.mime.startswith("text/") and isinstance(self.content, str):
|
65
|
+
result = get_text_markdown(self, attrs.result, escape=escape)
|
56
66
|
|
57
67
|
else:
|
58
|
-
source = get_source(
|
59
|
-
self,
|
60
|
-
include_attrs=False,
|
61
|
-
include_identifier=bool(identifier),
|
62
|
-
)
|
63
68
|
result = get_image_markdown(self)
|
64
69
|
|
65
|
-
if markdown := get_markdown(
|
70
|
+
if markdown := get_markdown(attrs.source, source, result, attrs.tabs):
|
66
71
|
return textwrap.indent(markdown, self.image.indent)
|
67
72
|
|
68
73
|
return "" # no cov
|
69
74
|
|
75
|
+
def _include_attributes(self) -> bool:
|
76
|
+
if "/" not in self.mime or not self.content:
|
77
|
+
return True
|
78
|
+
|
79
|
+
return self.mime.startswith("text/") and isinstance(self.content, str)
|
80
|
+
|
70
81
|
|
71
82
|
def get_source(
|
72
83
|
cell: Cell,
|
73
84
|
*,
|
85
|
+
console: bool = False,
|
74
86
|
include_attrs: bool = False,
|
75
87
|
include_identifier: bool = False,
|
76
88
|
) -> str:
|
89
|
+
if not (source := cell.image.source):
|
90
|
+
return ""
|
91
|
+
|
92
|
+
if console:
|
93
|
+
output = str(cell.content.rstrip())
|
94
|
+
source = f"{_add_prompt(source)}\n{output}"
|
95
|
+
|
77
96
|
attrs = [cell.language]
|
78
97
|
if include_attrs:
|
79
98
|
attrs.extend(cell.image.iter_parts())
|
80
99
|
attr = " ".join(attrs)
|
81
100
|
|
82
|
-
source = cell.image.source
|
83
101
|
if include_identifier:
|
84
102
|
source = f"# #{cell.image.identifier}\n{source}"
|
85
103
|
|
86
104
|
return f"```{attr}\n{source}\n```"
|
87
105
|
|
88
106
|
|
107
|
+
def _add_prompt(source: str) -> str:
|
108
|
+
lines: list[str] = []
|
109
|
+
for line in source.splitlines():
|
110
|
+
if not line:
|
111
|
+
lines.append("")
|
112
|
+
elif line.startswith(" "):
|
113
|
+
lines.append(f"... {line}")
|
114
|
+
else:
|
115
|
+
lines.append(f">>> {line}")
|
116
|
+
return "\n".join(lines)
|
117
|
+
|
118
|
+
|
89
119
|
def get_text_markdown(cell: Cell, result: str, *, escape: bool = False) -> str:
|
90
120
|
text = str(cell.content.rstrip())
|
91
121
|
|
@@ -103,6 +133,10 @@ def get_image_markdown(cell: Cell) -> str:
|
|
103
133
|
msg = f"{cell.image.url}#{cell.image.identifier} [{cell.mime}]"
|
104
134
|
logger.debug(f"Converting image: {msg}")
|
105
135
|
|
136
|
+
if "/" not in cell.mime:
|
137
|
+
cell.image.url = ""
|
138
|
+
return ""
|
139
|
+
|
106
140
|
ext = cell.mime.split("/")[1].split("+")[0]
|
107
141
|
cell.image.url = f"{uuid.uuid4()}.{ext}"
|
108
142
|
|
nbsync/logger.py
CHANGED
@@ -7,7 +7,9 @@ from typing import Any
|
|
7
7
|
_logger = logging.getLogger("nbsync")
|
8
8
|
|
9
9
|
|
10
|
-
def set_logger(
|
10
|
+
def set_logger(
|
11
|
+
logger: Logger | LoggerAdapter[Logger] | None = None,
|
12
|
+
) -> Logger | LoggerAdapter[Logger]:
|
11
13
|
global _logger # noqa: PLW0603
|
12
14
|
|
13
15
|
if logger:
|
nbsync/sync.py
CHANGED
@@ -72,7 +72,7 @@ def update_notebooks(
|
|
72
72
|
|
73
73
|
if url not in notebooks:
|
74
74
|
if url == ".md":
|
75
|
-
notebooks[url] = Notebook(nbformat.v4.new_notebook())
|
75
|
+
notebooks[url] = Notebook(nbformat.v4.new_notebook()) # pyright: ignore[reportUnknownMemberType]
|
76
76
|
else:
|
77
77
|
try:
|
78
78
|
notebooks[url] = Notebook(store.read(url))
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.3
|
2
2
|
Name: nbsync
|
3
|
-
Version: 0.3.
|
3
|
+
Version: 0.3.11
|
4
4
|
Summary: A core library to synchronize Jupyter notebooks and Markdown documents, enabling seamless integration and dynamic content execution
|
5
5
|
Keywords: jupyter,notebook,documentation,markdown,python,visualization,dynamic-execution,real-time-sync
|
6
6
|
Author: daizutabi
|
@@ -0,0 +1,10 @@
|
|
1
|
+
nbsync/__init__.py,sha256=3NuWx9D0LcBnXjEKrC-uypNDOz8rLWXI7mIjdN9xSeE,126
|
2
|
+
nbsync/cell.py,sha256=58RkBXzcYajMJzFbUBfdtxXScfkl642B-dckel5-5ZA,4909
|
3
|
+
nbsync/logger.py,sha256=4nXvoy5_5Xd2uXCiT_0u1LqOI6L5ZgV4PFCCmy3leVM,759
|
4
|
+
nbsync/markdown.py,sha256=6jDaDtCEkDLsukInF0EnauJ2pUki6nVswjyu3Zg2AUE,3832
|
5
|
+
nbsync/notebook.py,sha256=4F3V1KDHfJnIR5ZSb1xWdN9JGiC6mWUVimXXK1BFsbc,1450
|
6
|
+
nbsync/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
7
|
+
nbsync/sync.py,sha256=x7fsWjfKiQbenXXx9v9BYtfpx7pcmAlHj3Fo9PoHGnI,4034
|
8
|
+
nbsync-0.3.11.dist-info/WHEEL,sha256=4n27za1eEkOnA7dNjN6C5-O2rUiw6iapszm14Uj-Qmk,79
|
9
|
+
nbsync-0.3.11.dist-info/METADATA,sha256=BSEoL2e5Tciq1-GhQIobEdgkGKi7gaMPzQG_VB3Ftak,7105
|
10
|
+
nbsync-0.3.11.dist-info/RECORD,,
|
nbsync-0.3.9.dist-info/RECORD
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
nbsync/__init__.py,sha256=dcdb96c7d0f42dc0675e310aac2faeca93433b3f2b2d65c8ee622374df7149e1,126
|
2
|
-
nbsync/cell.py,sha256=329fc181d8dd9ded8e6d86cf24e3f3fc3abd7ea6396e765c1ddc040dc7876d7e,4233
|
3
|
-
nbsync/logger.py,sha256=b27351aa2f711128f807e67d1907df081960a9e56e8efb97b81bde58079b2163,736
|
4
|
-
nbsync/markdown.py,sha256=ea30da0ed0849032ecba42271741276ae276a54922ea756cc23caedd98360141,3832
|
5
|
-
nbsync/notebook.py,sha256=e05dd5d4a0c77c99c84796526f5c5674df491a20ba9965158a65d72b5045b1b7,1450
|
6
|
-
nbsync/py.typed,sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855,0
|
7
|
-
nbsync/sync.py,sha256=bc84b39fc0ff0da7be8dd2400519efbc3d3668bdf96d8216c79d8e8a700e455a,3990
|
8
|
-
nbsync-0.3.9.dist-info/WHEEL,sha256=2b400f346628f0064eb5bbf656b39df8dfcb092437ab08244409d295749b81a3,78
|
9
|
-
nbsync-0.3.9.dist-info/METADATA,sha256=d5966bfd9c5578e334b129b7dee4c8b0bd54d08d7a50d92a44ea0eb84837b6f7,7104
|
10
|
-
nbsync-0.3.9.dist-info/RECORD,,
|