1
1
from __future__ import annotations
2
+ import sys
3
+
4
+ sys .path .append ("." )
2
5
3
6
import pkgutil
4
7
import shutil
9
12
from red_utils .std import (
10
13
context_managers ,
11
14
dict_utils ,
12
- file_utils ,
15
+ path_utils ,
13
16
hash_utils ,
14
17
time_utils ,
15
18
uuid_utils ,
41
44
from red_utils import CustomException
42
45
from red_utils .ext .context_managers import cli_spinners
43
46
47
+
44
48
def test_file_utils_list () -> list [Path ]:
45
49
cwd = Path .cwd ()
46
50
search_dir = f"{ cwd } /red_utils"
47
51
48
- list_files_test = file_utils .list_files (in_dir = search_dir , ext_filter = ".py" )
52
+ list_files_test = path_utils .list_files (in_dir = search_dir , ext_filter = ".py" )
49
53
print (f".py files found in { search_dir } : { len (list_files_test )} " )
50
54
51
55
rand_index = random .randint (0 , len (list_files_test ) - 1 )
@@ -410,68 +414,28 @@ def delete_test_remnants(delete_dirs=delete_dirs, delete_files=delete_files):
410
414
for _d in delete_dirs :
411
415
d_path : Path = Path (_d )
412
416
413
- try :
414
- if d_path .exists ():
415
- print (f"Deleting directory: { str (d_path )} " )
416
- shutil .rmtree (d_path , ignore_errors = True )
417
-
418
- except FileNotFoundError as fnf :
419
- print (
420
- FileNotFoundError (
421
- f"Could not find dir { str (d_path )} . Details: { fnf } "
422
- )
423
- )
424
- except PermissionError as perm :
425
- print (
426
- PermissionError (
427
- f"Insufficient permissions to delete dir { str (d_path )} . Details: { perm } "
428
- )
429
- )
430
- except Exception as exc :
431
- print (
432
- Exception (
433
- f"Unhandled exception deleting dir { str (d_path )} . Details: { exc } "
434
- )
435
- )
417
+ path_utils .delete_path (_d )
436
418
437
419
for _f in delete_files :
438
420
f_path : Path = Path (_f )
439
421
440
- try :
441
- if f_path .exists ():
442
- print (f"Deleting file { str (f_path )} " )
443
- f_path .unlink ()
444
-
445
- except FileNotFoundError as fnf :
446
- print (
447
- FileNotFoundError (
448
- f"Could not find file { str (f_path )} . Details: { fnf } "
449
- )
450
- )
451
- except PermissionError as perm :
452
- print (
453
- PermissionError (
454
- f"Insufficient permissions to delete file { str (f_path )} . Details: { perm } "
455
- )
456
- )
457
- except Exception as exc :
458
- print (
459
- Exception (
460
- f"Unhandled exception deleting file { str (f_path )} . Details: { exc } "
461
- )
462
- )
422
+ path_utils .delete_path (f_path )
463
423
464
424
delete_test_remnants ()
465
425
466
426
427
+ def test_ensuredirs (_dirs : list [Path ] = [Path ("test" ), Path ("test/testing" )]):
428
+ path_utils .ensure_dirs_exist (ensure_dirs = _dirs )
429
+
430
+
467
431
def main ():
468
432
"""Main function to control flow of demo.
469
433
470
434
Comment functions you don't want to execute.
471
435
"""
472
436
## Delete lists for cleanup function
473
437
cleanup_dirs : list [str ] = [".cache" , ".db" , ".serialize" ]
474
- cleanup_files : list [str ] = ["test.db" ]
438
+ cleanup_files : list [str ] = ["test.db" , "test" ]
475
439
476
440
print (test_file_utils_list ())
477
441
@@ -496,6 +460,8 @@ def main():
496
460
497
461
test_sqlalchemy_utils ()
498
462
463
+ test_ensuredirs ()
464
+
499
465
with cli_spinners .SimpleSpinner ("Cleaning up..." ):
500
466
post_test_cleanup (delete_files = cleanup_files , delete_dirs = cleanup_dirs )
501
467
time .sleep (0.5 )
0 commit comments