Пример #1
0
def run_with_python(input_path, delim, policy, csv_encoding, query,
                    output_delim, output_policy, output_path):
    with rbql.RbqlPyEnv() as worker_env:
        tmp_path = worker_env.module_path
        try:
            rbql.parse_to_py([query], tmp_path, delim, policy, output_delim,
                             output_policy, csv_encoding, None)
        except rbql.RBParsingError as e:
            report_error_and_exit('RBQL_Parsing', str(e))
        try:
            report = {'result_path': output_path}
            rbconvert = worker_env.import_worker()
            src = None
            if input_path:
                src = codecs.open(input_path, encoding=csv_encoding)
            else:
                src = rbql.get_encoded_stdin(csv_encoding)
            warnings = None
            with codecs.open(output_path, 'w', encoding=csv_encoding) as dst:
                warnings = rbconvert.rb_transform(src, dst)
            if warnings is not None:
                warnings = rbql.make_warnings_human_readable(warnings)
                report['warnings'] = warnings
            worker_env.remove_env_dir()
            report_success_and_exit(report)
        except Exception as e:
            error_msg = 'Error: Unable to use generated python module.\n'
            error_msg += 'Location of the generated module: {}\n'.format(
                tmp_path)
            error_msg += 'Original python exception:\n{}'.format(str(e))
            report_error_and_exit('Wrapper', error_msg)
Пример #2
0
def run_with_python(args):
    delim = rbql.normalize_delim(args.delim)
    policy = args.policy
    if policy is None:
        policy = 'quoted' if delim in [';', ','] else 'simple'
    query = args.query
    query_path = args.query_file
    #convert_only = args.convert_only
    input_path = args.input_table_path
    output_path = args.output_table_path
    import_modules = args.libs
    csv_encoding = args.csv_encoding
    output_delim, output_policy = interpret_format(args.out_format)

    rbql_lines = None
    if query is None and query_path is None:
        print_error_and_exit(
            'Error: provide either "--query" or "--query_path" option')
    if query is not None and query_path is not None:
        print_error_and_exit(
            'Error: unable to use both "--query" and "--query_path" options')
    if query_path is not None:
        assert query is None
        rbql_lines = codecs.open(query_path, encoding='utf-8').readlines()
    else:
        assert query_path is None
        rbql_lines = [query]

    with rbql.RbqlPyEnv() as worker_env:
        tmp_path = worker_env.module_path
        try:
            rbql.parse_to_py(rbql_lines, tmp_path, delim, policy, output_delim,
                             output_policy, csv_encoding, import_modules)
        except rbql.RBParsingError as e:
            print_error_and_exit('RBQL Parsing Error: \t{}'.format(e))
        try:
            rbconvert = worker_env.import_worker()
            src = None
            if input_path:
                src = codecs.open(input_path, encoding=csv_encoding)
            else:
                src = rbql.get_encoded_stdin(csv_encoding)
            warnings = None
            if output_path:
                with codecs.open(output_path, 'w',
                                 encoding=csv_encoding) as dst:
                    warnings = rbconvert.rb_transform(src, dst)
            else:
                dst = rbql.get_encoded_stdout(csv_encoding)
                warnings = rbconvert.rb_transform(src, dst)
            if warnings is not None:
                hr_warnings = rbql.make_warnings_human_readable(warnings)
                for warning in hr_warnings:
                    eprint('Warning: {}'.format(warning))
            worker_env.remove_env_dir()
        except Exception as e:
            error_msg = 'Error: Unable to use generated python module.\n'
            error_msg += 'Location of the generated module: {}\n\n'.format(
                tmp_path)
            error_msg += 'Original python exception:\n{}\n'.format(str(e))
            print_error_and_exit(error_msg)