Mini Shell

Direktori : /usr/share/l.v.e-manager/utils/
Upload File :
Current File : //usr/share/l.v.e-manager/utils/sync_locales.py

#!/opt/cloudlinux/venv/bin/python3
# coding:utf-8

# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import copy
import argparse
import os
import json

BASE_DIR = os.path.join(os.path.dirname(__file__), "../")
USER_LOCALES_SOURCE = '/usr/share/l.v.e-locales/'
MAP = {
    'cPanel':       BASE_DIR + 'cpanel/spa/assets/i18n',
    'Plesk':        BASE_DIR + 'plesk/htdocs/modules/plesk-lvemanager/i18n/',
    'DirectAdmin':  BASE_DIR + 'directadmin/lvemanager_spa/images/assets/i18n/',
}

I18N_SOURCE = BASE_DIR + 'commons/spa-resources/i18n/'

def main():
    """
    Analyze arguments and merge locale files
    :return:
    """
    parser = argparse.ArgumentParser(
        description='Convert yaml locales to json')

    parser.add_argument(
        '--panel', '-p',
        type=str,
        help="The target control panel"
    )

    args = parser.parse_args()

    if not os.path.isdir(USER_LOCALES_SOURCE):
        return
    panel_path = MAP.get(args.panel, I18N_SOURCE)
    for locale in available_locales(USER_LOCALES_SOURCE):
        user_locale = read_locale_json(USER_LOCALES_SOURCE, locale)
        current_locale = read_locale_json(I18N_SOURCE, locale)
        if current_locale:
            current_locale = nested_merge(copy.deepcopy(current_locale), user_locale)
        write_locale_json(panel_path, locale, current_locale)

def available_locales(path):
    """
    Get list of locales in directory
    :param path: directory with locales
    :return: list of locales
    """
    for locale in os.listdir(path):
        filename = '{}/{}'.format(path, locale)
        if os.path.isfile(filename) and filename.endswith('.json'):
            yield locale.split('.')[0]

def read_locale_json(path, locale):
    """
    read locale from directory
    :param path: directory with locales
    :param locale: locale (en, etc)
    :return: locale data
    """
    filename = '{}/{}.json'.format(path, locale)
    if os.path.isfile(filename):
        with open(filename, 'r') as f:
            return json.load(f)


def write_locale_json(path, locale, data):
    """
    Save json into file
    :param path: directory with locales
    :param locale: locale (en, etc)
    :param data: locale dict
    :return:
    """
    with open('{}/{}.json'.format(path, locale), 'w') as f:
        return json.dump(data, f, indent=4)

def nested_merge(a, b, path=None):
    """
    Merger a into b
    :param a: dict
    :param b: dict
    :return: merger dict
    """
    path = path or []
    for key in b:
        if key in a:
            if isinstance(a[key], dict) and isinstance(b[key], dict):
                nested_merge(a[key], b[key], path + [str(key)])
            else:
                a[key] = b[key]
        else:
            a[key] = b[key]
    return a

if __name__ == '__main__':
    main()

Zerion Mini Shell 1.0