-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbgsio.py
1310 lines (1013 loc) · 43.2 KB
/
bgsio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import shutil
from glob import glob
import yaml
from collections import OrderedDict
import requests
import toml
from pathlib import Path
from typing import Callable, Optional
from bgsutils import find_value_in_dict
def find_files_with_key_or_value(surveys_dirpath:str, search_value:str):
"""
This function navigates through a directory and its subdirectories to find .yaml files.
It then loads each file into a dictionary and searches the dictionary for a given key or value.
Parameters:
surveys_dirpath (str): The path of the directory to search.
search_value: The key or value to search for in the .yaml files.
Returns:
list: A list of dictionaries, each containing a filename, an unknown key, its value, and a list of keys.
Usage:
Search withing `surveys_dir` for the value `v1`:
`results = find_files_with_key_or_value('surveys_dir', 'v1')`
```
surveys_dir
├── survey_1.yaml
└── survey_2.yaml
```
survey_1.yaml
```
APP:
SURVEYS:
Survey 1:
FRAMES:
FRAME_1:
Data: 'v1'
```
survey_2.yaml
```
APP:
SURVEYS:
Survey 2:
FRAMES:
FRAME_1:
Data: 'v1'
```
`
"""
# Check if the provided directory exists
if not os.path.exists(surveys_dirpath):
raise ValueError(f"The provided directory {surveys_dirpath} does not exist.")
result_list = []
# Walk through the directory
for root, dirs, files in os.walk(surveys_dirpath):
for file in files:
# Check if the file is a .yaml file
if file.endswith(".yaml"):
# Construct the full file path
file_path = os.path.join(root, file)
# Open and load the yaml file
try:
with open(file_path, 'r') as stream:
data = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(f"Error loading YAML file {file_path}: {exc}")
continue
# Search for the key or value in the data
paths = find_value_in_dict(data, search_value)
for path in paths:
result_list.append({
'yaml_file': file,
'unknown_key': path[-1],
'value': data[path[-1]],
'nested_list': path
})
return result_list
def check_nested_dict(nested_dict:dict, keys_list:list):
"""
Helper function to recursively search for keys in a nested dictionary.
Parameters:
nested_dict (dict): The dictionary to search.
keys_list (list): The list of keys to check for.
Returns:
bool: True if all keys are found, False otherwise.
"""
for keys in keys_list:
temp_dict = nested_dict
for key in keys:
if key in temp_dict:
temp_dict = temp_dict[key]
else:
break
else:
return True
return False
def get_yaml_files_with_keys(dirpath:dict, keys_list:list):
"""
This function navigates through a directory and its subdirectories to find .yaml files.
It then checks if these files contain all the specified keys from a provided list.
Parameters:
dirpath (str): The path of the directory to search.
keys_list (list): The list of keys to check for in the .yaml files.
Returns:
dict: A dictionary with filenames as keys and boolean values indicating whether the
file contains all the specified keys.
Usage:
To check for the keys 'APP>SURVEYS>SURVEY_NAME' and 'APP>CONFIG>REMOTE>IP',
you would call the function like this:
`results = get_yaml_files_with_keys('surveys_dir', [['APP', 'SURVEYS', 'SURVEY_NAME'], ['APP', 'CONFIG', 'REMOTE', 'IP']])`
"""
# Dictionary to store results
file_dict = {}
# Check if the provided directory exists
if not os.path.exists(dirpath):
raise ValueError(f"The provided directory {dirpath} does not exist.")
# Walk through the directory
for root, dirs, files in os.walk(dirpath):
for file in files:
# Check if the file is a .yaml file
if file.endswith(".yaml"):
# Construct the full file path
file_path = os.path.join(root, file)
# Open and load the yaml file
try:
with open(file_path, 'r') as stream:
data = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(f"Error loading YAML file {file_path}: {exc}")
continue
# Check if all keys in the keys_list are in the data
if check_nested_dict(data, keys_list):
file_dict[file] = True
else:
file_dict[file] = False
return file_dict
def is_directory_empty(directory:str):
"""Check if a directory is empty or not.
Args:
directory (str): Path to the directory.
Returns:
bool: True if directory is empty, False otherwise.
Raises:
ValueError: If the provided path is not a directory.
OSError: If there is an error accessing the directory.
"""
try:
if not os.path.isdir(directory):
raise ValueError(f"{directory} is not a directory")
return len(os.listdir(directory)) == 0
except OSError as e:
print(f"Error accessing directory {directory}. Reason: {e}")
raise
def delete_directory_contents(directory:str):
"""Delete the contents of a directory if it is not empty.
Args:
directory (str): Path to the directory.
Raises:
ValueError: If the provided path is not a directory.
OSError: If there is an error accessing the directory or its contents.
"""
try:
if not os.path.isdir(directory):
raise ValueError(f"{directory} is not a directory")
if not is_directory_empty(directory):
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except OSError as e:
print(f'Failed to delete {file_path}. Reason: {e}')
raise
except OSError as e:
print(f"Error accessing directory {directory}. Reason: {e}")
raise
def check_or_create_subdirectory(directory: str, subdirectory: str, callback: Callable[[str], None]) -> None:
"""
Checks if a subdirectory exists within a specified directory. If the subdirectory does not exist, it is created.
The callback function is then called with a message indicating whether the subdirectory was created or already existed.
Args:
directory (str): The absolute or relative path of the parent directory.
subdirectory (str): The name of the subdirectory to be checked/created.
callback (Callable[[str], None]): A callback function that accepts a string message as an argument.
This function is called after the check/create operation with a message
indicating whether the subdirectory was created or already existed.
Returns:
None
Raises:
OSError: If the directory cannot be created due to a system-related error (like lack of permissions,
or the parent directory not existing)
Usage:
def callback_function(message: str) -> None:
print(message)
check_or_create_subdirectory('/path/to/directory', 'new_subdirectory', callback_function)
"""
try:
subdirectory_path = os.path.join(directory, subdirectory)
os.makedirs(subdirectory_path, exist_ok=True)
if os.path.isdir(subdirectory_path):
callback(f"The subdirectory '{subdirectory}' already exists at: {subdirectory_path}")
else:
callback(f"The subdirectory '{subdirectory}' was created at: {subdirectory_path}")
except OSError as e:
callback(f"Could not create the subdirectory '{subdirectory}' at: {subdirectory_path}. Error: {str(e)}")
def callback_function(message: str) -> None:
print(message)
def get_files_dictionary(dirpath: str, file_extension: str, keep_extension_in_key:bool = False) -> dict:
"""
Recursively search for files with the specified `file_extension` in the given `dirpath` and return a dictionary
with the filename (without extension) as the key and the full normalized path of the file as the value.
Args:
dirpath (str): The directory path to start the search.
file_extension (str): The file extension to filter the files.
keep_extension_in_key (bool, optional): If True, the file extension will be kept in the key of the dictionary.
Returns:
dict: A dictionary where the keys are the filenames (without extension) and the values are the full normalized paths of the files.
"""
if dirpath is None:
raise ValueError("The directory path cannot be None.")
if not os.path.isdir(dirpath):
raise ValueError(f"The specified directory path '{dirpath}' does not exist or is not a directory.")
files_dictionary = {}
for root, dirs, files in os.walk(dirpath):
for file in files:
if file.endswith(file_extension):
full_path = os.path.join(root, file)
if keep_extension_in_key:
files_dictionary[file] = os.path.normpath(full_path)
else:
filename_without_extension = os.path.splitext(file)[0]
files_dictionary[filename_without_extension] = os.path.normpath(full_path)
return files_dictionary
def create_directory_list(dirpath: str) -> list:
"""
Creates a list of directories within a given directory, excluding files.
Args:
dirpath (str): The path of the directory.
Returns:
list: A list of directories within the given directory.
Raises:
OSError: If there is an error accessing the directory.
"""
try:
directories = []
for item in os.listdir(dirpath):
item_path = os.path.join(dirpath, item)
if os.path.isdir(item_path):
directories.append(item_path)
return directories
except OSError as e:
raise OSError(f"Error accessing directory: {e}")
def create_file_list_with_extension(dirpath: str, extension: str) -> list:
"""
Creates a list of files within a given directory, filtered by file extension.
Args:
dirpath (str): The path of the directory.
extension (str): The file extension to filter by (e.g., ".txt", ".csv").
Returns:
list: A list of file paths within the given directory that match the specified file extension.
Raises:
OSError: If there is an error accessing the directory.
"""
try:
files = []
for item in os.listdir(dirpath):
item_path = os.path.join(dirpath, item)
if os.path.isfile(item_path) and item.endswith(extension):
files.append(item_path)
return files
except OSError as e:
raise OSError(f"Error accessing directory: {e}")
def check_dirpath_owner(dirpath: str):
"""
Checks the ownership of a directory.
Args:
dirpath (str): The path of the directory to check ownership for.
Returns:
dict: A dictionary containing the owner and group of the directory if it exists, otherwise an empty dictionary.
"""
path = Path(dirpath)
if path.exists():
return {"Owner": path.owner(), "Group": path.group()}
else:
return {}
def check_directory_exist_and_writable(dirpath, callback=None):
"""
Checks if a directory exists and if it is writable.
Args:
dirpath (str): The path of the directory to be checked.
callback (Optional[Callable[[str], None]], optional): A callback function
to be executed after checking the directory. The callback function
should accept a single string argument, which will be a status message.
Returns:
success (Optional[bool]): True if the directory exists and is writable,
False if the directory exists but is not writable, None if the directory doesn't exist.
"""
success = None
message = None
if os.path.exists(dirpath) and os.path.isdir(dirpath):
# Check if directory exists
if os.access(dirpath, os.W_OK):
# Check if directory is writable
message = f"The directory {dirpath} exists and is writable."
success = True
else:
ownership = check_dirpath_owner(dirpath)
# Get directory ownership
message = f"The directory {dirpath} exists but is not writable. Ownership: `{ownership}`"
success = False
else:
message = f"The directory {dirpath} does not exist."
success = None
if callback:
callback(message)
return success
def create_new_directory(dirpath, callback: Optional[Callable[[str], None]] = None):
"""
Creates a new directory if it does not exist already.
Args:
dirpath (str): The path of the directory to be created.
callback (Optional[Callable[[str], None]], optional): A callback function
to be executed after directory creation. The callback function
should accept a single string argument, which will be a status message.
Returns:
str: The path of the directory.
Raises:
OSError: If there is an error while creating the directory.
"""
try:
if not os.path.exists(dirpath):
os.makedirs(dirpath) # Create the directory if it doesn't exist
message = f"The directory {dirpath} was created."
else:
message = f"The directory {dirpath} already exists."
except OSError as e:
raise OSError(f"Error creating directory: {e}")
if callback:
callback(message)
return dirpath
def create_subdirectory(directory: str, subdirectory: str, callback: Optional[Callable[[str], None]] = None) -> str:
"""
**depreciation warning**: Use check_or_create_subdirectory instead.
Check if a subdirectory exists within a given directory and create it if it doesn't exist.
The function raises exceptions if the provided directory path is invalid or
if there's an error while creating the subdirectory. After the subdirectory is
successfully created or if it already exists, a callback function can be executed
with a status message as its argument.
Args:
directory (str): Path to the directory to check/create the subdirectory in.
subdirectory (str): Name of the subdirectory to check/create.
callback (Optional[Callable[[str], None]], optional): A callback function
to be executed after creating the subdirectory. The callback function
should accept a single string argument, which will be a status message.
Defaults to None.
Returns:
str: The full path to the subdirectory.
Raises:
ValueError: If the provided directory path is invalid.
OSError: If there is an error while creating the subdirectory.
"""
if not os.path.isdir(directory):
raise ValueError(f"'{directory}' is not a valid directory path.")
subdirectory_path = os.path.join(directory, subdirectory)
try:
if not os.path.exists(subdirectory_path):
os.makedirs(subdirectory_path)
message = f"Subdirectory '{subdirectory}' created in '{directory}'."
else:
message = f"Subdirectory '{subdirectory}' already exists in '{directory}'."
except OSError as e:
message = f"Error while creating the subdirectory: {str(e)}"
raise OSError(message) from e
if callback:
callback(message)
return subdirectory_path
def load_toml_variables(file_path: str) -> dict:
"""
Load variables from a .toml file into a dictionary.
Args:
file_path (str): Path to the .toml file.
Returns:
dict: Dictionary containing the loaded variables.
Raises:
IOError: If there is an error while loading the .toml file.
"""
try:
with open(file_path, "r") as file:
data = toml.load(file)
return data
except IOError:
raise IOError(f"Error: Unable to load .toml file from {file_path}")
return {}
def load_yaml(filepath: str) -> dict:
"""
Loads a YAML file.
Can be used as stand-alone script by providing a command-line argument:
python load_yaml.py --filepath /file/path/to/filename.yaml
python load_yaml.py --filepath http://example.com/path/to/filename.yaml
Args:
filepath (str): The absolute path to the YAML file or a URL to the YAML file.
Returns:
dict: The contents of the YAML file as a dictionary.
Raises:
FileNotFoundError: If the file does not exist.
yaml.YAMLError: If there is an error while loading the YAML file.
"""
if filepath.startswith('http://') or filepath.startswith('https://'):
try:
response = requests.get(filepath)
response.raise_for_status() # Raises a HTTPError if the response status is 4xx, 5xx
yaml_data = yaml.safe_load(response.text)
except (requests.RequestException, yaml.YAMLError) as e:
raise Exception(f'Error loading YAML from `{filepath}`. \n {str(e)}')
else:
return yaml_data
else:
if not os.path.isfile(filepath):
raise FileNotFoundError(f"No such file or directory: '{filepath}'")
with open(filepath, 'r') as file_descriptor:
try:
yaml_data = yaml.safe_load(file_descriptor)
except yaml.YAMLError as msg:
raise yaml.YAMLError(f'File `{filepath}` loading error. \n {msg}')
else:
return yaml_data
def load_yaml_from_file(file):
"""
Loads YAML data from a file object.
Args:
file: The file object representing the YAML file.
Returns:
dict: The loaded YAML data.
Raises:
yaml.YAMLError: If there is an error while loading the YAML data from the file.
"""
try:
yaml_data = yaml.safe_load(file)
except yaml.YAMLError as e:
raise yaml.YAMLError(f'File loading error. \n {e}')
else:
return yaml_data
def get_available_services(services_filepath: str) -> OrderedDict:
"""
Retrieves available services from a yaml file. These services can be used to
create a multi-page app using Streamlit.
Args:
services_filepath (str): The absolute path to the yaml file containing the services.
Returns:
OrderedDict: An ordered dictionary of services if any are available.
The dictionary is ordered based on the order of services in the yaml file.
Each key-value pair corresponds to a service name and its associated information.
Returns None if the yaml file does not contain any services.
Raises:
FileNotFoundError: If the services_filepath does not exist.
"""
if not os.path.isfile(services_filepath):
raise FileNotFoundError(f"No such file or directory: '{services_filepath}'")
available_services = load_yaml(filepath=os.path.abspath(services_filepath))
if available_services:
services_dict = OrderedDict({service['name']: service for service in available_services})
return services_dict
return None
def path_exists(path, path_type):
"""
Checks if a given path exists, whether it's a local file, remote URL, or LAN path.
Args:
path (str): The path to check.
path_type (str): The type of the path. It can be "local", "remote", or "lan".
Returns:
bool: True if the path exists, False otherwise.
"""
if path_type == "remote":
return is_remote_url(path)
elif path_type == "lan":
return is_lan_path(path)
else:
return os.path.exists(path)
def is_remote_url(path):
"""
Checks if the given path is a remote URL.
Args:
path (str): The path to check.
Returns:
bool: True if the path is a remote URL, False otherwise.
"""
return path.startswith('http://') or path.startswith('https://')
def is_lan_path(path):
"""
Checks if the given path is a LAN path.
Args:
path (str): The path to check.
Returns:
bool: True if the path is a LAN path, False otherwise.
"""
return path.startswith(os.sep + os.sep)
def find_files_with_key_or_value(surveys_dirpath:str, search_value:str):
"""
This function navigates through a directory and its subdirectories to find .yaml files.
It then loads each file into a dictionary and searches the dictionary for a given key or value.
Parameters:
surveys_dirpath (str): The path of the directory to search.
search_value: The key or value to search for in the .yaml files.
Returns:
list: A list of dictionaries, each containing a filename, an unknown key, its value, and a list of keys.
Usage:
Search withing `surveys_dir` for the value `v1`:
`results = find_files_with_key_or_value('surveys_dir', 'v1')`
```
surveys_dir
├── survey_1.yaml
└── survey_2.yaml
```
survey_1.yaml
```
APP:
SURVEYS:
Survey 1:
FRAMES:
FRAME_1:
Data: 'v1'
```
survey_2.yaml
```
APP:
SURVEYS:
Survey 2:
FRAMES:
FRAME_1:
Data: 'v1'
```
`
"""
# Check if the provided directory exists
if not os.path.exists(surveys_dirpath):
raise ValueError(f"The provided directory {surveys_dirpath} does not exist.")
result_list = []
# Walk through the directory
for root, dirs, files in os.walk(surveys_dirpath):
for file in files:
# Check if the file is a .yaml file
if file.endswith(".yaml"):
# Construct the full file path
file_path = os.path.join(root, file)
# Open and load the yaml file
try:
with open(file_path, 'r') as stream:
data = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(f"Error loading YAML file {file_path}: {exc}")
continue
# Search for the key or value in the data
paths = find_value_in_dict(data, search_value)
for path in paths:
result_list.append({
'yaml_file': file,
'unknown_key': path[-1],
'value': data[path[-1]],
'nested_list': path
})
return result_list
def check_nested_dict(nested_dict:dict, keys_list:list):
"""
Helper function to recursively search for keys in a nested dictionary.
Parameters:
nested_dict (dict): The dictionary to search.
keys_list (list): The list of keys to check for.
Returns:
bool: True if all keys are found, False otherwise.
"""
for keys in keys_list:
temp_dict = nested_dict
for key in keys:
if key in temp_dict:
temp_dict = temp_dict[key]
else:
break
else:
return True
return False
def get_yaml_files_with_keys(dirpath:dict, keys_list:list):
"""
This function navigates through a directory and its subdirectories to find .yaml files.
It then checks if these files contain all the specified keys from a provided list.
Parameters:
dirpath (str): The path of the directory to search.
keys_list (list): The list of keys to check for in the .yaml files.
Returns:
dict: A dictionary with filenames as keys and boolean values indicating whether the
file contains all the specified keys.
Usage:
To check for the keys 'APP>SURVEYS>SURVEY_NAME' and 'APP>CONFIG>REMOTE>IP',
you would call the function like this:
`results = get_yaml_files_with_keys('surveys_dir', [['APP', 'SURVEYS', 'SURVEY_NAME'], ['APP', 'CONFIG', 'REMOTE', 'IP']])`
"""
# Dictionary to store results
file_dict = {}
# Check if the provided directory exists
if not os.path.exists(dirpath):
raise ValueError(f"The provided directory {dirpath} does not exist.")
# Walk through the directory
for root, dirs, files in os.walk(dirpath):
for file in files:
# Check if the file is a .yaml file
if file.endswith(".yaml"):
# Construct the full file path
file_path = os.path.join(root, file)
# Open and load the yaml file
try:
with open(file_path, 'r') as stream:
data = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(f"Error loading YAML file {file_path}: {exc}")
continue
# Check if all keys in the keys_list are in the data
if check_nested_dict(data, keys_list):
file_dict[file] = True
else:
file_dict[file] = False
return file_dict
def is_directory_empty(directory:str):
"""Check if a directory is empty or not.
Args:
directory (str): Path to the directory.
Returns:
bool: True if directory is empty, False otherwise.
Raises:
ValueError: If the provided path is not a directory.
OSError: If there is an error accessing the directory.
"""
try:
if not os.path.isdir(directory):
raise ValueError(f"{directory} is not a directory")
return len(os.listdir(directory)) == 0
except OSError as e:
print(f"Error accessing directory {directory}. Reason: {e}")
raise
def delete_directory_contents(directory:str):
"""Delete the contents of a directory if it is not empty.
Args:
directory (str): Path to the directory.
Raises:
ValueError: If the provided path is not a directory.
OSError: If there is an error accessing the directory or its contents.
"""
try:
if not os.path.isdir(directory):
raise ValueError(f"{directory} is not a directory")
if not is_directory_empty(directory):
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except OSError as e:
print(f'Failed to delete {file_path}. Reason: {e}')
raise
except OSError as e:
print(f"Error accessing directory {directory}. Reason: {e}")
raise
def check_or_create_subdirectory(directory: str, subdirectory: str, callback: Callable[[str], None]) -> None:
"""
Checks if a subdirectory exists within a specified directory. If the subdirectory does not exist, it is created.
The callback function is then called with a message indicating whether the subdirectory was created or already existed.
Args:
directory (str): The absolute or relative path of the parent directory.
subdirectory (str): The name of the subdirectory to be checked/created.
callback (Callable[[str], None]): A callback function that accepts a string message as an argument.
This function is called after the check/create operation with a message
indicating whether the subdirectory was created or already existed.
Returns:
None
Raises:
OSError: If the directory cannot be created due to a system-related error (like lack of permissions,
or the parent directory not existing)
Usage:
def callback_function(message: str) -> None:
print(message)
check_or_create_subdirectory('/path/to/directory', 'new_subdirectory', callback_function)
"""
try:
subdirectory_path = os.path.join(directory, subdirectory)
os.makedirs(subdirectory_path, exist_ok=True)
if os.path.isdir(subdirectory_path):
callback(f"The subdirectory '{subdirectory}' already exists at: {subdirectory_path}")
else:
callback(f"The subdirectory '{subdirectory}' was created at: {subdirectory_path}")
except OSError as e:
callback(f"Could not create the subdirectory '{subdirectory}' at: {subdirectory_path}. Error: {str(e)}")
def get_files_dictionary(dirpath: str, file_extension: str, keep_extension_in_key:bool = False) -> dict:
"""
Recursively search for files with the specified `file_extension` in the given `dirpath` and return a dictionary
with the filename (without extension) as the key and the full normalized path of the file as the value.
Args:
dirpath (str): The directory path to start the search.
file_extension (str): The file extension to filter the files.
keep_extension_in_key (bool, optional): If True, the file extension will be kept in the key of the dictionary.
Returns:
dict: A dictionary where the keys are the filenames (without extension) and the values are the full normalized paths of the files.
"""
if dirpath is None:
raise ValueError("The directory path cannot be None.")
if not os.path.isdir(dirpath):
raise ValueError(f"The specified directory path '{dirpath}' does not exist or is not a directory.")
files_dictionary = {}
for root, dirs, files in os.walk(dirpath):
for file in files:
if file.endswith(file_extension):
full_path = os.path.join(root, file)
if keep_extension_in_key:
files_dictionary[file] = os.path.normpath(full_path)
else:
filename_without_extension = os.path.splitext(file)[0]
files_dictionary[filename_without_extension] = os.path.normpath(full_path)
return files_dictionary
def create_directory_list(dirpath: str) -> list:
"""
Creates a list of directories within a given directory, excluding files.
Args:
dirpath (str): The path of the directory.
Returns:
list: A list of directories within the given directory.
Raises:
OSError: If there is an error accessing the directory.
"""
try:
directories = []
for item in os.listdir(dirpath):
item_path = os.path.join(dirpath, item)
if os.path.isdir(item_path):
directories.append(item_path)
return directories
except OSError as e:
raise OSError(f"Error accessing directory: {e}")
def create_file_list_with_extension(dirpath: str, extension: str) -> list:
"""
Creates a list of files within a given directory, filtered by file extension.
Args:
dirpath (str): The path of the directory.
extension (str): The file extension to filter by (e.g., ".txt", ".csv").
Returns:
list: A list of file paths within the given directory that match the specified file extension.
Raises:
OSError: If there is an error accessing the directory.
"""
try:
files = []
for item in os.listdir(dirpath):
item_path = os.path.join(dirpath, item)
if os.path.isfile(item_path) and item.endswith(extension):
files.append(item_path)
return files
except OSError as e:
raise OSError(f"Error accessing directory: {e}")
def check_dirpath_owner(dirpath: str):
"""
Checks the ownership of a directory.
Args:
dirpath (str): The path of the directory to check ownership for.
Returns:
dict: A dictionary containing the owner and group of the directory if it exists, otherwise an empty dictionary.
"""
path = Path(dirpath)
if path.exists():
return {"Owner": path.owner(), "Group": path.group()}
else:
return {}
def check_directory_exist_and_writable(dirpath, callback=None):
"""
Checks if a directory exists and if it is writable.
Args:
dirpath (str): The path of the directory to be checked.
callback (Optional[Callable[[str], None]], optional): A callback function
to be executed after checking the directory. The callback function
should accept a single string argument, which will be a status message.