diff --git a/src/yunohost/app.py b/src/yunohost/app.py index 17223228d..948b1eba0 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -157,17 +157,12 @@ def app_list(offset=None, limit=None, filter=None, raw=False, installed=False, w with_backup -- Return only apps with backup feature (force --installed filter) """ - if offset: offset = int(offset) - else: offset = 0 - if limit: limit = int(limit) - else: limit = 1000 + offset = int(offset) if offset else 0 + limit = int(limit) if limit else 1000 installed = with_backup or installed app_dict = {} - if raw: - list_dict = {} - else: - list_dict = [] + list_dict = {} if raw else [] try: applists = app_listlists()['lists'] @@ -178,10 +173,12 @@ def app_list(offset=None, limit=None, filter=None, raw=False, installed=False, w for applist in applists: with open(os.path.join(REPO_PATH, applist + '.json')) as json_list: - for app, info in json.loads(str(json_list.read())).items(): - if app not in app_dict: - info['repository'] = applist - app_dict[app] = info + for app, info in json.load(json_list).items(): + if app in app_dict: + continue + + info['repository'] = applist + app_dict[app] = info for app in os.listdir(APPS_SETTING_PATH): if app not in app_dict: @@ -192,63 +189,70 @@ def app_list(offset=None, limit=None, filter=None, raw=False, installed=False, w app_dict[app] = app_dict[original_app] continue with open( APPS_SETTING_PATH + app +'/manifest.json') as json_manifest: - app_dict[app] = {"manifest":json.loads(str(json_manifest.read()))} + app_dict[app] = {"manifest": json.loads(json_manifest)} + + app_dict[app]['repository'] = None - if len(app_dict) > (0 + offset) and limit > 0: - sorted_app_dict = {} - for sorted_keys in sorted(app_dict.keys())[offset:]: - sorted_app_dict[sorted_keys] = app_dict[sorted_keys] + if not (len(app_dict) > (0 + offset) and limit > 0): + return {'apps': list_dict} if not raw else list_dict - i = 0 - for app_id, app_info_dict in sorted_app_dict.items(): - if i < limit: - if (filter and ((filter in app_id) or (filter in app_info_dict['manifest']['name']))) or not filter: - app_installed = _is_installed(app_id) + sorted_app_dict = {} + for sorted_keys in sorted(app_dict.keys())[offset:]: + sorted_app_dict[sorted_keys] = app_dict[sorted_keys] - # Only installed apps filter - if installed and not app_installed: - continue + i = 0 + for app_id, app_info_dict in sorted_app_dict.items(): + if i >= limit: + break - # Filter only apps with backup and restore scripts - if with_backup and ( - not os.path.isfile(APPS_SETTING_PATH + app_id + '/scripts/backup') or - not os.path.isfile(APPS_SETTING_PATH + app_id + '/scripts/restore') - ): - continue + if not ((filter and ((filter in app_id) or (filter in app_info_dict['manifest']['name']))) or not filter): + continue - if raw: - app_info_dict['installed'] = app_installed - if app_installed: - app_info_dict['status'] = _get_app_status(app_id) + app_installed = _is_installed(app_id) - # dirty: we used to have manifest containing multi_instance value in form of a string - # but we've switched to bool, this line ensure retrocompatibility - app_info_dict["manifest"]["multi_instance"] = is_true(app_info_dict["manifest"].get("multi_instance", False)) + # Only installed apps filter + if installed and not app_installed: + continue - list_dict[app_id] = app_info_dict - else: - label = None - if app_installed: - app_info_dict_raw = app_info(app=app_id, raw=True) - label = app_info_dict_raw['settings']['label'] - list_dict.append({ - 'id': app_id, - 'name': app_info_dict['manifest']['name'], - 'label': label, - 'description': _value_for_locale( - app_info_dict['manifest']['description']), - # FIXME: Temporarly allow undefined license - 'license': app_info_dict['manifest'].get('license', - m18n.n('license_undefined')), - 'installed': app_installed - }) - i += 1 - else: - break - if not raw: - list_dict = { 'apps': list_dict } - return list_dict + # Filter only apps with backup and restore scripts + if with_backup and ( + not os.path.isfile(APPS_SETTING_PATH + app_id + '/scripts/backup') or + not os.path.isfile(APPS_SETTING_PATH + app_id + '/scripts/restore') + ): + continue + + if raw: + app_info_dict['installed'] = app_installed + if app_installed: + app_info_dict['status'] = _get_app_status(app_id) + + # dirty: we used to have manifest containing multi_instance value in form of a string + # but we've switched to bool, this line ensure retrocompatibility + app_info_dict["manifest"]["multi_instance"] = is_true(app_info_dict["manifest"].get("multi_instance", False)) + + list_dict[app_id] = app_info_dict + + else: + label = None + if app_installed: + app_info_dict_raw = app_info(app=app_id, raw=True) + label = app_info_dict_raw['settings']['label'] + + list_dict.append({ + 'id': app_id, + 'name': app_info_dict['manifest']['name'], + 'label': label, + 'description': _value_for_locale(app_info_dict['manifest']['description']), + # FIXME: Temporarly allow undefined license + 'license': app_info_dict['manifest'].get('license', m18n.n('license_undefined')), + 'installed': app_installed + }) + + i += 1 + + + return {'apps': list_dict} if not raw else list_dict def app_info(app, show_status=False, raw=False):