# Path: easy_karabiner/fucking_string.py
# def ensure_utf8(s):
#     # convert from any object to `unicode`
#     if not isinstance(s, basestring):
#         s = unicode(s)
# 
#     if isinstance(s, unicode):
#         s = s.encode('utf-8')
#     return unicode(s, encoding='utf-8')


import os
import subprocess
from . import util
from .fucking_string import ensure_utf8
    # list absolute path of all applications
    LIST_APP_PATH_CMD = ('mdfind', '-0', 'kMDItemContentType==com.apple.application-bundle')
    # get Bundle-Identifier and Display-Name of applications by absolute path
    LIST_APP_INFO_CMD = ('mdls', '-raw',
                         '-name', 'kMDItemCFBundleIdentifier',
                         '-name', 'kMDItemDisplayName')

    raw_paths = call(LIST_APP_PATH_CMD).split('\x00')
    app_paths = [p for p in raw_paths if p.strip()]

    raw_infos = call(LIST_APP_INFO_CMD + tuple(app_paths))
    info_lines = raw_infos.split('\x00')

    app_ids = info_lines[0::2]
    app_infos = tuple(zip(app_paths, app_ids))
    # display name not always equal to basename,
    # because system language and application name maybe not English
    display_names = tuple(info_lines[1::2])
    basenames = tuple(get_name_without_suffix(p) for p in app_paths)

    name_map = tuple(zip(display_names + basenames, app_infos + app_infos))
    return dict(name_map)


def get_app_info(name):
    if not hasattr(get_app_info, 'infos'):
        get_app_info.infos = get_all_app_info()

    if name.endswith('.app'):
        name = name[:-4]

# Path: easy_karabiner/fucking_string.py
# def ensure_utf8(s):
#     # convert from any object to `unicode`
#     if not isinstance(s, basestring):
#         s = unicode(s)
# 
#     if isinstance(s, unicode):
#         s = s.encode('utf-8')
#     return unicode(s, encoding='utf-8')


# Path: easy_karabiner/osxkit.py
import os
import subprocess
from . import util
from .fucking_string import ensure_utf8

    # list absolute path of all applications
    LIST_APP_PATH_CMD = ('mdfind', '-0', 'kMDItemContentType==com.apple.application-bundle')
    # get Bundle-Identifier and Display-Name of applications by absolute path
    LIST_APP_INFO_CMD = ('mdls', '-raw',
                         '-name', 'kMDItemCFBundleIdentifier',
                         '-name', 'kMDItemDisplayName')

    raw_paths = call(LIST_APP_PATH_CMD).split('\x00')
    app_paths = [p for p in raw_paths if p.strip()]

    raw_infos = call(LIST_APP_INFO_CMD + tuple(app_paths))
    info_lines = raw_infos.split('\x00')

    app_ids = info_lines[0::2]
    app_infos = tuple(zip(app_paths, app_ids))
    # display name not always equal to basename,
    # because system language and application name maybe not English
    display_names = tuple(info_lines[1::2])
    basenames = tuple(get_name_without_suffix(p) for p in app_paths)

    name_map = tuple(zip(display_names + basenames, app_infos + app_infos))
    return dict(name_map)


def get_app_info(name):
    if not hasattr(get_app_info, 'infos'):
        get_app_info.infos = get_all_app_info()

    if name.endswith('.app'):
        name = name[:-4]

    name = ensure_utf8(name)