diff --git a/shaketune/graph_creators/graph_creator.py b/shaketune/graph_creators/graph_creator.py index 41546c3..b507dd2 100644 --- a/shaketune/graph_creators/graph_creator.py +++ b/shaketune/graph_creators/graph_creator.py @@ -23,10 +23,10 @@ class GraphCreator(abc.ABC): def __init__(self, config: ShakeTuneConfig, graph_type: str): self._config = config + self._version = config.version self._graph_date = datetime.now().strftime('%Y%m%d_%H%M%S') - self._version = ShakeTuneConfig.get_git_version() self._type = graph_type - self._folder = self._config.get_results_folder(graph_type) + self._folder = config.get_results_folder(graph_type) def _save_figure( self, fig: Figure, measurements_manager: MeasurementsManager, axis_label: Optional[str] = None diff --git a/shaketune/helpers/common_func.py b/shaketune/helpers/common_func.py index 0f876eb..01a8a69 100644 --- a/shaketune/helpers/common_func.py +++ b/shaketune/helpers/common_func.py @@ -9,7 +9,6 @@ import math -from pathlib import Path import numpy as np from scipy.signal import spectrogram @@ -25,28 +24,6 @@ ] -# This is used to print the current S&T version on top of the png graph file -def get_git_version(): - try: - # Get the absolute path of the script, resolving any symlinks - # Then get 2 times to parent dir to be at the git root folder - from git import GitCommandError, Repo - - script_path = Path(__file__).resolve() - repo_path = script_path.parents[1] - repo = Repo(repo_path) - - try: - version = repo.git.describe('--tags') - except GitCommandError: - # If no tag is found, use the simplified commit SHA instead - version = repo.head.commit.hexsha[:7] - return version - - except Exception: - return None - - # This is Klipper's spectrogram generation function adapted to use Scipy def compute_spectrogram(data): N = data.shape[0] diff --git a/shaketune/shaketune.py b/shaketune/shaketune.py index 1d05e72..11977f8 100644 --- a/shaketune/shaketune.py +++ b/shaketune/shaketune.py @@ -71,21 +71,28 @@ def __init__(self, config) -> None: gcode = self._printer.lookup_object('gcode') ConsoleOutput.register_output_callback(gcode.respond_info) - self._initialize_config(config) + self._initialize_st_config() self._register_commands() # Initialize the ShakeTune object and its configuration - def _initialize_config(self, config) -> None: - result_folder = config.get('result_folder', default=DEFAULT_FOLDER) + def _initialize_st_config(self) -> None: + result_folder = self._config.get('result_folder', default=DEFAULT_FOLDER) result_folder_path = Path(result_folder).expanduser() if result_folder else None - keep_n_results = config.getint('number_of_results_to_keep', default=DEFAULT_NUMBER_OF_RESULTS, minval=0) - keep_raw_data = config.getboolean('keep_raw_data', default=DEFAULT_KEEP_RAW_DATA) - dpi = config.getint('dpi', default=DEFAULT_DPI, minval=100, maxval=500) - m_chunk_size = config.getint('measurements_chunk_size', default=DEFAULT_MEASUREMENTS_CHUNK_SIZE, minval=2) - self._st_config = ShakeTuneConfig(result_folder_path, keep_n_results, keep_raw_data, m_chunk_size, dpi) + keep_n_results = self._config.getint('number_of_results_to_keep', default=DEFAULT_NUMBER_OF_RESULTS, minval=0) + keep_raw_data = self._config.getboolean('keep_raw_data', default=DEFAULT_KEEP_RAW_DATA) + dpi = self._config.getint('dpi', default=DEFAULT_DPI, minval=100, maxval=500) + m_chunk_size = self._config.getint('measurements_chunk_size', default=DEFAULT_MEASUREMENTS_CHUNK_SIZE, minval=2) + self._st_config = ShakeTuneConfig( + self._printer.get_start_args().get('git_info', None) if self.IN_DANGER else None, + result_folder_path, + keep_n_results, + keep_raw_data, + m_chunk_size, + dpi, + ) - self.timeout = config.getfloat('timeout', DEFAULT_TIMEOUT, above=0.0) - self._show_macros = config.getboolean('show_macros_in_webui', default=DEFAULT_SHOW_MACROS) + self.timeout = self._config.getfloat('timeout', DEFAULT_TIMEOUT, above=0.0) + self._show_macros = self._config.getboolean('show_macros_in_webui', default=DEFAULT_SHOW_MACROS) # Create the Klipper commands to allow the user to run Shake&Tune's tools def _register_commands(self) -> None: @@ -145,6 +152,12 @@ def _on_klippy_connect(self) -> None: 'No [resonance_tester] config section found in printer.cfg! Please add one to use Shake&Tune!' ) + def _print_version(self) -> None: + if self.IN_DANGER: + ConsoleOutput.print(f'Shake&Tune in DK version: {self._st_config.version}') + else: + ConsoleOutput.print(f'Shake&Tune version: {self._st_config.version}') + # ------------------------------------------------------------------------------------------ # ------------------------------------------------------------------------------------------ # Following are all the Shake&Tune commands that are registered to the Klipper console @@ -152,7 +165,7 @@ def _on_klippy_connect(self) -> None: # ------------------------------------------------------------------------------------------ def cmd_EXCITATE_AXIS_AT_FREQ(self, gcmd) -> None: - ConsoleOutput.print(f'Shake&Tune version: {ShakeTuneConfig.get_git_version()}') + self._print_version() static_freq_graph_creator = StaticGraphCreator(self._st_config) st_process = ShakeTuneProcess( self._st_config, @@ -163,7 +176,7 @@ def cmd_EXCITATE_AXIS_AT_FREQ(self, gcmd) -> None: excitate_axis_at_freq(gcmd, self._config, st_process) def cmd_AXES_MAP_CALIBRATION(self, gcmd) -> None: - ConsoleOutput.print(f'Shake&Tune version: {ShakeTuneConfig.get_git_version()}') + self._print_version() axes_map_graph_creator = AxesMapGraphCreator(self._st_config) st_process = ShakeTuneProcess( self._st_config, @@ -174,7 +187,7 @@ def cmd_AXES_MAP_CALIBRATION(self, gcmd) -> None: axes_map_calibration(gcmd, self._config, st_process) def cmd_COMPARE_BELTS_RESPONSES(self, gcmd) -> None: - ConsoleOutput.print(f'Shake&Tune version: {ShakeTuneConfig.get_git_version()}') + self._print_version() belt_graph_creator = BeltsGraphCreator(self._st_config) st_process = ShakeTuneProcess( self._st_config, @@ -185,7 +198,7 @@ def cmd_COMPARE_BELTS_RESPONSES(self, gcmd) -> None: compare_belts_responses(gcmd, self._config, st_process) def cmd_AXES_SHAPER_CALIBRATION(self, gcmd) -> None: - ConsoleOutput.print(f'Shake&Tune version: {ShakeTuneConfig.get_git_version()}') + self._print_version() shaper_graph_creator = ShaperGraphCreator(self._st_config) st_process = ShakeTuneProcess( self._st_config, @@ -196,7 +209,7 @@ def cmd_AXES_SHAPER_CALIBRATION(self, gcmd) -> None: axes_shaper_calibration(gcmd, self._config, st_process) def cmd_CREATE_VIBRATIONS_PROFILE(self, gcmd) -> None: - ConsoleOutput.print(f'Shake&Tune version: {ShakeTuneConfig.get_git_version()}') + self._print_version() vibration_profile_creator = VibrationsGraphCreator(self._st_config) st_process = ShakeTuneProcess( self._st_config, diff --git a/shaketune/shaketune_config.py b/shaketune/shaketune_config.py index c3d1885..ae0e6f9 100644 --- a/shaketune/shaketune_config.py +++ b/shaketune/shaketune_config.py @@ -27,12 +27,14 @@ class ShakeTuneConfig: def __init__( self, + dk_git_info: dict = None, result_folder: Path = RESULTS_BASE_FOLDER, keep_n_results: int = 10, keep_raw_data: bool = False, chunk_size: int = 2, dpi: int = 150, ) -> None: + self._dk_git_info = dk_git_info self._result_folder = result_folder self.keep_n_results = keep_n_results @@ -43,6 +45,8 @@ def __init__( self.klipper_folder = KLIPPER_FOLDER self.klipper_log_folder = KLIPPER_LOG_FOLDER + self.version = self._get_version() + def get_results_folder(self, type: str = None) -> Path: if type is None: return self._result_folder @@ -53,21 +57,24 @@ def get_results_subfolders(self) -> Path: subfolders = [self._result_folder / subfolder for subfolder in RESULTS_SUBFOLDERS.values()] return subfolders - @staticmethod - def get_git_version() -> str: - try: - from git import GitCommandError, Repo - - # Get the absolute path of the script, resolving any symlinks - # Then get 1 times to parent dir to be at the git root folder - script_path = Path(__file__).resolve() - repo_path = script_path.parents[1] - repo = Repo(repo_path) + def _get_version(self) -> str: + if self._dk_git_info: + return f"DK-{self._dk_git_info['version']} ({self._dk_git_info['branch']})" + else: try: - version = repo.git.describe('--tags') - except GitCommandError: - version = repo.head.commit.hexsha[:7] # If no tag is found, use the simplified commit SHA instead - return version - except Exception as e: - ConsoleOutput.print(f'Warning: unable to retrieve Shake&Tune version number: {e}') - return 'unknown' + from git import GitCommandError, Repo + + # Get the absolute path of the script, resolving any symlinks + # Then get 1 times to parent dir to be at the git root folder + script_path = Path(__file__).resolve() + repo_path = script_path.parents[1] + repo = Repo(repo_path) + try: + version = repo.git.describe('--tags') + except GitCommandError: + version = repo.head.commit.hexsha[:7] # If no tag is found, use the simplified commit SHA instead + branch = repo.active_branch.name + return f'OG-{version} ({branch})' + except Exception as e: + ConsoleOutput.print(f'Warning: unable to retrieve Shake&Tune version number: {e}') + return 'unknown'