Skip to content

Commit

Permalink
Merge pull request #516 from OpenTrafficCam/bug/missing-metadata-when…
Browse files Browse the repository at this point in the history
…-loading-otconfig-via-cli

bug/missing-metadata-when-loading-otconfig-via-cli
  • Loading branch information
randy-seng authored Jun 5, 2024
2 parents f5a3ca4 + 68afaa0 commit 2750c6f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 15 deletions.
9 changes: 8 additions & 1 deletion OTAnalytics/application/use_cases/preload_input_files.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from OTAnalytics.application.run_configuration import RunConfiguration
from OTAnalytics.application.use_cases.apply_cli_cuts import ApplyCliCuts
from OTAnalytics.application.use_cases.load_otconfig import LoadOtconfig
from OTAnalytics.application.use_cases.load_otflow import LoadOtflow
from OTAnalytics.application.use_cases.load_track_files import LoadTrackFiles

Expand All @@ -8,17 +9,23 @@ class PreloadInputFiles:
def __init__(
self,
load_track_files: LoadTrackFiles,
load_otconfig: LoadOtconfig,
load_otflow: LoadOtflow,
apply_cli_cuts: ApplyCliCuts,
):
self._load_track_files = load_track_files
self._load_otconfig = load_otconfig
self._load_otflow = load_otflow
self._apply_cli_cuts = apply_cli_cuts

def load(self, run_config: RunConfiguration) -> None:
if run_config.config_file:
# use case load_otconfig already loads the track files
self._load_otconfig.load(run_config.config_file)
if run_config.otflow:
self._load_otflow(run_config.otflow)
if run_config.track_files:

if run_config.track_files and not run_config.config_file:
self._load_track_files(list(run_config.track_files))

if run_config.sections:
Expand Down
16 changes: 8 additions & 8 deletions OTAnalytics/plugin_ui/customtkinter_gui/dummy_viewmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,9 @@ def get_all_track_files(self) -> set[Path]:

def set_frame_project(self, project_frame: AbstractFrameProject) -> None:
self._frame_project = project_frame
self._show_current_project()
self.show_current_project()

def _show_current_project(self) -> None:
def show_current_project(self, _: Any = None) -> None:
if self._frame_project is None:
raise MissingInjectedInstanceError(type(self._frame_project).__name__)
project = self._application._datastore.project
Expand Down Expand Up @@ -599,8 +599,8 @@ def _load_otconfig(self, otconfig_file: Path) -> None:
return
logger().info(f"{OTCONFIG_FILE_TYPE} file to load: {otconfig_file}")
self._application.load_otconfig(file=Path(otconfig_file))
self._show_current_project()
self._show_current_svz_metadata()
self.show_current_project()
self.update_svz_metadata_view()

def set_tracks_frame(self, tracks_frame: AbstractFrameTracks) -> None:
self._frame_tracks = tracks_frame
Expand Down Expand Up @@ -1662,8 +1662,8 @@ def start_new_project(self) -> None:
if proceed.canceled:
return
self._application.start_new_project()
self._show_current_project()
self._show_current_svz_metadata()
self.show_current_project()
self.update_svz_metadata_view()
logger().info("Start new project.")

def update_project_name(self, name: str) -> None:
Expand Down Expand Up @@ -1823,9 +1823,9 @@ def get_weather_types(self) -> ColumnResources:

def set_svz_metadata_frame(self, frame: AbstractFrameSvzMetadata) -> None:
self._frame_svz_metadata = frame
self._show_current_svz_metadata()
self.update_svz_metadata_view()

def _show_current_svz_metadata(self) -> None:
def update_svz_metadata_view(self, _: Any = None) -> None:
if self._frame_svz_metadata is None:
raise MissingInjectedInstanceError(type(self._frame_svz_metadata).__name__)
project = self._application._datastore.project
Expand Down
13 changes: 12 additions & 1 deletion OTAnalytics/plugin_ui/customtkinter_gui/frame_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,17 @@ def _wire_callbacks(self) -> None:
self._coordinate_x.trace_add("write", callback=self._update_metadata)
self._coordinate_y.trace_add("write", callback=self._update_metadata)

self._activate_update()

def _activate_update(self) -> None:
self._update = True

def _deactivate_update(self) -> None:
self._update = False

def _update_metadata(self, name: str, other: str, mode: str) -> None:
self._viewmodel.update_svz_metadata(self.__build_metadata())
if self._update:
self._viewmodel.update_svz_metadata(self.__build_metadata())

def __build_metadata(self) -> dict:
return {
Expand All @@ -403,6 +412,7 @@ def __build_metadata(self) -> dict:
}

def update(self, metadata: dict) -> None:
self._deactivate_update()
if metadata:
self._tk_number.set(self.__get_display_value(TK_NUMBER, metadata))
self._counting_location_number.set(
Expand Down Expand Up @@ -443,6 +453,7 @@ def update(self, metadata: dict) -> None:
self._remark.set("")
self._coordinate_x.set("")
self._coordinate_y.set("")
self._activate_update()

@staticmethod
def __get_display_value(field: str, metadata: dict) -> str:
Expand Down
15 changes: 13 additions & 2 deletions OTAnalytics/plugin_ui/main_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,8 @@ def start_gui(self, run_config: RunConfiguration) -> None:
load_otconfig.register(file_state.last_saved_config.set)
project_updater.register(dummy_viewmodel.update_quick_save_button)
track_file_repository.register(dummy_viewmodel.update_quick_save_button)
project_updater.register(dummy_viewmodel.show_current_project)
project_updater.register(dummy_viewmodel.update_svz_metadata_view)

for group in layer_groups:
group.register(image_updater.notify_layers)
Expand All @@ -608,7 +610,10 @@ def start_gui(self, run_config: RunConfiguration) -> None:
cut_tracks_intersecting_section, track_repository
)
preload_input_files = self.create_preload_input_files(
load_otflow, load_track_files, apply_cli_cuts
load_otconfig=load_otconfig,
load_otflow=load_otflow,
load_track_files=load_track_files,
apply_cli_cuts=apply_cli_cuts,
)
OTAnalyticsGui(
main_window, dummy_viewmodel, layer_groups, preload_input_files, run_config
Expand Down Expand Up @@ -1021,11 +1026,17 @@ def _create_track_to_video_repository(self) -> TrackToVideoRepository:

def create_preload_input_files(
self,
load_otconfig: LoadOtconfig,
load_otflow: LoadOtflow,
load_track_files: LoadTrackFiles,
apply_cli_cuts: ApplyCliCuts,
) -> PreloadInputFiles:
return PreloadInputFiles(load_track_files, load_otflow, apply_cli_cuts)
return PreloadInputFiles(
load_track_files=load_track_files,
load_otconfig=load_otconfig,
load_otflow=load_otflow,
apply_cli_cuts=apply_cli_cuts,
)

def create_apply_cli_cuts(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,73 @@


class TestPreloadInputFiles:
def test_load(self) -> None:
def test_load_config_file(self) -> None:
load_track_files = Mock()
load_otconfig = Mock()
load_otflow = Mock()
apply_cli_cuts = Mock()

run_config = Mock()
type(run_config).config_file = PropertyMock(
return_value=Path("path/to/my/otconfig")
)
type(run_config).track_files = PropertyMock(
return_value=[Path("path/one.ottrk"), Path("path/two.ottrk")]
)
type(run_config).otflow = None
type(run_config).sections = PropertyMock(return_value={Mock(), Mock()})

preload_input_files = PreloadInputFiles(
load_track_files, load_otconfig, load_otflow, apply_cli_cuts
)
preload_input_files.load(run_config)

load_otflow.assert_not_called()
load_otconfig.load.assert_called_once_with(run_config.config_file)
load_track_files.assert_not_called()
apply_cli_cuts.apply.assert_called_once_with(
run_config.sections, preserve_cutting_sections=True
)

def test_load_flow_file(self) -> None:
load_track_files = Mock()
load_otconfig = Mock()
load_otflow = Mock()
apply_cli_cuts = Mock()

run_config = Mock()
type(run_config).otflow = PropertyMock(return_value=Path("path/to/my/otflow"))
type(run_config).config_file = PropertyMock(return_value=None)
type(run_config).track_files = PropertyMock(
return_value=[Path("path/one.ottrk"), Path("path/two.ottrk")]
)
type(run_config).sections = PropertyMock(return_value={Mock(), Mock()})

preload_input_files = PreloadInputFiles(
load_track_files, load_otflow, apply_cli_cuts
load_track_files, load_otconfig, load_otflow, apply_cli_cuts
)
preload_input_files.load(run_config)

load_otflow.assert_called_once_with(run_config.otflow)
load_otconfig.assert_not_called()
load_track_files.assert_called_once_with(run_config.track_files)
apply_cli_cuts.apply.assert_called_once_with(
run_config.sections, preserve_cutting_sections=True
)

def test_load_nothing_to_load(self) -> None:
load_track_files = Mock()
load_otconfig = Mock()
load_otflow = Mock()
apply_cli_cuts = Mock()

run_config = Mock()
type(run_config).otflow = PropertyMock(return_value=None)
type(run_config).config_file = PropertyMock(return_value=None)
type(run_config).track_files = PropertyMock(return_value=[])
type(run_config).sections = PropertyMock(return_value=set())
preload_input_files = PreloadInputFiles(
load_track_files, load_otflow, apply_cli_cuts
load_track_files, load_otconfig, load_otflow, apply_cli_cuts
)
preload_input_files.load(run_config)

Expand Down

0 comments on commit 2750c6f

Please sign in to comment.