def render_template(template_file, output_file): location = find(template_file, "metrics/").replace("metrics", "") template_file = path.join(template_file.split(".")[0], template_file) # Template render template_loader = FileSystemLoader(searchpath="metrics/") template_env = Environment(loader=template_loader) TEMPLATE_FILE = location with indent(4, quote="* "): puts(colored.green("Going to render template file at --> " + location)) with indent(4, quote="* "): puts(colored.green("Rendered SQL --> ")) template = template_env.get_template(TEMPLATE_FILE) output_text = template.render(config_dict) if output_file: filename = path.join(".", "build", output_file) os.makedirs(path.dirname(filename), exist_ok=True) with open(filename, "w+") as text_file: print(output_text, file=text_file) return output_text else: print( highlight(output_text, SqlLexer(), TerminalFormatter(style="monokai"))) return output_text
def _pretend_to_run(self, migration, method): """ Pretend to run the migration. :param migration: The migration :type migration: orator.migrations.migration.Migration :param method: The method to execute :type method: str """ self._note('') names = [] for query in self._get_queries(migration, method): name = migration.__class__.__name__ bindings = None if isinstance(query, tuple): query, bindings = query query = highlight( query, SqlLexer(), CommandFormatter() ).strip() if bindings: query = (query, bindings) if name not in names: self._note('[<info>{}</info>]'.format(name)) names.append(name) self._note(query)
def test_can_analyze_text(): mysql_lexer = MySqlLexer() sql_lexer = SqlLexer() tsql_lexer = TransactSqlLexer() code_to_expected_lexer_map = { 'select `a`, `bc` from some': mysql_lexer, 'select a, bc from some': sql_lexer, 'select [a], [bc] from some': tsql_lexer, '-- `a`, `bc`\nselect [a], [bc] from some': tsql_lexer, '-- `a`, `bc`\nselect [a], [bc] from some; go': tsql_lexer, } sql_lexers = set(code_to_expected_lexer_map.values()) for code, expected_lexer in code_to_expected_lexer_map.items(): ratings_and_lexers = list((lexer.analyse_text(code), lexer.name) for lexer in sql_lexers) best_rating, best_lexer_name = sorted(ratings_and_lexers, reverse=True)[0] expected_rating = expected_lexer.analyse_text(code) message = ( 'lexer must be %s (rating %.2f) instead of ' '%s (rating %.2f) for analyse_text() on code:\n%s') % ( expected_lexer.name, expected_rating, best_lexer_name, best_rating, code ) assert expected_lexer.name == best_lexer_name, message
def _pretty_format_sql(text: str): import pygments from pygments.formatters.terminal import TerminalFormatter from pygments.lexers.sql import SqlLexer text = pygments.highlight(code=text, formatter=TerminalFormatter(), lexer=SqlLexer()).rstrip() return text
def pretty_print_query(query): """ Converts your SQLALchemy query object into pretty SQL Code """ parsed_query = sqlparse.format(str(query), reindent=True) lexer = SqlLexer() formatter = TerminalFormatter(bg="dark") print(pygments.highlight(parsed_query, lexer, formatter))
def format(self, record): try: sql = sqlparse.format(record.sql, keyword_case='upper', identifier_case='lower', truncate_strings=50, reindent=True).strip('\n') sql = '\n\t| '.join([l for l in sql.split('\n')]) sql = highlight(sql, SqlLexer(), TerminalFormatter()) return '({:.3f}) | {}'.format(record.duration, sql) except: # fall back to the default formatting if anything happens return super().format(record)
def printsql(sql_string): """Prints an SQL statement with syntax highlighting""" style_string = """ <style> {pygments_css} </style> """ dis.display( dis.HTML( style_string.format( pygments_css=HtmlFormatter().get_style_defs(".highlight")))) dis.display( dis.HTML(data=highlight(sql_string, SqlLexer(), HtmlFormatter())))
class FunctionFile(HTMLFile): CSS = ['highlight.css'] LEXERS = { 'plpgsql': PlPgsqlLexer(), 'sql': SqlLexer(), } def _get_lexer(self, language): return self.LEXERS.get(language) def _render(self, function, language, code): lexer = self._get_lexer(language) if lexer is not None: highlighted = highlight(code, lexer, FORMATTER) else: highlighted = '<pre>%s</pre>' % escape(code) self.write('<h1>Function %s</h1>' % function) self.write('%s language' % language) self.write(highlighted)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) name = kwargs["name"] target = kwargs.get("target", "dev") if target == "dev": db_target = "top_develop" elif target == "stg": db_target = "top_staging" elif target == "prd": db_target = "top_production" else: raise Http404 sql = """ select mst.id ,mst.name as name ,(select query from {} where master_id = mst.id order by create_date desc limit 1) as query from top_schemamaster mst where mst.name = %s """.format( db_target ) rows = [x for x in models.SchemaMaster.objects.raw(sql, [name])] if not rows: raise Http404 context["code"] = highlight( rows[0].query, SqlLexer(), HtmlFormatter(full=True, linenos=True, title=name, wrapcode=True), ) return context
def prettify_statement(statement): statement = sqlparse.format(statement, keyword_case='upper', reindent=True) return indent(highlight(statement, SqlLexer(), Terminal256Formatter(style='native'))).rstrip()
def format(self, record): sql = sqlparse.format(record.sql.strip(), reindent=True) record.statement = pygments.highlight(sql, SqlLexer(), TerminalTrueColorFormatter(style="monokai")) return super().format(record)
x: input Returns: the input within a ``pre`` tag """ return f"<pre>{x}</pre>" # ============================================================================= # SQL formatting # ============================================================================= SQL_BASE_CSS_CLASS = "sq" # brief is good SQL_FORMATTER = HtmlFormatter(cssclass=SQL_BASE_CSS_CLASS) SQL_LEXER = SqlLexer() def prettify_sql_html(sql: str, reformat: bool = False, indent_width: int = 4) -> str: """ Formats SQL (optionally), and highlights it with Pygments. Args: sql: raw SQL text reformat: reformat the layout? indent_width: if reformatting, what indent should we use? Returns: str: HTML
import logging import sqlparse from pygments import highlight from pygments.formatters.terminal256 import Terminal256Formatter from pygments.lexers.sql import SqlLexer from django.conf import settings TIME_CRIT = 0.2 TIME_WARN = 0.05 TIME_FORMAT = u'\x1b[0;30;{bgcolor}m {duration:.3f}s \x1b[0m\n{msg}' LEXER = SqlLexer() FORMATTER = Terminal256Formatter(style=settings.SQL_FORMATTER_STYLE) class SqlFormatter(logging.Formatter): def format(self, record): try: sql = record.sql.strip() duration = record.duration except AttributeError: return super(SqlFormatter, self).format(record) sql = sqlparse.format(sql, reindent=True, keyword_case='upper') bgcolor = 41 if duration > TIME_CRIT else 43 if duration > TIME_WARN else 42 return TIME_FORMAT.format(bgcolor=bgcolor, duration=duration, msg=highlight(sql, LEXER, FORMATTER))