From 578d5ac6d14341d04be35fc3c3665dc9b5e7e665 Mon Sep 17 00:00:00 2001 From: Boris Nezlobin <146669165+borisnezlobin@users.noreply.github.com> Date: Wed, 28 Aug 2024 03:27:11 -0700 Subject: [PATCH] Add timezone grid caching to CLI (#17) (#21) * add cli caching, will read from timezone.json or --grid argument * update output in test_executable_without_args * update find_sun to use cached timezone_grid.json * Bump minor version number --------- Co-authored-by: Galen Reich <54807169+GalenReich@users.noreply.github.com> --- pyproject.toml | 2 +- src/shadowfinder/cli.py | 19 +++++++++++++++++-- src/shadowfinder/shadowfinder.py | 9 +++++++-- tests/test_executable.py | 3 +++ 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2ef5a8e..640799b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ShadowFinder" -version = "0.4.0" +version = "0.5.0" description = "Find possible locations of shadows." authors = ["Bellingcat"] license = "MIT License" diff --git a/src/shadowfinder/cli.py b/src/shadowfinder/cli.py index dfc9032..3fc1470 100644 --- a/src/shadowfinder/cli.py +++ b/src/shadowfinder/cli.py @@ -41,6 +41,7 @@ def find( date: str, time: str, time_format: str = "utc", + grid: str = "timezone_grid.json", ) -> None: """ Find the shadow length of an object given its height and the date and time. @@ -59,7 +60,7 @@ def find( shadow_finder = ShadowFinder( object_height, shadow_length, date_time, time_format ) - shadow_finder.quick_find() + shadow_finder.quick_find(grid) @staticmethod def find_sun( @@ -67,6 +68,7 @@ def find_sun( date: str, time: str, time_format: str = "utc", + grid: str = "timezene_grid.json", ) -> None: """ Locate a shadow based on the solar altitude angle and the date and time. @@ -86,4 +88,17 @@ def find_sun( time_format=time_format, sun_altitude_angle=sun_altitude_angle, ) - shadow_finder.quick_find() + shadow_finder.quick_find(grid) + + @staticmethod + def generate_timezone_grid( + grid: str = "timezone_grid.json", + ) -> None: + """ + Generate a timezone grid file. + :param grid: File path to save the timezone grid. + """ + + shadow_finder = ShadowFinder() + shadow_finder.generate_timezone_grid() + shadow_finder.save_timezone_grid(grid) diff --git a/src/shadowfinder/shadowfinder.py b/src/shadowfinder/shadowfinder.py index a3d17f2..63f67ce 100644 --- a/src/shadowfinder/shadowfinder.py +++ b/src/shadowfinder/shadowfinder.py @@ -93,8 +93,13 @@ def set_details( # Lengths and angle are None and we use the same values as before pass - def quick_find(self): - self.generate_timezone_grid() + def quick_find(self, timezone_grid="timezone_grid.json"): + # try to load timezone grid from file, generate if not found + try: + self.load_timezone_grid(timezone_grid) + except FileNotFoundError: + self.generate_timezone_grid() + self.find_shadows() fig = self.plot_shadows() diff --git a/tests/test_executable.py b/tests/test_executable.py index 3ce70ba..97ffaa3 100644 --- a/tests/test_executable.py +++ b/tests/test_executable.py @@ -24,6 +24,9 @@ def test_executable_without_args(): find_sun Locate a shadow based on the solar altitude angle and the date and time. + + generate_timezone_grid + Generate a timezone grid file. """ # WHEN