forked from yuj/sublime-parquet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parquet.py
59 lines (46 loc) · 1.86 KB
/
parquet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
import sublime
import sublime_plugin
import os
import subprocess
"""
Parquet package for Sublime Text, with python tooling.
https://github.com/yuj/sublime-parquet
Requires the python port of parquet-tools
https://github.com/ktrueda/parquet-tools
"""
COMMAND_NOT_FOUND_MSG = """'parquet-tools' not found.\n
Make sure python is in your PATH and install with 'pip install parquet-tools'
"""
PYTHON_COMMAND = ["parquet-tools", "csv"]
UNICODE_ERROR = "Non-unicode characters prevented rendering: \n"
def run_command(command):
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
return iter(p.stdout.readline, b"")
class ParquetCommand(sublime_plugin.TextCommand):
def run(self, edit, filename=None):
if filename is None or not filename.endswith(".parquet"):
return
command = PYTHON_COMMAND
command.append(filename)
pos = 0
try:
for line in run_command(command):
row = line.decode("utf-8")
pos += self.view.insert(edit, pos, row.rstrip() + "\n")
except (UnicodeDecodeError, TypeError):
self.view.set_name(os.path.basename(filename))
self.view.set_read_only(True)
sublime.error_message(UNICODE_ERROR + str(line))
except (FileNotFoundError, AttributeError):
sublime.error_message(COMMAND_NOT_FOUND_MSG)
self.view.set_name(os.path.basename(filename))
self.view.set_read_only(True)
class OpenParquetFile(sublime_plugin.EventListener):
def on_load(self, view):
filename = view.file_name()
if filename.endswith(".parquet"):
sublime.status_message("opening parquet file: " + filename)
parquet_view = view.window().new_file()
view.close()
parquet_view.run_command("parquet", {"filename": filename})