xulbux 1.6.7__py3-none-any.whl → 1.6.9__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 xulbux might be problematic. Click here for more details.
- xulbux/__init__.py +4 -36
- xulbux/_cli_.py +4 -4
- xulbux/xx_code.py +58 -43
- xulbux/xx_color.py +91 -58
- xulbux/xx_console.py +176 -40
- xulbux/xx_data.py +60 -53
- xulbux/xx_env_path.py +3 -19
- xulbux/xx_file.py +18 -30
- xulbux/xx_format_codes.py +60 -17
- xulbux/xx_json.py +105 -50
- xulbux/xx_path.py +71 -21
- xulbux/xx_regex.py +6 -14
- xulbux/xx_string.py +4 -1
- xulbux/xx_system.py +1 -5
- {xulbux-1.6.7.dist-info → xulbux-1.6.9.dist-info}/METADATA +19 -40
- xulbux-1.6.9.dist-info/RECORD +21 -0
- {xulbux-1.6.7.dist-info → xulbux-1.6.9.dist-info}/WHEEL +1 -1
- xulbux-1.6.7.dist-info/RECORD +0 -21
- {xulbux-1.6.7.dist-info → xulbux-1.6.9.dist-info}/entry_points.txt +0 -0
- {xulbux-1.6.7.dist-info → xulbux-1.6.9.dist-info/licenses}/LICENSE +0 -0
- {xulbux-1.6.7.dist-info → xulbux-1.6.9.dist-info}/top_level.txt +0 -0
xulbux/xx_format_codes.py
CHANGED
|
@@ -182,6 +182,8 @@ _COMPILED: dict[str, Pattern] = { # PRECOMPILE REGULAR EXPRESSIONS
|
|
|
182
182
|
+ Regex.brackets("(", ")", is_group=True, strip_spaces=False, ignore_in_strings=False)
|
|
183
183
|
+ r")?"
|
|
184
184
|
),
|
|
185
|
+
"escape_char": _re.compile(r"(\s*)(\/|\\)"),
|
|
186
|
+
"escape_char_cond": _re.compile(r"(\s*\[\s*)(\/|\\)(?!\2+)"),
|
|
185
187
|
"bg?_default": _re.compile(r"(?i)((?:" + _PREFIX_RX["BG"] + r")?)\s*default"),
|
|
186
188
|
"bg_default": _re.compile(r"(?i)" + _PREFIX_RX["BG"] + r"\s*default"),
|
|
187
189
|
"modifier": _re.compile(
|
|
@@ -265,9 +267,11 @@ class FormatCodes:
|
|
|
265
267
|
return color in ANSI.color_map or Color.is_valid_rgba(color) or Color.is_valid_hexa(color)
|
|
266
268
|
|
|
267
269
|
def replace_keys(match: _re.Match) -> str:
|
|
268
|
-
formats = match.group(1)
|
|
269
|
-
|
|
270
|
+
_formats = formats = match.group(1)
|
|
271
|
+
auto_reset_escaped = match.group(2)
|
|
270
272
|
auto_reset_txt = match.group(3)
|
|
273
|
+
if formats_escaped := bool(_COMPILED["escape_char_cond"].match(match.group(0))):
|
|
274
|
+
_formats = formats = _COMPILED["escape_char"].sub(r"\1", formats) # REMOVE / OR \\
|
|
271
275
|
if auto_reset_txt and auto_reset_txt.count("[") > 0 and auto_reset_txt.count("]") > 0:
|
|
272
276
|
auto_reset_txt = FormatCodes.to_ansi(auto_reset_txt, default_color, brightness_steps, False)
|
|
273
277
|
if not formats:
|
|
@@ -279,7 +283,7 @@ class FormatCodes:
|
|
|
279
283
|
r if (r := FormatCodes.__get_replacement(k, default_color, brightness_steps)) != k else f"[{k}]"
|
|
280
284
|
for k in format_keys
|
|
281
285
|
]
|
|
282
|
-
if auto_reset_txt and not
|
|
286
|
+
if auto_reset_txt and not auto_reset_escaped:
|
|
283
287
|
reset_keys = []
|
|
284
288
|
for k in format_keys:
|
|
285
289
|
k_lower = k.lower()
|
|
@@ -308,17 +312,20 @@ class FormatCodes:
|
|
|
308
312
|
else:
|
|
309
313
|
ansi_resets = []
|
|
310
314
|
if not (len(ansi_formats) == 1 and ansi_formats[0].count(f"{ANSI.char}{ANSI.start}") >= 1) and not all(
|
|
311
|
-
f.startswith(f"{ANSI.char}{ANSI.start}") for f in ansi_formats):
|
|
315
|
+
f.startswith(f"{ANSI.char}{ANSI.start}") for f in ansi_formats): # FORMATTING WAS INVALID
|
|
312
316
|
return match.group(0)
|
|
313
|
-
|
|
314
|
-
"
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
317
|
+
elif formats_escaped: # FORMATTING WAS VALID BUT ESCAPED
|
|
318
|
+
return f"[{_formats}]({auto_reset_txt})" if auto_reset_txt else f"[{_formats}]"
|
|
319
|
+
else:
|
|
320
|
+
return (
|
|
321
|
+
"".join(ansi_formats) + (
|
|
322
|
+
f"({FormatCodes.to_ansi(auto_reset_txt, default_color, brightness_steps, False)})"
|
|
323
|
+
if auto_reset_escaped and auto_reset_txt else auto_reset_txt if auto_reset_txt else ""
|
|
324
|
+
) + ("" if auto_reset_escaped else "".join(ansi_resets))
|
|
325
|
+
)
|
|
319
326
|
|
|
320
327
|
string = "\n".join(_COMPILED["formatting"].sub(replace_keys, line) for line in string.split("\n"))
|
|
321
|
-
return (FormatCodes.__get_default_ansi(default_color) if _default_start else "") + string if use_default else string
|
|
328
|
+
return ((FormatCodes.__get_default_ansi(default_color) if _default_start else "") + string) if use_default else string
|
|
322
329
|
|
|
323
330
|
@staticmethod
|
|
324
331
|
def escape_ansi(ansi_string: str) -> str:
|
|
@@ -326,14 +333,50 @@ class FormatCodes:
|
|
|
326
333
|
return ansi_string.replace(ANSI.char, ANSI.escaped_char)
|
|
327
334
|
|
|
328
335
|
@staticmethod
|
|
329
|
-
def remove_ansi(
|
|
330
|
-
|
|
331
|
-
|
|
336
|
+
def remove_ansi(
|
|
337
|
+
ansi_string: str,
|
|
338
|
+
get_removals: bool = False,
|
|
339
|
+
_ignore_linebreaks: bool = False,
|
|
340
|
+
) -> str | tuple[str, tuple[tuple[int, str], ...]]:
|
|
341
|
+
"""Removes all ANSI codes from the string.\n
|
|
342
|
+
--------------------------------------------------------------------------------------------------
|
|
343
|
+
If `get_removals` is true, additionally to the cleaned string, a list of tuples will be returned.
|
|
344
|
+
Each tuple contains the position of the removed ansi code and the removed ansi code.\n
|
|
345
|
+
If `_ignore_linebreaks` is true, linebreaks will be ignored for the removal positions."""
|
|
346
|
+
if get_removals:
|
|
347
|
+
removals = []
|
|
348
|
+
|
|
349
|
+
def replacement(match: _re.Match) -> str:
|
|
350
|
+
start_pos = match.start() - sum(len(removed) for _, removed in removals)
|
|
351
|
+
if removals and removals[-1][0] == start_pos:
|
|
352
|
+
start_pos = removals[-1][0]
|
|
353
|
+
removals.append((start_pos, match.group()))
|
|
354
|
+
return ""
|
|
355
|
+
|
|
356
|
+
clean_string = _COMPILED["ansi_seq"].sub(
|
|
357
|
+
replacement,
|
|
358
|
+
ansi_string.replace("\n", "") if _ignore_linebreaks else ansi_string
|
|
359
|
+
)
|
|
360
|
+
return _COMPILED["ansi_seq"].sub("", ansi_string) if _ignore_linebreaks else clean_string, tuple(removals)
|
|
361
|
+
else:
|
|
362
|
+
return _COMPILED["ansi_seq"].sub("", ansi_string)
|
|
332
363
|
|
|
333
364
|
@staticmethod
|
|
334
|
-
def remove_formatting(
|
|
335
|
-
|
|
336
|
-
|
|
365
|
+
def remove_formatting(
|
|
366
|
+
string: str,
|
|
367
|
+
get_removals: bool = False,
|
|
368
|
+
_ignore_linebreaks: bool = False,
|
|
369
|
+
) -> str | tuple[str, tuple[tuple[int, str], ...]]:
|
|
370
|
+
"""Removes all formatting codes from the string.\n
|
|
371
|
+
---------------------------------------------------------------------------------------------------
|
|
372
|
+
If `get_removals` is true, additionally to the cleaned string, a list of tuples will be returned.
|
|
373
|
+
Each tuple contains the position of the removed formatting code and the removed formatting code.\n
|
|
374
|
+
If `_ignore_linebreaks` is true, linebreaks will be ignored for the removal positions."""
|
|
375
|
+
return FormatCodes.remove_ansi(
|
|
376
|
+
FormatCodes.to_ansi(string),
|
|
377
|
+
get_removals=get_removals,
|
|
378
|
+
_ignore_linebreaks=_ignore_linebreaks,
|
|
379
|
+
)
|
|
337
380
|
|
|
338
381
|
@staticmethod
|
|
339
382
|
def __config_console() -> None:
|
xulbux/xx_json.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
from .xx_data import Data
|
|
2
2
|
from .xx_file import File
|
|
3
|
+
from .xx_path import Path
|
|
3
4
|
|
|
5
|
+
from typing import Any
|
|
4
6
|
import json as _json
|
|
5
|
-
import os as _os
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
class Json:
|
|
@@ -15,16 +16,16 @@ class Json:
|
|
|
15
16
|
return_original: bool = False,
|
|
16
17
|
) -> dict | tuple[dict, dict]:
|
|
17
18
|
"""Read JSON files, ignoring comments.\n
|
|
18
|
-
|
|
19
|
+
------------------------------------------------------------------
|
|
19
20
|
If only `comment_start` is found at the beginning of an item,
|
|
20
21
|
the whole item is counted as a comment and therefore ignored.
|
|
21
22
|
If `comment_start` and `comment_end` are found inside an item,
|
|
22
23
|
the the section from `comment_start` to `comment_end` is ignored.
|
|
23
|
-
If `return_original` is
|
|
24
|
+
If `return_original` is true, the original JSON is returned
|
|
24
25
|
additionally. (returns: `[processed_json, original_json]`)"""
|
|
25
26
|
if not json_file.endswith(".json"):
|
|
26
27
|
json_file += ".json"
|
|
27
|
-
file_path =
|
|
28
|
+
file_path = Path.extend_or_make(json_file, prefer_script_dir=True)
|
|
28
29
|
with open(file_path, "r") as f:
|
|
29
30
|
content = f.read()
|
|
30
31
|
try:
|
|
@@ -38,66 +39,120 @@ class Json:
|
|
|
38
39
|
|
|
39
40
|
@staticmethod
|
|
40
41
|
def create(
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
json_file: str,
|
|
43
|
+
data: dict,
|
|
43
44
|
indent: int = 2,
|
|
44
45
|
compactness: int = 1,
|
|
45
46
|
force: bool = False,
|
|
46
47
|
) -> str:
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
48
|
+
"""Create a nicely formatted JSON file from a dictionary.\n
|
|
49
|
+
----------------------------------------------------------------------
|
|
50
|
+
The `indent` is the amount of spaces to use for indentation.\n
|
|
51
|
+
The `compactness` can be `0`, `1` or `2` and indicates how compact
|
|
52
|
+
the data should be formatted (see `Data.to_str()`).\n
|
|
53
|
+
The function will throw a `FileExistsError` if a file with the same
|
|
54
|
+
name already exists and a `SameContentFileExistsError` if a file with
|
|
55
|
+
the same name and content already exists.
|
|
56
|
+
To always overwrite the file, set the `force` parameter to `True`."""
|
|
57
|
+
if not json_file.endswith(".json"):
|
|
58
|
+
json_file += ".json"
|
|
59
|
+
file_path = Path.extend_or_make(json_file, prefer_script_dir=True)
|
|
60
|
+
File.create(
|
|
61
|
+
file=file_path,
|
|
62
|
+
content=Data.to_str(data, indent, compactness, as_json=True),
|
|
63
|
+
force=force,
|
|
64
|
+
)
|
|
65
|
+
return file_path
|
|
60
66
|
|
|
61
67
|
@staticmethod
|
|
62
68
|
def update(
|
|
63
69
|
json_file: str,
|
|
64
|
-
update_values: str
|
|
70
|
+
update_values: dict[str, Any],
|
|
65
71
|
comment_start: str = ">>",
|
|
66
72
|
comment_end: str = "<<",
|
|
67
|
-
|
|
73
|
+
path_sep: str = "->",
|
|
68
74
|
) -> None:
|
|
69
|
-
"""
|
|
70
|
-
|
|
71
|
-
The
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
the new value at the end of the path.\n
|
|
75
|
-
In this example:
|
|
75
|
+
"""Update single/multiple values inside JSON files, without needing to know the rest of the data.\n
|
|
76
|
+
----------------------------------------------------------------------------------------------------
|
|
77
|
+
The `update_values` parameter is a dictionary, where the keys are the paths to the data to update,
|
|
78
|
+
and the values are the new values to set.\n
|
|
79
|
+
Example: For this JSON data:
|
|
76
80
|
```python
|
|
77
81
|
{
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
+
"healthy": {
|
|
83
|
+
"fruit": ["apples", "bananas", "oranges"],
|
|
84
|
+
"vegetables": ["carrots", "broccoli", "celery"]
|
|
85
|
+
}
|
|
82
86
|
}
|
|
83
87
|
```
|
|
84
|
-
...
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
88
|
+
... the `update_values` dictionary could look like this:
|
|
89
|
+
```python
|
|
90
|
+
{
|
|
91
|
+
# CHANGE VALUE "apples" TO "strawberries"
|
|
92
|
+
"healthy->fruit->0": "strawberries",
|
|
93
|
+
# CHANGE VALUE UNDER KEY "vegetables" TO [1, 2, 3]
|
|
94
|
+
"healthy->vegetables": [1, 2, 3]
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
In this example, if you want to change the value of `"apples"`, you can use `healthy->fruit->apples`
|
|
98
|
+
as the value-path. If you don't know that the first list item is `"apples"`, you can use the items
|
|
99
|
+
list index inside the value-path, so `healthy->fruit->0`.\n
|
|
100
|
+
⇾ If the given value-path doesn't exist, it will be created.\n
|
|
101
|
+
-----------------------------------------------------------------------------------------------------
|
|
89
102
|
If only `comment_start` is found at the beginning of an item, the whole item is counted as a comment
|
|
90
|
-
and therefore ignored. If `comment_start` and `comment_end` are found inside an item, the
|
|
91
|
-
from `comment_start` to `comment_end` is ignored."""
|
|
92
|
-
if isinstance(update_values, str):
|
|
93
|
-
update_values = [update_values]
|
|
94
|
-
valid_entries = [(parts[0].strip(), parts[1]) for update_value in update_values
|
|
95
|
-
if len(parts := update_value.split(str(sep[1]).strip())) == 2]
|
|
96
|
-
value_paths, new_values = zip(*valid_entries) if valid_entries else ([], [])
|
|
103
|
+
and therefore completely ignored. If `comment_start` and `comment_end` are found inside an item, the
|
|
104
|
+
section from `comment_start` to `comment_end` is counted as a comment and ignored."""
|
|
97
105
|
processed_data, data = Json.read(json_file, comment_start, comment_end, return_original=True)
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
106
|
+
|
|
107
|
+
def create_nested_path(data_obj: dict, path_keys: list[str], value: Any) -> dict:
|
|
108
|
+
current = data_obj
|
|
109
|
+
last_idx = len(path_keys) - 1
|
|
110
|
+
for i, key in enumerate(path_keys):
|
|
111
|
+
if i == last_idx:
|
|
112
|
+
if isinstance(current, dict):
|
|
113
|
+
current[key] = value
|
|
114
|
+
elif isinstance(current, list) and key.isdigit():
|
|
115
|
+
idx = int(key)
|
|
116
|
+
while len(current) <= idx:
|
|
117
|
+
current.append(None)
|
|
118
|
+
current[idx] = value
|
|
119
|
+
else:
|
|
120
|
+
raise TypeError(f"Cannot set key '{key}' on {type(current).__name__}")
|
|
121
|
+
else:
|
|
122
|
+
next_key = path_keys[i + 1]
|
|
123
|
+
if isinstance(current, dict):
|
|
124
|
+
if key not in current:
|
|
125
|
+
current[key] = [] if next_key.isdigit() else {}
|
|
126
|
+
current = current[key]
|
|
127
|
+
elif isinstance(current, list) and key.isdigit():
|
|
128
|
+
idx = int(key)
|
|
129
|
+
while len(current) <= idx:
|
|
130
|
+
current.append(None)
|
|
131
|
+
if current[idx] is None:
|
|
132
|
+
current[idx] = [] if next_key.isdigit() else {}
|
|
133
|
+
current = current[idx]
|
|
134
|
+
else:
|
|
135
|
+
raise TypeError(f"Cannot navigate through {type(current).__name__}")
|
|
136
|
+
return data_obj
|
|
137
|
+
|
|
138
|
+
for value_path, new_value in update_values.items():
|
|
139
|
+
try:
|
|
140
|
+
path_id = Data.get_path_id(
|
|
141
|
+
data=processed_data,
|
|
142
|
+
value_paths=value_path,
|
|
143
|
+
path_sep=path_sep,
|
|
144
|
+
ignore_not_found=True,
|
|
145
|
+
)
|
|
146
|
+
if path_id is not None:
|
|
147
|
+
if 'update' not in locals():
|
|
148
|
+
update = {}
|
|
149
|
+
update[path_id] = new_value
|
|
150
|
+
else:
|
|
151
|
+
keys = value_path.split(path_sep)
|
|
152
|
+
data = create_nested_path(data, keys, new_value)
|
|
153
|
+
except Exception:
|
|
154
|
+
keys = value_path.split(path_sep)
|
|
155
|
+
data = create_nested_path(data, keys, new_value)
|
|
156
|
+
if 'update' in locals() and update:
|
|
157
|
+
data = Data.set_value_by_path_id(data, update)
|
|
158
|
+
Json.create(json_file=json_file, data=data, force=True)
|
xulbux/xx_path.py
CHANGED
|
@@ -6,15 +6,18 @@ import sys as _sys
|
|
|
6
6
|
import os as _os
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
class PathNotFoundError(FileNotFoundError):
|
|
10
|
+
...
|
|
11
|
+
|
|
12
12
|
|
|
13
13
|
class _Cwd:
|
|
14
|
+
|
|
14
15
|
def __get__(self, obj, owner=None):
|
|
15
16
|
return _os.getcwd()
|
|
16
17
|
|
|
18
|
+
|
|
17
19
|
class _ScriptDir:
|
|
20
|
+
|
|
18
21
|
def __get__(self, obj, owner=None):
|
|
19
22
|
if getattr(_sys, "frozen", False):
|
|
20
23
|
base_path = _os.path.dirname(_sys.executable)
|
|
@@ -22,13 +25,11 @@ class _ScriptDir:
|
|
|
22
25
|
main_module = _sys.modules["__main__"]
|
|
23
26
|
if hasattr(main_module, "__file__"):
|
|
24
27
|
base_path = _os.path.dirname(_os.path.abspath(main_module.__file__))
|
|
25
|
-
elif (hasattr(main_module, "__spec__") and main_module.__spec__
|
|
26
|
-
and getattr(main_module.__spec__, "origin", None)):
|
|
28
|
+
elif (hasattr(main_module, "__spec__") and main_module.__spec__ and getattr(main_module.__spec__, "origin", None)):
|
|
27
29
|
base_path = _os.path.dirname(_os.path.abspath(main_module.__spec__.origin))
|
|
28
30
|
else:
|
|
29
31
|
raise RuntimeError("Can only get base directory if accessed from a file.")
|
|
30
32
|
return base_path
|
|
31
|
-
# YAPF: enable
|
|
32
33
|
|
|
33
34
|
|
|
34
35
|
class Path:
|
|
@@ -39,9 +40,26 @@ class Path:
|
|
|
39
40
|
"""The path to the directory of the current script."""
|
|
40
41
|
|
|
41
42
|
@staticmethod
|
|
42
|
-
def extend(
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
def extend(
|
|
44
|
+
rel_path: str,
|
|
45
|
+
search_in: str | list[str] = None,
|
|
46
|
+
raise_error: bool = False,
|
|
47
|
+
use_closest_match: bool = False,
|
|
48
|
+
) -> Optional[str]:
|
|
49
|
+
"""Tries to locate and extend a relative path to an absolute path.\n
|
|
50
|
+
--------------------------------------------------------------------------------
|
|
51
|
+
If the `rel_path` couldn't be located in predefined directories, it will be
|
|
52
|
+
searched in the `search_in` directory/s. If the `rel_path` is still not found,
|
|
53
|
+
it returns `None` or raises a `PathNotFoundError` if `raise_error` is true.\n
|
|
54
|
+
--------------------------------------------------------------------------------
|
|
55
|
+
If `use_closest_match` is true, it is possible to have typos in the `search_in`
|
|
56
|
+
path/s and it will still find the file if it is under one of those paths."""
|
|
57
|
+
if rel_path in (None, ""):
|
|
58
|
+
if raise_error:
|
|
59
|
+
raise PathNotFoundError("Path is empty.")
|
|
60
|
+
return None
|
|
61
|
+
elif _os.path.isabs(rel_path):
|
|
62
|
+
return rel_path
|
|
45
63
|
|
|
46
64
|
def get_closest_match(dir: str, part: str) -> Optional[str]:
|
|
47
65
|
try:
|
|
@@ -56,7 +74,7 @@ class Path:
|
|
|
56
74
|
for part in parts:
|
|
57
75
|
if _os.path.isfile(current):
|
|
58
76
|
return current
|
|
59
|
-
closest_match = get_closest_match(current, part) if
|
|
77
|
+
closest_match = get_closest_match(current, part) if use_closest_match else part
|
|
60
78
|
current = _os.path.join(current, closest_match) if closest_match else None
|
|
61
79
|
if current is None:
|
|
62
80
|
return None
|
|
@@ -71,20 +89,20 @@ class Path:
|
|
|
71
89
|
parts[i] = _os.environ[parts[i].upper()]
|
|
72
90
|
return "".join(parts)
|
|
73
91
|
|
|
74
|
-
|
|
75
|
-
if _os.path.isabs(
|
|
76
|
-
drive, rel_path = _os.path.splitdrive(
|
|
92
|
+
rel_path = _os.path.normpath(expand_env_path(rel_path))
|
|
93
|
+
if _os.path.isabs(rel_path):
|
|
94
|
+
drive, rel_path = _os.path.splitdrive(rel_path)
|
|
77
95
|
rel_path = rel_path.lstrip(_os.sep)
|
|
78
|
-
search_dirs = (drive + _os.sep) if drive else
|
|
96
|
+
search_dirs = [(drive + _os.sep) if drive else _os.sep]
|
|
79
97
|
else:
|
|
80
|
-
rel_path =
|
|
98
|
+
rel_path = rel_path.lstrip(_os.sep)
|
|
81
99
|
base_dir = Path.script_dir
|
|
82
|
-
search_dirs =
|
|
100
|
+
search_dirs = [
|
|
83
101
|
_os.getcwd(),
|
|
84
102
|
base_dir,
|
|
85
103
|
_os.path.expanduser("~"),
|
|
86
104
|
_tempfile.gettempdir(),
|
|
87
|
-
|
|
105
|
+
]
|
|
88
106
|
if search_in:
|
|
89
107
|
search_dirs.extend([search_in] if isinstance(search_in, str) else search_in)
|
|
90
108
|
path_parts = rel_path.split(_os.sep)
|
|
@@ -92,19 +110,51 @@ class Path:
|
|
|
92
110
|
full_path = _os.path.join(search_dir, rel_path)
|
|
93
111
|
if _os.path.exists(full_path):
|
|
94
112
|
return full_path
|
|
95
|
-
match = find_path(search_dir, path_parts) if
|
|
113
|
+
match = find_path(search_dir, path_parts) if use_closest_match else None
|
|
96
114
|
if match:
|
|
97
115
|
return match
|
|
98
116
|
if raise_error:
|
|
99
|
-
raise
|
|
100
|
-
return
|
|
117
|
+
raise PathNotFoundError(f"Path '{rel_path}' not found in specified directories.")
|
|
118
|
+
return None
|
|
119
|
+
|
|
120
|
+
@staticmethod
|
|
121
|
+
def extend_or_make(
|
|
122
|
+
rel_path: str,
|
|
123
|
+
search_in: str | list[str] = None,
|
|
124
|
+
prefer_script_dir: bool = True,
|
|
125
|
+
use_closest_match: bool = False,
|
|
126
|
+
) -> str:
|
|
127
|
+
"""Tries to locate and extend a relative path to an absolute path, and if the `rel_path`
|
|
128
|
+
couldn't be located, it generates a path, as if it was located.\n
|
|
129
|
+
-----------------------------------------------------------------------------------------
|
|
130
|
+
If the `rel_path` couldn't be located in predefined directories, it will be searched in
|
|
131
|
+
the `search_in` directory/s. If the `rel_path` is still not found, it will makes a path
|
|
132
|
+
that points to where the `rel_path` would be in the script directory, even though the
|
|
133
|
+
`rel_path` doesn't exist there. If `prefer_script_dir` is false, it will instead make a
|
|
134
|
+
path that points to where the `rel_path` would be in the CWD.\n
|
|
135
|
+
-----------------------------------------------------------------------------------------
|
|
136
|
+
If `use_closest_match` is true, it is possible to have typos in the `search_in` path/s
|
|
137
|
+
and it will still find the file if it is under one of those paths."""
|
|
138
|
+
try:
|
|
139
|
+
return Path.extend(rel_path, search_in, raise_error=True, use_closest_match=use_closest_match)
|
|
140
|
+
except PathNotFoundError:
|
|
141
|
+
normalized_rel_path = _os.path.normpath(rel_path)
|
|
142
|
+
base = Path.script_dir if prefer_script_dir else _os.getcwd()
|
|
143
|
+
return _os.path.join(base, normalized_rel_path)
|
|
101
144
|
|
|
102
145
|
@staticmethod
|
|
103
146
|
def remove(path: str, only_content: bool = False) -> None:
|
|
147
|
+
"""Removes the directory or the directory's content at the specified path.\n
|
|
148
|
+
-----------------------------------------------------------------------------
|
|
149
|
+
Normally it removes the directory and its content, but if `only_content` is
|
|
150
|
+
true, the directory is kept and only its contents are removed."""
|
|
104
151
|
if not _os.path.exists(path):
|
|
105
152
|
return None
|
|
106
153
|
if not only_content:
|
|
107
|
-
|
|
154
|
+
if _os.path.isfile(path) or _os.path.islink(path):
|
|
155
|
+
_os.unlink(path)
|
|
156
|
+
elif _os.path.isdir(path):
|
|
157
|
+
_shutil.rmtree(path)
|
|
108
158
|
elif _os.path.isdir(path):
|
|
109
159
|
for filename in _os.listdir(path):
|
|
110
160
|
file_path = _os.path.join(path, filename)
|
xulbux/xx_regex.py
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Very useful and complicated (generated) regex patterns.
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
1
|
import regex as _rx
|
|
6
2
|
import re as _re
|
|
7
3
|
|
|
@@ -55,11 +51,7 @@ class Regex:
|
|
|
55
51
|
return rf'(?<!["\'])(?:{pattern})(?!["\'])'
|
|
56
52
|
|
|
57
53
|
@staticmethod
|
|
58
|
-
def all_except(
|
|
59
|
-
disallowed_pattern: str,
|
|
60
|
-
ignore_pattern: str = "",
|
|
61
|
-
is_group: bool = False,
|
|
62
|
-
) -> str:
|
|
54
|
+
def all_except(disallowed_pattern: str, ignore_pattern: str = "", is_group: bool = False) -> str:
|
|
63
55
|
"""Matches everything except `disallowed_pattern`, unless the `disallowed_pattern`
|
|
64
56
|
is found inside a string (`'...'` or `"..."`).\n
|
|
65
57
|
------------------------------------------------------------------------------------
|
|
@@ -97,7 +89,7 @@ class Regex:
|
|
|
97
89
|
- `r` 0-255 (int: red)
|
|
98
90
|
- `g` 0-255 (int: green)
|
|
99
91
|
- `b` 0-255 (int: blue)
|
|
100
|
-
- `a` 0-1 (float: opacity)\n
|
|
92
|
+
- `a` 0.0-1.0 (float: opacity)\n
|
|
101
93
|
----------------------------------------------------------------------------
|
|
102
94
|
If the `fix_sep` is set to nothing, any char that is not a letter or number
|
|
103
95
|
can be used to separate the RGBA values, including just a space."""
|
|
@@ -130,7 +122,7 @@ class Regex:
|
|
|
130
122
|
- `h` 0-360 (int: hue)
|
|
131
123
|
- `s` 0-100 (int: saturation)
|
|
132
124
|
- `l` 0-100 (int: lightness)
|
|
133
|
-
- `a` 0-1 (float: opacity)\n
|
|
125
|
+
- `a` 0.0-1.0 (float: opacity)\n
|
|
134
126
|
----------------------------------------------------------------------------
|
|
135
127
|
If the `fix_sep` is set to nothing, any char that is not a letter or number
|
|
136
128
|
can be used to separate the HSLA values, including just a space."""
|
|
@@ -138,9 +130,9 @@ class Regex:
|
|
|
138
130
|
fix_sep = r"[^0-9A-Z]"
|
|
139
131
|
else:
|
|
140
132
|
fix_sep = _re.escape(fix_sep)
|
|
141
|
-
hsl_part = rf"""((?:0*(?:360|3[0-5][0-9]|[12][0-9][0-9]|[1-9]?[0-9])))
|
|
142
|
-
(?:\s*{fix_sep}\s*)((?:0*(?:100|[1-9][0-9]|[0-9])))
|
|
143
|
-
(?:\s*{fix_sep}\s*)((?:0*(?:100|[1-9][0-9]|[0-9])))"""
|
|
133
|
+
hsl_part = rf"""((?:0*(?:360|3[0-5][0-9]|[12][0-9][0-9]|[1-9]?[0-9]))(?:\s*°)?)
|
|
134
|
+
(?:\s*{fix_sep}\s*)((?:0*(?:100|[1-9][0-9]|[0-9]))(?:\s*%)?)
|
|
135
|
+
(?:\s*{fix_sep}\s*)((?:0*(?:100|[1-9][0-9]|[0-9]))(?:\s*%)?)"""
|
|
144
136
|
return (
|
|
145
137
|
rf"""(?ix)
|
|
146
138
|
(?:hsl|hsla)?\s*(?:\(?\s*{hsl_part}
|
xulbux/xx_string.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
from typing import Any
|
|
1
2
|
import json as _json
|
|
2
3
|
import ast as _ast
|
|
3
4
|
import re as _re
|
|
@@ -6,7 +7,7 @@ import re as _re
|
|
|
6
7
|
class String:
|
|
7
8
|
|
|
8
9
|
@staticmethod
|
|
9
|
-
def to_type(string: str) ->
|
|
10
|
+
def to_type(string: str) -> Any:
|
|
10
11
|
"""Will convert a string to the found type, including complex nested structures."""
|
|
11
12
|
string = string.strip()
|
|
12
13
|
try:
|
|
@@ -102,4 +103,6 @@ class String:
|
|
|
102
103
|
@staticmethod
|
|
103
104
|
def split_count(string: str, count: int) -> list[str]:
|
|
104
105
|
"""Will split the string every `count` characters."""
|
|
106
|
+
if count <= 0:
|
|
107
|
+
raise ValueError("Count must be greater than 0.")
|
|
105
108
|
return [string[i:i + count] for i in range(0, len(string), count)]
|
xulbux/xx_system.py
CHANGED
|
@@ -7,11 +7,8 @@ import sys as _sys
|
|
|
7
7
|
import os as _os
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
# YAPF: disable
|
|
11
|
-
class ProcessNotFoundError(Exception):
|
|
12
|
-
pass
|
|
13
|
-
|
|
14
10
|
class _IsElevated:
|
|
11
|
+
|
|
15
12
|
def __get__(self, obj, owner=None):
|
|
16
13
|
try:
|
|
17
14
|
if _os.name == "nt":
|
|
@@ -21,7 +18,6 @@ class _IsElevated:
|
|
|
21
18
|
except Exception:
|
|
22
19
|
pass
|
|
23
20
|
return False
|
|
24
|
-
# YAPF: enable
|
|
25
21
|
|
|
26
22
|
|
|
27
23
|
class System:
|