Skip to content

Commit da6907b

Browse files
authored
Merge pull request #14 from FoamyGuy/settings_toml
add settings.toml to the rendered screenshots if needed.
2 parents 68ca817 + 3b577a3 commit da6907b

File tree

2 files changed

+88
-12
lines changed

2 files changed

+88
-12
lines changed

create_requirement_images.py

+51-12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
get_files_for_example,
2424
get_learn_guide_cp_projects,
2525
)
26+
from settings_required import settings_required
2627

2728
os.makedirs("generated_images", exist_ok=True)
2829

@@ -66,6 +67,7 @@ def asset_path(asset_name):
6667
"py": file_icon,
6768
"mpy": file_icon,
6869
"txt": file_empty_icon,
70+
"toml": file_icon,
6971
"bmp": file_image_icon,
7072
"wav": file_music_icon,
7173
"mp3": file_music_icon,
@@ -78,15 +80,17 @@ def asset_path(asset_name):
7880
}
7981

8082
# If this is not done, the images fail to load in the subprocesses.
81-
for file_icon in FILE_TYPE_ICON_MAP.values():
82-
file_icon.load()
83+
for _file_icon in FILE_TYPE_ICON_MAP.values():
84+
_file_icon.load()
8385

8486

8587
def generate_requirement_image(
8688
project_files, libs, image_name
87-
): # pylint: disable=too-many-statements
89+
): # pylint: disable=too-many-statements, too-many-locals
8890
"""Generate a single requirement image"""
8991

92+
context = {"added_settings_toml": False}
93+
9094
def make_line(
9195
requirement_name, position=(0, 0), icon=None, hidden=False, triangle_icon=None
9296
): # pylint: disable=too-many-branches
@@ -159,7 +163,8 @@ def make_line(
159163
font=font,
160164
)
161165

162-
def make_header(position, project_files):
166+
def make_header(position, project_files, files_and_libs):
167+
# pylint: disable=too-many-locals
163168
# Static files
164169
make_line(
165170
"CIRCUITPY",
@@ -194,6 +199,21 @@ def make_header(position, project_files):
194199
icon=file_icon,
195200
)
196201

202+
# TODO: Add settings.toml if it's needed
203+
204+
if settings_required(files_and_libs):
205+
make_line(
206+
"settings.toml",
207+
(position[0] + INDENT_SIZE * 2, position[1] + (LINE_SPACING * 6)),
208+
icon=file_icon,
209+
)
210+
context["added_settings_toml"] = True
211+
212+
if project_files:
213+
print(image_name)
214+
print(project_files)
215+
print("=============")
216+
197217
# dynamic files from project dir in learn guide repo
198218
rows_added = 0
199219
project_files_to_draw = []
@@ -210,13 +230,18 @@ def make_header(position, project_files):
210230
if ".circuitpython.skip-screenshot" not in cur_file[1]:
211231
project_folders_to_draw[cur_file[0]] = cur_file[1]
212232

233+
begin_y_offset = 7 if context["added_settings_toml"] else 6
213234
for i, file in enumerate(sorted(project_files_to_draw)):
214235
cur_file_extension = file.split(".")[-1]
215236

216237
cur_file_icon = FILE_TYPE_ICON_MAP.get(cur_file_extension, file_empty_icon)
238+
217239
make_line(
218240
file,
219-
(position[0] + INDENT_SIZE * 2, position[1] + (LINE_SPACING * (6 + i))),
241+
(
242+
position[0] + INDENT_SIZE * 2,
243+
position[1] + (LINE_SPACING * (begin_y_offset + i)),
244+
),
220245
icon=cur_file_icon,
221246
)
222247
rows_added += 1
@@ -229,7 +254,8 @@ def make_header(position, project_files):
229254
position[0] + INDENT_SIZE * 2,
230255
position[1]
231256
+ (
232-
LINE_SPACING * (6 + i + len(project_files_to_draw) + extra_rows)
257+
LINE_SPACING
258+
* (begin_y_offset + i + len(project_files_to_draw) + extra_rows)
233259
),
234260
),
235261
triangle_icon=down_triangle,
@@ -246,7 +272,7 @@ def make_header(position, project_files):
246272
sub_file,
247273
(
248274
position[0] + INDENT_SIZE * 3,
249-
position[1] + (LINE_SPACING * (6 + rows_added)),
275+
position[1] + (LINE_SPACING * (begin_y_offset + rows_added)),
250276
),
251277
triangle_icon=triangle_icon,
252278
icon=cur_file_icon,
@@ -257,7 +283,7 @@ def make_header(position, project_files):
257283
"lib",
258284
(
259285
position[0] + INDENT_SIZE * 2,
260-
position[1] + (LINE_SPACING * (5 + rows_added + 1)),
286+
position[1] + (LINE_SPACING * ((begin_y_offset - 1) + rows_added + 1)),
261287
),
262288
triangle_icon=down_triangle,
263289
)
@@ -333,11 +359,20 @@ def make_libraries(libraries, position):
333359
triangle_icon = right_triangle
334360
make_line(
335361
lib_name,
336-
(position[0] + INDENT_SIZE * 3, position[1] + (LINE_SPACING * i)),
362+
(
363+
position[0] + INDENT_SIZE * 3,
364+
position[1]
365+
+ (
366+
LINE_SPACING
367+
* (i + (1 if context["added_settings_toml"] else 0))
368+
),
369+
),
337370
triangle_icon=triangle_icon,
338371
)
339372

340373
final_list_to_render = sort_libraries(libs)
374+
if settings_required(final_list_to_render):
375+
context["added_settings_toml"] = True
341376

342377
if "code.py" in project_files:
343378
project_files.remove("code.py")
@@ -352,16 +387,20 @@ def make_libraries(libraries, position):
352387
+ 7 * LINE_SPACING
353388
+ len(final_list_to_render) * LINE_SPACING
354389
+ (project_files_count) * LINE_SPACING
390+
+ (1 if context["added_settings_toml"] else 0) * LINE_SPACING
355391
)
356392
img = Image.new("RGB", (OUT_WIDTH, image_height), "#303030")
357393
draw = ImageDraw.Draw(img)
358394

359395
make_background_highlights(
360-
7 + len(final_list_to_render) + project_files_count,
396+
7
397+
+ len(final_list_to_render)
398+
+ project_files_count
399+
+ (1 if context["added_settings_toml"] else 0),
361400
offset=(PADDING, PADDING),
362401
)
363-
364-
make_header((PADDING, PADDING), project_files)
402+
print(f"fltr: {final_list_to_render}")
403+
make_header((PADDING, PADDING), project_files, final_list_to_render)
365404
make_libraries(
366405
final_list_to_render,
367406
(PADDING, PADDING + (LINE_SPACING * (7 + project_files_count))),

settings_required.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# SPDX-FileCopyrightText: 2023 Tim C
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
Utility function to check whether settings.toml file should be rendered for a project.
6+
Based on whether it uses certain libraries or not.
7+
"""
8+
9+
10+
LIBRARIES_THAT_REQUIRE_SETTINGS = [
11+
"adafruit_requests.mpy",
12+
"adafruit_esp32spi",
13+
"adafruit_minimqtt",
14+
"adafruit_portalbase",
15+
"adafruit_azureiot",
16+
]
17+
18+
19+
def settings_required(files_and_libs):
20+
"""
21+
Returns True if the project needs ot have a settings.toml file
22+
23+
:param files_and_libs list: a List of all files and libraries used in the project
24+
"""
25+
26+
if "settings.toml" in files_and_libs:
27+
# settings.toml file is already in the files so we don't need to add it again
28+
return False
29+
30+
# if any of the libraries that require settings.toml are included in this project
31+
if any(
32+
libs_that_require_settings in files_and_libs
33+
for libs_that_require_settings in LIBRARIES_THAT_REQUIRE_SETTINGS
34+
):
35+
return True
36+
37+
return False

0 commit comments

Comments
 (0)