sapiopycommons 2025.6.6a559__py3-none-any.whl → 2025.6.10a560__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.
Potentially problematic release.
This version of sapiopycommons might be problematic. Click here for more details.
- sapiopycommons/chem/IndigoMolecules.py +29 -1
- sapiopycommons/chem/Molecules.py +3 -3
- sapiopycommons/general/html_formatter.py +456 -0
- sapiopycommons/general/sapio_links.py +12 -4
- {sapiopycommons-2025.6.6a559.dist-info → sapiopycommons-2025.6.10a560.dist-info}/METADATA +1 -1
- {sapiopycommons-2025.6.6a559.dist-info → sapiopycommons-2025.6.10a560.dist-info}/RECORD +8 -7
- {sapiopycommons-2025.6.6a559.dist-info → sapiopycommons-2025.6.10a560.dist-info}/WHEEL +0 -0
- {sapiopycommons-2025.6.6a559.dist-info → sapiopycommons-2025.6.10a560.dist-info}/licenses/LICENSE +0 -0
|
@@ -10,9 +10,36 @@ indigo.setOption("render-stereo-style", "ext")
|
|
|
10
10
|
indigo.setOption("aromaticity-model", "generic")
|
|
11
11
|
indigo.setOption("render-coloring", True)
|
|
12
12
|
indigo.setOption("molfile-saving-mode", "3000")
|
|
13
|
+
indigo.setOption("dearomatize-verification", False)
|
|
13
14
|
indigo_inchi = IndigoInchi(indigo)
|
|
14
15
|
|
|
15
16
|
|
|
17
|
+
def get_aromatic_dearomatic_forms(m: IndigoObject):
|
|
18
|
+
"""
|
|
19
|
+
Get the aromatic and dearomatic forms of the molecule. Retain the original form if it's not inversible.
|
|
20
|
+
Inversible: after aromatic-dearomatic-aromatic transformation, the molecule is the same as the first aromatic transformation.
|
|
21
|
+
:param m: molecule from indigo.
|
|
22
|
+
:return: pair of indigo objects, first is aromatic, second is dearomatic.
|
|
23
|
+
"""
|
|
24
|
+
try:
|
|
25
|
+
aromatic_reaction = m.clone()
|
|
26
|
+
aromatic_reaction.aromatize()
|
|
27
|
+
dearomatic_reaction = aromatic_reaction.clone()
|
|
28
|
+
dearomatic_reaction.dearomatize()
|
|
29
|
+
second_aromatic_reaction = dearomatic_reaction.clone()
|
|
30
|
+
second_aromatic_reaction.aromatize()
|
|
31
|
+
match = indigo.exactMatch(aromatic_reaction, second_aromatic_reaction)
|
|
32
|
+
if match:
|
|
33
|
+
return aromatic_reaction, dearomatic_reaction
|
|
34
|
+
else:
|
|
35
|
+
return m, dearomatic_reaction
|
|
36
|
+
except (Exception):
|
|
37
|
+
# If aromatization then following deromatization fails, we just skip it.
|
|
38
|
+
dearomatic_reaction = m.clone()
|
|
39
|
+
dearomatic_reaction.dearomatize()
|
|
40
|
+
return m, dearomatic_reaction
|
|
41
|
+
|
|
42
|
+
|
|
16
43
|
# Function to process dative bonds in a molecule
|
|
17
44
|
# Returns True if at least one dative bond (_BOND_COORDINATION) was removed
|
|
18
45
|
def remove_dative_bonds_in_mol(molecule: IndigoObject) -> bool:
|
|
@@ -51,7 +78,8 @@ def remove_dative_in_reaction(reaction: IndigoObject) -> bool:
|
|
|
51
78
|
:param reaction: The reaction to remove dative bonds.
|
|
52
79
|
:return: Whether there are any dative bonds in the reaction that were removed.
|
|
53
80
|
"""
|
|
54
|
-
reactant_dative_removed: bool = any(
|
|
81
|
+
reactant_dative_removed: bool = any(
|
|
82
|
+
remove_dative_bonds_in_mol(reactant) for reactant in reaction.iterateReactants())
|
|
55
83
|
product_dative_removed: bool = any(remove_dative_bonds_in_mol(product) for product in reaction.iterateProducts())
|
|
56
84
|
return reactant_dative_removed or product_dative_removed
|
|
57
85
|
|
sapiopycommons/chem/Molecules.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Author Yechen Qiao
|
|
2
2
|
# Common Molecule Utilities for Molecule Transfers with Sapio
|
|
3
|
-
|
|
3
|
+
from indigo import IndigoObject
|
|
4
4
|
from rdkit import Chem
|
|
5
5
|
from rdkit.Chem import Crippen, MolToInchi
|
|
6
6
|
from rdkit.Chem import Descriptors
|
|
@@ -10,7 +10,7 @@ from rdkit.Chem.MolStandardize import rdMolStandardize
|
|
|
10
10
|
from rdkit.Chem.SaltRemover import SaltRemover
|
|
11
11
|
from rdkit.Chem.rdchem import Mol, RWMol, Bond
|
|
12
12
|
|
|
13
|
-
from sapiopycommons.chem.IndigoMolecules import indigo, renderer, indigo_inchi
|
|
13
|
+
from sapiopycommons.chem.IndigoMolecules import indigo, renderer, indigo_inchi, get_aromatic_dearomatic_forms
|
|
14
14
|
|
|
15
15
|
metal_disconnector = rdMolStandardize.MetalDisconnector()
|
|
16
16
|
tautomer_params = Chem.MolStandardize.rdMolStandardize.CleanupParameters()
|
|
@@ -247,7 +247,7 @@ def mol_to_sapio_substance(mol: Mol, include_stereoisomers=False,
|
|
|
247
247
|
molecule["image"] = None
|
|
248
248
|
# We need to test the INCHI can be loaded back to indigo.
|
|
249
249
|
indigo_mol = indigo.loadMolecule(molBlock)
|
|
250
|
-
indigo_mol
|
|
250
|
+
indigo_mol = get_aromatic_dearomatic_forms(indigo_mol)[0] # Get the aromatic form of the molecule.
|
|
251
251
|
if enhanced_stereo:
|
|
252
252
|
# Remove enhanced stereo layer when generating InChI as the stereo hash is generated separately for reg.
|
|
253
253
|
Chem.CanonicalizeEnhancedStereo(inchi_mol)
|
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from typing import Final
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class HtmlFormatter:
|
|
8
|
+
"""
|
|
9
|
+
A class for formatting text in HTML with tag classes supported by the client.
|
|
10
|
+
"""
|
|
11
|
+
TIMESTAMP_TEXT__CSS_CLASS_NAME: Final[str] = "timestamp-text"
|
|
12
|
+
HEADER_1_TEXT__CSS_CLASS_NAME: Final[str] = "header1-text"
|
|
13
|
+
HEADER_2_TEXT__CSS_CLASS_NAME: Final[str] = "header2-text"
|
|
14
|
+
HEADER_3_TEXT__CSS_CLASS_NAME: Final[str] = "header3-text"
|
|
15
|
+
BODY_TEXT__CSS_CLASS_NAME: Final[str] = "body-text"
|
|
16
|
+
CAPTION_TEXT__CSS_CLASS_NAME: Final[str] = "caption-text"
|
|
17
|
+
|
|
18
|
+
@staticmethod
|
|
19
|
+
def timestamp(text: str) -> str:
|
|
20
|
+
"""
|
|
21
|
+
Given a text string, return that same text string HTML formatted using the timestamp CSS class.
|
|
22
|
+
|
|
23
|
+
:param text: The text to format.
|
|
24
|
+
:return: The HTML formatted text.
|
|
25
|
+
"""
|
|
26
|
+
return f'<span class="{HtmlFormatter.TIMESTAMP_TEXT__CSS_CLASS_NAME}">{text}</span>'
|
|
27
|
+
|
|
28
|
+
@staticmethod
|
|
29
|
+
def header_1(text: str) -> str:
|
|
30
|
+
"""
|
|
31
|
+
Given a text string, return that same text string HTML formatted using the header 1 CSS class.
|
|
32
|
+
|
|
33
|
+
:param text: The text to format.
|
|
34
|
+
:return: The HTML formatted text.
|
|
35
|
+
"""
|
|
36
|
+
return f'<span class="{HtmlFormatter.HEADER_1_TEXT__CSS_CLASS_NAME}">{text}</span>'
|
|
37
|
+
|
|
38
|
+
@staticmethod
|
|
39
|
+
def header_2(text: str) -> str:
|
|
40
|
+
"""
|
|
41
|
+
Given a text string, return that same text string HTML formatted using the header 2 CSS class.
|
|
42
|
+
|
|
43
|
+
:param text: The text to format.
|
|
44
|
+
:return: The HTML formatted text.
|
|
45
|
+
"""
|
|
46
|
+
return f'<span class="{HtmlFormatter.HEADER_2_TEXT__CSS_CLASS_NAME}">{text}</span>'
|
|
47
|
+
|
|
48
|
+
@staticmethod
|
|
49
|
+
def header_3(text: str) -> str:
|
|
50
|
+
"""
|
|
51
|
+
Given a text string, return that same text string HTML formatted using the header 3 CSS class.
|
|
52
|
+
|
|
53
|
+
:param text: The text to format.
|
|
54
|
+
:return: The HTML formatted text.
|
|
55
|
+
"""
|
|
56
|
+
return f'<span class="{HtmlFormatter.HEADER_3_TEXT__CSS_CLASS_NAME}">{text}</span>'
|
|
57
|
+
|
|
58
|
+
@staticmethod
|
|
59
|
+
def body(text: str) -> str:
|
|
60
|
+
"""
|
|
61
|
+
Given a text string, return that same text string HTML formatted using the body text CSS class.
|
|
62
|
+
|
|
63
|
+
:param text: The text to format.
|
|
64
|
+
:return: The HTML formatted text.
|
|
65
|
+
"""
|
|
66
|
+
return f'<span class="{HtmlFormatter.BODY_TEXT__CSS_CLASS_NAME}">{text}</span>'
|
|
67
|
+
|
|
68
|
+
@staticmethod
|
|
69
|
+
def caption(text: str) -> str:
|
|
70
|
+
"""
|
|
71
|
+
Given a text string, return that same text string HTML formatted using the caption text CSS class.
|
|
72
|
+
|
|
73
|
+
:param text: The text to format.
|
|
74
|
+
:return: The HTML formatted text.
|
|
75
|
+
"""
|
|
76
|
+
return f'<span class="{HtmlFormatter.CAPTION_TEXT__CSS_CLASS_NAME}">{text}</span>'
|
|
77
|
+
|
|
78
|
+
@staticmethod
|
|
79
|
+
def replace_newlines(text: str) -> str:
|
|
80
|
+
"""
|
|
81
|
+
Given a text string, return that same text string HTML formatted with newlines replaced by HTML line breaks.
|
|
82
|
+
|
|
83
|
+
:param text: The text to format.
|
|
84
|
+
:return: The HTML formatted text.
|
|
85
|
+
"""
|
|
86
|
+
return re.sub("\r?\n", "<br>", text)
|
|
87
|
+
|
|
88
|
+
@staticmethod
|
|
89
|
+
def scrub_html(text: str) -> str:
|
|
90
|
+
"""
|
|
91
|
+
Given a string that contains HTML, return that same string with all HTML removed.
|
|
92
|
+
|
|
93
|
+
:param text: The HTML string to scrub.
|
|
94
|
+
:return: The scrubbed text.
|
|
95
|
+
"""
|
|
96
|
+
if not text:
|
|
97
|
+
return ""
|
|
98
|
+
|
|
99
|
+
from bs4 import BeautifulSoup
|
|
100
|
+
return BeautifulSoup(text, "html.parser").get_text()
|
|
101
|
+
|
|
102
|
+
@staticmethod
|
|
103
|
+
def scrub_markdown(text: str) -> str:
|
|
104
|
+
"""
|
|
105
|
+
Given a string that contains markdown, return that same string with all markdown removed.
|
|
106
|
+
|
|
107
|
+
:param text: The markdown string to scrub.
|
|
108
|
+
:return: The scrubbed text.
|
|
109
|
+
"""
|
|
110
|
+
if not text:
|
|
111
|
+
return ""
|
|
112
|
+
|
|
113
|
+
# --- Remove Headers ---
|
|
114
|
+
# Level 1-6 headers (# to ######)
|
|
115
|
+
text = re.sub(r"^#{1,6}\s*(.*)$", r"\1", text, flags=re.MULTILINE).strip()
|
|
116
|
+
|
|
117
|
+
# --- Remove Emphasis ---
|
|
118
|
+
# Bold (**text** or __text__)
|
|
119
|
+
text = re.sub(r"\*\*(.*?)\*\*", r"\1", text)
|
|
120
|
+
text = re.sub(r"__(.*?)__", r"\1", text)
|
|
121
|
+
|
|
122
|
+
# Italic (*text* or _text_)
|
|
123
|
+
text = re.sub(r"\*(.*?)\*", r"\1", text)
|
|
124
|
+
text = re.sub(r"_(.*?)_", r"\1", text)
|
|
125
|
+
|
|
126
|
+
# --- Remove Strikethrough ---
|
|
127
|
+
# Strikethrough (~~text~~)
|
|
128
|
+
text = re.sub(r"~~(.*?)~~", r"\1", text)
|
|
129
|
+
|
|
130
|
+
# --- Remove Links ---
|
|
131
|
+
# Links ([text](url))
|
|
132
|
+
text = re.sub(r"\[(.*?)]\((.*?)\)", r"\1", text)
|
|
133
|
+
|
|
134
|
+
# --- Remove Images ---
|
|
135
|
+
# Images ()
|
|
136
|
+
text = re.sub(r"!\\[(.*?)\\]\\((.*?)\\)", "", text) # remove the entire image tag
|
|
137
|
+
|
|
138
|
+
# --- Remove Code ---
|
|
139
|
+
# Inline code (`code`)
|
|
140
|
+
text = re.sub(r"`(.*?)`", r"\1", text)
|
|
141
|
+
|
|
142
|
+
# Code blocks (```code```)
|
|
143
|
+
text = re.sub(r"```.*?```", "", text, flags=re.DOTALL) # multiline code blocks
|
|
144
|
+
|
|
145
|
+
# --- Remove Lists ---
|
|
146
|
+
# Unordered lists (* item, - item, + item)
|
|
147
|
+
text = re.sub(r"(?m)^[*\-+]\s+", "", text)
|
|
148
|
+
|
|
149
|
+
# Ordered lists (1. item)
|
|
150
|
+
text = re.sub(r"(?m)^\d+\.\s+", "", text)
|
|
151
|
+
|
|
152
|
+
# --- Remove Blockquotes ---
|
|
153
|
+
# Blockquotes (> text)
|
|
154
|
+
text = re.sub(r"(?m)^>\s+", "", text)
|
|
155
|
+
|
|
156
|
+
# --- Remove Horizontal Rules ---
|
|
157
|
+
# Horizontal rules (---, ***, ___)
|
|
158
|
+
text = re.sub(r"(?m)^[-_*]{3,}\s*$", "", text) # Remove horizontal rules
|
|
159
|
+
|
|
160
|
+
# --- Remove HTML tags (basic)---
|
|
161
|
+
# This is a very simple HTML tag removal, it does not handle nested tags or attributes properly.
|
|
162
|
+
text = re.sub(r"<[^>]*>", "", text)
|
|
163
|
+
|
|
164
|
+
# --- Remove escaped characters ---
|
|
165
|
+
text = re.sub(r"\\([!\"#$%&'()*+,./:;<=>?@\\[]^_`{|}~-])", r"\1", text)
|
|
166
|
+
|
|
167
|
+
return text
|
|
168
|
+
|
|
169
|
+
@staticmethod
|
|
170
|
+
def convert_markdown_to_html(text: str) -> str:
|
|
171
|
+
"""
|
|
172
|
+
Given a markdown string, convert it to HTML and return the HTML string.
|
|
173
|
+
|
|
174
|
+
:param text: The markdown string to convert.
|
|
175
|
+
:return: The HTML string.
|
|
176
|
+
"""
|
|
177
|
+
if not text:
|
|
178
|
+
return ""
|
|
179
|
+
|
|
180
|
+
# Replace newlines with break tags and tabs with em spaces.
|
|
181
|
+
text = text.replace("\r\n", "<br>").replace("\n", "<br>").replace("\t", " ")
|
|
182
|
+
|
|
183
|
+
# Format code blocks to maintain indentation.
|
|
184
|
+
text = HtmlFormatter.format_code_blocks(text, "<br>")
|
|
185
|
+
|
|
186
|
+
# Convert any other markdown to HTML.
|
|
187
|
+
text = HtmlFormatter._convert_markdown_by_line(text)
|
|
188
|
+
|
|
189
|
+
return text
|
|
190
|
+
|
|
191
|
+
@staticmethod
|
|
192
|
+
def format_code_blocks(text: str, newline: str = "\n") -> str:
|
|
193
|
+
"""
|
|
194
|
+
Locate each markdown code block in the given text and format it with HTML code and preformatting tags
|
|
195
|
+
to maintain indentation and add language-specific syntax highlighting.
|
|
196
|
+
|
|
197
|
+
:param text: The text to format.
|
|
198
|
+
:param newline: The newline character to expect in the input text.
|
|
199
|
+
:return: The formatted text.
|
|
200
|
+
"""
|
|
201
|
+
# Extract all the code blocks from the text
|
|
202
|
+
code_blocks = HtmlFormatter.extract_code_blocks(text, newline)
|
|
203
|
+
if not code_blocks:
|
|
204
|
+
return text
|
|
205
|
+
|
|
206
|
+
# Iterate through the code blocks, adding them to the text with the <pre><code> </code></pre>
|
|
207
|
+
# so that indentation is preserved.
|
|
208
|
+
current_index = 0
|
|
209
|
+
formatted = []
|
|
210
|
+
for code_block in code_blocks:
|
|
211
|
+
formatted.append(text[current_index:code_block.start_index])
|
|
212
|
+
formatted.append(code_block.to_html())
|
|
213
|
+
current_index = code_block.end_index
|
|
214
|
+
# Append the rest of the text after the last code block.
|
|
215
|
+
formatted.append(text[current_index:])
|
|
216
|
+
return "".join(formatted)
|
|
217
|
+
|
|
218
|
+
@staticmethod
|
|
219
|
+
def sanitize_code_blocks(text: str, newline: str = "\n") -> str:
|
|
220
|
+
"""
|
|
221
|
+
Given the input text, remove all code blocks while leaving all other text unchanged.
|
|
222
|
+
For use in any location where we don't want to display code (because it's scary).
|
|
223
|
+
|
|
224
|
+
:param text: The text to sanitize.
|
|
225
|
+
:param newline: The newline character to expect in the input text.
|
|
226
|
+
:return: The sanitized text.
|
|
227
|
+
"""
|
|
228
|
+
code_blocks = HtmlFormatter.extract_code_blocks(text, newline)
|
|
229
|
+
|
|
230
|
+
if not code_blocks:
|
|
231
|
+
return text
|
|
232
|
+
|
|
233
|
+
current_index = 0
|
|
234
|
+
formatted_text = []
|
|
235
|
+
|
|
236
|
+
for block in code_blocks:
|
|
237
|
+
formatted_text.append(text[current_index: block.start_index])
|
|
238
|
+
current_index = block.end_index
|
|
239
|
+
|
|
240
|
+
formatted_text.append(text[current_index:])
|
|
241
|
+
|
|
242
|
+
return "".join(formatted_text)
|
|
243
|
+
|
|
244
|
+
@staticmethod
|
|
245
|
+
def extract_code_blocks(text: str, newline: str = "\n") -> list[CodeBlock]:
|
|
246
|
+
"""
|
|
247
|
+
Extract all code blocks from the given response.
|
|
248
|
+
|
|
249
|
+
:param text: The text to extract the code blocks from.
|
|
250
|
+
:param newline: The newline character to expect in the input text.
|
|
251
|
+
:return: A list of code blocks.
|
|
252
|
+
"""
|
|
253
|
+
code: list[CodeBlock] = []
|
|
254
|
+
current_index = 0
|
|
255
|
+
while current_index < len(text):
|
|
256
|
+
code_block = HtmlFormatter.next_code_block(text, current_index, newline)
|
|
257
|
+
if code_block is None:
|
|
258
|
+
break
|
|
259
|
+
code.append(code_block)
|
|
260
|
+
current_index = code_block.end_index
|
|
261
|
+
return code
|
|
262
|
+
|
|
263
|
+
@staticmethod
|
|
264
|
+
def next_code_block(text: str, start_index: int, newline: str = "\n") -> CodeBlock | None:
|
|
265
|
+
"""
|
|
266
|
+
Extract the next code block from the given response, starting at the given index.
|
|
267
|
+
|
|
268
|
+
:param text: The text to extract the code block from.
|
|
269
|
+
:param start_index: The index to start searching for the code block at.
|
|
270
|
+
:param newline: The newline character to expect in the input text.
|
|
271
|
+
:return: The extracted code block. Null if no code block is found after the start index.
|
|
272
|
+
"""
|
|
273
|
+
# Find the start of the next code block.
|
|
274
|
+
start_tag = text.find("```", start_index)
|
|
275
|
+
if start_tag == -1:
|
|
276
|
+
return None
|
|
277
|
+
|
|
278
|
+
# Extract the language from the starting tag of the code block.
|
|
279
|
+
first_line = text.find(newline, start_tag)
|
|
280
|
+
if first_line == -1:
|
|
281
|
+
return None
|
|
282
|
+
language = text[start_tag + 3:first_line].strip()
|
|
283
|
+
first_line += len(newline)
|
|
284
|
+
|
|
285
|
+
# Find the end of the code block.
|
|
286
|
+
code: str
|
|
287
|
+
end_tag = text.find("```", first_line)
|
|
288
|
+
# If there is no end to the code block, just return the rest of the text as a code block.
|
|
289
|
+
if end_tag == -1:
|
|
290
|
+
end_tag = len(text)
|
|
291
|
+
code = text[first_line:end_tag]
|
|
292
|
+
else:
|
|
293
|
+
code = text[first_line:end_tag]
|
|
294
|
+
end_tag += 3
|
|
295
|
+
return CodeBlock(code, language, start_tag, end_tag)
|
|
296
|
+
|
|
297
|
+
@staticmethod
|
|
298
|
+
def _convert_markdown_by_line(text: str) -> str:
|
|
299
|
+
"""
|
|
300
|
+
Convert markdown to HTML for each line in the given markdown text. Line breaks are expected to be represented
|
|
301
|
+
by break tags already.
|
|
302
|
+
|
|
303
|
+
:param text: The markdown text to convert.
|
|
304
|
+
:return: The HTML text.
|
|
305
|
+
"""
|
|
306
|
+
html = []
|
|
307
|
+
lines = text.split("<br>")
|
|
308
|
+
|
|
309
|
+
in_unordered_list = False
|
|
310
|
+
in_ordered_list = False
|
|
311
|
+
|
|
312
|
+
for line in lines:
|
|
313
|
+
# Skip code blocks, as these have already been formatted.
|
|
314
|
+
# Also skip empty lines.
|
|
315
|
+
if "</code></pre>" in line or not line.strip():
|
|
316
|
+
html.append(line + "<br>")
|
|
317
|
+
continue
|
|
318
|
+
processed_line = HtmlFormatter._process_line(line.strip())
|
|
319
|
+
|
|
320
|
+
# Handle headings
|
|
321
|
+
if processed_line.startswith("# "):
|
|
322
|
+
HtmlFormatter._close_lists(html, in_unordered_list, in_ordered_list)
|
|
323
|
+
in_unordered_list = False
|
|
324
|
+
in_ordered_list = False
|
|
325
|
+
html.append(HtmlFormatter.header_1(processed_line[2:].strip()) + "<br>")
|
|
326
|
+
elif processed_line.startswith("## "):
|
|
327
|
+
HtmlFormatter._close_lists(html, in_unordered_list, in_ordered_list)
|
|
328
|
+
in_unordered_list = False
|
|
329
|
+
in_ordered_list = False
|
|
330
|
+
html.append(HtmlFormatter.header_2(processed_line[3:].strip()) + "<br>")
|
|
331
|
+
elif processed_line.startswith("### "):
|
|
332
|
+
HtmlFormatter._close_lists(html, in_unordered_list, in_ordered_list)
|
|
333
|
+
in_unordered_list = False
|
|
334
|
+
in_ordered_list = False
|
|
335
|
+
html.append(HtmlFormatter.header_3(processed_line[4:].strip()) + "<br>")
|
|
336
|
+
# Handle unordered lists
|
|
337
|
+
elif processed_line.startswith("* "):
|
|
338
|
+
if not in_unordered_list:
|
|
339
|
+
HtmlFormatter._close_lists(html, False, in_ordered_list) # Close any previous ordered list.
|
|
340
|
+
in_ordered_list = False
|
|
341
|
+
html.append("<ul>")
|
|
342
|
+
in_unordered_list = True
|
|
343
|
+
html.append("<li>" + HtmlFormatter.body(processed_line[2:].strip()) + "</li>")
|
|
344
|
+
# Handle ordered lists
|
|
345
|
+
elif re.match(r"^\d+\. .*", processed_line): # Matches "1. text"
|
|
346
|
+
if not in_ordered_list:
|
|
347
|
+
HtmlFormatter._close_lists(html, in_unordered_list, False) # Close any previous unordered list.
|
|
348
|
+
in_unordered_list = False
|
|
349
|
+
html.append("<ol>")
|
|
350
|
+
in_ordered_list = True
|
|
351
|
+
html.append(
|
|
352
|
+
"<li>" + HtmlFormatter.body(processed_line[processed_line.find('.') + 2:].strip()) + "</li>")
|
|
353
|
+
|
|
354
|
+
# Handle regular paragraphs
|
|
355
|
+
else:
|
|
356
|
+
HtmlFormatter._close_lists(html, in_unordered_list, in_ordered_list)
|
|
357
|
+
in_unordered_list = False
|
|
358
|
+
in_ordered_list = False
|
|
359
|
+
html.append(HtmlFormatter.body(processed_line.strip()) + "<br>")
|
|
360
|
+
|
|
361
|
+
# Close any open lists at the end
|
|
362
|
+
HtmlFormatter._close_lists(html, in_unordered_list, in_ordered_list)
|
|
363
|
+
|
|
364
|
+
return "".join(html)
|
|
365
|
+
|
|
366
|
+
@staticmethod
|
|
367
|
+
def _close_lists(text: list, in_unordered_list: bool, in_ordered_list: bool):
|
|
368
|
+
"""
|
|
369
|
+
Close any open unordered or ordered lists in the given HTML string.
|
|
370
|
+
|
|
371
|
+
:param text: The HTML string to append to.
|
|
372
|
+
:param in_unordered_list: Whether an unordered list is currently open.
|
|
373
|
+
:param in_ordered_list: Whether an ordered list is currently open.
|
|
374
|
+
"""
|
|
375
|
+
if in_unordered_list:
|
|
376
|
+
text.append("</ul>")
|
|
377
|
+
if in_ordered_list:
|
|
378
|
+
text.append("</ol>")
|
|
379
|
+
|
|
380
|
+
@staticmethod
|
|
381
|
+
def _process_line(line: str) -> str:
|
|
382
|
+
"""
|
|
383
|
+
Process a single line of markdown text and convert it to HTML.
|
|
384
|
+
|
|
385
|
+
:param line: The line of markdown text to process.
|
|
386
|
+
:return: The HTML formatted line.
|
|
387
|
+
"""
|
|
388
|
+
# Bold: **text**
|
|
389
|
+
line = re.sub(r"\*\*(.*?)\*\*", r"<strong>\1</strong>", line)
|
|
390
|
+
|
|
391
|
+
# Italic: *text*
|
|
392
|
+
line = re.sub(r"\*(.*?)\*", r"<em>\1</em>", line)
|
|
393
|
+
|
|
394
|
+
# Code: `text`
|
|
395
|
+
line = re.sub(r"`(.*?)`", r"<code>\1</code>", line)
|
|
396
|
+
|
|
397
|
+
return line
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
class CodeBlock:
|
|
401
|
+
"""
|
|
402
|
+
A class representing a code block extracted from a response.
|
|
403
|
+
"""
|
|
404
|
+
def __init__(self, code: str, language: str, start_index: int, end_index: int):
|
|
405
|
+
"""
|
|
406
|
+
:param code: The text of the code block.
|
|
407
|
+
:param language: The language of the code block.
|
|
408
|
+
:param start_index: The index of the first character of the code block in the original response.
|
|
409
|
+
:param end_index: The index after the last character of the code block in the original response.
|
|
410
|
+
"""
|
|
411
|
+
if code is None:
|
|
412
|
+
raise ValueError("Code cannot be None")
|
|
413
|
+
if language is None:
|
|
414
|
+
language = ""
|
|
415
|
+
if start_index < 0 or end_index < 0 or start_index > end_index:
|
|
416
|
+
raise ValueError("Invalid start or end index")
|
|
417
|
+
|
|
418
|
+
# Replace em spaces within code blocks with quadruple spaces and break tags with newlines.
|
|
419
|
+
# Code editors that the code is copy/pasted into might not recognize em spaces as valid indentation,
|
|
420
|
+
# and the library that adds the language-specific syntax highlighting expects newlines instead of break
|
|
421
|
+
# tags.
|
|
422
|
+
if "<br>" in code:
|
|
423
|
+
code = code.replace("<br>", "\n")
|
|
424
|
+
if " " in code:
|
|
425
|
+
code = code.replace(" ", " ")
|
|
426
|
+
# We don't want mixed whitespace, so replace all tabs with quad spaces.
|
|
427
|
+
if "\t" in code:
|
|
428
|
+
code = code.replace("\t", " ")
|
|
429
|
+
|
|
430
|
+
self.code = code
|
|
431
|
+
self.language = language.strip()
|
|
432
|
+
self.start_index = start_index
|
|
433
|
+
self.end_index = end_index
|
|
434
|
+
|
|
435
|
+
def to_html(self) -> str:
|
|
436
|
+
"""
|
|
437
|
+
:return: The HTML representation of this code block.
|
|
438
|
+
"""
|
|
439
|
+
start_tag: str
|
|
440
|
+
if self.language:
|
|
441
|
+
lang_class = f'class="language-{self.language}"'
|
|
442
|
+
start_tag = f"<pre {lang_class}><code {lang_class}>"
|
|
443
|
+
else:
|
|
444
|
+
start_tag = "<pre><code>"
|
|
445
|
+
end_tag = "</code></pre>"
|
|
446
|
+
|
|
447
|
+
return start_tag + self.code + end_tag
|
|
448
|
+
|
|
449
|
+
def to_markdown(self) -> str:
|
|
450
|
+
"""
|
|
451
|
+
:return: The markdown representation of this code block.
|
|
452
|
+
"""
|
|
453
|
+
start_tag = f"```{self.language}\n"
|
|
454
|
+
end_tag = "```" if self.code.endswith("\n") else "\n```"
|
|
455
|
+
|
|
456
|
+
return start_tag + self.code + end_tag
|
|
@@ -10,7 +10,8 @@ class SapioNavigationLinker:
|
|
|
10
10
|
Given a URL to a system's webservice API (example: https://company.exemplareln.com/webservice/api), construct
|
|
11
11
|
URLs for navigation links to various locations in the system.
|
|
12
12
|
"""
|
|
13
|
-
|
|
13
|
+
client_url: str
|
|
14
|
+
webservice_url: str
|
|
14
15
|
|
|
15
16
|
def __init__(self, url: str | SapioUser | SapioWebhookContext):
|
|
16
17
|
"""
|
|
@@ -21,7 +22,14 @@ class SapioNavigationLinker:
|
|
|
21
22
|
url = url.user.url
|
|
22
23
|
elif isinstance(url, SapioUser):
|
|
23
24
|
url = url.url
|
|
24
|
-
self.
|
|
25
|
+
self.webservice_url = url.rstrip("/")
|
|
26
|
+
self.client_url = url.rstrip("/").replace('webservice/api', 'veloxClient')
|
|
27
|
+
|
|
28
|
+
def homepage(self) -> str:
|
|
29
|
+
"""
|
|
30
|
+
:return: A URL for navigating to the system's homepage.
|
|
31
|
+
"""
|
|
32
|
+
return self.client_url + "/#view=homepage"
|
|
25
33
|
|
|
26
34
|
def data_record(self, record_identifier: RecordIdentifier, data_type_name: DataTypeIdentifier | None = None) -> str:
|
|
27
35
|
"""
|
|
@@ -39,7 +47,7 @@ class SapioNavigationLinker:
|
|
|
39
47
|
if not data_type_name:
|
|
40
48
|
raise SapioException("Unable to create a data record link without a data type name. "
|
|
41
49
|
"Only a record ID was provided.")
|
|
42
|
-
return self.
|
|
50
|
+
return self.client_url + f"/#dataType={data_type_name};recordId={record_id};view=dataRecord"
|
|
43
51
|
|
|
44
52
|
def experiment(self, experiment: ExperimentIdentifier) -> str:
|
|
45
53
|
"""
|
|
@@ -47,4 +55,4 @@ class SapioNavigationLinker:
|
|
|
47
55
|
object, experiment protocol, or a notebook ID.
|
|
48
56
|
:return: A URL for navigating to the input experiment.
|
|
49
57
|
"""
|
|
50
|
-
return self.
|
|
58
|
+
return self.client_url + f"/#notebookExperimentId={AliasUtil.to_notebook_id(experiment)};view=eln"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: sapiopycommons
|
|
3
|
-
Version: 2025.6.
|
|
3
|
+
Version: 2025.6.10a560
|
|
4
4
|
Summary: Official Sapio Python API Utilities Package
|
|
5
5
|
Project-URL: Homepage, https://github.com/sapiosciences
|
|
6
6
|
Author-email: Jonathan Steck <jsteck@sapiosciences.com>, Yechen Qiao <yqiao@sapiosciences.com>
|
|
@@ -2,8 +2,8 @@ sapiopycommons/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
|
2
2
|
sapiopycommons/callbacks/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
3
|
sapiopycommons/callbacks/callback_util.py,sha256=lnrwG9hCVmA2GuzxlRrGKw7Eq5hzz7GBZvizgcKkSX0,134996
|
|
4
4
|
sapiopycommons/callbacks/field_builder.py,sha256=rnIP-RJafk3mZlAx1eJ8a0eSW9Ps_L6_WadCmusnENw,38772
|
|
5
|
-
sapiopycommons/chem/IndigoMolecules.py,sha256=
|
|
6
|
-
sapiopycommons/chem/Molecules.py,sha256=
|
|
5
|
+
sapiopycommons/chem/IndigoMolecules.py,sha256=7ucCaRMLu1zfH2uPIvXwRTSdpNcS03O1P9p_O-5B4xQ,5110
|
|
6
|
+
sapiopycommons/chem/Molecules.py,sha256=mVqPn32MPMjF0iZas-5MFkS-upIdoW5OB72KKZmJRJA,12523
|
|
7
7
|
sapiopycommons/chem/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
8
|
sapiopycommons/customreport/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
9
|
sapiopycommons/customreport/auto_pagers.py,sha256=TeyvqesENFd66Ll1gwgP10RgJvgVT1dAOv1cKooCYDk,14860
|
|
@@ -40,8 +40,9 @@ sapiopycommons/general/custom_report_util.py,sha256=NwwmejSQLwSbrndEk1gPyFNYk9GZ
|
|
|
40
40
|
sapiopycommons/general/data_structure_util.py,sha256=fbQR_Fh4Scg67IpFPbQW9wVLw1oxlYxqp4LjBRTpjgU,4702
|
|
41
41
|
sapiopycommons/general/directive_util.py,sha256=7SeQrd2Ye5JHlXZtJZaVGgtaSLdq_Vm9EObuxf44Pz8,3905
|
|
42
42
|
sapiopycommons/general/exceptions.py,sha256=aPlzK1cvxeMU5UsokYlLrIBGltUfJZ7LH8zvLh9DxpI,3233
|
|
43
|
+
sapiopycommons/general/html_formatter.py,sha256=HE3OeGgwOw6x53zGSc4-UzP4-JoOmQIz3pX-DzNVg94,17138
|
|
43
44
|
sapiopycommons/general/popup_util.py,sha256=HKILegU1uCL_6abNlNL0Wn3xgX2JNa_kJeq7e5CZu6Q,31923
|
|
44
|
-
sapiopycommons/general/sapio_links.py,sha256=
|
|
45
|
+
sapiopycommons/general/sapio_links.py,sha256=YkcVKNLrSGoM7tCCXBAsIbIxylctwdcEyhePrRMODe0,2859
|
|
45
46
|
sapiopycommons/general/storage_util.py,sha256=ovmK_jN7v09BoX07XxwShpBUC5WYQOM7dbKV_VeLXJU,8892
|
|
46
47
|
sapiopycommons/general/time_util.py,sha256=jU1urPoZRv6evNucR0-288EyT4PrsDpCr-H1-7BKq9A,12363
|
|
47
48
|
sapiopycommons/multimodal/multimodal.py,sha256=PFaGJPbKvW__tnxb8KkgkJZOKjQdgxF_kGfD5chet1s,6779
|
|
@@ -61,7 +62,7 @@ sapiopycommons/webhook/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3
|
|
|
61
62
|
sapiopycommons/webhook/webhook_context.py,sha256=D793uLsb1691SalaPnBUk3rOSxn_hYLhdvkaIxjNXss,1909
|
|
62
63
|
sapiopycommons/webhook/webhook_handlers.py,sha256=tUVNCw05CDGu1gFDm2g558hX_O203WVm_n__ojjoRRM,39841
|
|
63
64
|
sapiopycommons/webhook/webservice_handlers.py,sha256=tyaYGG1-v_JJrJHZ6cy5mGCxX9z1foLw7pM4MDJlFxs,14297
|
|
64
|
-
sapiopycommons-2025.6.
|
|
65
|
-
sapiopycommons-2025.6.
|
|
66
|
-
sapiopycommons-2025.6.
|
|
67
|
-
sapiopycommons-2025.6.
|
|
65
|
+
sapiopycommons-2025.6.10a560.dist-info/METADATA,sha256=M4tA963cTPGJR8zHZNPz-HaeVhD_adMnBAVB1sli1gw,3143
|
|
66
|
+
sapiopycommons-2025.6.10a560.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
|
|
67
|
+
sapiopycommons-2025.6.10a560.dist-info/licenses/LICENSE,sha256=HyVuytGSiAUQ6ErWBHTqt1iSGHhLmlC8fO7jTCuR8dU,16725
|
|
68
|
+
sapiopycommons-2025.6.10a560.dist-info/RECORD,,
|
|
File without changes
|
{sapiopycommons-2025.6.6a559.dist-info → sapiopycommons-2025.6.10a560.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|