config2py 0.1.35__py3-none-any.whl → 0.1.36__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.
- config2py/util.py +56 -53
- {config2py-0.1.35.dist-info → config2py-0.1.36.dist-info}/METADATA +1 -1
- {config2py-0.1.35.dist-info → config2py-0.1.36.dist-info}/RECORD +6 -7
- config2py/tests/test_util.py +0 -39
- {config2py-0.1.35.dist-info → config2py-0.1.36.dist-info}/LICENSE +0 -0
- {config2py-0.1.35.dist-info → config2py-0.1.36.dist-info}/WHEEL +0 -0
- {config2py-0.1.35.dist-info → config2py-0.1.36.dist-info}/top_level.txt +0 -0
config2py/util.py
CHANGED
|
@@ -7,6 +7,8 @@ from pathlib import Path
|
|
|
7
7
|
from typing import Optional, Union, Any, Callable, Set, Iterable
|
|
8
8
|
import getpass
|
|
9
9
|
|
|
10
|
+
from dol import process_path
|
|
11
|
+
|
|
10
12
|
from i2 import mk_sentinel # TODO: Only i2 dependency. Consider replacing.
|
|
11
13
|
|
|
12
14
|
# def mk_sentinel(name): # TODO: Only i2 dependency. Here's replacement, but not picklable
|
|
@@ -183,65 +185,66 @@ DFLT_APP_DATA_FOLDER = os.getenv(
|
|
|
183
185
|
)
|
|
184
186
|
|
|
185
187
|
|
|
186
|
-
def
|
|
187
|
-
*path: Iterable[str],
|
|
188
|
-
ensure_dir_exists=False,
|
|
189
|
-
assert_exists=False,
|
|
190
|
-
ensure_endswith_slash=False,
|
|
191
|
-
ensure_does_not_end_with_slash=False,
|
|
192
|
-
expanduser=True,
|
|
193
|
-
expandvars=True,
|
|
194
|
-
abspath=True,
|
|
195
|
-
rootdir: str = '',
|
|
196
|
-
) -> str:
|
|
188
|
+
def create_directories(dirpath, max_dirs_to_make=None):
|
|
197
189
|
"""
|
|
198
|
-
|
|
190
|
+
Create directories up to a specified limit.
|
|
199
191
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
assert_exists (bool): Whether to assert that the path exists.
|
|
204
|
-
ensure_endswith_slash (bool): Whether to ensure the path ends with a slash.
|
|
205
|
-
ensure_does_not_end_with_slash (bool): Whether to ensure the path does not end with a slash.
|
|
206
|
-
expanduser (bool): Whether to expand the user in the path.
|
|
207
|
-
expandvars (bool): Whether to expand environment variables in the path.
|
|
208
|
-
abspath (bool): Whether to convert the path to an absolute path.
|
|
209
|
-
rootdir (str): The root directory to prepend to the path.
|
|
192
|
+
Parameters:
|
|
193
|
+
dirpath (str): The directory path to create.
|
|
194
|
+
max_dirs_to_make (int, optional): The maximum number of directories to create. If None, there's no limit.
|
|
210
195
|
|
|
211
196
|
Returns:
|
|
212
|
-
|
|
197
|
+
bool: True if the directory was created successfully, False otherwise.
|
|
198
|
+
|
|
199
|
+
Raises:
|
|
200
|
+
ValueError: If max_dirs_to_make is negative.
|
|
201
|
+
|
|
202
|
+
Examples:
|
|
203
|
+
>>> import tempfile, shutil
|
|
204
|
+
>>> temp_dir = tempfile.mkdtemp()
|
|
205
|
+
>>> target_dir = os.path.join(temp_dir, 'a', 'b', 'c')
|
|
206
|
+
>>> create_directories(target_dir, max_dirs_to_make=2)
|
|
207
|
+
False
|
|
208
|
+
>>> create_directories(target_dir, max_dirs_to_make=3)
|
|
209
|
+
True
|
|
210
|
+
>>> os.path.isdir(target_dir)
|
|
211
|
+
True
|
|
212
|
+
>>> shutil.rmtree(temp_dir) # Cleanup
|
|
213
|
+
|
|
214
|
+
>>> temp_dir = tempfile.mkdtemp()
|
|
215
|
+
>>> target_dir = os.path.join(temp_dir, 'a', 'b', 'c', 'd')
|
|
216
|
+
>>> create_directories(target_dir)
|
|
217
|
+
True
|
|
218
|
+
>>> os.path.isdir(target_dir)
|
|
219
|
+
True
|
|
220
|
+
>>> shutil.rmtree(temp_dir) # Cleanup
|
|
221
|
+
"""
|
|
222
|
+
if max_dirs_to_make is not None and max_dirs_to_make < 0:
|
|
223
|
+
raise ValueError('max_dirs_to_make must be non-negative or None')
|
|
213
224
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
>>> from functools import partial
|
|
217
|
-
>>> process_path('a', 'b', 'c', rootdir='/root/dir/', ensure_endswith_slash=True)
|
|
218
|
-
'/root/dir/a/b/c/'
|
|
225
|
+
if os.path.exists(dirpath):
|
|
226
|
+
return True
|
|
219
227
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
if
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
if ensure_dir_exists:
|
|
241
|
-
os.makedirs(path, exist_ok=True)
|
|
242
|
-
if assert_exists:
|
|
243
|
-
assert os.path.exists(path), f'Path does not exist: {path}'
|
|
244
|
-
return path
|
|
228
|
+
if max_dirs_to_make is None:
|
|
229
|
+
os.makedirs(dirpath, exist_ok=True)
|
|
230
|
+
return True
|
|
231
|
+
|
|
232
|
+
# Calculate the number of directories to create
|
|
233
|
+
dirs_to_make = []
|
|
234
|
+
current_path = dirpath
|
|
235
|
+
|
|
236
|
+
while not os.path.exists(current_path):
|
|
237
|
+
dirs_to_make.append(current_path)
|
|
238
|
+
current_path, _ = os.path.split(current_path)
|
|
239
|
+
|
|
240
|
+
if len(dirs_to_make) > max_dirs_to_make:
|
|
241
|
+
return False
|
|
242
|
+
|
|
243
|
+
# Create directories from the top level down
|
|
244
|
+
for dir_to_make in reversed(dirs_to_make):
|
|
245
|
+
os.mkdir(dir_to_make)
|
|
246
|
+
|
|
247
|
+
return True
|
|
245
248
|
|
|
246
249
|
|
|
247
250
|
# Note: First possible i2 dependency -- vendoring for now
|
|
@@ -3,14 +3,13 @@ config2py/base.py,sha256=fDKClGpgNecWFL9a7Y_FpqwuRxAAD_XCuByW68Yq5KE,15880
|
|
|
3
3
|
config2py/errors.py,sha256=QdwGsoJhv6LHDHp-_yyz4oUg1Fgu4S-S7O2nuA0a5cw,203
|
|
4
4
|
config2py/s_configparser.py,sha256=XhxFz6-PG4-QsecJfbjLFdBWHcPU6dwgqwkTZyY_y3E,15873
|
|
5
5
|
config2py/tools.py,sha256=W2YQm-PerKRN8prsxVfcBCChx75pZ9H8UqWH8pGxECE,9191
|
|
6
|
-
config2py/util.py,sha256=
|
|
6
|
+
config2py/util.py,sha256=xVxfOduKsv1BdNIFVVdPMmA0wxBYyipCFYpPD-r6A6Y,16136
|
|
7
7
|
config2py/scrap/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
8
|
config2py/tests/__init__.py,sha256=sk-yGJQOZES2z70M4xmZB57tsxSktX_84ybDuV8Cz5Q,297
|
|
9
9
|
config2py/tests/test_tools.py,sha256=km9RNh-gDtSJNjYiLQdkWcRi9IB5MwpijD3U2XCyi1Y,3728
|
|
10
|
-
config2py/tests/test_util.py,sha256=DNJn60dvyr7xb86Spoz_VDS4cwU2mtcRo1_MgoCrKCY,1391
|
|
11
10
|
config2py/tests/utils_for_testing.py,sha256=Vz6EDY27uy_RZCSceZ7jqXkp_CXe52KAZSXcYKivazM,162
|
|
12
|
-
config2py-0.1.
|
|
13
|
-
config2py-0.1.
|
|
14
|
-
config2py-0.1.
|
|
15
|
-
config2py-0.1.
|
|
16
|
-
config2py-0.1.
|
|
11
|
+
config2py-0.1.36.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
12
|
+
config2py-0.1.36.dist-info/METADATA,sha256=VGU8KFs1Xt3w8HL2cp-895nDZhbmXxgpTjBMhc_2lR0,14559
|
|
13
|
+
config2py-0.1.36.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
|
14
|
+
config2py-0.1.36.dist-info/top_level.txt,sha256=DFnlOIKMIGWQRROr3voJFhWFViHaWgTTeWZjC5YC9QQ,10
|
|
15
|
+
config2py-0.1.36.dist-info/RECORD,,
|
config2py/tests/test_util.py
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import tempfile
|
|
2
|
-
import os
|
|
3
|
-
import pytest
|
|
4
|
-
from config2py.util import process_path
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
def test_process_path():
|
|
8
|
-
# Create a temporary directory
|
|
9
|
-
with tempfile.TemporaryDirectory() as temp_dir:
|
|
10
|
-
temp_path = os.path.join(temp_dir, 'foo/bar')
|
|
11
|
-
|
|
12
|
-
output_path = process_path(temp_path)
|
|
13
|
-
assert output_path == temp_path
|
|
14
|
-
assert not os.path.exists(output_path)
|
|
15
|
-
|
|
16
|
-
output_path = process_path(temp_path, expanduser=False)
|
|
17
|
-
assert output_path == temp_path
|
|
18
|
-
assert not os.path.exists(output_path)
|
|
19
|
-
|
|
20
|
-
with pytest.raises(AssertionError):
|
|
21
|
-
output_path = process_path(temp_path, assert_exists=True)
|
|
22
|
-
|
|
23
|
-
output_path = process_path(temp_path, ensure_dir_exists=True)
|
|
24
|
-
assert output_path == temp_path
|
|
25
|
-
assert os.path.exists(output_path)
|
|
26
|
-
|
|
27
|
-
output_path = process_path(temp_path, assert_exists=True)
|
|
28
|
-
assert output_path == temp_path
|
|
29
|
-
assert os.path.exists(output_path)
|
|
30
|
-
|
|
31
|
-
# If path doesn't end with a (system file separator) slash, add one:
|
|
32
|
-
output_path = process_path(temp_path, ensure_endswith_slash=True)
|
|
33
|
-
assert output_path == temp_path + os.path.sep
|
|
34
|
-
|
|
35
|
-
# If path ends with a (system file separator) slash, remove it.
|
|
36
|
-
output_path = process_path(
|
|
37
|
-
temp_path + os.path.sep, ensure_does_not_end_with_slash=True
|
|
38
|
-
)
|
|
39
|
-
assert output_path == temp_path
|
|
File without changes
|
|
File without changes
|
|
File without changes
|