query.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # -*- coding: utf-8 -*-
  2. #
  3. # cms.py - simple WSGI/Python based CMS script
  4. #
  5. # Copyright (C) 2011-2019 Michael Buesch <m@bues.ch>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #from cms.cython_support cimport * #@cy
  20. from cms.exception import *
  21. from cms.util import * #+cimport
  22. __all__ = [
  23. "CMSQuery",
  24. ]
  25. class CMSQuery(object):
  26. __slots__ = (
  27. "__queryDict",
  28. )
  29. def __init__(self, queryDict):
  30. self.__queryDict = {}
  31. for k, v in queryDict.items():
  32. assert isinstance(k, str)
  33. assert isinstance(v, (bytes, bytearray))
  34. self.__queryDict[k] = v.decode("UTF-8", "strict")
  35. def items(self):
  36. return self.__queryDict.items()
  37. def get(self, name, default=""):
  38. return self.__queryDict.get(name, default)
  39. def getInt(self, name, default=0):
  40. try:
  41. return int(self.__queryDict.get(name, default))
  42. except (ValueError) as e:
  43. return default
  44. def getBool(self, name, default=False):
  45. return stringBool(self.__queryDict.get(name, str(default)), default)
  46. def __hash__(self):
  47. return hash(frozenset(self.__queryDict.items()))
  48. def __eq__(self, other):
  49. return (isinstance(other, self.__class__) and
  50. self.__queryDict == other.__queryDict)