From da2b3691c76654455745e3c5d333f0dc09031ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Lebleu?= Date: Wed, 27 Jan 2016 22:31:04 +0100 Subject: [PATCH] [enh] Use a common function to retrieve app settings --- locales/en.json | 1 + src/yunohost/app.py | 53 +++++++++++++++++++++++++-------------------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/locales/en.json b/locales/en.json index 8414a0ba9..7610b10e3 100644 --- a/locales/en.json +++ b/locales/en.json @@ -17,6 +17,7 @@ "app_unknown" : "Unknown app", "app_no_upgrade" : "No app to upgrade", "app_not_installed" : "{:s} is not installed", + "app_not_correctly_installed" : "{app:s} seems to be not correctly installed", "custom_app_url_required" : "You must provide an URL to upgrade your custom app {:s}", "app_recent_version_required" : "{:s} requires a more recent version of the moulinette", "app_upgraded" : "{:s} successfully upgraded", diff --git a/src/yunohost/app.py b/src/yunohost/app.py index 97cab2c1e..30ba491f8 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -218,8 +218,7 @@ def app_info(app, show_status=False, raw=False): m18n.n('app_not_installed', app)) if raw: ret = app_list(filter=app, raw=True)[app] - with open(apps_setting_path + app +'/settings.yml') as f: - ret['settings'] = yaml.load(f) + ret['settings'] = _get_app_settings(app) return ret app_setting_path = apps_setting_path + app @@ -265,9 +264,8 @@ def app_map(app=None, raw=False, user=None): apps = os.listdir(apps_setting_path) for app_id in apps: - with open(apps_setting_path + app_id +'/settings.yml') as f: - app_settings = yaml.load(f) - if not isinstance(app_settings, dict): + app_settings = _get_app_settings(app_id) + if not app_settings: continue if 'domain' not in app_settings: continue @@ -734,12 +732,7 @@ def app_makedefault(auth, app, domain=None): """ from yunohost.domain import domain_list - if not _is_installed(app): - raise MoulinetteError(errno.EINVAL, m18n.n('app_not_installed', app)) - - with open(apps_setting_path + app +'/settings.yml') as f: - app_settings = yaml.load(f) - + app_settings = _get_app_settings(app) app_domain = app_settings['domain'] app_path = app_settings['path'] @@ -782,17 +775,7 @@ def app_setting(app, key, value=None, delete=False): delete -- Delete the key """ - if not _is_installed(app): - raise MoulinetteError(errno.EINVAL, - m18n.n('app_not_installed', app)) - - settings_file = apps_setting_path + app +'/settings.yml' - try: - with open(settings_file) as f: - app_settings = yaml.load(f) - except IOError: - # Do not fail if setting file is not there - app_settings = {} + app_settings = _get_app_settings(app) if value is None and not delete: try: @@ -812,7 +795,8 @@ def app_setting(app, key, value=None, delete=False): value=yaml.load(value) app_settings[key] = value - with open(settings_file, 'w') as f: + with open(os.path.join( + apps_setting_path, app, 'settings.yml'), 'w') as f: yaml.safe_dump(app_settings, f, default_flow_style=False) @@ -1012,6 +996,29 @@ def app_ssowatconf(auth): msignals.display(m18n.n('ssowat_conf_generated'), 'success') +def _get_app_settings(app_id): + """ + Get settings of an installed app + + Keyword arguments: + app_id -- The app id + + """ + if not _is_installed(app_id): + raise MoulinetteError(errno.EINVAL, + m18n.n('app_not_installed', app_id)) + try: + with open(os.path.join( + apps_setting_path, app_id, 'settings.yml')) as f: + settings = yaml.load(f) + if app_id == settings['id']: + return settings + except (IOError, KeyError): + logger.exception(m18n.n('app_not_correctly_installed', + app=app_id)) + return {} + + def _get_app_status(app_id, format_date=False): """ Get app status or create it if needed