From db601a412d83cb20795cddc931ddbf19948fa03e Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Thu, 7 Jul 2016 10:36:37 +0200 Subject: [PATCH 1/6] [mod] try to clean a bit app_list code --- src/yunohost/app.py | 126 +++++++++++++++++++++++--------------------- 1 file changed, 65 insertions(+), 61 deletions(-) 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): From 4deaed1c7839d4f5472efdaf76dbe801457f75db Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Tue, 7 Feb 2017 10:30:57 -0500 Subject: [PATCH 2/6] Trying to add comments and simplify some overly complicated parts --- src/yunohost/app.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/yunohost/app.py b/src/yunohost/app.py index 948b1eba0..a7f5f3d43 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -171,6 +171,7 @@ def app_list(offset=None, limit=None, filter=None, raw=False, installed=False, w app_fetchlist() applists = app_listlists()['lists'] + # Construct a dictionnary of apps, based on known app lists for applist in applists: with open(os.path.join(REPO_PATH, applist + '.json')) as json_list: for app, info in json.load(json_list).items(): @@ -180,42 +181,54 @@ def app_list(offset=None, limit=None, filter=None, raw=False, installed=False, w info['repository'] = applist app_dict[app] = info + # Get app list from the app settings directory for app in os.listdir(APPS_SETTING_PATH): if app not in app_dict: - # Look for forks + # Handle multi-instance case like wordpress__2 if '__' in app: original_app = app[:app.index('__')] if original_app in app_dict: 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(json_manifest)} + # FIXME : What if it's not !?!? + with open(os.path.join(APPS_SETTING_PATH, app, 'manifest.json')) as json_manifest: + app_dict[app] = {"manifest": json.load(json_manifest)} app_dict[app]['repository'] = None + # ??? if not (len(app_dict) > (0 + offset) and limit > 0): return {'apps': list_dict} if not raw else list_dict + # Build dict taking account of offset (ordered with sorted) sorted_app_dict = {} + print( sorted(app_dict.keys())[offset:]) for sorted_keys in sorted(app_dict.keys())[offset:]: sorted_app_dict[sorted_keys] = app_dict[sorted_keys] i = 0 for app_id, app_info_dict in sorted_app_dict.items(): + + print(app_id) + + # Apply limit if i >= limit: break - if not ((filter and ((filter in app_id) or (filter in app_info_dict['manifest']['name']))) or not filter): + # Apply filter if there's one + if (filter and + (filter not in app_id) and + (filter not in app_info_dict['manifest']['name'])): continue + # Ignore non-installed app if user wants only installed apps app_installed = _is_installed(app_id) - - # Only installed apps filter if installed and not app_installed: continue - # Filter only apps with backup and restore scripts + # Ignore apps which don't have backup/restore script if user wants + # only apps with backup features 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') @@ -248,7 +261,7 @@ def app_list(offset=None, limit=None, filter=None, raw=False, installed=False, w 'license': app_info_dict['manifest'].get('license', m18n.n('license_undefined')), 'installed': app_installed }) - + i += 1 From cc9364f37ebfaf3de35820266b27821f7e7621fd Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Tue, 7 Feb 2017 10:49:34 -0500 Subject: [PATCH 3/6] Trying to make offset / limit consistent --- src/yunohost/app.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/yunohost/app.py b/src/yunohost/app.py index a7f5f3d43..79857f075 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -201,20 +201,16 @@ def app_list(offset=None, limit=None, filter=None, raw=False, installed=False, w if not (len(app_dict) > (0 + offset) and limit > 0): return {'apps': list_dict} if not raw else list_dict - # Build dict taking account of offset (ordered with sorted) - sorted_app_dict = {} - print( sorted(app_dict.keys())[offset:]) - for sorted_keys in sorted(app_dict.keys())[offset:]: - sorted_app_dict[sorted_keys] = app_dict[sorted_keys] + # Sort app list + sorted_app_list = sorted(app_dict.keys()) + + # Take into account offset + sorted_app_list = sorted_app_list[offset:] i = 0 - for app_id, app_info_dict in sorted_app_dict.items(): + for app_id in sorted_app_list: - print(app_id) - - # Apply limit - if i >= limit: - break + app_info_dict = app_dict[app_id] # Apply filter if there's one if (filter and @@ -261,8 +257,11 @@ def app_list(offset=None, limit=None, filter=None, raw=False, installed=False, w 'license': app_info_dict['manifest'].get('license', m18n.n('license_undefined')), 'installed': app_installed }) - + + # Count listed apps and apply limit i += 1 + if i >= limit: + break return {'apps': list_dict} if not raw else list_dict From 02c974e8ca54e35d08b688388279e99d0d039b99 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Tue, 7 Feb 2017 20:47:04 +0100 Subject: [PATCH 4/6] [mod] remove useless addition --- src/yunohost/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/yunohost/app.py b/src/yunohost/app.py index 79857f075..8e67e5030 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -198,7 +198,7 @@ def app_list(offset=None, limit=None, filter=None, raw=False, installed=False, w app_dict[app]['repository'] = None # ??? - if not (len(app_dict) > (0 + offset) and limit > 0): + if not (len(app_dict) > offset and limit > 0): return {'apps': list_dict} if not raw else list_dict # Sort app list From 50cc3536acf2e2dee0dce70d0f81c97e47748d7a Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Mon, 13 Mar 2017 23:51:35 +0100 Subject: [PATCH 5/6] [mod] remove offset/limit from app_list, they aren't used anymore --- data/actionsmap/yunohost.yml | 6 ------ src/yunohost/app.py | 18 +----------------- 2 files changed, 1 insertion(+), 23 deletions(-) diff --git a/data/actionsmap/yunohost.yml b/data/actionsmap/yunohost.yml index 25eb6cf6d..ef951496c 100644 --- a/data/actionsmap/yunohost.yml +++ b/data/actionsmap/yunohost.yml @@ -426,12 +426,6 @@ app: action_help: List apps api: GET /apps arguments: - -l: - full: --limit - help: Maximum number of app fetched - -o: - full: --offset - help: Starting number for app fetching -f: full: --filter help: Name filter of app_id or app_name diff --git a/src/yunohost/app.py b/src/yunohost/app.py index 8e67e5030..45579c22e 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -144,7 +144,7 @@ def app_removelist(name): logger.success(m18n.n('appslist_removed')) -def app_list(offset=None, limit=None, filter=None, raw=False, installed=False, with_backup=False): +def app_list(filter=None, raw=False, installed=False, with_backup=False): """ List apps @@ -157,8 +157,6 @@ 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) """ - offset = int(offset) if offset else 0 - limit = int(limit) if limit else 1000 installed = with_backup or installed app_dict = {} @@ -197,17 +195,9 @@ def app_list(offset=None, limit=None, filter=None, raw=False, installed=False, w app_dict[app]['repository'] = None - # ??? - if not (len(app_dict) > offset and limit > 0): - return {'apps': list_dict} if not raw else list_dict - # Sort app list sorted_app_list = sorted(app_dict.keys()) - # Take into account offset - sorted_app_list = sorted_app_list[offset:] - - i = 0 for app_id in sorted_app_list: app_info_dict = app_dict[app_id] @@ -258,12 +248,6 @@ def app_list(offset=None, limit=None, filter=None, raw=False, installed=False, w 'installed': app_installed }) - # Count listed apps and apply limit - i += 1 - if i >= limit: - break - - return {'apps': list_dict} if not raw else list_dict From 4be2720cb05a11587c14ab50a52ac25b1d3fa195 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Tue, 14 Mar 2017 00:35:28 +0100 Subject: [PATCH 6/6] [mod] implement ljf comment --- src/yunohost/app.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/yunohost/app.py b/src/yunohost/app.py index 45579c22e..0884ab7ce 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -173,11 +173,9 @@ def app_list(filter=None, raw=False, installed=False, with_backup=False): for applist in applists: with open(os.path.join(REPO_PATH, applist + '.json')) as json_list: for app, info in json.load(json_list).items(): - if app in app_dict: - continue - - info['repository'] = applist - app_dict[app] = info + if app not in app_dict: + info['repository'] = applist + app_dict[app] = info # Get app list from the app settings directory for app in os.listdir(APPS_SETTING_PATH):