Skip to content

Commit

Permalink
Added some safety checks and forced C locale for console printing
Browse files Browse the repository at this point in the history
  • Loading branch information
Félix Boisselier committed Oct 27, 2023
1 parent c102d41 commit a03a3c2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 19 deletions.
20 changes: 16 additions & 4 deletions K-ShakeTune/scripts/graph_belts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
######################################################################
Expand Down
20 changes: 16 additions & 4 deletions K-ShakeTune/scripts/graph_shaper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
######################################################################
Expand Down
20 changes: 16 additions & 4 deletions K-ShakeTune/scripts/graph_vibrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
######################################################################
Expand Down
31 changes: 24 additions & 7 deletions K-ShakeTune/scripts/is_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand All @@ -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)
Expand Down

0 comments on commit a03a3c2

Please sign in to comment.