<commit_before>#!/usr/bin/python3

import argparse
import logging
import os
import json
from enum import Enum
from keyman_config.kmpmetadata import parsemetadata, parseinfdata
from keyman_config.get_kmp import user_keyman_dir

class InstallArea(Enum):
    IA_OS = 1
    IA_SHARED = 2
    IA_USER = 3
    IA_UNKNOWN = 99

def get_installed_kmp(area):
    """
    Get list of installed keyboards in an install area.

	Args:
		area (InstallArea): install area to check
            InstallArea.IA_USER: ~/.local/share/keyman and ~/.kmfl
            InstallArea.IA_SHARED: /usr/local/share/keyman
            InstallArea.IA_OS: /usr/share/keyman
    Returns:
        list: Installed keyboards
            dict: Keyboard
                id (str): Keyboard ID
                name (str): Keyboard name
                kmpname (str): Keyboard name in local
                version (str): Keyboard version
                kmpversion (str):
                path (str): base path where keyboard is installed
                description (str): Keyboard description
    """
    check_paths = []
    if area == InstallArea.IA_USER:
        home = os.path.expanduser("~")
        check_paths = [ user_keyman_dir(), os.path.join(home, ".kmfl") ]
    elif area == InstallArea.IA_SHARED:
        check_paths = [ "/usr/local/share/keyman" ]
    elif area == InstallArea.IA_OS:
        check_paths = [ "/usr/share/keyman" ]

    return get_installed_kmp_paths(check_paths)


def get_installed_kmp_paths(check_paths):
    """
    Get list of installed keyboards.

	Args:
		check_paths (list): list of paths to check

    Returns:
        list: Installed keyboards
            dict: Keyboard
                id (str): Keyboard ID
                name (str): Keyboard name
                kmpname (str): Keyboard name in local
                version (str): Keyboard version
                kmpversion (str):
                path (str): base path where keyboard is installed
                description (str): Keyboard description
    """
    installed_keyboards = {}
    for keymanpath in check_paths:
        if os.path.isdir(keymanpath):
            for o in os.listdir(keymanpath):
                if os.path.isdir(os.path.join(keymanpath,o)) and o != "icons":
                    name = md_name = version = md_version = description = kbdata = None
                    metadata = parsemetadata(os.path.join(keymanpath, o, "kmp.json"))
                    if not metadata[0]:
                        metadata = parseinfdata(os.path.join(keymanpath, o, "kmp.inf"))
                    kbjson = os.path.join(keymanpath, o, o + ".json")
                    if os.path.isfile(kbjson):
                        with open(kbjson, "r") as read_file:
                            kbdata = json.load(read_file)
                    if kbdata:
                        if 'description' in kbdata:
                            description = kbdata['description']
                        version = kbdata['version']
                        name = kbdata['name']
                    if metadata[0]:
                        info = metadata[0]
                        md_version = info['version']['description']
                        md_name = info['name']['description']
                    if not name:
                        version = md_version
                        name = md_name

                    installed_keyboards[o] = { "id" : o, "name" : name, "kmpname" : md_name, "version" : version, "kmpversion" : md_version, "path" : keymanpath, "description" : description}
    return installed_keyboards


def get_kmp_version(keyboardid):
    """
    Get version of the kmp for a keyboard ID.
    This return the highest version if installed in more than one area

    Args:
        keyboardid (dict): Keyboard ID
    Returns:
        str: kmp version if keyboard ID is installed
        None: if not found
    """
    version = None
    user_kmp = get_installed_kmp(InstallArea.IA_USER)
    shared_kmp = get_installed_kmp(InstallArea.IA_SHARED)
    os_kmp = get_installed_kmp(InstallArea.IA_OS)

    if keyboardid in os_kmp:
        version = os_kmp[keyboardid]['version']

    if keyboardid in shared_kmp:
        shared_version = shared_kmp[keyboardid]['version']
        if version:
            if version < shared_version:
                version = shared_version
        else:
            version = shared_version

    if keyboardid in user_kmp:
        user_version = user_kmp[keyboardid]['version']
        if version:
            if version < user_version:
                version = user_version
        else:
            version = shared_version

    return version

def get_kmp_version_user(keyboardid):
    """
    Get version of the kmp for a keyboard ID.
    This only checks the user area.

    Args:
        keyboardid (dict): Keyboard ID
    Returns:
        str: kmp version if keyboard ID is installed
        None: if not found
    """<commit_msg>[linux] user_version typo<commit_after>#!/usr/bin/python3

import argparse
import logging
import os
import json
from enum import Enum
from keyman_config.kmpmetadata import parsemetadata, parseinfdata
from keyman_config.get_kmp import user_keyman_dir

class InstallArea(Enum):
    IA_OS = 1
    IA_SHARED = 2
    IA_USER = 3
    IA_UNKNOWN = 99

def get_installed_kmp(area):
    """
    Get list of installed keyboards in an install area.

	Args:
		area (InstallArea): install area to check
            InstallArea.IA_USER: ~/.local/share/keyman and ~/.kmfl
            InstallArea.IA_SHARED: /usr/local/share/keyman
            InstallArea.IA_OS: /usr/share/keyman
    Returns:
        list: Installed keyboards
            dict: Keyboard
                id (str): Keyboard ID
                name (str): Keyboard name
                kmpname (str): Keyboard name in local
                version (str): Keyboard version
                kmpversion (str):
                path (str): base path where keyboard is installed
                description (str): Keyboard description
    """
    check_paths = []
    if area == InstallArea.IA_USER:
        home = os.path.expanduser("~")
        check_paths = [ user_keyman_dir(), os.path.join(home, ".kmfl") ]
    elif area == InstallArea.IA_SHARED:
        check_paths = [ "/usr/local/share/keyman" ]
    elif area == InstallArea.IA_OS:
        check_paths = [ "/usr/share/keyman" ]

    return get_installed_kmp_paths(check_paths)


def get_installed_kmp_paths(check_paths):
    """
    Get list of installed keyboards.

	Args:
		check_paths (list): list of paths to check

    Returns:
        list: Installed keyboards
            dict: Keyboard
                id (str): Keyboard ID
                name (str): Keyboard name
                kmpname (str): Keyboard name in local
                version (str): Keyboard version
                kmpversion (str):
                path (str): base path where keyboard is installed
                description (str): Keyboard description
    """
    installed_keyboards = {}
    for keymanpath in check_paths:
        if os.path.isdir(keymanpath):
            for o in os.listdir(keymanpath):
                if os.path.isdir(os.path.join(keymanpath,o)) and o != "icons":
                    name = md_name = version = md_version = description = kbdata = None
                    metadata = parsemetadata(os.path.join(keymanpath, o, "kmp.json"))
                    if not metadata[0]:
                        metadata = parseinfdata(os.path.join(keymanpath, o, "kmp.inf"))
                    kbjson = os.path.join(keymanpath, o, o + ".json")
                    if os.path.isfile(kbjson):
                        with open(kbjson, "r") as read_file:
                            kbdata = json.load(read_file)
                    if kbdata:
                        if 'description' in kbdata:
                            description = kbdata['description']
                        version = kbdata['version']
                        name = kbdata['name']
                    if metadata[0]:
                        info = metadata[0]
                        md_version = info['version']['description']
                        md_name = info['name']['description']
                    if not name:
                        version = md_version
                        name = md_name

                    installed_keyboards[o] = { "id" : o, "name" : name, "kmpname" : md_name, "version" : version, "kmpversion" : md_version, "path" : keymanpath, "description" : description}
    return installed_keyboards


def get_kmp_version(keyboardid):
    """
    Get version of the kmp for a keyboard ID.
    This return the highest version if installed in more than one area

    Args:
        keyboardid (dict): Keyboard ID
    Returns:
        str: kmp version if keyboard ID is installed
        None: if not found
    """
    version = None
    user_kmp = get_installed_kmp(InstallArea.IA_USER)
    shared_kmp = get_installed_kmp(InstallArea.IA_SHARED)
    os_kmp = get_installed_kmp(InstallArea.IA_OS)

    if keyboardid in os_kmp:
        version = os_kmp[keyboardid]['version']

    if keyboardid in shared_kmp:
        shared_version = shared_kmp[keyboardid]['version']
        if version:
            if version < shared_version:
                version = shared_version
        else:
            version = shared_version

    if keyboardid in user_kmp:
        user_version = user_kmp[keyboardid]['version']
        if version:
            if version < user_version:
                version = user_version
        else:
            version = user_version

    return version

def get_kmp_version_user(keyboardid):
    """
    Get version of the kmp for a keyboard ID.
    This only checks the user area.

    Args:
        keyboardid (dict): Keyboard ID
    Returns:
        str: kmp version if keyboard ID is installed
        None: if not found
    """
{'fraction_non_alphanumeric': 0.08498583569405099, 'fraction_numerical': 0.001821125050586807, 'mean_word_length': 1.8495243586047851, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '932631', 'n_tokens_mistral': 2850, 'n_tokens_neox': 2696, 'n_words': 776}