Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tokens.py 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
  4. #
  5. # This module is part of python-sqlparse and is released under
  6. # the BSD License: https://opensource.org/licenses/BSD-3-Clause
  7. from sqlparse import tokens as T
  8. from sqlparse.compat import text_type
  9. class _CaseFilter(object):
  10. ttype = None
  11. def __init__(self, case=None):
  12. case = case or 'upper'
  13. self.convert = getattr(text_type, case)
  14. def process(self, stream):
  15. for ttype, value in stream:
  16. if ttype in self.ttype:
  17. value = self.convert(value)
  18. yield ttype, value
  19. class KeywordCaseFilter(_CaseFilter):
  20. ttype = T.Keyword
  21. class IdentifierCaseFilter(_CaseFilter):
  22. ttype = T.Name, T.String.Symbol
  23. def process(self, stream):
  24. for ttype, value in stream:
  25. if ttype in self.ttype and value.strip()[0] != '"':
  26. value = self.convert(value)
  27. yield ttype, value
  28. class TruncateStringFilter(object):
  29. def __init__(self, width, char):
  30. self.width = width
  31. self.char = char
  32. def process(self, stream):
  33. for ttype, value in stream:
  34. if ttype != T.Literal.String.Single:
  35. yield ttype, value
  36. continue
  37. if value[:2] == "''":
  38. inner = value[2:-2]
  39. quote = "''"
  40. else:
  41. inner = value[1:-1]
  42. quote = "'"
  43. if len(inner) > self.width:
  44. value = ''.join((quote, inner[:self.width], self.char, quote))
  45. yield ttype, value