Ejemplo n.º 1
0
def _get_lexer(codesyntax):
    if codesyntax in ("cpp", "javascript"):
        return lexers.JavascriptLexer()
    elif codesyntax == "python":
        return lexers.PythonLexer()
    elif codesyntax == "json":
        return lexers.JsonLexer()
    elif codesyntax == "xml" or codesyntax == "html":
        return lexers.HtmlLexer()
    elif codesyntax == "yml" or codesyntax == "yaml":
        return lexers.YamlLexer()
    elif codesyntax == "css":
        return lexers.CssLexer()
    elif codesyntax == "sql":
        return lexers.SqlLexer()
    elif codesyntax == "bash" or codesyntax == "sh":
        return lexers.BashLexer()
    elif codesyntax == "go":
        return lexers.GoLexer()
    elif codesyntax == "diff":
        return lexers.DiffLexer()
    elif codesyntax == "emacslisp":
        return lexers.EmacsLispLexer()
    elif codesyntax == "lisp":
        return lexers.CommonLispLexer()
    elif codesyntax == "rust":
        return lexers.RustLexer()
    elif codesyntax == "jsx":
        return BabylonLexer()
    elif codesyntax:
        raise NotImplementedError(codesyntax)
    else:
        return lexers.TextLexer()
Ejemplo n.º 2
0
    def create_table_from_avro_schema(self, keyspace, table, avro_schema, partition_key, cluster_key):

        avro_json = avro_schema.to_json()
        columns_clause = ', '.join(map(lambda f: "\"" + f['name'] + "\"" + ' ' + SonarCassandraSession.avro2cass(f['type']), avro_json['fields']))
        primary_key_clause = SonarCassandraSession.primary_key(partition_key, cluster_key)

        create_query = "CREATE TABLE {}.{} ({}, PRIMARY KEY {})".format(keyspace, table, columns_clause, primary_key_clause)

        if self.debug or self.dry:
            pretty_print(create_query, title="Create table CQL", lexer=lexers.SqlLexer())
        if not self.dry:
            self.session.execute(create_query, timeout=None) 
Ejemplo n.º 3
0
    def table_exists(self, keyspace, table):

        exists_query = "SELECT table_name FROM system_schema.tables WHERE keyspace_name='{}' AND table_name='{}'".format(keyspace, table)

        if self.debug:
            pretty_print(exists_query, title="Check for Cassandra table CQL", lexer=lexers.SqlLexer())

        if not self.dry:
            try:
                results = self.session.execute(exists_query)
            except AuthenticationFailed:
                raise Exception("Cassandra user '{}' unauthorized to view system_schema.tables on hosts '{}'!".format(self.username, self.hosts))

            if self.debug:
                pretty_print(results.current_rows, title="Query results")

            if results.current_rows:
                return True
            else:
                return False
        else:
            return True
Ejemplo n.º 4
0
import sqlparse

from django.utils.log import ServerFormatter
from pygments import formatters, highlight, lexers

TIME_CRIT = 0.2
TIME_WARN = 0.05
TIME_FORMAT = u'\x1b[0;30;{bgcolor}m {duration:.3f}s \x1b[0m\n{msg}'

LEXER = lexers.SqlLexer()
FORMATTER = formatters.Terminal256Formatter(style='monokai')


class SqlFormatter(ServerFormatter):
    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')
        bg_color = 41 if duration > TIME_CRIT else 43 if duration > TIME_WARN else 42

        return TIME_FORMAT.format(bgcolor=bg_color,
                                  duration=duration,
                                  msg=highlight(sql, LEXER, FORMATTER))