Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Args:
args_verbose_counter: verbosity level (currently only 2 levels are supported: NORMAL, VERBOSE)
dummy: create a NullHandler logger if true
Returns:
logging.Logger instance
"""
if dummy:
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())
else:
logger = logging.getLogger('stm32pio')
logger.setLevel(logging.DEBUG if args_verbose_counter else logging.INFO)
handler = logging.StreamHandler()
formatter = stm32pio.util.DispatchingFormatter(
verbosity=stm32pio.util.Verbosity.VERBOSE if args_verbose_counter else stm32pio.util.Verbosity.NORMAL,
general={
stm32pio.util.Verbosity.NORMAL: logging.Formatter("%(levelname)-8s %(message)s"),
stm32pio.util.Verbosity.VERBOSE: logging.Formatter(
f"%(levelname)-8s %(funcName)-{stm32pio.settings.log_fieldwidth_function}s %(message)s")
})
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.debug("debug logging enabled")
return logger
settings.setArrayIndex(index)
restored_projects_paths.append(settings.value('path'))
settings.endArray()
settings.endGroup()
engine = QQmlApplicationEngine(parent=app)
qmlRegisterType(ProjectListItem, 'ProjectListItem', 1, 0, 'ProjectListItem')
qmlRegisterType(Settings, 'Settings', 1, 0, 'Settings')
projects_model = ProjectsList(parent=engine)
boards_model = QStringListModel(parent=engine)
engine.rootContext().setContextProperty('appVersion', stm32pio.app.__version__)
engine.rootContext().setContextProperty('Logging', stm32pio.util.logging_levels)
engine.rootContext().setContextProperty('projectsModel', projects_model)
engine.rootContext().setContextProperty('boardsModel', boards_model)
engine.rootContext().setContextProperty('appSettings', settings)
engine.load(QUrl.fromLocalFile(str(MODULE_PATH.joinpath('main.qml'))))
main_window = engine.rootObjects()[0]
# Getting PlatformIO boards can take a long time when the PlatformIO cache is outdated but it is important to have
# them before the projects list is restored, so we start a dedicated loading thread. We actually can add other
# start-up operations here if there will be a need to. Use the same Worker class to spawn the thread at the pool
def loading():
boards = ['None'] + stm32pio.util.get_platformio_boards('platformio')
boards_model.setStringList(boards)
# more details)
cubemx_script_file, cubemx_script_name = tempfile.mkstemp()
# We must remove the temp directory, so do not let any exception break our plans
try:
# buffering=0 leads to the immediate flushing on writing
with open(cubemx_script_file, mode='w+b', buffering=0) as cubemx_script:
cubemx_script_template = string.Template(self.config.get('project', 'cubemx_script_content'))
cubemx_script_content = cubemx_script_template.substitute(ioc_file_absolute_path=self.ioc_file,
project_dir_absolute_path=self.path)
cubemx_script.write(cubemx_script_content.encode()) # should encode, since mode='w+b'
command_arr = [self.config.get('app', 'java_cmd'), '-jar', self.config.get('app', 'cubemx_cmd'), '-q',
cubemx_script_name, '-s'] # -q: read the commands from the file, -s: silent performance
# Redirect the output of the subprocess into the logging module (with DEBUG level)
with stm32pio.util.LogPipe(self.logger, logging.DEBUG) as log:
result = subprocess.run(command_arr, stdout=log.pipe, stderr=log.pipe)
result_output = log.value
except Exception as e:
raise e # re-raise an exception after the 'finally' block
finally:
pathlib.Path(cubemx_script_name).unlink()
error_msg = "code generation error"
if result.returncode == 0:
# CubeMX 0 return code doesn't necessarily means the correct generation (e.g. migration dialog has appeared
# and 'Cancel' was chosen, or CubeMX_version < ioc_file_version), should analyze the output
if 'Code succesfully generated' in result_output:
self.logger.info("successful code generation")
return result.returncode
else: