Python sqlparse.tokens.Whitespace() Examples
The following are 30
code examples of sqlparse.tokens.Whitespace().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sqlparse.tokens
, or try the search function
.
Example #1
Source File: filters.py From uroboroSQL-formatter with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _process(self, tlist): token = self._get_next_comment(tlist) while token: tidx = tlist.token_index(token) prev = tlist.token_prev(tidx, False) next_ = tlist.token_next(tidx, False) # Replace by whitespace if prev and next exist and if they're not # whitespaces. This doesn't apply if prev or next is a paranthesis. if (prev is not None and next_ is not None and not prev.is_whitespace() and not next_.is_whitespace() and not (prev.match(T.Punctuation, '(') or next_.match(T.Punctuation, ')'))): tlist.tokens[tidx] = sql.Token(T.Whitespace, ' ') else: tlist.tokens.pop(tidx) token = self._get_next_comment(tlist)
Example #2
Source File: filters.py From codenn with MIT License | 6 votes |
def process(self, stack, stmt): if isinstance(stmt, sql.Statement): self._curr_stmt = stmt self._process(stmt) if isinstance(stmt, sql.Statement): if self._last_stmt is not None: if unicode(self._last_stmt).endswith('\n'): nl = '\n' else: nl = '\n\n' stmt.tokens.insert( 0, sql.Token(T.Whitespace, nl)) if self._last_stmt != stmt: self._last_stmt = stmt # FIXME: Doesn't work ;)
Example #3
Source File: filters.py From uroboroSQL-formatter with BSD 3-Clause "New" or "Revised" License | 6 votes |
def process(self, stack, stmt): if isinstance(stmt, sql.Statement): self._curr_stmt = stmt self._process(stmt) if isinstance(stmt, sql.Statement): if self._last_stmt is not None: if str(self._last_stmt).endswith('\n'): nl = '\n' else: nl = '\n\n' stmt.tokens.insert( 0, sql.Token(T.Whitespace, nl)) if self._last_stmt != stmt: self._last_stmt = stmt # FIXME: Doesn't work ;)
Example #4
Source File: filters.py From codenn with MIT License | 6 votes |
def _process(self, tlist): token = self._get_next_comment(tlist) while token: tidx = tlist.token_index(token) prev = tlist.token_prev(tidx, False) next_ = tlist.token_next(tidx, False) # Replace by whitespace if prev and next exist and if they're not # whitespaces. This doesn't apply if prev or next is a paranthesis. if (prev is not None and next_ is not None and not prev.is_whitespace() and not next_.is_whitespace() and not (prev.match(T.Punctuation, '(') or next_.match(T.Punctuation, ')'))): tlist.tokens[tidx] = sql.Token(T.Whitespace, ' ') else: tlist.tokens.pop(tidx) token = self._get_next_comment(tlist)
Example #5
Source File: filters.py From uroboroSQL-formatter with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __custom_process_insert_values_lr(self, tlist): #INSERT の場合VALUES前後に空白1つをセット values_token = tlist.token_next_match(0, T.Keyword, "VALUES") if values_token: prv = tlist.token_prev(values_token, skip_ws=False) if prv and prv.is_whitespace(): prv.value = " " prv = tlist.token_prev(prv, skip_ws=False) while prv and prv.is_whitespace(): prv.value = "" prv = tlist.token_prev(prv, skip_ws=False) else: tlist.insert_before(values_token, sql.Token(T.Whitespace, " ")) nxt = tlist.token_next(values_token, skip_ws=False) if nxt and nxt.is_whitespace(): nxt.value = " " nxt = tlist.token_next(nxt, skip_ws=False) while nxt and nxt.is_whitespace(): nxt.value = "" nxt = tlist.token_next(nxt, skip_ws=False) else: tlist.insert_after(values_token, sql.Token(T.Whitespace, " "))
Example #6
Source File: others.py From SublimeText-SQLTools with GNU General Public License v3.0 | 6 votes |
def _process(tlist): def get_next_comment(): # TODO(andi) Comment types should be unified, see related issue38 return tlist.token_next_by(i=sql.Comment, t=T.Comment) tidx, token = get_next_comment() while token: pidx, prev_ = tlist.token_prev(tidx, skip_ws=False) nidx, next_ = tlist.token_next(tidx, skip_ws=False) # Replace by whitespace if prev and next exist and if they're not # whitespaces. This doesn't apply if prev or next is a paranthesis. if (prev_ is None or next_ is None or prev_.is_whitespace or prev_.match(T.Punctuation, '(') or next_.is_whitespace or next_.match(T.Punctuation, ')')): tlist.tokens.remove(token) else: tlist.tokens[tidx] = sql.Token(T.Whitespace, ' ') tidx, token = get_next_comment()
Example #7
Source File: others.py From SublimeText-SQLTools with GNU General Public License v3.0 | 6 votes |
def _process(tlist): ttypes = (T.Operator, T.Comparison) tidx, token = tlist.token_next_by(t=ttypes) while token: nidx, next_ = tlist.token_next(tidx, skip_ws=False) if next_ and next_.ttype != T.Whitespace: tlist.insert_after(tidx, sql.Token(T.Whitespace, ' ')) pidx, prev_ = tlist.token_prev(tidx, skip_ws=False) if prev_ and prev_.ttype != T.Whitespace: tlist.insert_before(tidx, sql.Token(T.Whitespace, ' ')) tidx += 1 # has to shift since token inserted before it # assert tlist.token_index(token) == tidx tidx, token = tlist.token_next_by(t=ttypes, idx=tidx)
Example #8
Source File: sql.py From uroboroSQL-formatter with BSD 3-Clause "New" or "Revised" License | 5 votes |
def get_alias(self): """Returns the alias for this identifier or ``None``.""" # "name AS alias" kw = self.token_next_match(0, T.Keyword, 'AS') if kw is not None: return self._get_first_name(kw, keywords=True) # "name alias" or "complicated column expression alias" if len(self.tokens) > 2 \ and self.token_next_by_type(0, T.Whitespace) is not None: return self._get_first_name(reverse=True) return None
Example #9
Source File: functions.py From uroboroSQL-formatter with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __call__(self, stream): for token_type, value in stream: if token_type not in Whitespace: return token_type in Keyword and value == self.type
Example #10
Source File: filters.py From uroboroSQL-formatter with BSD 3-Clause "New" or "Revised" License | 5 votes |
def StripWhitespace(stream): "Strip the useless whitespaces from a stream leaving only the minimal ones" last_type = None has_space = False ignore_group = frozenset((Comparison, Punctuation)) for token_type, value in stream: # We got a previous token (not empty first ones) if last_type: if token_type in Whitespace: has_space = True continue # Ignore first empty spaces and dot-commas elif token_type in (Whitespace, Whitespace.Newline, ignore_group): continue # Yield a whitespace if it can't be ignored if has_space: if not ignore_group.intersection((last_type, token_type)): yield Whitespace, ' ' has_space = False # Yield the token and set its type for checking with the next one yield token_type, value last_type = token_type
Example #11
Source File: filters.py From uroboroSQL-formatter with BSD 3-Clause "New" or "Revised" License | 5 votes |
def nl(self): # TODO: newline character should be configurable space = (self.char * ((self.indent * self.width) + self.offset)) # Detect runaway indenting due to parsing errors if len(space) > 200: # something seems to be wrong, flip back self.indent = self.offset = 0 space = (self.char * ((self.indent * self.width) + self.offset)) ws = '\n' + space return sql.Token(T.Whitespace, ws)
Example #12
Source File: filters.py From uroboroSQL-formatter with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _process(self, stream, varname, has_nl): # SQL query asignation to varname if self.count > 1: yield sql.Token(T.Whitespace, '\n') yield sql.Token(T.Name, varname) yield sql.Token(T.Whitespace, ' ') yield sql.Token(T.Operator, '=') yield sql.Token(T.Whitespace, ' ') if has_nl: yield sql.Token(T.Operator, '(') yield sql.Token(T.Text, "'") # Print the tokens on the quote for token in stream: # Token is a new line separator if token.is_whitespace() and '\n' in token.value: # Close quote and add a new line yield sql.Token(T.Text, " '") yield sql.Token(T.Whitespace, '\n') # Quote header on secondary lines yield sql.Token(T.Whitespace, ' ' * (len(varname) + 4)) yield sql.Token(T.Text, "'") # Indentation after_lb = token.value.split('\n', 1)[1] if after_lb: yield sql.Token(T.Whitespace, after_lb) continue # Token has escape chars elif "'" in token.value: token.value = token.value.replace("'", "\\'") # Put the token yield sql.Token(T.Text, token.value) # Close quote yield sql.Token(T.Text, "'") if has_nl: yield sql.Token(T.Operator, ')')
Example #13
Source File: filter.py From uroboroSQL-formatter with BSD 3-Clause "New" or "Revised" License | 5 votes |
def process(self, stack, stream): "Process the stream" consume_ws = False splitlevel = 0 stmt = None stmt_tokens = [] # Run over all stream tokens for ttype, value in stream: # Yield token if we finished a statement and there's no whitespaces if consume_ws and ttype not in (T.Whitespace, T.Comment.Single): stmt.tokens = stmt_tokens yield stmt # Reset filter and prepare to process next statement self._reset() consume_ws = False splitlevel = 0 stmt = None # Create a new statement if we are not currently in one of them if stmt is None: stmt = Statement() stmt_tokens = [] # Change current split level (increase, decrease or remain equal) splitlevel += self._change_splitlevel(ttype, value) # Append the token to the current statement stmt_tokens.append(Token(ttype, value)) # Check if we get the end of a statement if splitlevel <= 0 and ttype is T.Punctuation and value == ';': consume_ws = True # Yield pending statement (if any) if stmt is not None: stmt.tokens = stmt_tokens yield stmt
Example #14
Source File: filter.py From codenn with MIT License | 5 votes |
def process(self, stack, stream): "Process the stream" consume_ws = False splitlevel = 0 stmt = None stmt_tokens = [] # Run over all stream tokens for ttype, value in stream: # Yield token if we finished a statement and there's no whitespaces if consume_ws and ttype not in (T.Whitespace, T.Comment.Single): stmt.tokens = stmt_tokens yield stmt # Reset filter and prepare to process next statement self._reset() consume_ws = False splitlevel = 0 stmt = None # Create a new statement if we are not currently in one of them if stmt is None: stmt = Statement() stmt_tokens = [] # Change current split level (increase, decrease or remain equal) splitlevel += self._change_splitlevel(ttype, value) # Append the token to the current statement stmt_tokens.append(Token(ttype, value)) # Check if we get the end of a statement if splitlevel <= 0 and ttype is T.Punctuation and value == ';': consume_ws = True # Yield pending statement (if any) if stmt is not None: stmt.tokens = stmt_tokens yield stmt
Example #15
Source File: filters.py From uroboroSQL-formatter with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __process_parenthesis_for_enum(self, parenthesis): def proc_parenthesis(tokens, parent): for token in tokens: if tu.is_comma(token): next_token = parent.token_next(token, skip_ws=False) if next_token and next_token.is_whitespace(): next_token.value = " " else: parent.insert_after(token, sql.Token(T.Whitespace, " ")) elif tu.is_identifier_list(token): proc_parenthesis(token.tokens[:], token) elif token.is_group(): self._process(token) proc_parenthesis(tu.tokens_parenthesis_inner(parenthesis), parenthesis)
Example #16
Source File: filters.py From uroboroSQL-formatter with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __process_parenthesis_for_complist(self, tlist): open_punc = tlist.token_next_match(0, T.Punctuation, '(') self.indent += 1 tlist.insert_after(open_punc, self.nl()) self._process_default(tlist) comps = self._get_comparisons(tlist) tlist.insert_before(comps[0], sql.Token(T.Whitespace, "\t")) self._adjust_comparisons_indent(comps) close_punc = tlist.token_next_match(open_punc, T.Punctuation, ')') tlist.insert_before(close_punc, self.nl()) self.indent -= 1
Example #17
Source File: filters.py From uroboroSQL-formatter with BSD 3-Clause "New" or "Revised" License | 5 votes |
def nl_with_indent(self, offset): count = ((self.indent * self.width) + self.offset + offset) if count < 0: count = 0 space = "\t" * count sws = '\n' + space return sql.Token(T.Whitespace, sws)
Example #18
Source File: filters.py From uroboroSQL-formatter with BSD 3-Clause "New" or "Revised" License | 5 votes |
def indent_space(self, offset=0): space = ("\t" * ((self.indent * self.width) + self.offset + offset)) return sql.Token(T.Whitespace, space)
Example #19
Source File: filters.py From uroboroSQL-formatter with BSD 3-Clause "New" or "Revised" License | 5 votes |
def one_indent_space(self): return sql.Token(T.Whitespace, "\t")
Example #20
Source File: filters.py From uroboroSQL-formatter with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _adjust_comparisons_indent(self, comparisons): """ Comparisonの内部インデントの調整 """ ids = [] for token in comparisons: if not token.is_group(): continue if tu.is_comparison(token): ids.append(_ComparisonObject(token, self.indent + self.offset, self.local_config)) max_width_left = 0 max_width_operator = 0 max_width_right = 0 for comparison in ids: max_width_left = max(max_width_left, comparison.width_left) max_width_operator = max(max_width_operator, comparison.width_operator) max_width_right = max(max_width_right, comparison.width_right) for comparison in ids: if comparison.right_tokens: left = comparison.left_lines[-1] left_space = "\t" * int(calc_tab_padding_count(left, max_width_left)) if len(comparison.left_lines) > 1: left_space += "\t" comparison.token.insert_after(comparison.left_tokens[-1], sql.Token(T.Whitespace, left_space)) op_space = "\t" * int(calc_tab_padding_count(comparison.operator_string, max_width_operator)) comparison.token.insert_after(comparison.operator_tokens[-1], sql.Token(T.Whitespace, op_space)) if comparison.line_comment: right = comparison.right_lines[-1] right_space = "\t" * int(calc_tab_padding_count(right, max_width_right)) if len(comparison.right_lines) > 1: right_space += "\t\t" + ("\t" * int(calc_tab_padding_count("", max_width_left))) comparison.token.insert_after(comparison.right_tokens[-1], sql.Token(T.Whitespace, right_space))
Example #21
Source File: sql_parse.py From incubator-superset with Apache License 2.0 | 5 votes |
def _get_table(tlist: TokenList) -> Optional[Table]: """ Return the table if valid, i.e., conforms to the [[catalog.]schema.]table construct. :param tlist: The SQL tokens :returns: The table if the name conforms """ # Strip the alias if present. idx = len(tlist.tokens) if tlist.has_alias(): ws_idx, _ = tlist.token_next_by(t=Whitespace) if ws_idx != -1: idx = ws_idx tokens = tlist.tokens[:idx] if ( len(tokens) in (1, 3, 5) and all(imt(token, t=[Name, String]) for token in tokens[::2]) and all(imt(token, m=(Punctuation, ".")) for token in tokens[1::2]) ): return Table(*[remove_quotes(token.value) for token in tokens[::-2]]) return None
Example #22
Source File: sql_metadata.py From sql-metadata with MIT License | 5 votes |
def get_query_tokens(query: str) -> List[sqlparse.sql.Token]: """ :type query str :rtype: list[sqlparse.sql.Token] """ query = preprocess_query(query) parsed = sqlparse.parse(query) # handle empty queries (#12) if not parsed: return [] tokens = TokenList(parsed[0].tokens).flatten() # print([(token.value, token.ttype) for token in tokens]) return [token for token in tokens if token.ttype is not Whitespace]
Example #23
Source File: others.py From SublimeText-SQLTools with GNU General Public License v3.0 | 5 votes |
def _stripws_identifierlist(self, tlist): # Removes newlines before commas, see issue140 last_nl = None for token in list(tlist.tokens): if last_nl and token.ttype is T.Punctuation and token.value == ',': tlist.tokens.remove(last_nl) last_nl = token if token.is_whitespace else None # next_ = tlist.token_next(token, skip_ws=False) # if (next_ and not next_.is_whitespace and # token.ttype is T.Punctuation and token.value == ','): # tlist.insert_after(token, sql.Token(T.Whitespace, ' ')) return self._stripws_default(tlist)
Example #24
Source File: sql.py From SublimeText-SQLTools with GNU General Public License v3.0 | 5 votes |
def __init__(self, ttype, value): value = text_type(value) self.value = value self.ttype = ttype self.parent = None self.is_group = False self.is_keyword = ttype in T.Keyword self.is_whitespace = self.ttype in T.Whitespace self.normalized = value.upper() if self.is_keyword else value
Example #25
Source File: sql.py From SublimeText-SQLTools with GNU General Public License v3.0 | 5 votes |
def get_alias(self): """Returns the alias for this identifier or ``None``.""" # "name AS alias" kw_idx, kw = self.token_next_by(m=(T.Keyword, 'AS')) if kw is not None: return self._get_first_name(kw_idx + 1, keywords=True) # "name alias" or "complicated column expression alias" _, ws = self.token_next_by(t=T.Whitespace) if len(self.tokens) > 2 and ws is not None: return self._get_first_name(reverse=True)
Example #26
Source File: aligned_indent.py From SublimeText-SQLTools with GNU General Public License v3.0 | 5 votes |
def nl(self, offset=1): # offset = 1 represent a single space after SELECT offset = -len(offset) if not isinstance(offset, int) else offset # add two for the space and parens indent = self.indent * (2 + self._max_kwd_len) return sql.Token(T.Whitespace, self.n + self.char * ( self._max_kwd_len + offset + indent + self.offset))
Example #27
Source File: output.py From SublimeText-SQLTools with GNU General Public License v3.0 | 5 votes |
def _process(self, stream, varname, has_nl): # SQL query asignation to varname if self.count > 1: yield sql.Token(T.Whitespace, '\n') yield sql.Token(T.Name, varname) yield sql.Token(T.Whitespace, ' ') yield sql.Token(T.Operator, '=') yield sql.Token(T.Whitespace, ' ') if has_nl: yield sql.Token(T.Operator, '(') yield sql.Token(T.Text, "'") # Print the tokens on the quote for token in stream: # Token is a new line separator if token.is_whitespace and '\n' in token.value: # Close quote and add a new line yield sql.Token(T.Text, " '") yield sql.Token(T.Whitespace, '\n') # Quote header on secondary lines yield sql.Token(T.Whitespace, ' ' * (len(varname) + 4)) yield sql.Token(T.Text, "'") # Indentation after_lb = token.value.split('\n', 1)[1] if after_lb: yield sql.Token(T.Whitespace, after_lb) continue # Token has escape chars elif "'" in token.value: token.value = token.value.replace("'", "\\'") # Put the token yield sql.Token(T.Text, token.value) # Close quote yield sql.Token(T.Text, "'") if has_nl: yield sql.Token(T.Operator, ')')
Example #28
Source File: reindent.py From SublimeText-SQLTools with GNU General Public License v3.0 | 5 votes |
def nl(self, offset=0): return sql.Token( T.Whitespace, self.n + self.char * max(0, self.leading_ws + offset))
Example #29
Source File: reindent.py From SublimeText-SQLTools with GNU General Public License v3.0 | 5 votes |
def _process_identifierlist(self, tlist): identifiers = list(tlist.get_identifiers()) first = next(identifiers.pop(0).flatten()) num_offset = 1 if self.char == '\t' else self._get_offset(first) if not tlist.within(sql.Function): with offset(self, num_offset): position = 0 for token in identifiers: # Add 1 for the "," separator position += len(token.value) + 1 if position > (self.wrap_after - self.offset): adjust = 0 if self.comma_first: adjust = -2 _, comma = tlist.token_prev( tlist.token_index(token)) if comma is None: continue token = comma tlist.insert_before(token, self.nl(offset=adjust)) if self.comma_first: _, ws = tlist.token_next( tlist.token_index(token), skip_ws=False) if (ws is not None and ws.ttype is not T.Text.Whitespace): tlist.insert_after( token, sql.Token(T.Whitespace, ' ')) position = 0 self._process_default(tlist)
Example #30
Source File: reindent.py From SublimeText-SQLTools with GNU General Public License v3.0 | 5 votes |
def process(self, stmt): self._curr_stmt = stmt self._process(stmt) if self._last_stmt is not None: nl = '\n' if text_type(self._last_stmt).endswith('\n') else '\n\n' stmt.tokens.insert(0, sql.Token(T.Whitespace, nl)) self._last_stmt = stmt return stmt