Skip to content

Commit

Permalink
fix separator encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
mechatroner committed Jul 12, 2020
1 parent bf7ba42 commit d51f041
Showing 1 changed file with 16 additions and 35 deletions.
51 changes: 16 additions & 35 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import json
from functools import partial

import binascii

import sublime_plugin
Expand Down Expand Up @@ -33,28 +32,18 @@

# FIXME add special handling of whitespace-separated grammar. Treat consecutive whitespaces as a single separator

# FIXME tab char in multichar separators is getting saved as-is when saving to get_table_index_path()

def get_table_index_path():
global table_index_path_cached
if table_index_path_cached is not None:
table_index_path_cached
user_home_dir = os.path.expanduser('~')
packages_path = sublime.packages_path()
sublime_user_dir = os.path.join(packages_path, 'User')
if os.path.exists(sublime_user_dir):
table_index_path_cached = os.path.join(sublime_user_dir, 'rbql_table_index')
else:
table_index_path_cached = os.path.join(user_home_dir, '.rbql_table_index')
if table_index_path_cached is None:
table_index_path_cached = os.path.join(sublime.packages_path(), 'User', 'rbql_table_index_hex')
return table_index_path_cached


def get_table_names_path():
global table_names_path_cached
if table_names_path_cached is not None:
return table_names_path_cached
user_home_dir = os.path.expanduser('~')
table_names_path_cached = os.path.join(user_home_dir, '.rbql_table_names') # TODO move to Package/User after improving RBQL architecture
if table_names_path_cached is None:
table_names_path_cached = os.path.join(os.path.expanduser('~'), '.rbql_table_names') # TODO move to Package/User after improving RBQL architecture
return table_names_path_cached


Expand All @@ -74,12 +63,20 @@ def get_table_names_path():
}'''


def encode_delim(delim):
return binascii.hexlify(delim.encode('utf-8')).decode('ascii')


def decode_delim(delim):
return binascii.unhexlify(delim.encode('ascii')).decode('utf-8')


def get_syntax_file_basename(delim, policy):
assert policy in policy_map.keys()
for k, v in legacy_syntax_names.items():
if (delim, policy) == k:
return v
return 'Rainbow_CSV_hex_{}_{}.sublime-syntax'.format(binascii.hexlify(delim.encode('utf-8')).decode('ascii'), policy_map[policy])
return 'Rainbow_CSV_hex_{}_{}.sublime-syntax'.format(encode_delim(delim), policy_map[policy])


def ensure_syntax_file(delim, policy):
Expand Down Expand Up @@ -231,29 +228,13 @@ def do_adjust_color_scheme(style):
dst.write(syntax_data)



def adjust_color_scheme(view):
try:
do_adjust_color_scheme(view.style())
except Exception as e:
print('Unable to auto adjust color scheme. Unexpected Exception: {}'.format(e))



def index_decode_delim(delim):
# FIXME consider multichar delims
if delim == 'TAB':
return '\t'
return delim


def index_encode_delim(delim):
# FIXME consider multichar delims
if delim == '\t':
return 'TAB'
return delim


def try_read_index(index_path):
lines = []
try:
Expand Down Expand Up @@ -294,7 +275,7 @@ def load_rainbow_params(file_path):
delim, policy = record[1:3]
if policy not in ['simple', 'quoted']:
return (None, None)
delim = index_decode_delim(delim)
delim = decode_delim(delim)
return (delim, policy)
return (None, None)

Expand All @@ -310,7 +291,7 @@ def update_records(records, record_key, new_record):
def save_rainbow_params(file_path, delim, policy):
table_index_path = get_table_index_path()
records = try_read_index(table_index_path)
new_record = [file_path, index_encode_delim(delim), policy, '']
new_record = [file_path, encode_delim(delim), policy, '']
update_records(records, file_path, new_record)
if len(records) > 100:
records.pop(0)
Expand Down Expand Up @@ -367,7 +348,7 @@ def get_dialect_from_grammar_basename(grammar_basename):
wpos = encoded_dialect.rfind('_')
if wpos == -1:
return None
delim = (binascii.unhexlify(encoded_dialect[:wpos].encode('ascii'))).decode('utf-8')
delim = decode_delim(encoded_dialect[:wpos])
policy = policy_map_inv.get(encoded_dialect[wpos + 1:], None)
if delim is None or policy is None:
return None
Expand Down

0 comments on commit d51f041

Please sign in to comment.