# Path: kano_apps/AppData.py
# def get_applications(parse_cmds=True, all_locales=False, current_locale=True):
#     """ Get all the applications installed on the system.
# 
#         :param parse_cmds:
#         :type parse_cmds: bool
# 
#         :returns: A dict with all apps.
#         :rtype: dict
#     """
# 
#     loc = os.path.expanduser(_SYSTEM_APPLICATIONS_LOC)
#     blacklist = [
#         "idle3.desktop", "idle.desktop", "idle-python2.7.desktop",
#         "idle-python3.2.desktop", "xarchiver.desktop",
#         "make-minecraft.desktop", "make-music.desktop", "make-pong.desktop",
#         "make-snake.desktop", "kano-video.desktop", "lxsession-edit.desktop",
#         "lxrandr.desktop", "lxinput.desktop", "obconf.desktop",
#         "openbox.desktop", "libfm-pref-apps.desktop", "lxappearance.desktop",
#         "htop.desktop", "pcmanfm-desktop-pref.desktop", "video-cli.desktop",
#         "lxsession-default-apps.desktop", "pcmanfm.desktop", "help.desktop",
#         "rxvt-unicode.desktop", "org.gnome.gedit.desktop",
#         "chromium-browser.desktop", "openjdk-7-policytool.desktop"
#     ]
# 
#     def _collect_apps(application_dir, skip_checks=False):
#         if not os.path.exists(application_dir):
#             return {}
# 
#         _applications = {}
# 
#         for f in os.listdir(os.path.join(application_dir, '')):
#             fp = os.path.join(application_dir, f)
# 
#             if os.path.isdir(fp):
#                 continue
# 
#             if f[-4:] == ".app":
#                 data = load_from_app_file(fp, not skip_checks and parse_cmds)
#                 if data is not None:
#                     if not skip_checks and not is_app_installed(data):
#                         data["_install"] = True
# 
#                     _applications[f] = data
#                     if "overrides" in data:
#                         blacklist.extend(data["overrides"])
# 
#             elif f[-8:] == ".desktop" and f[0:5] != "auto_":
#                 data = _load_from_dentry(fp)
#                 if data is not None:
#                     _applications[f] = data
# 
#         return _applications
# 
#     locales = [
#         (DEFAULT_LOCALE, _SYSTEM_APPLICATIONS_LOC)
#     ]
# 
#     locale_code = None
# 
#     if all_locales:
#         locales += [
#             (locale, get_locale_dir(locale))
#             for locale in os.listdir(APP_LOCALE_DIR)
#         ]
#     elif current_locale:
#         current_locale = ensure_utf_locale(get_locale())
#         if current_locale and get_current_translation():
#             locale_code = current_locale.split('.')[0]
#             locales.append(
#                 (locale_code, get_locale_dir(locale_code))
#             )
# 
#     apps = {}
# 
#     for locale, locale_dir in locales:
#         locale_apps = _collect_apps(
#             locale_dir, skip_checks=locale != DEFAULT_LOCALE
#         )
# 
#         for app_name, app_data in locale_apps.iteritems():
#             if app_name not in apps:
#                 apps[app_name] = {}
# 
#             if locale == DEFAULT_LOCALE:
#                 apps[app_name].update(app_data)
#             else:
#                 if LOCALE_KEY not in apps[app_name]:
#                     apps[app_name][LOCALE_KEY] = {}
# 
#                 apps[app_name][LOCALE_KEY][locale] = app_data
# 
#     if locale_code:
#         apps = flatten_locale(apps, locale_code)
#         filtered_apps = [
#             app for f, app in apps.iteritems()
#             if os.path.basename(app['origin']) not in blacklist
#         ]
# 
#         return sorted(filtered_apps, key=lambda a: a["title"])
# 
#     return apps


import json
import os
from mock import patch
from kano_apps.AppData import get_applications
    with patch(
        'kano_apps.AppData._SYSTEM_APPLICATIONS_LOC',
        os.path.join(application_dir.strpath, '')
    ):
        apps = get_applications()

    assert len(apps) == 1

    test_app = apps[0]
    assert test_app['title'] == 'Pruebas'
    assert test_app['launch_command'] == {
        'cmd': 'echo',
        'args': [u'¡Hola!'],
    }


def test_get_applications_blacklist(tmpdir):
    application_dir = tmpdir.mkdir('applications')

    # create some fake applications
    test_app_1 = application_dir.join('test-app-1.app')
    test_app_1.write(json.dumps(TEST_APP))

    blacklisted_app = application_dir.join('blacklisted.app')
    blacklisted_app.write(json.dumps(TEST_APP))

    with patch(
        'kano_apps.AppData._SYSTEM_APPLICATIONS_LOC',
        os.path.join(application_dir.strpath, '')
    ):

# Path: kano_apps/AppData.py
# def get_applications(parse_cmds=True, all_locales=False, current_locale=True):
#     """ Get all the applications installed on the system.
# 
#         :param parse_cmds:
#         :type parse_cmds: bool
# 
#         :returns: A dict with all apps.
#         :rtype: dict
#     """
# 
#     loc = os.path.expanduser(_SYSTEM_APPLICATIONS_LOC)
#     blacklist = [
#         "idle3.desktop", "idle.desktop", "idle-python2.7.desktop",
#         "idle-python3.2.desktop", "xarchiver.desktop",
#         "make-minecraft.desktop", "make-music.desktop", "make-pong.desktop",
#         "make-snake.desktop", "kano-video.desktop", "lxsession-edit.desktop",
#         "lxrandr.desktop", "lxinput.desktop", "obconf.desktop",
#         "openbox.desktop", "libfm-pref-apps.desktop", "lxappearance.desktop",
#         "htop.desktop", "pcmanfm-desktop-pref.desktop", "video-cli.desktop",
#         "lxsession-default-apps.desktop", "pcmanfm.desktop", "help.desktop",
#         "rxvt-unicode.desktop", "org.gnome.gedit.desktop",
#         "chromium-browser.desktop", "openjdk-7-policytool.desktop"
#     ]
# 
#     def _collect_apps(application_dir, skip_checks=False):
#         if not os.path.exists(application_dir):
#             return {}
# 
#         _applications = {}
# 
#         for f in os.listdir(os.path.join(application_dir, '')):
#             fp = os.path.join(application_dir, f)
# 
#             if os.path.isdir(fp):
#                 continue
# 
#             if f[-4:] == ".app":
#                 data = load_from_app_file(fp, not skip_checks and parse_cmds)
#                 if data is not None:
#                     if not skip_checks and not is_app_installed(data):
#                         data["_install"] = True
# 
#                     _applications[f] = data
#                     if "overrides" in data:
#                         blacklist.extend(data["overrides"])
# 
#             elif f[-8:] == ".desktop" and f[0:5] != "auto_":
#                 data = _load_from_dentry(fp)
#                 if data is not None:
#                     _applications[f] = data
# 
#         return _applications
# 
#     locales = [
#         (DEFAULT_LOCALE, _SYSTEM_APPLICATIONS_LOC)
#     ]
# 
#     locale_code = None
# 
#     if all_locales:
#         locales += [
#             (locale, get_locale_dir(locale))
#             for locale in os.listdir(APP_LOCALE_DIR)
#         ]
#     elif current_locale:
#         current_locale = ensure_utf_locale(get_locale())
#         if current_locale and get_current_translation():
#             locale_code = current_locale.split('.')[0]
#             locales.append(
#                 (locale_code, get_locale_dir(locale_code))
#             )
# 
#     apps = {}
# 
#     for locale, locale_dir in locales:
#         locale_apps = _collect_apps(
#             locale_dir, skip_checks=locale != DEFAULT_LOCALE
#         )
# 
#         for app_name, app_data in locale_apps.iteritems():
#             if app_name not in apps:
#                 apps[app_name] = {}
# 
#             if locale == DEFAULT_LOCALE:
#                 apps[app_name].update(app_data)
#             else:
#                 if LOCALE_KEY not in apps[app_name]:
#                     apps[app_name][LOCALE_KEY] = {}
# 
#                 apps[app_name][LOCALE_KEY][locale] = app_data
# 
#     if locale_code:
#         apps = flatten_locale(apps, locale_code)
#         filtered_apps = [
#             app for f, app in apps.iteritems()
#             if os.path.basename(app['origin']) not in blacklist
#         ]
# 
#         return sorted(filtered_apps, key=lambda a: a["title"])
# 
#     return apps


# Path: tests/kano_apps/test_AppData.py
import json
import os
from mock import patch
from kano_apps.AppData import get_applications

    with patch(
        'kano_apps.AppData._SYSTEM_APPLICATIONS_LOC',
        os.path.join(application_dir.strpath, '')
    ):
        apps = get_applications()

    assert len(apps) == 1

    test_app = apps[0]
    assert test_app['title'] == 'Pruebas'
    assert test_app['launch_command'] == {
        'cmd': 'echo',
        'args': [u'¡Hola!'],
    }


def test_get_applications_blacklist(tmpdir):
    application_dir = tmpdir.mkdir('applications')

    # create some fake applications
    test_app_1 = application_dir.join('test-app-1.app')
    test_app_1.write(json.dumps(TEST_APP))

    blacklisted_app = application_dir.join('blacklisted.app')
    blacklisted_app.write(json.dumps(TEST_APP))

    with patch(
        'kano_apps.AppData._SYSTEM_APPLICATIONS_LOC',
        os.path.join(application_dir.strpath, '')
    ):

        apps = get_applications()