Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
iperov
GitHub Repository: iperov/deepfacelab
Path: blob/master/core/pathex.py
628 views
1
from pathlib import Path
2
from os import scandir
3
4
image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff"]
5
6
def write_bytes_safe(p, bytes_data):
7
"""
8
writes to .tmp first and then rename to target filename
9
"""
10
p_tmp = p.parent / (p.name + '.tmp')
11
p_tmp.write_bytes(bytes_data)
12
if p.exists():
13
p.unlink()
14
p_tmp.rename (p)
15
16
def scantree(path):
17
"""Recursively yield DirEntry objects for given directory."""
18
for entry in scandir(path):
19
if entry.is_dir(follow_symlinks=False):
20
yield from scantree(entry.path) # see below for Python 2.x
21
else:
22
yield entry
23
24
def get_image_paths(dir_path, image_extensions=image_extensions, subdirs=False, return_Path_class=False):
25
dir_path = Path (dir_path)
26
27
result = []
28
if dir_path.exists():
29
30
if subdirs:
31
gen = scantree(str(dir_path))
32
else:
33
gen = scandir(str(dir_path))
34
35
for x in list(gen):
36
if any([x.name.lower().endswith(ext) for ext in image_extensions]):
37
result.append( x.path if not return_Path_class else Path(x.path) )
38
return sorted(result)
39
40
def get_image_unique_filestem_paths(dir_path, verbose_print_func=None):
41
result = get_image_paths(dir_path)
42
result_dup = set()
43
44
for f in result[:]:
45
f_stem = Path(f).stem
46
if f_stem in result_dup:
47
result.remove(f)
48
if verbose_print_func is not None:
49
verbose_print_func ("Duplicate filenames are not allowed, skipping: %s" % Path(f).name )
50
continue
51
result_dup.add(f_stem)
52
53
return sorted(result)
54
55
def get_paths(dir_path):
56
dir_path = Path (dir_path)
57
58
if dir_path.exists():
59
return [ Path(x) for x in sorted([ x.path for x in list(scandir(str(dir_path))) ]) ]
60
else:
61
return []
62
63
def get_file_paths(dir_path):
64
dir_path = Path (dir_path)
65
66
if dir_path.exists():
67
return [ Path(x) for x in sorted([ x.path for x in list(scandir(str(dir_path))) if x.is_file() ]) ]
68
else:
69
return []
70
71
def get_all_dir_names (dir_path):
72
dir_path = Path (dir_path)
73
74
if dir_path.exists():
75
return sorted([ x.name for x in list(scandir(str(dir_path))) if x.is_dir() ])
76
else:
77
return []
78
79
def get_all_dir_names_startswith (dir_path, startswith):
80
dir_path = Path (dir_path)
81
startswith = startswith.lower()
82
83
result = []
84
if dir_path.exists():
85
for x in list(scandir(str(dir_path))):
86
if x.name.lower().startswith(startswith):
87
result.append ( x.name[len(startswith):] )
88
return sorted(result)
89
90
def get_first_file_by_stem (dir_path, stem, exts=None):
91
dir_path = Path (dir_path)
92
stem = stem.lower()
93
94
if dir_path.exists():
95
for x in sorted(list(scandir(str(dir_path))), key=lambda x: x.name):
96
if not x.is_file():
97
continue
98
xp = Path(x.path)
99
if xp.stem.lower() == stem and (exts is None or xp.suffix.lower() in exts):
100
return xp
101
102
return None
103
104
def move_all_files (src_dir_path, dst_dir_path):
105
paths = get_file_paths(src_dir_path)
106
for p in paths:
107
p = Path(p)
108
p.rename ( Path(dst_dir_path) / p.name )
109
110
def delete_all_files (dir_path):
111
paths = get_file_paths(dir_path)
112
for p in paths:
113
p = Path(p)
114
p.unlink()
115
116