forked from cuddlyogre/ExportLDraw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem.py
More file actions
141 lines (102 loc) · 3.88 KB
/
filesystem.py
File metadata and controls
141 lines (102 loc) · 3.88 KB
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
import os
import string
import glob
from sys import platform
from pathlib import Path
defaults = dict()
defaults['ldraw_path'] = ''
defaults['prefer_unofficial'] = False
defaults['resolution'] = 'Standard'
ldraw_path = defaults['ldraw_path']
prefer_unofficial = defaults['prefer_unofficial']
resolution = defaults['resolution']
search_paths = []
texture_paths = []
lowercase_paths = {}
def reset_caches():
global search_paths
global texture_paths
global lowercase_paths
search_paths = []
texture_paths = []
lowercase_paths = {}
def append_search_path(path):
if path[0] != "" and os.path.isdir(path[0]):
search_paths.append(path)
def locate_ldraw():
ldraw_folder_name = 'ldraw'
home = str(Path.home())
ldraw_path = os.path.join(home, ldraw_folder_name)
if os.path.isdir(ldraw_path):
return ldraw_path
if platform == "linux" or platform == "linux2":
pass
# linux
elif platform == "darwin":
pass
# OS X
elif platform == "win32":
for drive_letter in string.ascii_lowercase:
ldraw_path = os.path.join(os.path.join(f"{drive_letter}:\\", ldraw_folder_name))
if os.path.isdir(ldraw_path):
return ldraw_path
return ""
def build_lowercase_paths():
global lowercase_paths
lowercase_paths = {}
for path in search_paths:
for file in glob.glob(os.path.join(path[0], path[1])):
lowercase_paths[file.lower()] = file
def build_search_paths(parent_filepath=None):
reset_caches()
# https://forums.ldraw.org/thread-24495-post-40577.html#pid40577
# append top level file's directory
if parent_filepath is not None:
append_search_path((os.path.dirname(parent_filepath), '**/*'))
append_search_path((os.path.dirname(parent_filepath), '*'))
append_search_path((os.path.join(ldraw_path), '*'))
if prefer_unofficial:
append_unofficial_paths()
append_official_paths()
else:
append_official_paths()
append_unofficial_paths()
build_lowercase_paths()
def append_paths(folder=''):
append_search_path((os.path.join(ldraw_path, folder, "models"), '**/*'))
append_search_path((os.path.join(ldraw_path, folder, "models"), '*'))
append_search_path((os.path.join(ldraw_path, folder, "parts", "textures"), '**/*'))
append_search_path((os.path.join(ldraw_path, folder, "parts", "textures"), '*'))
append_search_path((os.path.join(ldraw_path, folder, "parts"), '**/*'))
append_search_path((os.path.join(ldraw_path, folder, "parts"), '*'))
if resolution == "High":
append_search_path((os.path.join(ldraw_path, folder, "p", "48"), '**/*'))
append_search_path((os.path.join(ldraw_path, folder, "p", "48"), '*'))
elif resolution == "Low":
append_search_path((os.path.join(ldraw_path, folder, "p", "8"), '**/*'))
append_search_path((os.path.join(ldraw_path, folder, "p", "8"), '*'))
append_search_path((os.path.join(ldraw_path, folder, "p"), '**/*'))
append_search_path((os.path.join(ldraw_path, folder, "p"), '*'))
def append_official_paths():
append_paths()
def append_unofficial_paths():
append_paths(folder="unofficial")
# https://stackoverflow.com/a/8462613
def path_lowercase(path):
if path.lower() in lowercase_paths:
return lowercase_paths[path.lower()]
return path
def locate(filename):
part_path = filename.replace("\\", os.path.sep).replace("/", os.path.sep)
part_path = os.path.expanduser(part_path)
# full path was specified
if os.path.isfile(part_path):
return part_path
for path in search_paths:
full_path = os.path.join(path[0], part_path)
full_path = path_lowercase(full_path)
if os.path.isfile(full_path):
return full_path
# TODO: requests retrieve missing items from ldraw.org
print(f"missing {filename}")
return None