Skip to content

Commit 6032359

Browse files
author
mechatroner
committed
fix rbql integration
1 parent 3effe9e commit 6032359

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ RBQL backend language.
105105
Supported values: _"Python"_, _"JS"_
106106
To use RBQL with JavaScript (JS) you need to have Node JS installed and added to your system path.
107107

108+
#### "rbql_with_headers"
109+
Default: false
110+
RBQL will treat first records in all input and join files as headers.
111+
You can set this value to true if most of the CSV files you deal with have headers.
112+
You can override this setting on the query level by either adding `WITH (header)` or `WITH (noheader)` to the end of the query.
113+
108114
#### "rbql_output_format"
109115
Format of RBQL result set tables.
110116
Supported values: _"tsv"_, _"csv"_, _"input"_

RainbowCSV.sublime-settings

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
// In order to use RBQL with JavaScript (JS) you need to have Node JS installed and added to your system path.
3939
"rbql_backend_language": "Python",
4040

41+
// RBQL will treat first records in all input and join files as headers.
42+
// You can set this value to true if most of the CSV files you deal with have headers.
43+
// You can override this setting on the query level by either adding `WITH (header)` or `WITH (noheader)` to the end of the query.
44+
"rbql_with_headers": false,
4145

4246
// RBQL encoding for files and queries.
4347
// Supported values: "latin-1", "utf-8"

main.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
######## Test and Debug #########
2222
# To install this package in debug mode:
23-
# 1. Make sure you don't have production rainbow_csv installed, if it installed - uninstall it first, there should be no rainbow_csv folder in the Sublime Text 3/Packages dir
23+
# 1. Make sure you don't have production rainbow_csv installed, if it is installed - uninstall it first, there should be no rainbow_csv folder in the Sublime Text 3/Packages dir
2424
# 2. Just copy it into "Sublime Text 3/Packages" folder as "rainbow_csv", e.g.: cp -r sublime_rainbow_csv "/mnt/c/Users/mecha/AppData/Roaming/Sublime Text 3/Packages/rainbow_csv"
2525

2626
# To debug this package just use python's own print() function - all output would be redirected to sublime text console. View -> Show Console
@@ -586,6 +586,7 @@ def on_done_query_edit(input_line):
586586
input_delim, input_policy = input_dialect
587587
backend_language = get_backend_language(active_view)
588588
output_format = get_setting(active_view, 'rbql_output_format', 'input')
589+
with_headers = get_setting(active_view, 'rbql_with_headers', False)
589590
encoding = get_setting(active_view, 'rbql_encoding', 'utf-8')
590591
encoding = encoding.lower()
591592
if encoding not in ['latin-1', 'utf-8']:
@@ -597,7 +598,7 @@ def on_done_query_edit(input_line):
597598
sublime.error_message('RBQL Error. "rbql_output_format" must be in [{}]'.format(', '.join(format_map.keys())))
598599
return
599600
output_delim, output_policy = format_map[output_format]
600-
query_result = sublime_rbql.converged_execute(backend_language, file_path, input_line, input_delim, input_policy, output_delim, output_policy, encoding)
601+
query_result = sublime_rbql.converged_execute(backend_language, file_path, input_line, input_delim, input_policy, output_delim, output_policy, encoding, with_headers)
601602
error_type, error_details, warnings, dst_table_path = query_result
602603
if error_type is not None:
603604
sublime.error_message('Unable to execute RBQL query :(\nEdit your query and try again!\n\n\n\n\n=============================\nDetails:\n{}\n{}'.format(error_type, error_details))
@@ -794,8 +795,9 @@ def run(self, _edit):
794795
previous_query = self.view.settings().get('rbql_previous_query', '')
795796
backend_language = get_backend_language(self.view)
796797
pretty_language_name = prettify_language_name(backend_language)
797-
encoding = get_setting(self.view, 'rbql_encoding', 'utf-8')
798-
active_window.show_input_panel('Enter SQL-like RBQL query ({}/{}):'.format(pretty_language_name, encoding), previous_query, on_done_query_edit, None, on_query_cancel)
798+
with_headers = get_setting(self.view, 'rbql_with_headers', False)
799+
headers_report = 'with header' if with_headers else 'no header'
800+
active_window.show_input_panel('Enter SQL-like RBQL query ({}/{}):'.format(pretty_language_name, headers_report), previous_query, on_done_query_edit, None, on_query_cancel)
799801
self.view.settings().set('rbql_mode', True)
800802
show_column_names(self.view, delim, policy)
801803

sublime_rbql.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,25 @@ def system_has_node_js():
2424
return exit_code == 0 and len(out_data) and len(err_data) == 0
2525

2626

27-
def execute_python(src_table_path, encoding, query, input_delim, input_policy, out_delim, out_policy, dst_table_path):
27+
def execute_python(src_table_path, encoding, query, input_delim, input_policy, out_delim, out_policy, dst_table_path, with_headers):
2828
try:
2929
warnings = []
30-
rbql.query_csv(query, src_table_path, input_delim, input_policy, dst_table_path, out_delim, out_policy, encoding, warnings)
30+
rbql.query_csv(query, src_table_path, input_delim, input_policy, dst_table_path, out_delim, out_policy, encoding, warnings, with_headers)
3131
return (None, None, warnings)
3232
except Exception as e:
3333
error_type, error_msg = rbql.exception_to_error_info(e)
3434
return (error_type, error_msg, [])
3535

3636

37-
def execute_js(src_table_path, encoding, query, input_delim, input_policy, out_delim, out_policy, dst_table_path):
37+
def execute_js(src_table_path, encoding, query, input_delim, input_policy, out_delim, out_policy, dst_table_path, with_headers):
3838
import json
3939
if not system_has_node_js():
4040
return ('Execution Error', 'Node.js is not found in your OS, test command: "node --version"', [])
4141
script_dir = os.path.dirname(os.path.realpath(__file__))
4242
rbql_js_script_path = os.path.join(script_dir, 'rbql-js', 'cli_rbql.js')
4343
cmd = ['node', rbql_js_script_path, '--query', query, '--delim', input_delim, '--policy', input_policy, '--out-delim', out_delim, '--out-policy', out_policy, '--input', src_table_path, '--output', dst_table_path, '--encoding', encoding, '--error-format', 'json']
44+
if with_headers:
45+
cmd.append('--with-headers')
4446
if os.name == 'nt':
4547
# Without the startupinfo magic Windows will show console window for a longer period.
4648
si = subprocess.STARTUPINFO()
@@ -70,7 +72,7 @@ def execute_js(src_table_path, encoding, query, input_delim, input_policy, out_d
7072
return (error_type, error_msg, warnings)
7173

7274

73-
def converged_execute(meta_language, src_table_path, query, input_delim, input_policy, out_delim, out_policy, encoding):
75+
def converged_execute(meta_language, src_table_path, query, input_delim, input_policy, out_delim, out_policy, encoding, with_headers):
7476
try:
7577
tmp_dir = tempfile.gettempdir()
7678
table_name = os.path.basename(src_table_path)
@@ -80,9 +82,9 @@ def converged_execute(meta_language, src_table_path, query, input_delim, input_p
8082
dst_table_path = os.path.join(tmp_dir, dst_table_name)
8183
assert meta_language in ['python', 'js'], 'Meta language must be "python" or "js"'
8284
if meta_language == 'python':
83-
exec_result = execute_python(src_table_path, encoding, query, input_delim, input_policy, out_delim, out_policy, dst_table_path)
85+
exec_result = execute_python(src_table_path, encoding, query, input_delim, input_policy, out_delim, out_policy, dst_table_path, with_headers)
8486
else:
85-
exec_result = execute_js(src_table_path, encoding, query, input_delim, input_policy, out_delim, out_policy, dst_table_path)
87+
exec_result = execute_js(src_table_path, encoding, query, input_delim, input_policy, out_delim, out_policy, dst_table_path, with_headers)
8688
error_type, error_details, warnings = exec_result
8789
if error_type is not None:
8890
dst_table_path = None

0 commit comments

Comments
 (0)