From 5b579ff06d46a401cf9b1cf510aff8a1763c25a8 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Tue, 23 Jun 2020 19:30:11 -0700 Subject: [PATCH] frontpage: Read custom shortcuts from multiple locations Read from /etc/plinth, /usr/share/plinth and /var/lib/plinth. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/frontpage.py | 29 ++++++++++++++++++----------- plinth/modules/api/views.py | 4 +--- plinth/tests/conftest.py | 2 +- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/plinth/frontpage.py b/plinth/frontpage.py index 171fb021d..5e63bff1c 100644 --- a/plinth/frontpage.py +++ b/plinth/frontpage.py @@ -5,7 +5,7 @@ Manage application shortcuts on front page. import json import logging -import os +import pathlib from plinth import app, cfg @@ -129,8 +129,6 @@ class Shortcut(app.FollowerComponent): def add_custom_shortcuts(): custom_shortcuts = get_custom_shortcuts() - if not custom_shortcuts: - return for shortcut in custom_shortcuts['shortcuts']: web_app_url = _extract_web_app_url(shortcut) @@ -161,11 +159,20 @@ def _extract_web_app_url(custom_shortcut): def get_custom_shortcuts(): - cfg_dir = os.path.dirname(cfg.config_file) - shortcuts_file = os.path.join(cfg_dir, 'custom-shortcuts.json') - if os.path.isfile(shortcuts_file) and os.stat(shortcuts_file).st_size: - logger.info('Loading custom shortcuts from %s', shortcuts_file) - with open(shortcuts_file) as shortcuts: - custom_shortcuts = json.load(shortcuts) - return custom_shortcuts - return None + """Return a merged dictionary of all custom shortcuts.""" + shortcuts_dirs = [cfg.config_dir, cfg.data_dir, cfg.file_root] + + shortcuts = {'shortcuts': []} + for shortcuts_dir in shortcuts_dirs: + file_path = pathlib.Path(shortcuts_dir) / 'custom-shortcuts.json' + if not file_path.is_file() or not file_path.stat().st_size: + continue + + logger.info('Loading custom shortcuts from %s', file_path) + with file_path.open() as file_handle: + try: + shortcuts['shortcuts'] += json.load(file_handle)['shortcuts'] + except (KeyError, json.JSONDecodeError): + logger.info('Error loading shortcuts from %s', file_path) + + return shortcuts diff --git a/plinth/modules/api/views.py b/plinth/modules/api/views.py index 9fb5e82b9..d5a375409 100644 --- a/plinth/modules/api/views.py +++ b/plinth/modules/api/views.py @@ -40,9 +40,7 @@ def get_shortcuts_as_json(username=None): for shortcut in frontpage.Shortcut.list(username) if shortcut.component_id and shortcut.is_enabled() ] - custom_shortcuts = frontpage.get_custom_shortcuts() - if custom_shortcuts: - shortcuts += custom_shortcuts['shortcuts'] + shortcuts += frontpage.get_custom_shortcuts()['shortcuts'] return {'shortcuts': shortcuts} diff --git a/plinth/tests/conftest.py b/plinth/tests/conftest.py index 122dc1c11..2afad9b4a 100644 --- a/plinth/tests/conftest.py +++ b/plinth/tests/conftest.py @@ -31,7 +31,7 @@ NEXTCLOUD_SHORTCUT = { @pytest.fixture(name='custom_shortcuts_file') def fixture_custom_shortcuts_file(load_cfg, tmp_path): """Fixture to set path for a custom shortcuts file.""" - load_cfg.config_file = str(tmp_path / 'plinth.conf') + load_cfg.config_dir = str(tmp_path) return tmp_path / 'custom-shortcuts.json'