Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@export
def open_external_editor(filename=None, sql=None):
"""
Open external editor, wait for the user to type in his query,
return the query.
:return: list with one tuple, query as first element.
"""
message = None
filename = filename.strip().split(' ', 1)[0] if filename else None
sql = sql or ''
MARKER = '# Type your query above this line.\n'
# Populate the editor buffer with the partial sql (if available) and a
# placeholder comment.
query = click.edit(u'{sql}\n\n{marker}'.format(sql=sql, marker=MARKER),
@export
def get_filename(sql):
if sql.strip().startswith('\\e'):
command, _, filename = sql.partition(' ')
return filename.strip() or None
@export
def content_exceeds_width(row, width):
# Account for 3 characters between each column
separator_space = (len(row)*3)
# Add 2 columns for a bit of buffer
line_len = sum([len(x) for x in row]) + separator_space + 2
return line_len > width
@export
def get_watch_command(command):
match = re.match("(.*?)[\s]*\\\\watch (\d+);?$", command)
if match:
groups = match.groups()
return groups[0], int(groups[1])
return None, None
@export
def editor_command(command):
"""
Is this an external editor command? (\e or \ev)
:param command: string
Returns the specific external editor command found.
"""
# It is possible to have `\e filename` or `SELECT * FROM \e`. So we check
# for both conditions.
stripped = command.strip()
for sought in ('\\e ', '\\ev ', '\\ef '):
if stripped.startswith(sought):
return sought.strip()
for sought in ('\\e', ):
@export
def get_editor_query(sql):
"""Get the query part of an editor command."""
sql = sql.strip()
# The reason we can't simply do .strip('\e') is that it strips characters,
# not a substring. So it'll strip "e" in the end of the sql also!
# Ex: "select * from style\e" -> "select * from styl".
pattern = re.compile('(^\\\e|\\\e$)')
while pattern.search(sql):
sql = pattern.sub('', sql)
return sql
@export
def parse_special_command(sql):
command, _, arg = sql.partition(' ')
verbose = '+' in command
command = command.strip().replace('+', '')
return (command, verbose, arg.strip())