From e000b23971e449a43df8b84d96e4dc35c9414447 Mon Sep 17 00:00:00 2001 From: Ryan Jarvis Date: Thu, 11 Mar 2021 21:38:36 -0800 Subject: [PATCH 1/3] Use contextmanagers for writing to files (cherry picked from commit 68c825a307802e3ac91b150ce1de224b824b152a) --- pico_project.py | 229 +++++++++++++++++++++++------------------------- 1 file changed, 111 insertions(+), 118 deletions(-) diff --git a/pico_project.py b/pico_project.py index 3ace1b6..c94ae69 100755 --- a/pico_project.py +++ b/pico_project.py @@ -828,59 +828,58 @@ def GenerateMain(folder, projectName, features, cpp): else: filename = Path(folder) / (projectName + '.c') - file = open(filename, 'w') - - main = ('#include \n' - '#include "pico/stdlib.h"\n' - ) - file.write(main) - - if (features): - - # Add any includes - for feat in features: - if (feat in features_list): - o = '#include "' + features_list[feat][H_FILE] + '"\n' - file.write(o) - if (feat in stdlib_examples_list): - o = '#include "' + stdlib_examples_list[feat][H_FILE] + '"\n' - file.write(o) - - file.write('\n') - - # Add any defines - for feat in features: - if (feat in code_fragments_per_feature): - for s in code_fragments_per_feature[feat][DEFINES]: - file.write(s) - file.write('\n') - file.write('\n') + with open(filename, 'w') as file: - main = ('\n\n' - 'int main()\n' - '{\n' - ' stdio_init_all();\n\n' - ) + main = ('#include \n' + '#include "pico/stdlib.h"\n' + ) + file.write(main) + + if (features): + + # Add any includes + for feat in features: + if (feat in features_list): + o = '#include "' + features_list[feat][H_FILE] + '"\n' + file.write(o) + if (feat in stdlib_examples_list): + o = '#include "' + stdlib_examples_list[feat][H_FILE] + '"\n' + file.write(o) + + file.write('\n') + + # Add any defines + for feat in features: + if (feat in code_fragments_per_feature): + for s in code_fragments_per_feature[feat][DEFINES]: + file.write(s) + file.write('\n') + file.write('\n') - if (features): - # Add any initialisers - indent = 4 - for feat in features: - if (feat in code_fragments_per_feature): - for s in code_fragments_per_feature[feat][INITIALISERS]: - main += (" " * indent) - main += s - main += '\n' - main += '\n' + main = ('\n\n' + 'int main()\n' + '{\n' + ' stdio_init_all();\n\n' + ) - main += (' puts("Hello, world!");\n\n' - ' return 0;\n' - '}\n' - ) + if (features): + # Add any initialisers + indent = 4 + for feat in features: + if (feat in code_fragments_per_feature): + for s in code_fragments_per_feature[feat][INITIALISERS]: + main += (" " * indent) + main += s + main += '\n' + main += '\n' + + main += (' puts("Hello, world!");\n\n' + ' return 0;\n' + '}\n' + ) - file.write(main) + file.write(main) - file.close() def GenerateCMake(folder, params): @@ -906,82 +905,80 @@ def GenerateCMake(folder, params): filename = Path(folder) / CMAKELIST_FILENAME - file = open(filename, 'w') - - file.write(cmake_header1) + with open(filename, 'w') as file: - # OK, for the path, CMake will accept forward slashes on Windows, and thats - # seemingly a bit easier to handle than the backslashes + file.write(cmake_header1) - p = str(params.sdkPath).replace('\\','/') - p = '\"' + p + '\"' + # OK, for the path, CMake will accept forward slashes on Windows, and thats + # seemingly a bit easier to handle than the backslashes - file.write('set(PICO_SDK_PATH ' + p + ')\n\n') - file.write(cmake_header2) - file.write('project(' + params.projectName + ' C CXX ASM)\n') + p = str(params.sdkPath).replace('\\','/') + p = '\"' + p + '\"' - if params.exceptions: - file.write("\nset(PICO_CXX_ENABLE_EXCEPTIONS 1)\n") + file.write('set(PICO_SDK_PATH ' + p + ')\n\n') + file.write(cmake_header2) + file.write('project(' + params.projectName + ' C CXX ASM)\n') - if params.rtti: - file.write("\nset(PICO_CXX_ENABLE_RTTI 1)\n") + if params.exceptions: + file.write("\nset(PICO_CXX_ENABLE_EXCEPTIONS 1)\n") - file.write(cmake_header3) + if params.rtti: + file.write("\nset(PICO_CXX_ENABLE_RTTI 1)\n") - # add the preprocessor defines for overall configuration - if params.configs: - file.write('# Add any PICO_CONFIG entries specified in the Advanced settings\n') - for c, v in params.configs.items(): - if v == "True": - v = "1" - elif v == "False": - v = "0" - file.write('add_compile_definitions(' + c + '=' + v + ')\n') - file.write('\n') + file.write(cmake_header3) - # No GUI/command line to set a different executable name at this stage - executableName = params.projectName + # add the preprocessor defines for overall configuration + if params.configs: + file.write('# Add any PICO_CONFIG entries specified in the Advanced settings\n') + for c, v in params.configs.items(): + if v == "True": + v = "1" + elif v == "False": + v = "0" + file.write('add_compile_definitions(' + c + '=' + v + ')\n') + file.write('\n') - if params.wantCPP: - file.write('add_executable(' + params.projectName + ' ' + params.projectName + '.cpp )\n\n') - else: - file.write('add_executable(' + params.projectName + ' ' + params.projectName + '.c )\n\n') + # No GUI/command line to set a different executable name at this stage + executableName = params.projectName - file.write('pico_set_program_name(' + params.projectName + ' "' + executableName + '")\n') - file.write('pico_set_program_version(' + params.projectName + ' "0.1")\n\n') + if params.wantCPP: + file.write('add_executable(' + params.projectName + ' ' + params.projectName + '.cpp )\n\n') + else: + file.write('add_executable(' + params.projectName + ' ' + params.projectName + '.c )\n\n') - if params.wantRunFromRAM: - file.write('# no_flash means the target is to run from RAM\n') - file.write('pico_set_binary_type(' + params.projectName + ' no_flash)\n\n') + file.write('pico_set_program_name(' + params.projectName + ' "' + executableName + '")\n') + file.write('pico_set_program_version(' + params.projectName + ' "0.1")\n\n') - # Console output destinations - if params.wantUART: - file.write('pico_enable_stdio_uart(' + params.projectName + ' 1)\n') - else: - file.write('pico_enable_stdio_uart(' + params.projectName + ' 0)\n') + if params.wantRunFromRAM: + file.write('# no_flash means the target is to run from RAM\n') + file.write('pico_set_binary_type(' + params.projectName + ' no_flash)\n\n') - if params.wantUSB: - file.write('pico_enable_stdio_usb(' + params.projectName + ' 1)\n\n') - else: - file.write('pico_enable_stdio_usb(' + params.projectName + ' 0)\n\n') + # Console output destinations + if params.wantUART: + file.write('pico_enable_stdio_uart(' + params.projectName + ' 1)\n') + else: + file.write('pico_enable_stdio_uart(' + params.projectName + ' 0)\n') - # Standard libraries - file.write('# Add the standard library to the build\n') - file.write('target_link_libraries(' + params.projectName + ' ' + STANDARD_LIBRARIES + ')\n\n') + if params.wantUSB: + file.write('pico_enable_stdio_usb(' + params.projectName + ' 1)\n\n') + else: + file.write('pico_enable_stdio_usb(' + params.projectName + ' 0)\n\n') + # Standard libraries + file.write('# Add the standard library to the build\n') + file.write('target_link_libraries(' + params.projectName + ' ' + STANDARD_LIBRARIES + ')\n\n') - # Selected libraries/features - if (params.features): - file.write('# Add any user requested libraries\n') - file.write('target_link_libraries(' + params.projectName + '\n') - for feat in params.features: - if (feat in features_list): - file.write(" " + features_list[feat][LIB_NAME] + '\n') - file.write(' )\n\n') - file.write('pico_add_extra_outputs(' + params.projectName + ')\n\n') + # Selected libraries/features + if (params.features): + file.write('# Add any user requested libraries\n') + file.write('target_link_libraries(' + params.projectName + '\n') + for feat in params.features: + if (feat in features_list): + file.write(" " + features_list[feat][LIB_NAME] + '\n') + file.write(' )\n\n') - file.close() + file.write('pico_add_extra_outputs(' + params.projectName + ')\n\n') # Generates the requested project files, if any @@ -1078,21 +1075,17 @@ def generateProjectFiles(projectPath, projectName, sdkPath, projects, debugger): os.chdir(VSCODE_FOLDER) filename = VSCODE_LAUNCH_FILENAME - file = open(filename, 'w') - file.write(v1) - file.close() + with open(filename, 'w') as file: + file.write(v1) - file = open(VSCODE_C_PROPERTIES_FILENAME, 'w') - file.write(c1) - file.close() + with open(VSCODE_C_PROPERTIES_FILENAME, 'w') as file: + file.write(c1) - file = open(VSCODE_SETTINGS_FILENAME, 'w') - file.write(s1) - file.close() + with open(VSCODE_SETTINGS_FILENAME, 'w') as file: + file.write(s1) - file = open(VSCODE_EXTENSIONS_FILENAME, 'w') - file.write(e1) - file.close() + with open(VSCODE_EXTENSIONS_FILENAME, 'w') as file: + file.write(e1) else : print('Unknown project type requested') From 564f48b456aab8c40e7d51b59509af3b5270c6c2 Mon Sep 17 00:00:00 2001 From: Ryan Jarvis Date: Thu, 11 Mar 2021 21:39:57 -0800 Subject: [PATCH 2/3] remove unused imports (cherry picked from commit d182d43bda3a1950f907d25d62be41194f375693) --- pico_project.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/pico_project.py b/pico_project.py index c94ae69..1ed9596 100755 --- a/pico_project.py +++ b/pico_project.py @@ -7,19 +7,18 @@ # import argparse +import csv import os -import shutil -from pathlib import Path -import sys -import subprocess -from time import sleep import platform import shlex -import csv +import shutil +import subprocess +import sys +from pathlib import Path import tkinter as tk -from tkinter import messagebox as mb from tkinter import filedialog as fd +from tkinter import messagebox as mb from tkinter import simpledialog as sd from tkinter import ttk From 5e29ba38d564748d8f11a64a7a879572c9f4d0e6 Mon Sep 17 00:00:00 2001 From: Ryan Jarvis Date: Thu, 11 Mar 2021 21:45:32 -0800 Subject: [PATCH 3/3] Move stdlib import to top of file (cherry picked from commit 27e97c73dd9eee0e218ab85374b1cd9bffa74281) --- pico_project.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pico_project.py b/pico_project.py index 1ed9596..4b45343 100755 --- a/pico_project.py +++ b/pico_project.py @@ -14,6 +14,7 @@ import shutil import subprocess import sys +import threading from pathlib import Path import tkinter as tk @@ -288,7 +289,6 @@ def getCheckedItems(self): return values -import threading def thread_function(text, command, ok): l = shlex.split(command) @@ -1206,7 +1206,7 @@ def DoEverything(parent, params): # Check we have everything we need to compile etc c = CheckPrerequisites() -## TODO Do both warnings in the same error message so user does have to keep coming back to find still more to do +# TODO Do both warnings in the same error message so user does have to keep coming back to find still more to do if c == None: m = 'Unable to find the `' + COMPILER_NAME + '` compiler\n'