mechatroner / vscode_rainbow_csv

🌈Rainbow CSV - VS Code extension: Highlight CSV and TSV files in different rainbow colors to make them more readable
MIT License
432 stars 53 forks source link

RBQL doesn't support print statements or file open/close in UDF files for Python #159

Open ynain opened 10 months ago

ynain commented 10 months ago

I tried defining a UDF which would allow me to cache queries before running or reference previously cached ones from a file:

import json
import os
def save_query(*args, **kwargs):
    desc = f"{datetime.now()}: {kwargs['desc']}"
    qf = f"{os.getenv('TOOLING_DIR')}/QueryFile"
    print(qf)
    with open(qf, "w") as qryFile:
        cached_queries = json.loads(qryFile.read())
        query_entry = {
            kwargs['key']: {
                "query": f"{args}",
                "description": desc
            }}
        cached_queries.update(query_entry)
        qryFile.write(json.dumps(cached_queries))
    return args

custom_query = lambda : eval("<custom query as string>")

def run_query(qry_key):
    with open("f"{os.getenv('TOOLING_DIR')}/QueryFile", "r") as qryFile:
        cached_queries = json.loads(qryFile.read())
        if qry_key not in cached_queries.keys():
            raise KeyError("Undefined query")
        qry = cached_queries[qry_key]
    return qry['query']

This does not appear to work - it either fails with this error, or a similar one. I may be doing this incorrectly, as I assume that the RBQL queries are in a tuple format.

Screenshot 2024-01-11 at 7 21 19 PM

Is this because the extension is just reading from stdout?

async function run_command_and_parse_output(cmd, args) {
    let execution_result = null;
    try {
        execution_result = await command_async_wrapper(cmd, args);
...
    let json_report = execution_result.stdout;
    if (!json_report || execution_result.stderr) {
        let error_msg = execution_result.stderr || 'empty error';
        return {error_type: 'Integration', error_msg: error_msg};
    }
    try {
        return JSON.parse(json_report);
    } catch (e) {
        return {error_type: 'Integration', error_msg: 'Unable to parse JSON report'};
    }
}
...
function command_async_wrapper(cmd, args) {
    return new Promise(function (resolve, reject) {
        let stdout_data = '';
        let stderr_data = '';
        let process = child_process.spawn(cmd, args, {'windowsHide': true});
        process.stdout.on('data', function(data) {
            stdout_data += data.toString();
        });
        process.stderr.on('data', function(data) {
            stderr_data += data.toString();
        });
        process.on('close', function (code) { // Consider replacing 'close' with 'exit'.
            resolve({'exit_code': code, 'stdout': stdout_data, 'stderr': stderr_data});
        });
        process.on('error', function (err) {
            reject(err);
        });
    });
}