Skip to content

Commit 64c88a1

Browse files
authored
Merge pull request #8 from FoamyGuy/include_files_in_dirs
include files in first level of subdirectories
2 parents 06534e2 + f2552ee commit 64c88a1

File tree

2 files changed

+82
-27
lines changed

2 files changed

+82
-27
lines changed

create_requirement_images.py

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
TEXT_COLOR = "#B0B0B0"
3737
HIDDEN_TEXT_COLOR = "#808080"
3838

39-
SHOWN_FILETYPES = ["py", "mpy", "bmp", "pcf", "bdf", "wav", "mp3", "json", "txt"]
39+
SHOWN_FILETYPES = ["py", "mpy", "bmp", "pcf", "bdf", "wav", "mp3", "mid", "json", "txt"]
4040

4141
f = open("latest_bundle_data.json", "r")
4242
bundle_data = json.load(f)
@@ -70,6 +70,7 @@ def asset_path(asset_name):
7070
"bmp": file_image_icon,
7171
"wav": file_music_icon,
7272
"mp3": file_music_icon,
73+
"mid": file_music_icon,
7374
"pcf": file_font_icon,
7475
"bdf": file_font_icon,
7576
"json": file_icon,
@@ -159,73 +160,101 @@ def make_line(
159160

160161
def make_header(position, project_files):
161162
# Static files
162-
make_line("CIRCUITPY", position)
163+
make_line(
164+
"CIRCUITPY",
165+
(position[0] + INDENT_SIZE, position[1]),
166+
triangle_icon=down_triangle,
167+
)
163168
make_line(
164169
".fseventsd",
165-
(position[0] + INDENT_SIZE, position[1] + (LINE_SPACING * 1)),
170+
(position[0] + INDENT_SIZE * 2, position[1] + (LINE_SPACING * 1)),
166171
hidden=True,
167172
triangle_icon=right_triangle,
168173
)
169174
make_line(
170175
".metadata_never_index",
171-
(position[0] + INDENT_SIZE, position[1] + (LINE_SPACING * 2)),
176+
(position[0] + INDENT_SIZE * 2, position[1] + (LINE_SPACING * 2)),
172177
icon=file_empty_hidden_icon,
173178
hidden=True,
174179
)
175180
make_line(
176181
".Trashes",
177-
(position[0] + INDENT_SIZE, position[1] + (LINE_SPACING * 3)),
182+
(position[0] + INDENT_SIZE * 2, position[1] + (LINE_SPACING * 3)),
178183
icon=file_empty_hidden_icon,
179184
hidden=True,
180185
)
181186
make_line(
182187
"boot_out.txt",
183-
(position[0] + INDENT_SIZE, position[1] + (LINE_SPACING * 4)),
188+
(position[0] + INDENT_SIZE * 2, position[1] + (LINE_SPACING * 4)),
184189
)
185190
make_line(
186191
"code.py",
187-
(position[0] + INDENT_SIZE, position[1] + (LINE_SPACING * 5)),
192+
(position[0] + INDENT_SIZE * 2, position[1] + (LINE_SPACING * 5)),
188193
icon=file_icon,
189194
)
190195

191196
# dynamic files from project dir in learn guide repo
192197
rows_added = 0
193198
project_files_to_draw = []
194-
project_folders_to_draw = []
199+
project_folders_to_draw = {}
195200
for cur_file in project_files:
196-
if "." in cur_file[-5:]:
197-
cur_extension = cur_file.split(".")[-1]
198-
if cur_extension in SHOWN_FILETYPES:
199-
project_files_to_draw.append(cur_file)
200-
else:
201-
project_folders_to_draw.append(cur_file)
201+
# string for individual file
202+
if isinstance(cur_file, str):
203+
if "." in cur_file[-5:]:
204+
cur_extension = cur_file.split(".")[-1]
205+
if cur_extension in SHOWN_FILETYPES:
206+
project_files_to_draw.append(cur_file)
207+
# tuple for directory
208+
elif isinstance(cur_file, tuple):
209+
project_folders_to_draw[cur_file[0]] = cur_file[1]
202210

203211
for i, file in enumerate(sorted(project_files_to_draw)):
204212
cur_file_extension = file.split(".")[-1]
205213

206214
cur_file_icon = FILE_TYPE_ICON_MAP.get(cur_file_extension, file_empty_icon)
207215
make_line(
208216
file,
209-
(position[0] + INDENT_SIZE, position[1] + (LINE_SPACING * (6 + i))),
217+
(position[0] + INDENT_SIZE * 2, position[1] + (LINE_SPACING * (6 + i))),
210218
icon=cur_file_icon,
211219
)
212220
rows_added += 1
213221

214-
for i, file in enumerate(sorted(project_folders_to_draw)):
222+
extra_rows = 0
223+
for i, file in enumerate(sorted(project_folders_to_draw.keys())):
215224
make_line(
216225
file,
217226
(
218-
position[0] + INDENT_SIZE,
219-
position[1] + (LINE_SPACING * (6 + i + len(project_files_to_draw))),
227+
position[0] + INDENT_SIZE * 2,
228+
position[1]
229+
+ (
230+
LINE_SPACING * (6 + i + len(project_files_to_draw) + extra_rows)
231+
),
220232
),
221-
triangle_icon=right_triangle,
233+
triangle_icon=down_triangle,
222234
)
223235
rows_added += 1
236+
for sub_file in sorted(project_folders_to_draw[file]):
237+
extra_rows += 1
238+
cur_file_extension = sub_file.split(".")[-1]
239+
cur_file_icon = FILE_TYPE_ICON_MAP.get(cur_file_extension, folder_icon)
240+
triangle_icon = None
241+
if cur_file_icon == folder_icon:
242+
triangle_icon = right_triangle
243+
make_line(
244+
sub_file,
245+
(
246+
position[0] + INDENT_SIZE * 3,
247+
position[1] + (LINE_SPACING * (6 + rows_added)),
248+
),
249+
triangle_icon=triangle_icon,
250+
icon=cur_file_icon,
251+
)
252+
rows_added += 1
224253

225254
make_line(
226255
"lib",
227256
(
228-
position[0] + INDENT_SIZE,
257+
position[0] + INDENT_SIZE * 2,
229258
position[1] + (LINE_SPACING * (5 + rows_added + 1)),
230259
),
231260
triangle_icon=down_triangle,
@@ -280,6 +309,19 @@ def sort_libraries(libraries):
280309
package_list, file_list = get_dependencies(libraries)
281310
return sorted(package_list) + sorted(file_list)
282311

312+
def count_files(files_list):
313+
_count = 0
314+
for _file in files_list:
315+
# string for individual file
316+
if isinstance(_file, str):
317+
_count += 1
318+
319+
# tuple for directory
320+
elif isinstance(_file, tuple):
321+
_count += 1
322+
_count += len(_file[1])
323+
return _count
324+
283325
def make_libraries(libraries, position):
284326

285327
for i, lib_name in enumerate(libraries):
@@ -288,7 +330,7 @@ def make_libraries(libraries, position):
288330
triangle_icon = right_triangle
289331
make_line(
290332
lib_name,
291-
(position[0] + INDENT_SIZE * 2, position[1] + (LINE_SPACING * i)),
333+
(position[0] + INDENT_SIZE * 3, position[1] + (LINE_SPACING * i)),
292334
triangle_icon=triangle_icon,
293335
)
294336

@@ -300,7 +342,7 @@ def make_libraries(libraries, position):
300342
if "main.py" in project_files:
301343
project_files.remove("main.py")
302344

303-
project_files_count = len(project_files)
345+
project_files_count = count_files(project_files)
304346

305347
image_height = (
306348
PADDING * 2

get_imports.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"LEARN_GUIDE_REPO", "../Adafruit_Learning_System_Guides/"
2020
)
2121

22-
SHOWN_FILETYPES = ["py", "mpy", "bmp", "pcf", "bdf", "wav", "mp3", "json", "txt"]
22+
SHOWN_FILETYPES = ["py", "mpy", "bmp", "pcf", "bdf", "wav", "mp3", "mid", "json", "txt"]
2323
SHOWN_FILETYPES_EXAMPLE = [s for s in SHOWN_FILETYPES if s != "py"]
2424

2525

@@ -111,15 +111,28 @@ def get_files_for_project(project_name):
111111
"""Get the set of files for a learn project"""
112112
found_files = set()
113113
project_dir = "{}/{}/".format(LEARN_GUIDE_REPO, project_name)
114-
for file in os.listdir(project_dir):
114+
115+
full_tree = os.walk(project_dir)
116+
root_level = next(full_tree)
117+
118+
for file in root_level[2]:
115119
if "." in file:
116120
cur_extension = file.split(".")[-1]
117121
if cur_extension in SHOWN_FILETYPES:
118122
# print(file)
119123
found_files.add(file)
120-
else:
121-
# add dir
122-
found_files.add(file)
124+
125+
for _dir in root_level[1]:
126+
dir_tuple = (_dir, tuple())
127+
for cur_tuple in os.walk(project_dir):
128+
if cur_tuple[0].split("/")[-1] == _dir:
129+
for _sub_dir in cur_tuple[1]:
130+
dir_tuple = (dir_tuple[0], dir_tuple[1] + (_sub_dir,))
131+
for _sub_file in cur_tuple[2]:
132+
dir_tuple = (dir_tuple[0], dir_tuple[1] + (_sub_file,))
133+
134+
# e.g. ("dir_name", ("file_1.txt", "file_2.txt"))
135+
found_files.add(dir_tuple)
123136
return found_files
124137

125138

0 commit comments

Comments
 (0)