nai-cli 0.1.0__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.
- nai_cli/__init__.py +3 -0
- nai_cli/__main__.py +5 -0
- nai_cli/api.py +529 -0
- nai_cli/auth.py +240 -0
- nai_cli/batch.py +126 -0
- nai_cli/caches.py +56 -0
- nai_cli/charge.py +231 -0
- nai_cli/cli.py +110 -0
- nai_cli/comfyui.py +194 -0
- nai_cli/commands/__init__.py +61 -0
- nai_cli/commands/account.py +79 -0
- nai_cli/commands/batch.py +142 -0
- nai_cli/commands/config.py +57 -0
- nai_cli/commands/context.py +201 -0
- nai_cli/commands/copies.py +138 -0
- nai_cli/commands/cost.py +70 -0
- nai_cli/commands/generate.py +257 -0
- nai_cli/commands/generation.py +550 -0
- nai_cli/commands/grid.py +99 -0
- nai_cli/commands/image_tools.py +248 -0
- nai_cli/commands/interrogate.py +77 -0
- nai_cli/commands/lint.py +24 -0
- nai_cli/commands/local.py +62 -0
- nai_cli/commands/meta.py +102 -0
- nai_cli/commands/models.py +62 -0
- nai_cli/commands/output.py +165 -0
- nai_cli/commands/redact.py +48 -0
- nai_cli/commands/references.py +87 -0
- nai_cli/commands/spec.py +245 -0
- nai_cli/commands/stats.py +54 -0
- nai_cli/commands/strip.py +15 -0
- nai_cli/commands/tags.py +32 -0
- nai_cli/commands/texts.py +71 -0
- nai_cli/commands/vibe.py +151 -0
- nai_cli/commands/watermark.py +70 -0
- nai_cli/config.py +369 -0
- nai_cli/cost.py +114 -0
- nai_cli/data/random_prompt.json +2536 -0
- nai_cli/decode.py +87 -0
- nai_cli/enhance.py +137 -0
- nai_cli/errors.py +40 -0
- nai_cli/fs.py +209 -0
- nai_cli/grid.py +244 -0
- nai_cli/images.py +250 -0
- nai_cli/import_image.py +235 -0
- nai_cli/inpaint.py +476 -0
- nai_cli/ledger.py +146 -0
- nai_cli/lint.py +302 -0
- nai_cli/local_models.py +135 -0
- nai_cli/meta.py +606 -0
- nai_cli/models.py +308 -0
- nai_cli/naming.py +426 -0
- nai_cli/parser.py +611 -0
- nai_cli/pixel_snap.py +525 -0
- nai_cli/presets.py +276 -0
- nai_cli/prompt.py +457 -0
- nai_cli/random_prompt.py +285 -0
- nai_cli/request.py +621 -0
- nai_cli/rerun.py +177 -0
- nai_cli/resample.py +133 -0
- nai_cli/sam.py +63 -0
- nai_cli/share.py +392 -0
- nai_cli/sheet.py +86 -0
- nai_cli/tagger.py +464 -0
- nai_cli/tags.py +44 -0
- nai_cli/tidy.py +176 -0
- nai_cli/tokenizer.py +313 -0
- nai_cli/values.py +114 -0
- nai_cli/vibe.py +464 -0
- nai_cli-0.1.0.dist-info/METADATA +105 -0
- nai_cli-0.1.0.dist-info/RECORD +75 -0
- nai_cli-0.1.0.dist-info/WHEEL +4 -0
- nai_cli-0.1.0.dist-info/entry_points.txt +2 -0
- nai_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
- nai_cli-0.1.0.dist-info/licenses/THIRD_PARTY_NOTICES.md +56 -0
nai_cli/tokenizer.py
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
"""Token counts with the tokenizer files the official client downloads: Qwen3.5 BPE for V5, T5 Unigram for V4.5."""
|
|
2
|
+
|
|
3
|
+
import itertools
|
|
4
|
+
import re
|
|
5
|
+
import struct
|
|
6
|
+
import unicodedata
|
|
7
|
+
from collections.abc import Callable, Iterator
|
|
8
|
+
from typing import Any, Protocol
|
|
9
|
+
|
|
10
|
+
import regex
|
|
11
|
+
|
|
12
|
+
from nai_cli import decode, values
|
|
13
|
+
from nai_cli import prompt as P
|
|
14
|
+
|
|
15
|
+
FILES = {"qwen": "qwen35_tokenizer.def", "t5": "t5_tokenizer.def"}
|
|
16
|
+
# Part of the official download URL; a new version means new files
|
|
17
|
+
VERSION = 2
|
|
18
|
+
|
|
19
|
+
_JS_SPACE = re.compile("[" + re.escape(P.JS_SPACE) + "]+")
|
|
20
|
+
_T5_BRACKETS = re.compile(r"[\[\]{}]")
|
|
21
|
+
_T5_WEIGHTS = re.compile(r"-?[0-9]*\.?[0-9]*::")
|
|
22
|
+
_LONE_SURROGATE = re.compile(r"[\ud800-\udfff]")
|
|
23
|
+
# What a damaged or foreign file raises while parsing, besides ValueError
|
|
24
|
+
_BAD_FILE = (LookupError, TypeError, AttributeError, ValueError, regex.error)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Tokenizer(Protocol):
|
|
28
|
+
def encode(self, text: str) -> list[int]: ...
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _utf16_units(text: str) -> str:
|
|
32
|
+
"""Astral characters become surrogate pairs, since JavaScript walks strings by UTF-16 code unit."""
|
|
33
|
+
data = text.encode("utf-16-le", "surrogatepass")
|
|
34
|
+
return "".join(map(chr, struct.unpack(f"<{len(data) // 2}H", data)))
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class _Node:
|
|
38
|
+
__slots__ = ("prev", "token_id", "total")
|
|
39
|
+
|
|
40
|
+
def __init__(self, token_id: int, total: float, prev: "_Node | None") -> None:
|
|
41
|
+
self.token_id = token_id
|
|
42
|
+
self.total = total
|
|
43
|
+
self.prev = prev
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _best_after(candidates: list[_Node], token_id: int, score: float) -> _Node:
|
|
47
|
+
best, total = None, 0.0
|
|
48
|
+
for node in candidates:
|
|
49
|
+
value = node.total + score
|
|
50
|
+
# The first of equal scores wins
|
|
51
|
+
if best is None or value > total:
|
|
52
|
+
best, total = node, value
|
|
53
|
+
return _Node(token_id, total, best)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _pre_tokenizer(config: dict[str, Any]) -> Callable[[list[str]], list[str]]:
|
|
57
|
+
kind = config["type"]
|
|
58
|
+
if kind == "Sequence":
|
|
59
|
+
steps = [_pre_tokenizer(c) for c in config["pretokenizers"]]
|
|
60
|
+
|
|
61
|
+
def sequence(words: list[str]) -> list[str]:
|
|
62
|
+
for step in steps:
|
|
63
|
+
words = step(words)
|
|
64
|
+
return words
|
|
65
|
+
|
|
66
|
+
return sequence
|
|
67
|
+
if kind == "WhitespaceSplit":
|
|
68
|
+
return lambda words: [part for word in words for part in _JS_SPACE.split(word)]
|
|
69
|
+
if kind == "Metaspace":
|
|
70
|
+
replacement = config["replacement"]
|
|
71
|
+
add_prefix = config["add_prefix_space"]
|
|
72
|
+
prefix = config.get("str_rep") or replacement
|
|
73
|
+
if not isinstance(replacement, str) or not isinstance(prefix, str):
|
|
74
|
+
raise ValueError("Metaspace replacement must be text")
|
|
75
|
+
|
|
76
|
+
def metaspace(words: list[str]) -> list[str]:
|
|
77
|
+
out = []
|
|
78
|
+
for word in words:
|
|
79
|
+
# JavaScript's replace with a string pattern changes only the first space
|
|
80
|
+
word = word.replace(" ", prefix, 1)
|
|
81
|
+
out.append(prefix + word if add_prefix and not word.startswith(replacement) else word)
|
|
82
|
+
return out
|
|
83
|
+
|
|
84
|
+
return metaspace
|
|
85
|
+
raise ValueError(f"unsupported pre-tokenizer: {kind}")
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class T5:
|
|
89
|
+
"""SentencePiece Unigram: the best-scoring split by Viterbi over every vocabulary piece."""
|
|
90
|
+
|
|
91
|
+
def __init__(self, config: dict[str, Any]) -> None:
|
|
92
|
+
model = config["model"]
|
|
93
|
+
if model["type"] != "Unigram" or config["normalizer"]["type"] != "Precompiled":
|
|
94
|
+
raise ValueError("unsupported T5 tokenizer")
|
|
95
|
+
pieces = [piece for piece, _ in model["vocab"]]
|
|
96
|
+
self.scores = [float(score) for _, score in model["vocab"]]
|
|
97
|
+
self.unk_id: int = model["unk_id"]
|
|
98
|
+
if not values.is_whole(self.unk_id) or not 0 <= self.unk_id < len(pieces):
|
|
99
|
+
raise ValueError(f"unk_id {self.unk_id!r} is not in the vocabulary")
|
|
100
|
+
# The official client starts the minimum at 1e6 and scores unknown pieces 10 below it
|
|
101
|
+
self.unk_score = min([1e6, *self.scores]) - 10
|
|
102
|
+
self.scores[self.unk_id] = self.unk_score
|
|
103
|
+
# Later duplicates win, as in the official Map
|
|
104
|
+
self.ids = {piece: i for i, piece in enumerate(pieces)}
|
|
105
|
+
self.eos_id = self.ids["</s>"]
|
|
106
|
+
self.trie: dict[str, Any] = {}
|
|
107
|
+
for piece in pieces:
|
|
108
|
+
node = self.trie
|
|
109
|
+
for ch in piece:
|
|
110
|
+
node = node.setdefault(ch, {})
|
|
111
|
+
node[""] = True
|
|
112
|
+
self.pre_tokenize = _pre_tokenizer(config["pre_tokenizer"])
|
|
113
|
+
|
|
114
|
+
def encode(self, text: str) -> list[int]:
|
|
115
|
+
if not text:
|
|
116
|
+
return [self.eos_id]
|
|
117
|
+
text = _T5_WEIGHTS.sub("", _T5_BRACKETS.sub("", text))
|
|
118
|
+
ids: list[int] = []
|
|
119
|
+
for word in self.pre_tokenize([text]):
|
|
120
|
+
ids += self._viterbi(_utf16_units(word))
|
|
121
|
+
ids.append(self.eos_id)
|
|
122
|
+
return ids
|
|
123
|
+
|
|
124
|
+
def _pieces(self, units: str, start: int) -> Iterator[tuple[int, int, float]]:
|
|
125
|
+
# Pieces are keyed by code point but matched by code unit, so astral characters never match
|
|
126
|
+
node = self.trie
|
|
127
|
+
single = False
|
|
128
|
+
for end in range(start, len(units)):
|
|
129
|
+
node = node.get(units[end])
|
|
130
|
+
if node is None:
|
|
131
|
+
break
|
|
132
|
+
if "" in node:
|
|
133
|
+
token_id = self.ids[units[start : end + 1]]
|
|
134
|
+
single = single or end == start
|
|
135
|
+
yield end + 1 - start, token_id, self.scores[token_id]
|
|
136
|
+
if not single:
|
|
137
|
+
yield 1, self.unk_id, self.unk_score
|
|
138
|
+
|
|
139
|
+
def _viterbi(self, units: str) -> list[int]:
|
|
140
|
+
ending: list[list[_Node]] = [[] for _ in range(len(units) + 1)]
|
|
141
|
+
ending[0].append(_Node(-1, 0.0, None))
|
|
142
|
+
for start in range(len(units)):
|
|
143
|
+
for length, token_id, score in self._pieces(units, start):
|
|
144
|
+
ending[start + length].append(_best_after(ending[start], token_id, score))
|
|
145
|
+
ids = []
|
|
146
|
+
node = _best_after(ending[len(units)], -1, 0.0).prev
|
|
147
|
+
while node is not None and node.prev is not None:
|
|
148
|
+
ids.append(node.token_id)
|
|
149
|
+
node = node.prev
|
|
150
|
+
ids.reverse()
|
|
151
|
+
return ids
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _byte_chars() -> list[str]:
|
|
155
|
+
"""GPT-2's printable stand-ins for the 256 byte values."""
|
|
156
|
+
printable = [*range(0x21, 0x7F), *range(0xA1, 0xAD), *range(0xAE, 0x100)]
|
|
157
|
+
chars = {b: chr(b) for b in printable}
|
|
158
|
+
for b in range(256):
|
|
159
|
+
if b not in chars:
|
|
160
|
+
chars[b] = chr(256 + len(chars) - len(printable))
|
|
161
|
+
return [chars[b] for b in range(256)]
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
class _SpecialNode:
|
|
165
|
+
__slots__ = ("children", "value")
|
|
166
|
+
|
|
167
|
+
def __init__(self) -> None:
|
|
168
|
+
self.children: dict[str, _SpecialNode] = {}
|
|
169
|
+
self.value: str | None = None
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
class Qwen:
|
|
173
|
+
"""Byte-level BPE after splitting off special tokens and splitting the rest with the official pattern."""
|
|
174
|
+
|
|
175
|
+
def __init__(self, config: dict[str, Any]) -> None:
|
|
176
|
+
self.encoder: dict[str, int] = config["vocab"]
|
|
177
|
+
# The official client switches to a raw byte mode for such vocabularies, which is not ported
|
|
178
|
+
if any(token.startswith("0x") for token in self.encoder):
|
|
179
|
+
raise ValueError("byte-token vocabularies are not supported")
|
|
180
|
+
self.ranks = {(left, right): rank for rank, (left, right) in enumerate(config["merges"])}
|
|
181
|
+
settings = config.get("config") or {}
|
|
182
|
+
self.normalization: str | None = settings.get("normalization")
|
|
183
|
+
self.max_chars: int | None = settings.get("maxEncodeChars")
|
|
184
|
+
if self.normalization not in (None, "", "NFC", "NFD", "NFKC", "NFKD"):
|
|
185
|
+
raise ValueError("unknown Qwen normalization")
|
|
186
|
+
if self.max_chars is not None and (not values.is_whole(self.max_chars) or self.max_chars < 0):
|
|
187
|
+
raise ValueError("maxEncodeChars must be a non-negative integer")
|
|
188
|
+
self.ignore_merges = bool(settings.get("ignoreMerges"))
|
|
189
|
+
self.pattern = regex.compile(settings["splitRegex"])
|
|
190
|
+
self.specials = {t: self.encoder[t] for t in config["specialTokens"] if t in self.encoder}
|
|
191
|
+
# Specials missing from the vocabulary are still split off, then encoded as text
|
|
192
|
+
self.special_root = _SpecialNode()
|
|
193
|
+
for token in config["specialTokens"]:
|
|
194
|
+
node = self.special_root
|
|
195
|
+
for ch in token:
|
|
196
|
+
node = node.children.setdefault(ch, _SpecialNode())
|
|
197
|
+
node.value = token
|
|
198
|
+
self.byte_chars = _byte_chars()
|
|
199
|
+
self.cache: dict[str, list[int]] = {}
|
|
200
|
+
|
|
201
|
+
def encode(self, text: str) -> list[int]:
|
|
202
|
+
if self.normalization:
|
|
203
|
+
text = unicodedata.normalize(self.normalization, text)
|
|
204
|
+
chars = list(text)
|
|
205
|
+
if self.max_chars and len(chars) > self.max_chars:
|
|
206
|
+
chunks = ["".join(chars[i : i + self.max_chars]) for i in range(0, len(chars), self.max_chars)]
|
|
207
|
+
else:
|
|
208
|
+
chunks = [text]
|
|
209
|
+
ids: list[int] = []
|
|
210
|
+
for chunk in chunks:
|
|
211
|
+
for word in self._split(chunk):
|
|
212
|
+
special = self.specials.get(word)
|
|
213
|
+
if special is not None:
|
|
214
|
+
ids.append(special)
|
|
215
|
+
else:
|
|
216
|
+
ids += self._bpe(self._to_unicode(word))
|
|
217
|
+
return ids
|
|
218
|
+
|
|
219
|
+
def _split(self, text: str) -> list[str]:
|
|
220
|
+
"""Longest special-token matches, backtracking after a partial match, scanned as the official client does."""
|
|
221
|
+
words: list[str] = []
|
|
222
|
+
pending = ""
|
|
223
|
+
candidate = ""
|
|
224
|
+
node = self.special_root
|
|
225
|
+
matched: _SpecialNode | None = None
|
|
226
|
+
|
|
227
|
+
def flush() -> None:
|
|
228
|
+
nonlocal pending, candidate, node
|
|
229
|
+
if pending:
|
|
230
|
+
words.extend(m.group(0) for m in self.pattern.finditer(pending))
|
|
231
|
+
pending = ""
|
|
232
|
+
if candidate:
|
|
233
|
+
words.append(candidate)
|
|
234
|
+
candidate = ""
|
|
235
|
+
node = self.special_root
|
|
236
|
+
|
|
237
|
+
i = 0
|
|
238
|
+
while i < len(text):
|
|
239
|
+
ch = text[i]
|
|
240
|
+
child = node.children.get(ch)
|
|
241
|
+
if child is not None:
|
|
242
|
+
node = child
|
|
243
|
+
candidate += ch
|
|
244
|
+
i += 1
|
|
245
|
+
elif not candidate:
|
|
246
|
+
pending += ch
|
|
247
|
+
i += 1
|
|
248
|
+
elif matched is not None and matched.value:
|
|
249
|
+
rest = candidate[len(matched.value) :]
|
|
250
|
+
candidate = matched.value
|
|
251
|
+
i -= len(rest)
|
|
252
|
+
matched = None
|
|
253
|
+
flush()
|
|
254
|
+
else:
|
|
255
|
+
pending += candidate[0]
|
|
256
|
+
i -= len(candidate) - 1
|
|
257
|
+
candidate = ""
|
|
258
|
+
node = self.special_root
|
|
259
|
+
if node.value and candidate == node.value:
|
|
260
|
+
matched = node
|
|
261
|
+
if candidate:
|
|
262
|
+
if matched is not None and matched.value:
|
|
263
|
+
rest = candidate[len(matched.value) :]
|
|
264
|
+
candidate = matched.value
|
|
265
|
+
flush()
|
|
266
|
+
pending = rest
|
|
267
|
+
else:
|
|
268
|
+
pending += candidate
|
|
269
|
+
candidate = ""
|
|
270
|
+
flush()
|
|
271
|
+
return words
|
|
272
|
+
|
|
273
|
+
def _to_unicode(self, word: str) -> str:
|
|
274
|
+
# TextEncoder writes lone surrogates as U+FFFD
|
|
275
|
+
return "".join(self.byte_chars[b] for b in _LONE_SURROGATE.sub("\ufffd", word).encode("utf-8"))
|
|
276
|
+
|
|
277
|
+
def _bpe(self, word: str) -> list[int]:
|
|
278
|
+
if self.ignore_merges and word in self.encoder:
|
|
279
|
+
return [self.encoder[word]]
|
|
280
|
+
cached = self.cache.get(word)
|
|
281
|
+
if cached is not None:
|
|
282
|
+
return cached
|
|
283
|
+
parts = list(word)
|
|
284
|
+
while len(parts) > 1:
|
|
285
|
+
pair = min(itertools.pairwise(parts), key=lambda p: self.ranks.get(p, float("inf")))
|
|
286
|
+
if pair not in self.ranks:
|
|
287
|
+
break
|
|
288
|
+
parts = _merge(parts, *pair)
|
|
289
|
+
ids = [self.encoder[p] for p in parts if p in self.encoder]
|
|
290
|
+
self.cache[word] = ids
|
|
291
|
+
return ids
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def _merge(parts: list[str], left: str, right: str) -> list[str]:
|
|
295
|
+
merged = []
|
|
296
|
+
i = 0
|
|
297
|
+
while i < len(parts):
|
|
298
|
+
if parts[i] == left and i + 1 < len(parts) and parts[i + 1] == right:
|
|
299
|
+
merged.append(left + right)
|
|
300
|
+
i += 2
|
|
301
|
+
else:
|
|
302
|
+
merged.append(parts[i])
|
|
303
|
+
i += 1
|
|
304
|
+
return merged
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def parse(kind: str, raw: bytes) -> Tokenizer:
|
|
308
|
+
"""Raises ValueError for anything that is not a usable tokenizer file."""
|
|
309
|
+
try:
|
|
310
|
+
config = values.parse_json(decode.inflate(raw, decode.RAW))
|
|
311
|
+
return T5(config) if kind == "t5" else Qwen(config)
|
|
312
|
+
except _BAD_FILE as e:
|
|
313
|
+
raise ValueError(f"not a {kind} tokenizer file: {type(e).__name__}: {e}") from e
|
nai_cli/values.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"""Values as files and options give them: strict JSON, numbers that are not booleans, paths and option text."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import math
|
|
5
|
+
import re
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any, TypeGuard
|
|
9
|
+
|
|
10
|
+
_DIGITS = re.compile(r"[0-9]+")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def parse_json(text: str | bytes) -> Any:
|
|
14
|
+
"""json.loads for what files and replies hold; NaN, Infinity and numbers no float can hold are refused as
|
|
15
|
+
ValueError."""
|
|
16
|
+
try:
|
|
17
|
+
return json.loads(text, parse_constant=_not_json, parse_float=_finite_float, parse_int=_float_sized_int)
|
|
18
|
+
except RecursionError as e:
|
|
19
|
+
raise ValueError("JSON is nested too deeply") from e
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _not_json(name: str) -> None:
|
|
23
|
+
raise ValueError(f"{name} is not a JSON value")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _finite_float(text: str) -> float:
|
|
27
|
+
number = float(text)
|
|
28
|
+
if not math.isfinite(number):
|
|
29
|
+
raise ValueError(f"{text} is too large for a number")
|
|
30
|
+
return number
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _float_sized_int(text: str) -> int:
|
|
34
|
+
number = int(text)
|
|
35
|
+
if abs(number) > sys.float_info.max:
|
|
36
|
+
raise ValueError(f"{text[:20]}... is too large for a number")
|
|
37
|
+
return number
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def to_json(value: Any, indent: int | None = None) -> str:
|
|
41
|
+
"""Strict JSON text that always encodes as UTF-8: NaN and Infinity are refused as ValueError, and lone
|
|
42
|
+
surrogates, which POSIX file names and JSON escapes can carry, are written as \\u escapes."""
|
|
43
|
+
text = json.dumps(value, ensure_ascii=False, indent=indent, allow_nan=False)
|
|
44
|
+
return text.encode("utf-8", "backslashreplace").decode("utf-8")
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# Above this a float has gaps between whole numbers, so JavaScript writes it as it is
|
|
48
|
+
_JS_SAFE_INTEGER = 2**53
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def as_js(value: Any) -> Any:
|
|
52
|
+
"""value with every whole float made an int, as JSON.stringify writes 4.0 as 4, so a request body is the
|
|
53
|
+
official client's byte for byte."""
|
|
54
|
+
if isinstance(value, float) and value.is_integer() and abs(value) < _JS_SAFE_INTEGER:
|
|
55
|
+
return int(value)
|
|
56
|
+
if isinstance(value, dict):
|
|
57
|
+
return {k: as_js(v) for k, v in value.items()}
|
|
58
|
+
if isinstance(value, list | tuple):
|
|
59
|
+
return [as_js(v) for v in value]
|
|
60
|
+
return value
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def path(text: str | Path) -> Path:
|
|
64
|
+
"""A path as typed or written in the config; ~ is the home folder on every platform, not only in a shell."""
|
|
65
|
+
try:
|
|
66
|
+
return Path(text).expanduser()
|
|
67
|
+
except RuntimeError as e:
|
|
68
|
+
# No home folder is known, as in some containers
|
|
69
|
+
raise ValueError(f"{text}: {e}") from None
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
# Bundles held in an image keep both suffixes, so a number goes before them
|
|
73
|
+
_DOUBLE_SUFFIXES = (".naiv4vibebundle.png",)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def split_name(path: Path) -> tuple[str, str]:
|
|
77
|
+
"""The name before its suffix, and the suffix as written."""
|
|
78
|
+
lower = path.name.lower()
|
|
79
|
+
suffix = next((s for s in _DOUBLE_SUFFIXES if lower.endswith(s)), path.suffix)
|
|
80
|
+
stem = path.name[: len(path.name) - len(suffix)]
|
|
81
|
+
return stem, path.name[len(stem) :]
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def is_whole(value: Any) -> TypeGuard[int]:
|
|
85
|
+
# bool is a subclass of int
|
|
86
|
+
return isinstance(value, int) and not isinstance(value, bool)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def is_digits(text: str) -> bool:
|
|
90
|
+
"""ASCII digits only; str.isdigit also takes ² and other digits int() refuses."""
|
|
91
|
+
return _DIGITS.fullmatch(text) is not None
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def option(flag: str, text: str) -> str:
|
|
95
|
+
"""flag=text, which nai reads back as text: after =, since it may start with -, and a leading @ doubled, since
|
|
96
|
+
@PATH reads a file."""
|
|
97
|
+
return f"{flag}={'@' + text if text.startswith('@') else text}"
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def unsendable(text: str) -> str | None:
|
|
101
|
+
"""Why no request can carry text, if none can: a NUL, or half of a surrogate pair, which POSIX arguments that
|
|
102
|
+
are not UTF-8 and JSON escapes can hold."""
|
|
103
|
+
if "\x00" in text:
|
|
104
|
+
return "a NUL character"
|
|
105
|
+
if any("\ud800" <= ch <= "\udfff" for ch in text):
|
|
106
|
+
return "text that is not valid UTF-8"
|
|
107
|
+
return None
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def is_number(value: Any) -> TypeGuard[int | float]:
|
|
111
|
+
"""Finite and within float range too, since Python reads NaN, Infinity and integers of any size."""
|
|
112
|
+
if isinstance(value, bool) or not isinstance(value, int | float):
|
|
113
|
+
return False
|
|
114
|
+
return abs(value) <= sys.float_info.max if isinstance(value, int) else math.isfinite(value)
|