def do_describe(self, parsed): what = parsed.matched[1][1].lower() if (what == 'active' or what == 'pending') and parsed.matched[2][1].lower() == 'search' and parsed.matched[3][1].lower() == 'index': resource = parsed.matched[4][1].lower() ks = self.cql_unprotect_name(parsed.get_binding('ksname', None)) cf = self.cql_unprotect_name(parsed.get_binding('cfname')) self.describe_search_resource(ks, cf, what, resource) else: OriginalShell.do_describe(self, parsed)
def _start_cql(self): c = Cluster([self.hostname]) self.cqlshell = Shell("127.0.0.1", 9042, use_conn=c) self.cqlshell.use_paging = False self.outStringWriter = StringIO.StringIO() self.cqlshell.query_out = self.outStringWriter self.cqlshell.stdout = self.outStringWriter cqlsh.setup_cqlruleset(cql3handling) cqlsh.setup_cqldocs(cql3handling) # cql3handling pass
def perform_simple_statement(self, statement): if not statement: return False, None default_timeout = self.execution_profile.request_timeout # Boost the session timeout for any search core management statements statement_prefix = " ".join(statement.query_string.lower().split()) if (default_timeout is not None and default_timeout == cqlsh.DEFAULT_REQUEST_TIMEOUT_SECONDS and (statement_prefix.startswith("create search index") or statement_prefix.startswith("alter search index") or statement_prefix.startswith("reload search index") or statement_prefix.startswith("rebuild search index") or statement_prefix.startswith("commit search index") or statement_prefix.startswith("drop search index"))): self.execution_profile.request_timeout = core_management_timeout success, future = OriginalShell.perform_simple_statement( self, statement) # reset timeout to its original value self.execution_profile.request_timeout = default_timeout return success, future
def _start_cql(self): c = Cluster([self.hostname]) self.cqlshell = Shell("127.0.0.1", 9042, use_conn = c ) self.cqlshell.use_paging = False self.outStringWriter = StringIO.StringIO() self.cqlshell.query_out = self.outStringWriter self.cqlshell.stdout = self.outStringWriter cqlsh.setup_cqlruleset(cql3handling) cqlsh.setup_cqldocs(cql3handling) # cql3handling pass
class CQLKernel(Kernel): implementation = 'cql_kernel' implementation_version = __version__ @property def language_version(self): m = version_pat.search(self.banner) return m.group(1) _banner = None # TODO @property def banner(self): # if self._banner is None: # self._banner = check_output(['CQL', '--version']).decode('utf-8') # Todo get right version self._banner = "CQL Version x" return self._banner language_info = { 'name': 'CQL', 'codemirror_mode': 'sql', 'mimetype': 'text/x-cassandra', 'file_extension': '.cql' } def __init__(self, **kwargs): Kernel.__init__(self, **kwargs) self.hostname = os.environ.get('CASSANDRA_HOSTNAME', 'localhost') self._start_cql() def _start_cql(self): c = Cluster([self.hostname]) self.cqlshell = Shell("127.0.0.1", 9042, use_conn=c) self.cqlshell.use_paging = False self.outStringWriter = StringIO.StringIO() self.cqlshell.query_out = self.outStringWriter self.cqlshell.stdout = self.outStringWriter cqlsh.setup_cqlruleset(cql3handling) cqlsh.setup_cqldocs(cql3handling) # cql3handling pass def do_execute(self, code, silent, store_history=True, user_expressions=None, allow_stdin=False): cleanCode = code.strip() if not cleanCode: return { 'status': 'ok', 'execution_count': self.execution_count, 'payload': [], 'user_expressions': {} } if cleanCode[-1] != ';': cleanCode += ";" self.outStringWriter.truncate(0) old_stdout = sys.stdout old_stderr = sys.stderr sys.stdout = self.outStringWriter sys.stderr = self.outStringWriter self.cqlshell.onecmd(cleanCode) sys.stdout = old_stdout sys.stderr = old_stderr if not silent: outputStr = self.outStringWriter.getvalue().strip() #Format desc commands with codemirror (cool feature) if code.strip()[:4] == 'desc': outputStr = '<script>var x = CodeMirror.fromTextArea(document.getElementById("desc%d"), {readOnly: true, mode:"text/x-cassandra"} )</script><textarea id="desc%d">%s</textarea>' % ( self.execution_count, self.execution_count, outputStr) # CQL rows come back as HTML if outputStr[:1] == '<': mime_type = 'text/html' else: mime_type = 'text/plain' stream_content = { 'execution_count': self.execution_count, 'data': { mime_type: outputStr } } self.send_response(self.iopub_socket, 'execute_result', stream_content) # if exitcode: # return {'status': 'error', 'execution_count': self.execution_count, # 'ename': '', 'evalue': str(exitcode), 'traceback': []} # else: return { 'status': 'ok', 'execution_count': self.execution_count, 'payload': [], 'user_expressions': {} } def do_complete(self, code, cursor_pos): code = code[:cursor_pos] default = { 'matches': [], 'cursor_start': 0, 'cursor_end': cursor_pos, 'metadata': dict(), 'status': 'ok' } # if not code or code[-1] == ' ': # return default # if code[-1:] in ('.', '(', '<'): completed = code partial = '' else: index = code.rfind(' ') completed = code[:index + 1] partial = code[index + 1:] matches = cql3handling.CqlRuleSet.cql_complete( completed, partial, cassandra_conn=self.cqlshell, startsymbol='cqlshCommand') if not matches: return default tokens = code.replace(';', ' ').split() if not tokens: return default return { 'matches': sorted(matches), 'cursor_start': cursor_pos - len(partial), 'cursor_end': cursor_pos, 'metadata': dict(), 'status': 'ok' }
def __init__(self, hostname, port, color=False, username=None, password=None, encoding=None, stdin=None, tty=True, completekey=cqlsh.DEFAULT_COMPLETEKEY, browser=None, use_conn=None, cqlver=None, keyspace=None, tracing_enabled=False, expand_enabled=False, no_compact=False, display_nanotime_format=cqlsh.DEFAULT_NANOTIME_FORMAT, display_timestamp_format=cqlsh.DEFAULT_TIMESTAMP_FORMAT, display_date_format=cqlsh.DEFAULT_DATE_FORMAT, display_float_precision=cqlsh.DEFAULT_FLOAT_PRECISION, display_double_precision=cqlsh.DEFAULT_DOUBLE_PRECISION, display_timezone=None, max_trace_wait=cqlsh.DEFAULT_MAX_TRACE_WAIT, ssl=False, single_statement=None, request_timeout=cqlsh.DEFAULT_REQUEST_TIMEOUT_SECONDS, protocol_version=None, connect_timeout=cqlsh.DEFAULT_CONNECT_TIMEOUT_SECONDS, no_file_io=cqlsh.DEFAULT_NO_FILE_IO, is_subshell=False): from dselib.authfactory import get_auth_provider if username: if not password: password = getpass.getpass() auth_provider = get_auth_provider(cqlsh.CONFIG_FILE, os.environ, username, password, cqlsh.options) self.execute_as = None self.consistency_level = dse.ConsistencyLevel.ONE self.serial_consistency_level = dse.ConsistencyLevel.SERIAL conn = None execution_profile = ExecutionProfile( load_balancing_policy=WhiteListRoundRobinPolicy([hostname]), row_factory=ordered_dict_factory, request_timeout=request_timeout, consistency_level=self.consistency_level, serial_consistency_level=self.serial_consistency_level) self.execution_profiles = {EXEC_PROFILE_DEFAULT: execution_profile} if use_conn: conn = use_conn else: kwargs = {} if protocol_version is not None: kwargs['protocol_version'] = protocol_version conn = cqlsh.Cluster( contact_points=(hostname, ), port=port, cql_version=cqlver, auth_provider=auth_provider, ssl_options=sslhandling.ssl_settings( hostname, cqlsh.CONFIG_FILE) if ssl else None, control_connection_timeout=connect_timeout, connect_timeout=connect_timeout, execution_profiles=self.execution_profiles, **kwargs) OriginalShell.__init__( self, hostname, port, color=color, auth_provider=auth_provider, username=username, password=password, encoding=encoding, stdin=stdin, tty=tty, completekey=completekey, browser=browser, use_conn=conn, cqlver=cqlver, keyspace=keyspace, tracing_enabled=tracing_enabled, expand_enabled=expand_enabled, no_compact=no_compact, display_nanotime_format=display_nanotime_format, display_timestamp_format=display_timestamp_format, display_date_format=display_date_format, display_float_precision=display_float_precision, display_double_precision=display_double_precision, display_timezone=display_timezone, max_trace_wait=max_trace_wait, ssl=ssl, single_statement=single_statement, request_timeout=request_timeout, protocol_version=protocol_version, connect_timeout=connect_timeout, no_file_io=no_file_io, is_subshell=is_subshell) self.owns_connection = not use_conn self.execution_profile = self.session.get_execution_profile( EXEC_PROFILE_DEFAULT)
class CQLKernel(Kernel): implementation = 'cql_kernel' implementation_version = __version__ @property def language_version(self): m = version_pat.search(self.banner) return m.group(1) _banner = None # TODO @property def banner(self): # if self._banner is None: # self._banner = check_output(['CQL', '--version']).decode('utf-8') # Todo get right version self._banner = "CQL Version x" return self._banner language_info = {'name': 'CQL', 'codemirror_mode': 'sql', 'mimetype': 'text/x-cassandra', 'file_extension': '.cql'} def __init__(self, **kwargs): Kernel.__init__(self, **kwargs) self.hostname = os.environ.get('CASSANDRA_HOSTNAME','localhost') self._start_cql() def _start_cql(self): c = Cluster([self.hostname]) self.cqlshell = Shell("127.0.0.1", 9042, use_conn = c ) self.cqlshell.use_paging = False self.outStringWriter = StringIO.StringIO() self.cqlshell.query_out = self.outStringWriter self.cqlshell.stdout = self.outStringWriter cqlsh.setup_cqlruleset(cql3handling) cqlsh.setup_cqldocs(cql3handling) # cql3handling pass def do_execute(self, code, silent, store_history=True, user_expressions=None, allow_stdin=False): cleanCode = code.strip() if not cleanCode: return {'status': 'ok', 'execution_count': self.execution_count, 'payload': [], 'user_expressions': {}} if cleanCode[-1] != ';': cleanCode += ";" self.outStringWriter.truncate(0) old_stdout = sys.stdout old_stderr = sys.stderr sys.stdout = self.outStringWriter sys.stderr = self.outStringWriter self.cqlshell.onecmd(cleanCode) sys.stdout = old_stdout sys.stderr = old_stderr if not silent: outputStr = self.outStringWriter.getvalue().strip() #Format desc commands with codemirror (cool feature) if code.strip()[:4] == 'desc': outputStr = '<script>var x = CodeMirror.fromTextArea(document.getElementById("desc%d"), {readOnly: true, mode:"text/x-cassandra"} )</script><textarea id="desc%d">%s</textarea>' % (self.execution_count, self.execution_count,outputStr) # CQL rows come back as HTML if outputStr[:1] == '<': mime_type = 'text/html' else: mime_type = 'text/plain' stream_content = {'execution_count': self.execution_count, 'data': {mime_type: outputStr}} self.send_response(self.iopub_socket, 'execute_result', stream_content) # if exitcode: # return {'status': 'error', 'execution_count': self.execution_count, # 'ename': '', 'evalue': str(exitcode), 'traceback': []} # else: return {'status': 'ok', 'execution_count': self.execution_count, 'payload': [], 'user_expressions': {}} def do_complete(self, code, cursor_pos): code = code[:cursor_pos] default = {'matches': [], 'cursor_start': 0, 'cursor_end': cursor_pos, 'metadata': dict(), 'status': 'ok'} # if not code or code[-1] == ' ': # return default # if code[-1:] in ('.','(','<'): completed=code partial = '' else: index = code.rfind(' ') completed = code[:index+1] partial = code[index+1:] matches = cql3handling.CqlRuleSet.cql_complete(completed, partial, cassandra_conn=self.cqlshell, startsymbol='cqlshCommand') if not matches: return default tokens = code.replace(';', ' ').split() if not tokens: return default return {'matches': sorted(matches), 'cursor_start': cursor_pos - len(partial), 'cursor_end': cursor_pos, 'metadata': dict(), 'status': 'ok'}
def __init__(self, hostname, port, color=False, username=None, password=None, encoding=None, stdin=None, tty=True, completekey=cqlsh.DEFAULT_COMPLETEKEY, browser=None, use_conn=None, cqlver=None, keyspace=None, secure_connect_bundle=None, consistency_level=None, serial_consistency_level=None, tracing_enabled=False, timing_enabled=False, expand_enabled=False, display_nanotime_format=cqlsh.DEFAULT_NANOTIME_FORMAT, display_timestamp_format=cqlsh.DEFAULT_TIMESTAMP_FORMAT, display_date_format=cqlsh.DEFAULT_DATE_FORMAT, display_float_precision=cqlsh.DEFAULT_FLOAT_PRECISION, display_double_precision=cqlsh.DEFAULT_DOUBLE_PRECISION, display_timezone=None, max_trace_wait=cqlsh.DEFAULT_MAX_TRACE_WAIT, ssl=False, single_statement=None, request_timeout=cqlsh.DEFAULT_REQUEST_TIMEOUT_SECONDS, protocol_version=None, connect_timeout=cqlsh.DEFAULT_CONNECT_TIMEOUT_SECONDS, no_file_io=cqlsh.DEFAULT_NO_FILE_IO, is_subshell=False): from dselib.authfactory import get_auth_provider if username: if not password: password = getpass.getpass() auth_provider = get_auth_provider(cqlsh.CONFIG_FILE, os.environ, username, password, cqlsh.options) \ if not is_unix_socket(hostname) else None self.execute_as = None if not consistency_level: raise Exception('Argument consistency_level must not be None') if not serial_consistency_level: raise Exception( 'Argument serial_consistency_level must not be None') self.consistency_level = consistency_level self.serial_consistency_level = serial_consistency_level execution_profile = ExecutionProfile( row_factory=ordered_dict_factory, request_timeout=request_timeout, consistency_level=self.consistency_level, serial_consistency_level=self.serial_consistency_level) self.execution_profiles = {EXEC_PROFILE_DEFAULT: execution_profile} if use_conn: conn = use_conn else: conn = cluster_factory( hostname, port=port, protocol_version=protocol_version if protocol_version is not None else _NOT_SET, cql_version=cqlver, auth_provider=auth_provider, ssl_options=sslhandling.ssl_settings( hostname, cqlsh.CONFIG_FILE) if ssl else None, control_connection_timeout=connect_timeout, connect_timeout=connect_timeout, execution_profiles=self.execution_profiles, secure_connect_bundle=secure_connect_bundle) OriginalShell.__init__( self, hostname, port, color=color, auth_provider=auth_provider, username=username, password=password, encoding=encoding, stdin=stdin, tty=tty, completekey=completekey, browser=browser, use_conn=conn, cqlver=cqlver, keyspace=keyspace, secure_connect_bundle=secure_connect_bundle, consistency_level=consistency_level, serial_consistency_level=serial_consistency_level, tracing_enabled=tracing_enabled, timing_enabled=timing_enabled, expand_enabled=expand_enabled, display_nanotime_format=display_nanotime_format, display_timestamp_format=display_timestamp_format, display_date_format=display_date_format, display_float_precision=display_float_precision, display_double_precision=display_double_precision, display_timezone=display_timezone, max_trace_wait=max_trace_wait, ssl=ssl, single_statement=single_statement, request_timeout=request_timeout, protocol_version=protocol_version, connect_timeout=connect_timeout, no_file_io=no_file_io, is_subshell=is_subshell) self.owns_connection = not use_conn self.execution_profile = self.session.get_execution_profile( EXEC_PROFILE_DEFAULT)