Skip to content

Commit 1180d6b

Browse files
committed
Add force refresh of modules JSON on --update flag
1 parent 32b2ac5 commit 1180d6b

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

lean/click.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ def invoke(self, ctx: Context):
274274
update_manager = container.update_manager
275275
update_manager.show_announcements()
276276

277+
if ctx.params.get("update", False):
278+
update_manager.force_refresh_modules_json()
279+
277280
result = super().invoke(ctx)
278281

279282
update_manager.warn_if_cli_outdated()

lean/components/util/update_manager.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,23 @@ def show_announcements(self) -> None:
162162

163163
self._logger.info(Panel.fit(table, title="Announcements", box=box.SQUARE))
164164

165+
def force_refresh_modules_json(self) -> None:
166+
"""Forces a re-download of the modules JSON file, bypassing the 24-hour cache interval."""
167+
from lean.models import url as modules_url, file_path as modules_file_path
168+
from requests import get
169+
try:
170+
res = get(modules_url, timeout=5)
171+
if res.ok:
172+
from json import dump
173+
modules_file_path.parent.mkdir(parents=True, exist_ok=True)
174+
with open(modules_file_path, 'w', encoding='utf-8') as f:
175+
dump(res.json(), f, ensure_ascii=False, indent=4)
176+
else:
177+
res.raise_for_status()
178+
except Exception:
179+
# No need to do anything if file isn't available
180+
pass
181+
165182
def _should_check_for_updates(self, key: str, interval_hours: int) -> bool:
166183
"""Returns whether an update check should be performed.
167184

tests/components/util/test_update_manager.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,31 @@ def test_show_announcements_does_nothing_when_latest_announcements_shown_before(
277277
logger.info.assert_not_called()
278278

279279

280+
def test_force_refresh_modules_json_downloads_file(requests_mock: RequestsMock) -> None:
281+
from lean.models import url as modules_url, file_path as modules_file_path
282+
modules_json = {"modules": {"SomeModule": {"version": "1.0"}}}
283+
requests_mock.add(requests_mock.GET, modules_url, json=modules_json)
284+
285+
logger, storage, docker_manager, update_manager = create_objects()
286+
287+
update_manager.force_refresh_modules_json()
288+
289+
assert modules_file_path.is_file()
290+
with open(modules_file_path) as f:
291+
assert json.load(f) == modules_json
292+
293+
294+
def test_force_refresh_modules_json_does_nothing_when_offline(requests_mock: RequestsMock) -> None:
295+
from lean.models import url as modules_url
296+
from requests.exceptions import ConnectionError as RequestsConnectionError
297+
requests_mock.add(requests_mock.GET, modules_url, body=RequestsConnectionError())
298+
299+
logger, storage, docker_manager, update_manager = create_objects()
300+
301+
# Should not raise
302+
update_manager.force_refresh_modules_json()
303+
304+
280305
def test_show_announcements_does_nothing_when_there_are_no_announcements(requests_mock: RequestsMock) -> None:
281306
requests_mock.add(requests_mock.GET,
282307
"https://raw.githubusercontent.com/QuantConnect/lean-cli/master/announcements.json",

0 commit comments

Comments
 (0)