abstract-utilities 0.2.2.456__py3-none-any.whl → 0.2.2.458__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 abstract-utilities might be problematic. Click here for more details.

@@ -5,3 +5,4 @@ from .map_utils import *
5
5
  from .pdf_utils import *
6
6
  from .file_reader import *
7
7
  from .find_collect import *
8
+ from .type_checks import *
@@ -0,0 +1,83 @@
1
+ from .imports import *
2
+ import subprocess, os
3
+ from typing import Optional
4
+
5
+ # --- Base remote checker -----------------------------------------------------
6
+ def _remote_test(path: str, test_flag: str, user_at_host: str, timeout: int = 5) -> bool:
7
+ """
8
+ Run a remote shell test (e.g. -f, -d) via SSH.
9
+ Returns True if test succeeds, False otherwise.
10
+ """
11
+ cmd = f"[ {test_flag} {shlex.quote(path)} ] && echo 1 || echo 0"
12
+ try:
13
+ result = subprocess.check_output(
14
+ ["ssh", user_at_host, cmd],
15
+ stderr=subprocess.DEVNULL,
16
+ text=True,
17
+ timeout=timeout
18
+ ).strip()
19
+ return result == "1"
20
+ except Exception:
21
+ return False
22
+
23
+
24
+ # --- Individual path checks --------------------------------------------------
25
+ def is_remote_file(path: str, user_at_host: str) -> bool:
26
+ """True if remote path is a file."""
27
+ return _remote_test(path, "-f", user_at_host)
28
+
29
+
30
+ def is_remote_dir(path: str, user_at_host: str) -> bool:
31
+ """True if remote path is a directory."""
32
+ return _remote_test(path, "-d", user_at_host)
33
+
34
+
35
+ def is_local_file(path: str) -> bool:
36
+ """True if local path is a file."""
37
+ return os.path.isfile(path)
38
+
39
+
40
+ def is_local_dir(path: str) -> bool:
41
+ """True if local path is a directory."""
42
+ return os.path.isdir(path)
43
+
44
+
45
+ # --- Unified interface -------------------------------------------------------
46
+ def is_file(path: str,*args, user_at_host: Optional[str] = None,**kwargs) -> bool:
47
+ """Determine if path is a file (works local or remote)."""
48
+ if user_at_host:
49
+ return is_remote_file(path, user_at_host)
50
+ return is_local_file(path)
51
+
52
+
53
+ def is_dir(path: str, *args,user_at_host: Optional[str] = None,**kwargs)) -> bool:
54
+ """Determine if path is a directory (works local or remote)."""
55
+ if user_at_host:
56
+ return is_remote_dir(path, user_at_host)
57
+ return is_local_dir(path)
58
+
59
+
60
+ # --- Optional: keep your original all-in-one wrapper ------------------------
61
+ def check_path_type(
62
+ path: str,
63
+ user_at_host: Optional[str] = None,
64
+ ) -> str:
65
+ """
66
+ Return 'file', 'directory', 'missing', or 'unknown'.
67
+ Uses isolated is_file/is_dir functions.
68
+ """
69
+ if user_at_host:
70
+ if is_remote_file(path, user_at_host):
71
+ return "file"
72
+ elif is_remote_dir(path, user_at_host):
73
+ return "directory"
74
+ else:
75
+ return "missing"
76
+ else:
77
+ if os.path.isfile(path):
78
+ return "file"
79
+ elif os.path.isdir(path):
80
+ return "directory"
81
+ elif not os.path.exists(path):
82
+ return "missing"
83
+ return "unknown"
@@ -12,16 +12,14 @@ Usage:
12
12
  from abstract_utilities.read_write_utils import *
13
13
  """
14
14
 
15
- import os, json
15
+ import os
16
16
  from .ssh_utils.utils import run_cmd
17
+ from .file_utils.file_utils.type_checks import is_file,is_dir
17
18
  from .abstract_classes import run_pruned_func
18
19
  _FILE_PATH_KEYS = ['file', 'filepath', 'file_path', 'path', 'directory', 'f', 'dst', 'dest']
19
20
  _CONTENTS_KEYS = ['cont', 'content', 'contents', 'data', 'datas', 'dat', 'src', 'source']
20
21
 
21
22
 
22
-
23
- META_SUFFIX = ".meta.json"
24
-
25
23
  # --- Helper utilities --------------------------------------------------------
26
24
  def string_in_keys(strings, kwargs):
27
25
  """Find a matching keyword in kwargs that contains any of the given substrings."""
@@ -76,28 +74,59 @@ def check_read_write_params(*args, **kwargs):
76
74
  raise ValueError("Missing file_path argument.")
77
75
  return file_path, contents
78
76
 
79
-
80
- def write_to_file(*args, **kwargs):
77
+ def write_to_path(
78
+ file_path: str,
79
+ contents: str,
80
+ *,
81
+ user_at_host: str = None,
82
+ cwd: str | None = None,
83
+ password=None,
84
+ key=None,
85
+ env_path=None,
86
+ **kwargs
87
+ ) -> str:
81
88
  """
82
- Write contents to a file and save optional metadata like user_at_host.
89
+ Completely overwrite a file (locally or remotely).
90
+ Supports sudo and password-based remote execution.
83
91
  """
84
- file_path, contents = check_read_write_params(*args, **kwargs)
85
- if contents is None:
86
- raise ValueError("Missing contents to write.")
87
92
 
88
- os.makedirs(os.path.dirname(file_path) or ".", exist_ok=True)
89
- with open(file_path, "w", encoding="utf-8") as f:
90
- f.write(str(contents))
93
+ # sanitize for shell safety
94
+ quoted_path = shlex.quote(file_path)
95
+ quoted_data = shlex.quote(str(contents))
91
96
 
92
- # Store user_at_host metadata if provided
93
- user_at_host = kwargs.get("user_at_host")
94
- if user_at_host:
95
- meta_path = file_path + META_SUFFIX
96
- with open(meta_path, "w", encoding="utf-8") as m:
97
- json.dump({"user_at_host": user_at_host}, m)
97
+ # shell command that fully overwrites
98
+ # (no append, replaces contents entirely)
99
+ base_cmd = f"echo {quoted_data} > {quoted_path}"
98
100
 
99
- return file_path
100
- # --- Core functionality -------------------------------------------------------
101
+ # optional sudo password injection
102
+ full_cmd = get_print_sudo_cmd(
103
+ cmd=base_cmd,
104
+ password=password,
105
+ key=key,
106
+ env_path=env_path
107
+ )
108
+
109
+ # local or remote dispatch
110
+ if user_at_host:
111
+ return run_remote_cmd(
112
+ user_at_host=user_at_host,
113
+ cmd=full_cmd,
114
+ cwd=cwd,
115
+ password=password,
116
+ key=key,
117
+ env_path=env_path,
118
+ **kwargs
119
+ )
120
+ else:
121
+ return run_local_cmd(
122
+ cmd=full_cmd,
123
+ cwd=cwd,
124
+ password=password,
125
+ key=key,
126
+ env_path=env_path,
127
+ **kwargs
128
+ )
129
+ ### --- Core functionality -------------------------------------------------------
101
130
  ##def write_to_file(*args, **kwargs):
102
131
  ## """
103
132
  ## Write contents to a file (create if missing).
@@ -112,6 +141,32 @@ def write_to_file(*args, **kwargs):
112
141
  ## with open(file_path, "w", encoding="utf-8") as f:
113
142
  ## f.write(str(contents))
114
143
  ## return file_path
144
+ # --- Core functionality -------------------------------------------------------
145
+ def write_to_file(*args, **kwargs):
146
+ """
147
+ Write contents to a file (create if missing).
148
+
149
+ Returns the file_path written.
150
+ """
151
+ file_path, contents = check_read_write_params(*args, **kwargs)
152
+ if contents is None:
153
+ raise ValueError("Missing contents to write.")
154
+ user_at_host = kwargs.get("user_at_host")
155
+ if user_at_host:
156
+ kwargs["cwd"] = kwargs.get('cwd') or os.path.dirname(file_path)
157
+ # sanitize for shell safety
158
+ quoted_path = shlex.quote(file_path)
159
+ quoted_data = shlex.quote(str(contents))
160
+ # shell command that fully overwrites
161
+ # (no append, replaces contents entirely)
162
+ kwargs["cmd"] = f"echo {quoted_data} > {quoted_path}"
163
+ return run_pruned_func(run_cmd,**kwargs)
164
+
165
+
166
+ os.makedirs(os.path.dirname(file_path) or ".", exist_ok=True)
167
+ with open(file_path, "w", encoding="utf-8") as f:
168
+ f.write(str(contents))
169
+ return file_path
115
170
 
116
171
 
117
172
  def read_from_file(file_path,**kwargs):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: abstract_utilities
3
- Version: 0.2.2.456
3
+ Version: 0.2.2.458
4
4
  Summary: abstract_utilities is a collection of utility modules providing a variety of functions to aid in tasks such as data comparison, list manipulation, JSON handling, string manipulation, mathematical computations, and time operations.
5
5
  Home-page: https://github.com/AbstractEndeavors/abstract_utilities
6
6
  Author: putkoff
@@ -14,7 +14,7 @@ abstract_utilities/log_utils.py,sha256=W74Y-CmdQP4Kj88HmAgejVxWgyWlvgCKMwLvOfyFf
14
14
  abstract_utilities/math_utils.py,sha256=0o1ls1En03UAkYmxTBildCCJDfHygmNuvVnrNrLYtK0,6578
15
15
  abstract_utilities/parse_utils.py,sha256=Z5OGRwHuzCzY91fz0JJojk1BPAo1XF2quNNLuBF4_Vk,18602
16
16
  abstract_utilities/path_utils.py,sha256=X_U9cPBbNu5Wi0F3hQE0gXQX1gfhzxhxALbairTEOZU,19252
17
- abstract_utilities/read_write_utils.py,sha256=OeOzohHVpGnXYyA50aV4W_Xj6qarvwMhN9xNEy8KhW0,5254
17
+ abstract_utilities/read_write_utils.py,sha256=UAtan9PUS3EPuGExEOSgOAq6NgSqdCkjrJ_-PIEUBbk,6860
18
18
  abstract_utilities/safe_utils.py,sha256=_uoZny6dJjopVakOiaf0UIZcvRRXMh51FpfDUooe0xY,3733
19
19
  abstract_utilities/string_clean.py,sha256=-gY9i2yqjX5UClvSaKsSrzA4GjR7eaNI3GnFjZpt2bo,5923
20
20
  abstract_utilities/string_utils.py,sha256=02xdIEDySWEexrtY3skBYOdeDmr3XgE5j1nqAeAv7w8,352
@@ -41,7 +41,7 @@ abstract_utilities/env_utils/imports/imports.py,sha256=ZrGEf-J2lyVbb4MrNBX3DwHR0
41
41
  abstract_utilities/env_utils/imports/utils.py,sha256=oB7WhIm_-cHLrUHRXypZGCdWUtNRyePaVO5_kq5Cv84,4490
42
42
  abstract_utilities/file_utils/__init__.py,sha256=I4md5xU5nuBuKyxumvKmnrR0-UgBePX9QfY-QNS-Zso,101
43
43
  abstract_utilities/file_utils/req.py,sha256=CsdGHAWIHOLqjzyoOSZ7XYbNciVYnTgaUs5qOCHttE0,11837
44
- abstract_utilities/file_utils/file_utils/__init__.py,sha256=FjtDxrP7FGqSz6iF8zp_VJdtrWpTnEZmDO2JMUrqV9w,188
44
+ abstract_utilities/file_utils/file_utils/__init__.py,sha256=WOsWvRf7lomRoS80xT1n-R0jvbyHsxPXMsdoPAN62cc,215
45
45
  abstract_utilities/file_utils/file_utils/file_filters.py,sha256=khfbonAPEAhW1wxfFo0I4dawYPCrIKEjNc7VKb1RvzA,3437
46
46
  abstract_utilities/file_utils/file_utils/file_reader.py,sha256=2MRj2PGKq4C-iKL8dmhHwWnhmA8GPVsNaWkTREOF9vo,24545
47
47
  abstract_utilities/file_utils/file_utils/file_utils.py,sha256=2aVuD0sB-P2gmo_kUsE11aHc6WzLvHUAerMkmy3y9vk,7751
@@ -50,6 +50,7 @@ abstract_utilities/file_utils/file_utils/find_collect.py,sha256=bPM7EDrNHlvwZx7C
50
50
  abstract_utilities/file_utils/file_utils/imports.py,sha256=SXCMBuHUwqXbfRBk4LjKehsBKZa8-Po5UfEcNTwn4Es,24
51
51
  abstract_utilities/file_utils/file_utils/map_utils.py,sha256=B_MlkLP8s-o0yU0R3Y2LcTpBntBzysJO18qq181xz9c,1043
52
52
  abstract_utilities/file_utils/file_utils/pdf_utils.py,sha256=D_wg8h-SapCvqinxRIKxMri1jWZNpr5jGvKq9EJePfY,10335
53
+ abstract_utilities/file_utils/file_utils/type_checks.py,sha256=g5-Pwham8Ax6axfwkv4JA6hNdkz7I-Gf2H0YaIbW5ew,2626
53
54
  abstract_utilities/file_utils/imports/__init__.py,sha256=VWN_3t0gRSYCalXI0nRLEUB3O2kbqA_Y8wnFq_m5F7A,131
54
55
  abstract_utilities/file_utils/imports/classes.py,sha256=zw16D_h5AxJiks4ydbqkWkXVfvgmE-BpiC4eKInY_KI,12259
55
56
  abstract_utilities/file_utils/imports/constants.py,sha256=eIeSj48vtfa8CTYKuuZXbgJQepBrMracfVguaSuN41U,1626
@@ -78,7 +79,7 @@ abstract_utilities/ssh_utils/classes.py,sha256=3Q9BfLpyagNFYyiF4bt-5UCezeUJv9NK9
78
79
  abstract_utilities/ssh_utils/imports.py,sha256=oX8WAv-pkhizzko_h3fIUp9Vhsse4nR7RN2vwONxIx0,317
79
80
  abstract_utilities/ssh_utils/pexpect_utils.py,sha256=JBdOIXBTXAqE5TrsFjmPWJgwSaWyRJN8rbJ6y3_zKPY,10556
80
81
  abstract_utilities/ssh_utils/utils.py,sha256=smUWAx3nW1h0etTndJ_te9bkUX5YzQ8kYd9_gD1TXLk,4882
81
- abstract_utilities-0.2.2.456.dist-info/METADATA,sha256=7xIkE91A13osv1AF4TpEe6an6XkbK0z2j8Sgo9ovrco,28108
82
- abstract_utilities-0.2.2.456.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
83
- abstract_utilities-0.2.2.456.dist-info/top_level.txt,sha256=BF0GZ0xVFfN1K-hFIWPO3viNsOs1sSF86n1vHBg39FM,19
84
- abstract_utilities-0.2.2.456.dist-info/RECORD,,
82
+ abstract_utilities-0.2.2.458.dist-info/METADATA,sha256=MIT6HiI0-qSgcKCCOLaltyuBOBlRLIihTERdENlK-h4,28108
83
+ abstract_utilities-0.2.2.458.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
84
+ abstract_utilities-0.2.2.458.dist-info/top_level.txt,sha256=BF0GZ0xVFfN1K-hFIWPO3viNsOs1sSF86n1vHBg39FM,19
85
+ abstract_utilities-0.2.2.458.dist-info/RECORD,,