<commit_before>#!/usr/bin/env python

import sys
import yaml
import os
import requests
import json

from colorama import Fore, Back, Style


def get_url(conf):
    return 'https://localise.biz/api/'


def config(args):
    from os.path import expanduser
    home = expanduser("~")
    config_file = home + '/.localise/config.yml'
    if hasattr(args, config_file):
        config_file = args.config_file

    print "Create config file in %s" % (config_file)

    token = raw_input('Localise API token [None]: ')

    data = dict(
        api=dict(
            token=token
        ),
        translations=[
            dict(
                locale='en-US',
                format='po',
                file='/tmp/locale/en/messages.po'
            )
        ],
        push={
            "index": 'id',
            "ignore-new": 'false',
            "ignore-existing": 'false',
            "delete-absent": 'false'
        },
        pull=dict(
        )
    )

    if not os.path.exists(os.path.dirname(config_file)):
        os.makedirs(os.path.dirname(config_file))
    with open(config_file, 'w+') as out:
        out.write(yaml.dump(data, default_flow_style=False))


def push(conf):
    errors = []

    if not 'translations' in conf:
        sys.exit(
            Fore.RED + 'Could not find any translations to pull. Please make sure your configuration is formed correctly.' + Style.RESET_ALL)

    for translation in conf['translations']:
        if not 'locale' in translation or not 'format' in translation or not 'file' in translation:
            sys.exit(Fore.RED + 'Missing translation data.' + Style.RESET_ALL)

        try:
            with open(translation['file']) as f:
                file = f.read()
        except (IOError, OSError) as e:
            errors.append('Error: ' + str(e))
            break

        params = conf['push']
        params.update(dict(
            key=conf['api']['token'],
            locale=translation['locale']
        ))
        url = get_url(conf) + 'import/%s' % (translation['format'])

        r = requests.post(url, params=params, data={'src': file})
        if r.status_code != 200:
            message = 'Something went wrong. Please contact support.'
            res = json.loads(r.text)
            if r['error']:
                message = res['error'] + ' for file ' + translation['file']

            errors.append(message)

    # If there are any errors display them to the user
    if errors:
        for error in errors:
            print(Fore.RED + error + Style.RESET_ALL)
    else:
        sys.exit(Fore.GREEN + 'Successfully pushed ' + str(
            len(conf['translations'])) + ' file(s) to Localise.biz!' + Style.RESET_ALL)


def pull(conf):
    errors = []

    if not 'translations' in conf:
        sys.exit(
            Fore.RED + 'Could not find any translations to pull. Please make sure your configuration is formed correctly.' + Style.RESET_ALL)

    for translation in conf['translations']:
        if not 'locale' in translation or not 'format' in translation or not 'file' in translation:
            sys.exit(Fore.RED + 'Missing translation data.' + Style.RESET_ALL)

        url = get_url(conf) + 'export/locale/%s.%s?format=%s' % (
            translation['locale'], translation['format'], translation['format'])

        r = requests.get(url, stream=True, auth=(conf['api']['token'], ''))

        if r.status_code != 200:
            message = 'Something went wrong.'
            res = json.loads(r.text)
            if res['meta']['error']['message']:
                message = res['meta']['error']['message'] + ' for file ' + translation['file']

            errors.append(message)
        else:
            # Swap put the content of the file with the data
            with open(translation['file'], 'wb') as file:
                for chunk in r.iter_content(chunk_size=1024):
                    if chunk:
                        file.write(chunk)

    # If there are any errors display them to the user<commit_msg>Handle exception when pulling data from server<commit_after>#!/usr/bin/env python

import sys
import yaml
import os
import requests
import json

from colorama import Fore, Back, Style


def get_url(conf):
    return 'https://localise.biz/api/'


def config(args):
    from os.path import expanduser
    home = expanduser("~")
    config_file = home + '/.localise/config.yml'
    if hasattr(args, config_file):
        config_file = args.config_file

    print "Create config file in %s" % (config_file)

    token = raw_input('Localise API token [None]: ')

    data = dict(
        api=dict(
            token=token
        ),
        translations=[
            dict(
                locale='en-US',
                format='po',
                file='/tmp/locale/en/messages.po'
            )
        ],
        push={
            "index": 'id',
            "ignore-new": 'false',
            "ignore-existing": 'false',
            "delete-absent": 'false'
        },
        pull=dict(
        )
    )

    if not os.path.exists(os.path.dirname(config_file)):
        os.makedirs(os.path.dirname(config_file))
    with open(config_file, 'w+') as out:
        out.write(yaml.dump(data, default_flow_style=False))


def push(conf):
    errors = []

    if not 'translations' in conf:
        sys.exit(
            Fore.RED + 'Could not find any translations to pull. Please make sure your configuration is formed correctly.' + Style.RESET_ALL)

    for translation in conf['translations']:
        if not 'locale' in translation or not 'format' in translation or not 'file' in translation:
            sys.exit(Fore.RED + 'Missing translation data.' + Style.RESET_ALL)

        try:
            with open(translation['file']) as f:
                file = f.read()
        except (IOError, OSError) as e:
            errors.append('Error: ' + str(e))
            break

        params = conf['push']
        params.update(dict(
            key=conf['api']['token'],
            locale=translation['locale']
        ))
        url = get_url(conf) + 'import/%s' % (translation['format'])

        r = requests.post(url, params=params, data={'src': file})
        if r.status_code != 200:
            message = 'Something went wrong. Please contact support.'
            res = json.loads(r.text)
            if r['error']:
                message = res['error'] + ' for file ' + translation['file']

            errors.append(message)

    # If there are any errors display them to the user
    if errors:
        for error in errors:
            print(Fore.RED + error + Style.RESET_ALL)
    else:
        sys.exit(Fore.GREEN + 'Successfully pushed ' + str(
            len(conf['translations'])) + ' file(s) to Localise.biz!' + Style.RESET_ALL)


def pull(conf):
    errors = []

    if not 'translations' in conf:
        sys.exit(
            Fore.RED + 'Could not find any translations to pull. Please make sure your configuration is formed correctly.' + Style.RESET_ALL)

    for translation in conf['translations']:
        if not 'locale' in translation or not 'format' in translation or not 'file' in translation:
            sys.exit(Fore.RED + 'Missing translation data.' + Style.RESET_ALL)

        url = get_url(conf) + 'export/locale/%s.%s?format=%s' % (
            translation['locale'], translation['format'], translation['format'])

        r = requests.get(url, stream=True, auth=(conf['api']['token'], ''))

        if r.status_code != 200:
            message = 'Something went wrong.'
            try:
                res = json.loads(r.text)
                if res['meta']['error']['message']:
                    message = res['meta']['error']['message'] + ' for file ' + translation['file']
            except (ValueError) as e:
                message = 'Wrong response format'

            errors.append(message)
        else:
            # Swap put the content of the file with the data
            with open(translation['file'], 'wb') as file:
                for chunk in r.iter_content(chunk_size=1024):
                    if chunk:
                        file.write(chunk)

    # If there are any errors display them to the user
{'fraction_non_alphanumeric': 0.12573457394711068, 'fraction_numerical': 0.002448579823702253, 'mean_word_length': 2.0356744704570793, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '3058239', 'n_tokens_mistral': 2338, 'n_tokens_neox': 2242, 'n_words': 650}