Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
methods = dir(stm32pio.lib.Stm32pio) + ['main']
buffer_stdout, buffer_stderr = io.StringIO(), io.StringIO()
with contextlib.redirect_stdout(buffer_stdout), contextlib.redirect_stderr(buffer_stderr):
return_code = stm32pio.app.main(sys_argv=['-v', 'new', '-d', str(FIXTURE_PATH), '-b', TEST_PROJECT_BOARD])
self.assertEqual(return_code, 0, msg="Non-zero return code")
# stderr and not stdout contains the actual output (by default for the logging module)
self.assertEqual(len(buffer_stdout.getvalue()), 0,
msg="Process has printed something directly into STDOUT bypassing logging")
self.assertIn('DEBUG', buffer_stderr.getvalue(), msg="Verbose logging output hasn't been enabled on STDERR")
# Inject all methods' names in the regex. Inject the width of field in a log format string
regex = re.compile("^(?=(DEBUG) {0,4})(?=.{8} (?=(" + '|'.join(methods) + ") {0," +
str(stm32pio.settings.log_fieldwidth_function) + "})(?=.{" +
str(stm32pio.settings.log_fieldwidth_function) + "} [^ ]))", flags=re.MULTILINE)
self.assertGreaterEqual(len(re.findall(regex, buffer_stderr.getvalue())), 1,
msg="Logs messages doesn't match the format")
# The snippet of the actual STM32CubeMX output
self.assertIn("Starting STM32CubeMX", buffer_stderr.getvalue(), msg="STM32CubeMX has not printed its logs")
# execution
methods = dir(stm32pio.lib.Stm32pio) + ['main']
buffer_stdout, buffer_stderr = io.StringIO(), io.StringIO()
with contextlib.redirect_stdout(buffer_stdout), contextlib.redirect_stderr(buffer_stderr):
return_code = stm32pio.app.main(sys_argv=['-v', 'new', '-d', str(FIXTURE_PATH), '-b', TEST_PROJECT_BOARD])
self.assertEqual(return_code, 0, msg="Non-zero return code")
# stderr and not stdout contains the actual output (by default for the logging module)
self.assertEqual(len(buffer_stdout.getvalue()), 0,
msg="Process has printed something directly into STDOUT bypassing logging")
self.assertIn('DEBUG', buffer_stderr.getvalue(), msg="Verbose logging output hasn't been enabled on STDERR")
# Inject all methods' names in the regex. Inject the width of field in a log format string
regex = re.compile("^(?=(DEBUG) {0,4})(?=.{8} (?=(" + '|'.join(methods) + ") {0," +
str(stm32pio.settings.log_fieldwidth_function) + "})(?=.{" +
str(stm32pio.settings.log_fieldwidth_function) + "} [^ ]))", flags=re.MULTILINE)
self.assertGreaterEqual(len(re.findall(regex, buffer_stderr.getvalue())), 1,
msg="Logs messages doesn't match the format")
# The snippet of the actual STM32CubeMX output
self.assertIn("Starting STM32CubeMX", buffer_stderr.getvalue(), msg="STM32CubeMX has not printed its logs")
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
module_logger.setLevel(logging.DEBUG if value else logging.INFO)
qml_logger.setLevel(logging.DEBUG if value else logging.INFO)
projects_logger.setLevel(logging.DEBUG if value else logging.INFO)
formatter.verbosity = stm32pio.util.Verbosity.VERBOSE if value else stm32pio.util.Verbosity.NORMAL
settings = Settings(prefix='app/settings/', qs_kwargs={ 'parent': app },
external_triggers={ 'verbose': verbose_setter })
# Use "singleton" real logger for all projects just wrapping it into the LoggingAdapter for every project
projects_logger = logging.getLogger('stm32pio_gui.projects')
projects_logger.setLevel(logging.DEBUG if settings.get('verbose') else logging.INFO)
formatter = stm32pio.util.DispatchingFormatter(
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")
})
projects_logger_handler.setFormatter(formatter)
projects_logger.addHandler(projects_logger_handler)
verbose_setter(settings.get('verbose')) # set initial verbosity settings based on the saved state
settings.beginGroup('app')
restored_projects_paths: List[str] = []
for index in range(settings.beginReadArray('projects')):
settings.setArrayIndex(index)
restored_projects_paths.append(settings.value('path'))
settings.endArray()
settings.endGroup()
engine = QQmlApplicationEngine(parent=app)