|
13 | 13 | import os
|
14 | 14 | import traceback
|
15 | 15 |
|
| 16 | +import click |
16 | 17 | from PIL import Image, ImageDraw, ImageFont
|
17 | 18 |
|
18 | 19 | from get_imports import (
|
19 | 20 | get_libs_for_project,
|
20 | 21 | get_files_for_project,
|
| 22 | + get_libs_for_example, |
| 23 | + get_files_for_example, |
21 | 24 | get_learn_guide_cp_projects,
|
22 | 25 | )
|
23 | 26 |
|
|
40 | 43 | bundle_data = json.load(f)
|
41 | 44 | f.close()
|
42 | 45 |
|
43 |
| -font = ImageFont.truetype("Roboto-Regular.ttf", 24) |
44 |
| -right_triangle = Image.open("img/right_triangle.png") |
45 |
| -down_triangle = Image.open("img/down_triangle.png") |
| 46 | +def ASSET(x): |
| 47 | + return os.path.join(os.path.dirname(__file__), x) |
| 48 | +font = ImageFont.truetype(ASSET("Roboto-Regular.ttf"), 24) |
| 49 | +right_triangle = Image.open(ASSET("img/right_triangle.png")) |
| 50 | +down_triangle = Image.open(ASSET("img/down_triangle.png")) |
46 | 51 |
|
47 |
| -folder_icon = Image.open("img/folder.png") |
48 |
| -folder_hidden_icon = Image.open("img/folder_hidden.png") |
49 |
| -file_icon = Image.open("img/file.png") |
50 |
| -file_hidden_icon = Image.open("img/file_hidden.png") |
51 |
| -file_empty_icon = Image.open("img/file_empty.png") |
52 |
| -file_empty_hidden_icon = Image.open("img/file_empty_hidden.png") |
| 52 | +folder_icon = Image.open(ASSET("img/folder.png")) |
| 53 | +folder_hidden_icon = Image.open(ASSET("img/folder_hidden.png")) |
| 54 | +file_icon = Image.open(ASSET("img/file.png")) |
| 55 | +file_hidden_icon = Image.open(ASSET("img/file_hidden.png")) |
| 56 | +file_empty_icon = Image.open(ASSET("img/file_empty.png")) |
| 57 | +file_empty_hidden_icon = Image.open(ASSET("img/file_empty_hidden.png")) |
53 | 58 |
|
54 |
| -file_image_icon = Image.open("img/file_image.png") |
55 |
| -file_music_icon = Image.open("img/file_music.png") |
56 |
| -file_font_icon = Image.open("img/file_font.png") |
| 59 | +file_image_icon = Image.open(ASSET("img/file_image.png")) |
| 60 | +file_music_icon = Image.open(ASSET("img/file_music.png")) |
| 61 | +file_font_icon = Image.open(ASSET("img/file_font.png")) |
57 | 62 |
|
58 | 63 | FILE_TYPE_ICON_MAP = {
|
59 | 64 | "py": file_icon,
|
|
73 | 78 |
|
74 | 79 |
|
75 | 80 | def generate_requirement_image(
|
76 |
| - learn_guide_project, |
| 81 | + project_files, libs, image_name |
77 | 82 | ): # pylint: disable=too-many-statements
|
78 | 83 | """Generate a single requirement image"""
|
79 | 84 |
|
@@ -149,7 +154,7 @@ def make_line(
|
149 | 154 | font=font,
|
150 | 155 | )
|
151 | 156 |
|
152 |
| - def make_header(position, learn_guide_project): |
| 157 | + def make_header(position, project_files): |
153 | 158 | # Static files
|
154 | 159 | make_line("CIRCUITPY", position)
|
155 | 160 | make_line(
|
@@ -181,24 +186,17 @@ def make_header(position, learn_guide_project):
|
181 | 186 | )
|
182 | 187 |
|
183 | 188 | # dynamic files from project dir in learn guide repo
|
184 |
| - project_files = get_files_for_project(learn_guide_project) |
185 | 189 | rows_added = 0
|
186 | 190 | project_files_to_draw = []
|
187 | 191 | project_folders_to_draw = []
|
188 | 192 | for cur_file in project_files:
|
189 | 193 | if "." in cur_file[-5:]:
|
190 | 194 | cur_extension = cur_file.split(".")[-1]
|
191 | 195 | if cur_extension in SHOWN_FILETYPES:
|
192 |
| - if cur_file != "main.py": |
193 |
| - project_files_to_draw.append(cur_file) |
| 196 | + project_files_to_draw.append(cur_file) |
194 | 197 | else:
|
195 | 198 | project_folders_to_draw.append(cur_file)
|
196 | 199 |
|
197 |
| - try: |
198 |
| - project_files_to_draw.remove("code.py") |
199 |
| - except ValueError: |
200 |
| - pass |
201 |
| - |
202 | 200 | for i, file in enumerate(sorted(project_files_to_draw)):
|
203 | 201 | cur_file_extension = file.split(".")[-1]
|
204 | 202 |
|
@@ -291,50 +289,76 @@ def make_libraries(libraries, position):
|
291 | 289 | triangle_icon=triangle_icon,
|
292 | 290 | )
|
293 | 291 |
|
294 |
| - try: |
295 |
| - libs = get_libs_for_project(learn_guide_project) |
296 |
| - final_list_to_render = sort_libraries(libs) |
| 292 | + final_list_to_render = sort_libraries(libs) |
297 | 293 |
|
298 |
| - project_file_list = get_files_for_project(learn_guide_project) |
299 | 294 |
|
300 |
| - project_files_count = len(project_file_list) |
| 295 | + if "code.py" in project_files: |
| 296 | + project_files.remove("code.py") |
301 | 297 |
|
302 |
| - if "code.py" in project_file_list: |
303 |
| - project_files_count -= 1 |
| 298 | + if "main.py" in project_files: |
| 299 | + project_files.remove("main.py") |
304 | 300 |
|
305 |
| - if "main.py" in project_file_list: |
306 |
| - project_files_count -= 1 |
| 301 | + project_files_count = len(project_files) |
307 | 302 |
|
308 |
| - image_height = ( |
309 |
| - PADDING * 2 |
310 |
| - + 7 * LINE_SPACING |
311 |
| - + len(final_list_to_render) * LINE_SPACING |
312 |
| - + (project_files_count) * LINE_SPACING |
313 |
| - ) |
314 |
| - img = Image.new("RGB", (OUT_WIDTH, image_height), "#303030") |
315 |
| - draw = ImageDraw.Draw(img) |
| 303 | + image_height = ( |
| 304 | + PADDING * 2 |
| 305 | + + 7 * LINE_SPACING |
| 306 | + + len(final_list_to_render) * LINE_SPACING |
| 307 | + + (project_files_count) * LINE_SPACING |
| 308 | + ) |
| 309 | + img = Image.new("RGB", (OUT_WIDTH, image_height), "#303030") |
| 310 | + draw = ImageDraw.Draw(img) |
316 | 311 |
|
317 |
| - make_background_highlights( |
318 |
| - 7 + len(final_list_to_render) + project_files_count, |
319 |
| - offset=(PADDING, PADDING), |
320 |
| - ) |
| 312 | + make_background_highlights( |
| 313 | + 7 + len(final_list_to_render) + project_files_count, |
| 314 | + offset=(PADDING, PADDING), |
| 315 | + ) |
321 | 316 |
|
322 |
| - make_header((PADDING, PADDING), learn_guide_project) |
323 |
| - make_libraries( |
324 |
| - final_list_to_render, |
325 |
| - (PADDING, PADDING + (LINE_SPACING * (7 + project_files_count))), |
326 |
| - ) |
| 317 | + make_header((PADDING, PADDING), project_files) |
| 318 | + make_libraries( |
| 319 | + final_list_to_render, |
| 320 | + (PADDING, PADDING + (LINE_SPACING * (7 + project_files_count))), |
| 321 | + ) |
327 | 322 |
|
328 |
| - img.save( |
329 |
| - "generated_images/{}.png".format(learn_guide_project.replace("/", "_")) |
330 |
| - ) |
331 |
| - except SyntaxError as exc: |
332 |
| - print(exc) |
333 |
| - traceback.print_exc() |
334 |
| - print("SyntaxError finding imports for {}".format(learn_guide_project)) |
| 323 | + img.save( |
| 324 | + "generated_images/{}.png".format(image_name) |
| 325 | + ) |
335 | 326 |
|
| 327 | +def generate_learn_requirement_image( |
| 328 | + learn_guide_project, |
| 329 | +): |
| 330 | + image_name = learn_guide_project.replace("/", "_") |
| 331 | + libs = get_libs_for_project(learn_guide_project) |
| 332 | + project_files = get_files_for_project(learn_guide_project) |
| 333 | + generate_requirement_image(project_files, libs, image_name) |
| 334 | + |
| 335 | +def generate_example_requirement_image( |
| 336 | + example_path |
| 337 | +): |
| 338 | + image_name = "_".join(element for element in example_path.split('/') |
| 339 | + if element not in ('libraries', 'drivers', 'helpers', 'examples')) |
| 340 | + libs = get_libs_for_example(example_path) |
| 341 | + project_files = get_files_for_example(example_path) |
| 342 | + generate_requirement_image(project_files, libs, image_name) |
| 343 | + |
| 344 | +@click.group(invoke_without_command=True) |
| 345 | +@click.pass_context |
| 346 | +def cli(ctx): |
| 347 | + if ctx.invoked_subcommand is None: |
| 348 | + learn() |
| 349 | + |
| 350 | +@cli.command() |
| 351 | +def learn(): |
| 352 | + with Pool() as p: |
| 353 | + for _ in p.imap(generate_learn_requirement_image, get_learn_guide_cp_projects()): |
| 354 | + pass |
336 | 355 |
|
337 |
| -if __name__ == "__main__": |
| 356 | +@cli.command() |
| 357 | +@click.argument('paths', nargs=-1) |
| 358 | +def bundle(paths): |
338 | 359 | with Pool() as p:
|
339 |
| - for _ in p.imap(generate_requirement_image, get_learn_guide_cp_projects()): |
| 360 | + for _ in p.imap(generate_example_requirement_image, paths): |
340 | 361 | pass
|
| 362 | + |
| 363 | +if __name__ == "__main__": |
| 364 | + cli() |
0 commit comments