[mod] try to clean a bit app_list code

This commit is contained in:
Laurent Peuch 2016-07-07 10:36:37 +02:00
parent cc44512539
commit db601a412d

View file

@ -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,8 +173,10 @@ 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:
for app, info in json.load(json_list).items():
if app in app_dict:
continue
info['repository'] = applist
app_dict[app] = info
@ -192,18 +189,26 @@ 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:
if not (len(app_dict) > (0 + offset) and limit > 0):
return {'apps': list_dict} if not raw else list_dict
sorted_app_dict = {}
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():
if i < limit:
if (filter and ((filter in app_id) or (filter in app_info_dict['manifest']['name']))) or not filter:
if i >= limit:
break
if not ((filter and ((filter in app_id) or (filter in app_info_dict['manifest']['name']))) or not filter):
continue
app_installed = _is_installed(app_id)
# Only installed apps filter
@ -227,28 +232,27 @@ def app_list(offset=None, limit=None, filter=None, raw=False, installed=False, w
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']),
'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')),
'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
return {'apps': list_dict} if not raw else list_dict
def app_info(app, show_status=False, raw=False):