From a03a3c2e4b1df03fb99d0e158e62d01dd065a862 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Boisselier?= Date: Fri, 27 Oct 2023 14:44:06 +0200 Subject: [PATCH] Added some safety checks and forced C locale for console printing --- K-ShakeTune/scripts/graph_belts.py | 20 ++++++++++++---- K-ShakeTune/scripts/graph_shaper.py | 20 ++++++++++++---- K-ShakeTune/scripts/graph_vibrations.py | 20 ++++++++++++---- K-ShakeTune/scripts/is_workflow.py | 31 +++++++++++++++++++------ 4 files changed, 72 insertions(+), 19 deletions(-) diff --git a/K-ShakeTune/scripts/graph_belts.py b/K-ShakeTune/scripts/graph_belts.py index 2b8bdd0..5c545c9 100755 --- a/K-ShakeTune/scripts/graph_belts.py +++ b/K-ShakeTune/scripts/graph_belts.py @@ -28,10 +28,6 @@ from datetime import datetime matplotlib.use('Agg') -try: - locale.setlocale(locale.LC_TIME, locale.getdefaultlocale()) -except locale.Error: - locale.setlocale(locale.LC_TIME, 'C') ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # For paired peaks names @@ -54,6 +50,22 @@ } +# Set the best locale for time and date formating (generation of the titles) +try: + locale.setlocale(locale.LC_TIME, locale.getdefaultlocale()) +except locale.Error: + locale.setlocale(locale.LC_TIME, 'C') + +# Override the built-in print function to avoid problem in Klipper due to locale settings +original_print = print +def print_with_c_locale(*args, **kwargs): + original_locale = locale.setlocale(locale.LC_ALL, None) + locale.setlocale(locale.LC_ALL, 'C') + original_print(*args, **kwargs) + locale.setlocale(locale.LC_ALL, original_locale) +print = print_with_c_locale + + ###################################################################### # Computation of the PSD graph ###################################################################### diff --git a/K-ShakeTune/scripts/graph_shaper.py b/K-ShakeTune/scripts/graph_shaper.py index 987c51c..261937e 100755 --- a/K-ShakeTune/scripts/graph_shaper.py +++ b/K-ShakeTune/scripts/graph_shaper.py @@ -35,10 +35,6 @@ from datetime import datetime matplotlib.use('Agg') -try: - locale.setlocale(locale.LC_TIME, locale.getdefaultlocale()) -except locale.Error: - locale.setlocale(locale.LC_TIME, 'C') PEAKS_DETECTION_THRESHOLD = 0.05 @@ -52,6 +48,22 @@ } +# Set the best locale for time and date formating (generation of the titles) +try: + locale.setlocale(locale.LC_TIME, locale.getdefaultlocale()) +except locale.Error: + locale.setlocale(locale.LC_TIME, 'C') + +# Override the built-in print function to avoid problem in Klipper due to locale settings +original_print = print +def print_with_c_locale(*args, **kwargs): + original_locale = locale.setlocale(locale.LC_ALL, None) + locale.setlocale(locale.LC_ALL, 'C') + original_print(*args, **kwargs) + locale.setlocale(locale.LC_ALL, original_locale) +print = print_with_c_locale + + ###################################################################### # Computation ###################################################################### diff --git a/K-ShakeTune/scripts/graph_vibrations.py b/K-ShakeTune/scripts/graph_vibrations.py index f9875b3..0ba6a63 100755 --- a/K-ShakeTune/scripts/graph_vibrations.py +++ b/K-ShakeTune/scripts/graph_vibrations.py @@ -29,10 +29,6 @@ from datetime import datetime matplotlib.use('Agg') -try: - locale.setlocale(locale.LC_TIME, locale.getdefaultlocale()) -except locale.Error: - locale.setlocale(locale.LC_TIME, 'C') PEAKS_DETECTION_THRESHOLD = 0.05 @@ -46,6 +42,22 @@ } +# Set the best locale for time and date formating (generation of the titles) +try: + locale.setlocale(locale.LC_TIME, locale.getdefaultlocale()) +except locale.Error: + locale.setlocale(locale.LC_TIME, 'C') + +# Override the built-in print function to avoid problem in Klipper due to locale settings +original_print = print +def print_with_c_locale(*args, **kwargs): + original_locale = locale.setlocale(locale.LC_ALL, None) + locale.setlocale(locale.LC_ALL, 'C') + original_print(*args, **kwargs) + locale.setlocale(locale.LC_ALL, original_locale) +print = print_with_c_locale + + ###################################################################### # Computation ###################################################################### diff --git a/K-ShakeTune/scripts/is_workflow.py b/K-ShakeTune/scripts/is_workflow.py index 21f6fd6..be38add 100755 --- a/K-ShakeTune/scripts/is_workflow.py +++ b/K-ShakeTune/scripts/is_workflow.py @@ -63,7 +63,16 @@ def get_belts_graph(): current_date = datetime.now().strftime('%Y%m%d_%H%M%S') lognames = [] - for filename in glob.glob('/tmp/raw_data_axis*.csv'): + globbed_files = glob.glob('/tmp/raw_data_axis*.csv') + if not globbed_files: + print("No CSV files found in the /tmp folder to create the belt graphs!") + sys.exit(1) + if len(globbed_files) < 2: + print("Not enough CSV files found in the /tmp folder. Two files are required for the belt graphs!") + sys.exit(1) + sorted_files = sorted(globbed_files, key=os.path.getmtime, reverse=True) + + for filename in sorted_files[:2]: # Wait for the file handler to be released by Klipper while is_file_open(filename): time.sleep(3) @@ -86,13 +95,13 @@ def get_belts_graph(): def get_shaper_graph(): current_date = datetime.now().strftime('%Y%m%d_%H%M%S') + # Get all the files and sort them based on last modified time to select the most recent one globbed_files = glob.glob('/tmp/raw_data*.csv') - if len(globbed_files) > 1: - print("There is more than 1 measurement.csv found in the /tmp folder. Unable to plot the shaper graphs!") - print("Please clean the files in the /tmp folder and start again.") + if not globbed_files: + print("No CSV files found in the /tmp folder to create the input shaper graphs!") sys.exit(1) - - filename = globbed_files[0] + sorted_files = sorted(globbed_files, key=os.path.getmtime, reverse=True) + filename = sorted_files[0] # Wait for the file handler to be released by Klipper while is_file_open(filename): @@ -114,7 +123,15 @@ def get_vibrations_graph(axis_name): current_date = datetime.now().strftime('%Y%m%d_%H%M%S') lognames = [] - for filename in glob.glob('/tmp/adxl345-*.csv'): + globbed_files = glob.glob('/tmp/adxl345-*.csv') + if not globbed_files: + print("No CSV files found in the /tmp folder to create the vibration graphs!") + sys.exit(1) + if len(globbed_files) < 3: + print("Not enough CSV files found in the /tmp folder. At least 3 files are required for the vibration graphs!") + sys.exit(1) + + for filename in globbed_files: # Wait for the file handler to be released by Klipper while is_file_open(filename): time.sleep(3)