xulbux 1.5.7__py3-none-any.whl → 1.5.8__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/xx_env_path.py ADDED
@@ -0,0 +1,113 @@
1
+ """
2
+ Functions for modifying and checking the systems environment-variables:
3
+ - `EnvPath.paths()`
4
+ - `EnvPath.has_path()`
5
+ - `EnvPath.add_path()`
6
+ - `EnvPath.remove_path()`
7
+ """
8
+
9
+ from .xx_path import Path
10
+
11
+ import os as _os
12
+ import sys as _sys
13
+
14
+
15
+ class EnvPath:
16
+
17
+ @staticmethod
18
+ def paths(as_list: bool = False) -> str | list:
19
+ """Get the PATH environment variable."""
20
+ paths = _os.environ.get("PATH", "")
21
+ return paths.split(_os.pathsep) if as_list else paths
22
+
23
+ @staticmethod
24
+ def has_path(path: str = None, cwd: bool = False, base_dir: bool = False) -> bool:
25
+ """Check if a path is present in the PATH environment variable."""
26
+ if cwd:
27
+ path = _os.getcwd()
28
+ elif base_dir:
29
+ path = Path.get(base_dir=True)
30
+ elif path is None:
31
+ raise ValueError("A path must be provided or either 'cwd' or 'base_dir' must be True.")
32
+ paths = EnvPath.paths(as_list=True)
33
+ return _os.path.normpath(path) in [_os.path.normpath(p) for p in paths]
34
+
35
+ @staticmethod
36
+ def add_path(
37
+ path: str = None,
38
+ cwd: bool = False,
39
+ base_dir: bool = False,
40
+ ) -> None:
41
+ """Add a path to the PATH environment variable."""
42
+ path = EnvPath.__get(path, cwd, base_dir)
43
+ if not EnvPath.has_path(path):
44
+ EnvPath.__persistent(path, add=True)
45
+
46
+ @staticmethod
47
+ def remove_path(
48
+ path: str = None,
49
+ cwd: bool = False,
50
+ base_dir: bool = False,
51
+ ) -> None:
52
+ """Remove a path from the PATH environment variable."""
53
+ path = EnvPath.__get(path, cwd, base_dir)
54
+ if EnvPath.has_path(path):
55
+ EnvPath.__persistent(path, remove=True)
56
+
57
+ @staticmethod
58
+ def __get(
59
+ path: str = None,
60
+ cwd: bool = False,
61
+ base_dir: bool = False,
62
+ ) -> list:
63
+ """Get and/or normalize the paths.<br>
64
+ Raise an error if no path is provided and<br>
65
+ neither `cwd` or `base_dir` is `True`."""
66
+ if cwd:
67
+ path = _os.getcwd()
68
+ elif base_dir:
69
+ path = Path.get(base_dir=True)
70
+ elif path is None:
71
+ raise ValueError("A path must be provided or either 'cwd' or 'base_dir' must be True.")
72
+ return _os.path.normpath(path)
73
+
74
+ @staticmethod
75
+ def __persistent(path: str, add: bool = False, remove: bool = False) -> None:
76
+ """Add or remove a path from PATH persistently across sessions as well as the current session."""
77
+ if add == remove:
78
+ raise ValueError("Either add or remove must be True, but not both.")
79
+ current_paths = EnvPath.paths(as_list=True)
80
+ path = _os.path.normpath(path)
81
+ if remove:
82
+ current_paths = [p for p in current_paths if _os.path.normpath(p) != _os.path.normpath(path)]
83
+ elif add:
84
+ current_paths.append(path)
85
+ _os.environ["PATH"] = new_path = _os.pathsep.join(sorted(set(filter(bool, current_paths))))
86
+ if _sys.platform == "win32": # Windows
87
+ try:
88
+ import winreg as _winreg
89
+
90
+ key = _winreg.OpenKey(
91
+ _winreg.HKEY_CURRENT_USER,
92
+ "Environment",
93
+ 0,
94
+ _winreg.KEY_ALL_ACCESS,
95
+ )
96
+ _winreg.SetValueEx(key, "PATH", 0, _winreg.REG_EXPAND_SZ, new_path)
97
+ _winreg.CloseKey(key)
98
+ except ImportError:
99
+ print("Warning: Unable to make persistent changes on Windows.")
100
+ else: # UNIX-like (Linux/macOS)
101
+ shell_rc_file = _os.path.expanduser(
102
+ "~/.bashrc" if _os.path.exists(_os.path.expanduser("~/.bashrc")) else "~/.zshrc"
103
+ )
104
+ with open(shell_rc_file, "r+") as f:
105
+ content = f.read()
106
+ f.seek(0)
107
+ if remove:
108
+ new_content = [line for line in content.splitlines() if not line.endswith(f':{path}"')]
109
+ f.write("\n".join(new_content))
110
+ else:
111
+ f.write(f'{content.rstrip()}\n# Added by XulbuX\nexport PATH="{new_path}"\n')
112
+ f.truncate()
113
+ _os.system(f"source {shell_rc_file}")
xulbux/xx_file.py CHANGED
@@ -1,5 +1,5 @@
1
- from .xx_string import *
2
- from .xx_path import *
1
+ from .xx_string import String
2
+ from .xx_path import Path
3
3
 
4
4
  import os as _os
5
5
 
xulbux/xx_format_codes.py CHANGED
@@ -68,10 +68,9 @@ Per default, you can also use `+` and `-` to get lighter and darker `default_col
68
68
  """
69
69
 
70
70
  from ._consts_ import ANSI
71
- from .xx_string import *
72
- from .xx_regex import *
71
+ from .xx_string import String
72
+ from .xx_regex import Regex
73
73
  from .xx_color import *
74
- from .xx_data import *
75
74
 
76
75
  from functools import lru_cache
77
76
  import ctypes as _ctypes
xulbux/xx_json.py CHANGED
@@ -1,5 +1,5 @@
1
- from .xx_data import *
2
- from .xx_file import *
1
+ from .xx_data import Data
2
+ from .xx_file import File
3
3
 
4
4
  import json as _json
5
5
  import os as _os
xulbux/xx_regex.py CHANGED
@@ -69,7 +69,7 @@ class Regex:
69
69
  return r"(?<=\b)(" + (func_name if func_name else r"[\w_]+") + r")\s*" + Regex.brackets("(", ")", is_group=True)
70
70
 
71
71
  @staticmethod
72
- def rgba_str(fix_sep: str = ",", allow_alpha: bool = False) -> str:
72
+ def rgba_str(fix_sep: str = ",", allow_alpha: bool = True) -> str:
73
73
  """Matches an RGBA color inside a string.\n
74
74
  --------------------------------------------------------------------------------
75
75
  The RGBA color can be in the formats (for `fix_sep = ','`):<br>
@@ -87,7 +87,7 @@ class Regex:
87
87
  `a` 0-1 (float: opacity)\n
88
88
  --------------------------------------------------------------------------------
89
89
  If the `fix_sep` is set to nothing, any char that is not a letter or number<br>
90
- can be used to separate the RGB values, including just a space."""
90
+ can be used to separate the RGBA values, including just a space."""
91
91
  if fix_sep in (None, ""):
92
92
  fix_sep = r"[^0-9A-Z]"
93
93
  else:
@@ -105,7 +105,7 @@ class Regex:
105
105
  )
106
106
 
107
107
  @staticmethod
108
- def hsla_str(fix_sep: str = ",", allow_alpha: bool = False) -> str:
108
+ def hsla_str(fix_sep: str = ",", allow_alpha: bool = True) -> str:
109
109
  """Matches a HSLA color inside a string.\n
110
110
  --------------------------------------------------------------------------------
111
111
  The HSLA color can be in the formats (for `fix_sep = ','`):<br>
@@ -123,7 +123,7 @@ class Regex:
123
123
  `a` 0-1 (float: opacity)\n
124
124
  --------------------------------------------------------------------------------
125
125
  If the `fix_sep` is set to nothing, any char that is not a letter or number<br>
126
- can be used to separate the HSL values, including just a space."""
126
+ can be used to separate the HSLA values, including just a space."""
127
127
  if fix_sep in (None, ""):
128
128
  fix_sep = r"[^0-9A-Z]"
129
129
  else:
@@ -139,3 +139,21 @@ class Regex:
139
139
  if allow_alpha
140
140
  else rf"(?ix)(?:hsl|hsla)?\s*(?:\(?\s*{hsl_part}\s*\)?)"
141
141
  )
142
+
143
+ @staticmethod
144
+ def hexa_str(allow_alpha: bool = True) -> str:
145
+ """Matches a HEXA color inside a string.\n
146
+ --------------------------------------------------------------------------
147
+ The HEXA color can be in the formats (prefix `#`, `0x` or no prefix):<br>
148
+ `RGB`<br>
149
+ `RGBA` (if `allow_alpha=True`)<br>
150
+ `RRGGBB`<br>
151
+ `RRGGBBAA` (if `allow_alpha=True`)\n
152
+ --------------------------------------------------------------------------
153
+ ### Valid ranges:<br>
154
+ each channel from 0-9 and A-F (*case insensitive*)"""
155
+ return (
156
+ r"(?i)^(?:#|0x)?[0-9A-F]{8}|[0-9A-F]{6}|[0-9A-F]{4}|[0-9A-F]{3}$"
157
+ if allow_alpha
158
+ else r"(?i)^(?:#|0x)?[0-9A-F]{6}|[0-9A-F]{3}$"
159
+ )
xulbux/xx_string.py CHANGED
@@ -55,6 +55,35 @@ class String:
55
55
  .replace("\u200A", " ")
56
56
  )
57
57
 
58
+ @staticmethod
59
+ def escape(string: str, str_quotes: str = '"') -> str:
60
+ """Escapes the special characters and quotes inside a string.\n
61
+ ----------------------------------------------------------------------------
62
+ `str_quotes` can be either `"` or `'` and should match the quotes,<br>
63
+ the string will be put inside of. So if your string will be `"string"`,<br>
64
+ you should pass `"` to the parameter `str_quotes`.<br>
65
+ That way, if the string includes the same quotes, they will be escaped."""
66
+ string = (
67
+ string.replace("\\", r"\\")
68
+ .replace("\n", r"\n")
69
+ .replace("\r", r"\r")
70
+ .replace("\t", r"\t")
71
+ .replace("\b", r"\b")
72
+ .replace("\f", r"\f")
73
+ .replace("\a", r"\a")
74
+ )
75
+ if str_quotes == '"':
76
+ string = string.replace(r"\\'", "'").replace(r'"', r"\"")
77
+ elif str_quotes == "'":
78
+ string = string.replace(r'\\"', '"').replace(r"'", r"\'")
79
+ return string
80
+
81
+ @staticmethod
82
+ def is_empty(string: str, spaces_are_empty: bool = False):
83
+ """Returns `True` if the string is empty and `False` otherwise.<br>
84
+ If `spaces_are_empty` is true, it will also return `True` if the string is only spaces."""
85
+ return (string in (None, "")) or (spaces_are_empty and isinstance(string, str) and not string.strip())
86
+
58
87
  @staticmethod
59
88
  def single_char_repeats(string: str, char: str) -> int | bool:
60
89
  """If the string consists of only the same `char`, it returns the number of times it is present.<br>
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
- Name: XulbuX
3
- Version: 1.5.7
2
+ Name: xulbux
3
+ Version: 1.5.8
4
4
  Summary: A library which includes a lot of really helpful functions.
5
5
  Project-URL: Homepage, https://github.com/XulbuX-dev/PythonLibraryXulbuX
6
6
  Project-URL: Bug Reports, https://github.com/XulbuX-dev/PythonLibraryXulbuX/issues
@@ -62,7 +62,7 @@ For precise information about the library, see the library's [Wiki page](https:/
62
62
 
63
63
  On all operating systems, run the following command:
64
64
  ```bash
65
- pip install XulbuX
65
+ pip install xulbux
66
66
  ```
67
67
 
68
68
 
@@ -70,9 +70,9 @@ pip install XulbuX
70
70
 
71
71
  ```python
72
72
  # GENERAL LIBRARY
73
- import XulbuX as xx
73
+ import xulbux as xx
74
74
  # CUSTOM TYPES
75
- from XulbuX import rgba, hsla, hexa
75
+ from xulbux import rgba, hsla, hexa
76
76
  ```
77
77
  The library **$\color{#8085FF}\textsf{XulbuX}$** (*below used as* `xx` *with above imported types*) contains the following modules:
78
78
  ```python
@@ -84,12 +84,12 @@ The library **$\color{#8085FF}\textsf{XulbuX}$** (*below used as* `xx` *with abo
84
84
  • FILE OPERATIONS xx.File
85
85
  • JSON FILE OPERATIONS xx.Json
86
86
  • SYSTEM ACTIONS xx.System
87
- • MANAGE ENVIRONMENT VARS xx.EnvVars
88
- CMD LOG AND ACTIONS xx.Cmd
89
- • PRETTY PRINTING xx.FormatCodes
90
- COLOR OPERATIONS xx.Color
87
+ • MANAGE THE ENV PATH VAR xx.EnvPath
88
+ CONSOLE LOG AND ACTIONS xx.Console
89
+ EASY PRETTY PRINTING xx.FormatCodes
90
+ WORKING WITH COLORS xx.Color
91
91
  • DATA OPERATIONS xx.Data
92
- STR OPERATIONS xx.String
92
+ STRING OPERATIONS xx.String
93
93
  • CODE STRING OPERATIONS xx.Code
94
94
  • REGEX PATTERN TEMPLATES xx.Regex
95
95
  ```
@@ -0,0 +1,20 @@
1
+ xulbux/__init__.py,sha256=yhdvShCcbcLeBfKckjurNcrkCHIj1wWEezgtQPbytGI,1672
2
+ xulbux/_cli_.py,sha256=zCa4dROOwJ3TPIH8ed1gezRj309ljGOAapICRqq4OKs,3464
3
+ xulbux/_consts_.py,sha256=HqBCzFYSk9LS2TrxPCQBQcEjEB8McLodH4ZGZ8yP3Ec,4858
4
+ xulbux/xx_code.py,sha256=v3fhHelqxFXtp2RI7sY7mXusRKdiyOEzy_rmrC3N-Os,5158
5
+ xulbux/xx_color.py,sha256=Ib_jVAYHl3uVUvWUv1jE_QHScNvIPgm9KxEl2xSJfQo,45019
6
+ xulbux/xx_console.py,sha256=Xzl73_LTyt9AW09KAV9tsGdEdiltxPJwJq9nZChWrUQ,15482
7
+ xulbux/xx_data.py,sha256=cwjJeQHwf-b9yE6UTI0DO7US-hWtgTmIdvwKH5KKtmI,25969
8
+ xulbux/xx_env_path.py,sha256=ITdJ4DKh1zAvZrV5kph1z0jUDBhwEtOP4u1OKW0hMts,4309
9
+ xulbux/xx_file.py,sha256=Zhlx7TZ0u2wcl_7khgpMCoqlrcfsbiOh03sj4nsuPaQ,2616
10
+ xulbux/xx_format_codes.py,sha256=hRQ4Vecw2TTa-g5pKshLP-pep_Da7iy38o879McCrOc,13547
11
+ xulbux/xx_json.py,sha256=Sz-UicwU2vdTud4itMV-ljqePzpNByu5WgnX4lEUVRc,5056
12
+ xulbux/xx_path.py,sha256=KB1HGYCxFB3_T97RVACajy3-QiksbviZVIW9-iQcr4s,4558
13
+ xulbux/xx_regex.py,sha256=ce-K62uk1nvayOvLAtMQSD-IwKiVu2IATj0s1C1u6pM,7956
14
+ xulbux/xx_string.py,sha256=cQOvektdBKKe4CuBw6VOoxTElayghk0SQPuLdMILn9A,7650
15
+ xulbux/xx_system.py,sha256=yehO57ggWDRJbeFoTE1VisW4QrbkqZzAyjKoL3xHx9k,3935
16
+ xulbux-1.5.8.dist-info/METADATA,sha256=B95tctbGYaeyy-_nvGssLk08OJNWaFUF1JeTD5_wi2A,4340
17
+ xulbux-1.5.8.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
18
+ xulbux-1.5.8.dist-info/entry_points.txt,sha256=a3womfLIMZKnOFiyy-xnVb4g2qkZsHR5FbKKkljcGns,94
19
+ xulbux-1.5.8.dist-info/licenses/LICENSE,sha256=6NflEcvzFEe8_JFVNCPVwZBwBhlLLd4vqQi8WiX_Xk4,1084
20
+ xulbux-1.5.8.dist-info/RECORD,,
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ xulbux-help = xulbux._cli_:help_command
3
+ xx-help = xulbux._cli_:help_command
xulbux/xx_env_vars.py DELETED
@@ -1,73 +0,0 @@
1
- """
2
- Functions for modifying and checking the systems environment-variables:
3
- - `EnvVars.get_paths()`
4
- - `EnvVars.has_path()`
5
- - `EnvVars.add_path()`
6
- """
7
-
8
- from .xx_data import *
9
- from .xx_path import *
10
-
11
- import os as _os
12
-
13
-
14
- class EnvVars:
15
-
16
- @staticmethod
17
- def get_paths(as_list: bool = False) -> str | list:
18
- paths = _os.environ.get("PATH")
19
- return paths.split(_os.pathsep) if as_list else paths
20
-
21
- @staticmethod
22
- def has_path(path: str = None, cwd: bool = False, base_dir: bool = False) -> bool:
23
- if cwd:
24
- path = _os.getcwd()
25
- if base_dir:
26
- path = Path.get(base_dir=True)
27
- paths = EnvVars.get_paths()
28
- return path in paths
29
-
30
- @staticmethod
31
- def __add_sort_paths(add_path: str, current_paths: str) -> str:
32
- final_paths = Data.remove_empty_items(Data.remove_duplicates(f"{add_path};{current_paths}".split(_os.pathsep)))
33
- final_paths.sort()
34
- return f"{_os.pathsep.join(final_paths)};"
35
-
36
- @staticmethod
37
- def add_path(
38
- add_path: str = None,
39
- cwd: bool = False,
40
- base_dir: bool = False,
41
- persistent: bool = True,
42
- ) -> None:
43
- if cwd:
44
- add_path = _os.getcwd()
45
- if base_dir:
46
- add_path = Path.get(base_dir=True)
47
- if not EnvVars.has_path(add_path):
48
- final_paths = EnvVars.__add_sort_paths(add_path, EnvVars.get_paths())
49
- _os.environ["PATH"] = final_paths
50
- if persistent:
51
- if _os.name == "nt": # Windows
52
- try:
53
- import winreg as _winreg
54
-
55
- key = _winreg.OpenKey(
56
- _winreg.HKEY_CURRENT_USER,
57
- "Environment",
58
- 0,
59
- _winreg.KEY_ALL_ACCESS,
60
- )
61
- _winreg.SetValueEx(key, "PATH", 0, _winreg.REG_EXPAND_SZ, final_paths)
62
- _winreg.CloseKey(key)
63
- except ImportError:
64
- raise ImportError("Unable to make persistent changes on Windows.")
65
- else: # UNIX-LIKE (Linux/macOS)
66
- shell_rc_file = _os.path.expanduser(
67
- "~/.bashrc" if _os.path.exists(_os.path.expanduser("~/.bashrc")) else "~/.zshrc"
68
- )
69
- with open(shell_rc_file, "a") as f:
70
- f.write(f'\n# Added by XulbuX\nexport PATH="$PATH:{add_path}"\n')
71
- _os.system(f"source {shell_rc_file}")
72
- else:
73
- raise ValueError(f"{add_path} is already in PATH.")
@@ -1,20 +0,0 @@
1
- xulbux/__init__.py,sha256=h6v5Ld_jy1LeTD8hZLfOZUIljUScCRu1FnHRJP1Dp54,1660
2
- xulbux/_cli_.py,sha256=W6qLGAY5F17bVDCkgtU-XOwqe_UyXBxBonW8e0Gso74,3437
3
- xulbux/_consts_.py,sha256=HqBCzFYSk9LS2TrxPCQBQcEjEB8McLodH4ZGZ8yP3Ec,4858
4
- xulbux/xx_cmd.py,sha256=K8U96QSAbLgDG65zIQdniSFtJUqUgKoaHhxgyaq5qBQ,15315
5
- xulbux/xx_code.py,sha256=PDZi1HNzGfEfoI646N5FQOAL3nZ7cE8eETmW6pl-szA,5146
6
- xulbux/xx_color.py,sha256=sUZKZdx6dMNL48G5o_SHYMnYPx3sbGoGiu6NpeJkPSw,44053
7
- xulbux/xx_data.py,sha256=2lYt0kiZxgqqA0e8u8k6VQr8juVzrZRAgio4ehUteSc,27876
8
- xulbux/xx_env_vars.py,sha256=nJQtddsfDo2HDG1vzmcmz2h79N3jV-J-fLayGpASRUE,2706
9
- xulbux/xx_file.py,sha256=pQrTsTvaa8tBHOrN9pu2r7qxi8Wzon5V43TfVGitxko,2608
10
- xulbux/xx_format_codes.py,sha256=OqzegaaiLJ09id6LI842bH4oyXOXbVrBM5q5jh6-z7I,13562
11
- xulbux/xx_json.py,sha256=wAIwC5IO8oepsYoF1tN5c4DOjSBvT2HZakUxmA-F3BE,5050
12
- xulbux/xx_path.py,sha256=KB1HGYCxFB3_T97RVACajy3-QiksbviZVIW9-iQcr4s,4558
13
- xulbux/xx_regex.py,sha256=yTGtNtuW2Cng714iuwxWz9wDN3Bw06rzXfPUVvfiXvk,7159
14
- xulbux/xx_string.py,sha256=znfeRrAu8OA3Dd0cmMsOm2zXZKzudS4ZuH2Pny2ulEw,6212
15
- xulbux/xx_system.py,sha256=yehO57ggWDRJbeFoTE1VisW4QrbkqZzAyjKoL3xHx9k,3935
16
- xulbux-1.5.7.dist-info/METADATA,sha256=dLdhkqGBSyklBIzk_A4zgViiXfxTKoPlYuWigz_rr-0,4336
17
- xulbux-1.5.7.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
18
- xulbux-1.5.7.dist-info/entry_points.txt,sha256=KX2dtQr5OW5WdHcNqgj6rxFTqliIBNcNQq_FBOGS-0A,94
19
- xulbux-1.5.7.dist-info/licenses/LICENSE,sha256=6NflEcvzFEe8_JFVNCPVwZBwBhlLLd4vqQi8WiX_Xk4,1084
20
- xulbux-1.5.7.dist-info/RECORD,,
@@ -1,3 +0,0 @@
1
- [console_scripts]
2
- xulbux-help = XulbuX._cli_:help_command
3
- xx-help = XulbuX._cli_:help_command
File without changes