xulbux 1.6.0__py3-none-any.whl → 1.6.2__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_system.py CHANGED
@@ -1,12 +1,29 @@
1
1
  import subprocess as _subprocess
2
2
  import platform as _platform
3
+ import ctypes as _ctypes
3
4
  import time as _time
4
5
  import sys as _sys
5
6
  import os as _os
6
7
 
7
8
 
9
+ class ProcessNotFoundError(Exception):
10
+ pass
11
+
12
+
8
13
  class System:
9
14
 
15
+ @staticmethod
16
+ def is_elevated() -> bool:
17
+ """Returns `True` if the current user is an admin and `False` otherwise."""
18
+ try:
19
+ if _os.name == "nt":
20
+ return _ctypes.windll.shell32.IsUserAnAdmin() != 0
21
+ elif _os.name == "posix":
22
+ return _os.geteuid() == 0
23
+ except:
24
+ pass
25
+ return False
26
+
10
27
  @staticmethod
11
28
  def restart(
12
29
  prompt: object = None,
@@ -14,12 +31,11 @@ class System:
14
31
  continue_program: bool = False,
15
32
  force: bool = False,
16
33
  ) -> None:
17
- """Starts a system restart:<br>
18
- `prompt` is the message to be displayed in the systems restart notification.<br>
19
- `wait` is the time to wait until restarting in seconds.<br>
20
- `continue_program` is whether to continue the current Python program after calling this function.<br>
21
- `force` is whether to force a restart even if other processes are still running.
22
- """
34
+ """Starts a system restart:
35
+ - `prompt` is the message to be displayed in the systems restart notification.
36
+ - `wait` is the time to wait until restarting in seconds.
37
+ - `continue_program` is whether to continue the current Python program after calling this function.
38
+ - `force` is whether to force a restart even if other processes are still running."""
23
39
  system = _platform.system().lower()
24
40
  if system == "windows":
25
41
  if not force:
@@ -61,8 +77,8 @@ class System:
61
77
  ) -> None | list[str]:
62
78
  """Checks if the given list of libraries are installed. If not:
63
79
  - If `install_missing` is `False` the missing libraries will be returned as a list.
64
- - If `install_missing` is `True` the missing libraries will be installed. If `confirm_install` is `True` the user will first be asked if they want to install the missing libraries.
65
- """
80
+ - If `install_missing` is `True` the missing libraries will be installed.
81
+ - If `confirm_install` is `True` the user will first be asked if they want to install the missing libraries."""
66
82
  missing = []
67
83
  for lib in lib_names:
68
84
  try:
@@ -84,3 +100,38 @@ class System:
84
100
  return None
85
101
  except _subprocess.CalledProcessError:
86
102
  return missing
103
+
104
+ @staticmethod
105
+ def elevate(win_title: str | None = None, args: list | None = None) -> bool:
106
+ """Attempts to start a new process with elevated privileges.\n
107
+ ---------------------------------------------------------------------------------
108
+ The param `win_title` is window the title of the elevated process.
109
+ The param `args` is the arguments to be passed to the elevated process.\n
110
+ After the elevated process started, the original process will exit.
111
+ This means, that this method has to be run at the beginning of the program or
112
+ will have to continue in a new window after elevation.\n
113
+ ---------------------------------------------------------------------------------
114
+ Returns `True` if the current process already has elevated privileges and raises
115
+ a `PermissionError` if the user denied the elevation or the elevation failed."""
116
+ if System.is_elevated():
117
+ return True
118
+ if _os.name == "nt": # WINDOWS
119
+ if win_title:
120
+ args_str = f'-c "import ctypes; ctypes.windll.kernel32.SetConsoleTitleW(\\"{win_title}\\"); exec(open(\\"{_sys.argv[0]}\\").read())" {" ".join(args)}"'
121
+ else:
122
+ args_str = f'-c "exec(open(\\"{_sys.argv[0]}\\").read())" {" ".join(args)}'
123
+ result = _ctypes.windll.shell32.ShellExecuteW(None, "runas", _sys.executable, args_str, None, 1)
124
+ if result <= 32:
125
+ raise PermissionError("Failed to launch elevated process.")
126
+ else:
127
+ _sys.exit(0)
128
+ else: # POSIX
129
+ cmd = ["pkexec"]
130
+ if win_title:
131
+ cmd.extend(["--description", win_title])
132
+ cmd.extend([_sys.executable] + _sys.argv[1:] + ([] if args is None else args))
133
+ proc = _subprocess.Popen(cmd)
134
+ proc.wait()
135
+ if proc.returncode != 0:
136
+ raise PermissionError("Process elevation was denied.")
137
+ _sys.exit(0)
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: xulbux
3
- Version: 1.6.0
3
+ Version: 1.6.2
4
4
  Summary: A library which includes a lot of really helpful functions.
5
5
  Author-email: XulbuX <xulbux.real@gmail.com>
6
6
  License: MIT License
@@ -95,7 +95,7 @@ from xulbux import rgba, hsla, hexa
95
95
  | <h3>[`xx_console`](https://github.com/XulbuX-dev/PythonLibraryXulbuX/wiki/xx_console)</h3> | advanced actions related to the console (*pretty logging, advanced inputs, ...*) |
96
96
  | <h3>[`xx_data`](https://github.com/XulbuX-dev/PythonLibraryXulbuX/wiki/xx_data)</h3> | advanced operations with data structures (*compare, generate path ID's, pretty print/format, ...*) |
97
97
  | <h3>[`xx_env_path`](https://github.com/XulbuX-dev/PythonLibraryXulbuX/wiki/xx_env_path)</h3> | getting and editing the PATH variable (*get paths, check for paths, add paths, ...*) |
98
- | <h3>`xx_file`</h3> | advanced working with files (*create files, rename file-extensions, ...*) |
98
+ | <h3>[`xx_file`](https://github.com/XulbuX-dev/PythonLibraryXulbuX/wiki/xx_file)</h3> | advanced working with files (*create files, rename file-extensions, ...*) |
99
99
  | <h3>`xx_format_codes`</h3> | easy pretty printing with custom format codes (*print, inputs, custom format codes to ANSI, ...*) |
100
100
  | <h3>`xx_json`</h3> | advanced working with json files (*read, create, update, ...*) |
101
101
  | <h3>`xx_path`</h3> | advanced path operations (*get paths, smart-extend relative paths, delete paths, ...*) |
@@ -0,0 +1,21 @@
1
+ xulbux/__init__.py,sha256=fxqdXSxUa_iDtV2MWdq4aBXV3B0pQWgz2xHpLnm84zw,1658
2
+ xulbux/_cli_.py,sha256=U25ZrtpQgpKXtvOSTBBbh7-AJ_WTeZ95A66DQARAqzo,3558
3
+ xulbux/_consts_.py,sha256=qAkg6ZwTKhQFcpHpfXjF4QEao8hpDEECRGqrv8oxkCo,4572
4
+ xulbux/xx_code.py,sha256=yBP5WxCxNxjBiS6nVAmUBJpD0hX6fgnh5RWq-NmrnaY,5222
5
+ xulbux/xx_color.py,sha256=cDlgrekH88ZEBj8leIIlJbYzsf1RdS8RW3oGvuUfvC0,45129
6
+ xulbux/xx_console.py,sha256=rd31686X9eXB8__lcLaV7unWzIdXGM2yMOt9zNCsGbs,15079
7
+ xulbux/xx_data.py,sha256=OEKLbI1XeNTrittdz3s3mvQk8YrBoSovj9O1H-b7ArY,25844
8
+ xulbux/xx_env_path.py,sha256=iv3Jw0TsNDbbL_NySnazvIP9Iv9swymhiJIr2B3EG8k,4388
9
+ xulbux/xx_file.py,sha256=-58YnqKvrs5idIF91UzEki7o7qnskFvnQYkBaRrp7Vw,3122
10
+ xulbux/xx_format_codes.py,sha256=hTQBowcFX0gyAtjsYvgrEZK2EQ83b0W-IyqIUTkJAig,16986
11
+ xulbux/xx_json.py,sha256=q60lOj8Xg8c4L9cBu6SBZdJzFC7QbjDfFwcKKzBKj5w,5173
12
+ xulbux/xx_path.py,sha256=_xkH9cowPdi3nHw2q_TvN_i_5oG6GJut-QwPBLxnrAQ,4519
13
+ xulbux/xx_regex.py,sha256=zyxkS1bLlrSq26ErhO4UtrimIhW71_a7kox6ArCoK58,7670
14
+ xulbux/xx_string.py,sha256=Wa3qHxnk7AIpAVAn1vI_GBtkfYFwy4F_Xtj83ojEPKc,7168
15
+ xulbux/xx_system.py,sha256=uBpB1eKbR-bddO87jbWdoi5NScLnrxJONbyqU49LmCs,6426
16
+ xulbux-1.6.2.dist-info/LICENSE,sha256=6NflEcvzFEe8_JFVNCPVwZBwBhlLLd4vqQi8WiX_Xk4,1084
17
+ xulbux-1.6.2.dist-info/METADATA,sha256=PyPr14LhmMVg952AQ-5uxp5jd3GSFRVCOxjPfsQ7ebo,6836
18
+ xulbux-1.6.2.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
19
+ xulbux-1.6.2.dist-info/entry_points.txt,sha256=a3womfLIMZKnOFiyy-xnVb4g2qkZsHR5FbKKkljcGns,94
20
+ xulbux-1.6.2.dist-info/top_level.txt,sha256=FkK4EZajwfP36fnlrPaR98OrEvZpvdEOdW1T5zTj6og,7
21
+ xulbux-1.6.2.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.7.0)
2
+ Generator: setuptools (75.8.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,21 +0,0 @@
1
- xulbux/__init__.py,sha256=atWR3hKxFB9Jq182TOrwhLHIEaYW5go9F6xosD76dog,1658
2
- xulbux/_cli_.py,sha256=U25ZrtpQgpKXtvOSTBBbh7-AJ_WTeZ95A66DQARAqzo,3558
3
- xulbux/_consts_.py,sha256=2-swueg8B5retfsAQqmR8nFNc06tqAwpL__TOv3V4kw,4882
4
- xulbux/xx_code.py,sha256=dAksTh2Fw6-XTIGn8eEJVUlvJOsrWbctDZJDk4cs8FQ,5137
5
- xulbux/xx_color.py,sha256=AqMqPCwDQZVQwkQUM0IOO47GzKNhKapoCwDifs-ewI8,44906
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=nO2FtxqlBaW84L-qiTcQf16Im2xD-ZKPAytJDG4RCRw,2618
10
- xulbux/xx_format_codes.py,sha256=NIleZ9TrXdF0i9UMCZP7ZI_k1ChJjYlHCFAcHZHHq2c,14891
11
- xulbux/xx_json.py,sha256=1xAYBmpb4o8hOS_TaMQW2E5pZdE1YqbUdSJm-53wm-s,5054
12
- xulbux/xx_path.py,sha256=KB1HGYCxFB3_T97RVACajy3-QiksbviZVIW9-iQcr4s,4558
13
- xulbux/xx_regex.py,sha256=-dU1x4GDhFKEg0uQPT6uw9Yyo0vo7f98TtlicG9uBJo,7872
14
- xulbux/xx_string.py,sha256=21gV3ozegkimLbCk4PMDD_ObxYElREABxuZ0p1Ga2HY,7090
15
- xulbux/xx_system.py,sha256=yehO57ggWDRJbeFoTE1VisW4QrbkqZzAyjKoL3xHx9k,3935
16
- xulbux-1.6.0.dist-info/LICENSE,sha256=6NflEcvzFEe8_JFVNCPVwZBwBhlLLd4vqQi8WiX_Xk4,1084
17
- xulbux-1.6.0.dist-info/METADATA,sha256=ZGzDhbqYvTl-dg68I0WFesKfkvlG_Vm-2iJL8-DVRyg,6836
18
- xulbux-1.6.0.dist-info/WHEEL,sha256=A3WOREP4zgxI0fKrHUG8DC8013e3dK3n7a6HDbcEIwE,91
19
- xulbux-1.6.0.dist-info/entry_points.txt,sha256=a3womfLIMZKnOFiyy-xnVb4g2qkZsHR5FbKKkljcGns,94
20
- xulbux-1.6.0.dist-info/top_level.txt,sha256=FkK4EZajwfP36fnlrPaR98OrEvZpvdEOdW1T5zTj6og,7
21
- xulbux-1.6.0.dist-info/RECORD,,