util.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. # -*- coding: utf-8 -*-
  2. #
  3. # cms.py - simple WSGI/Python based CMS script
  4. #
  5. # Copyright (C) 2011-2023 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. import html
  22. import datetime as datetime_mod
  23. import io
  24. import os
  25. import stat
  26. BytesIO = io.BytesIO #+cdef-public-object
  27. datetime = datetime_mod.datetime #+cdef-public-object
  28. dt_timezone = datetime_mod.timezone #+cdef-public-object
  29. __all__ = [
  30. "BytesIO",
  31. "datetime",
  32. "dt_timezone",
  33. "UPPERCASE",
  34. "LOWERCASE",
  35. "NUMBERS",
  36. "isiterable",
  37. "findNot",
  38. "findAny",
  39. "htmlEscape",
  40. "stringBool",
  41. "StrCArray",
  42. "str2carray",
  43. "carray2str",
  44. "fs",
  45. ]
  46. UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' #+cdef-public-str
  47. LOWERCASE = 'abcdefghijklmnopqrstuvwxyz' #+cdef-public-str
  48. NUMBERS = '0123456789' #+cdef-public-str
  49. # Check if an object is iterable.
  50. def isiterable(obj): #@nocy
  51. #cdef ExBool_t isiterable(object obj) except ExBool_val: #@cy
  52. try:
  53. iter(obj)
  54. return True
  55. except TypeError:
  56. pass # obj is not an iterable.
  57. except Exception:
  58. raise CMSException(500, "isiterable: Unexpected exception.")
  59. return False
  60. # Find the index in 'string' that is _not_ in 'template'.
  61. # Start search at 'idx'.
  62. # Returns -1 on failure to find.
  63. def findNot(string, template, idx): #@nocy
  64. #cdef _Bool findNot(str string, str template, int64_t idx): #@cy
  65. #@cy cdef int64_t slen
  66. if idx >= 0:
  67. slen = len(string)
  68. while idx < slen:
  69. if string[idx] not in template:
  70. return idx
  71. idx += 1
  72. return -1
  73. # Find the index in 'string' that matches _any_ character in 'template'.
  74. # Start search at 'idx'.
  75. # Returns -1 on failure to find.
  76. def findAny(string, template, idx): #@nocy
  77. #cdef _Bool findAny(str string, str template, int64_t idx): #@cy
  78. #@cy cdef int64_t slen
  79. if idx >= 0:
  80. slen = len(string)
  81. while idx < slen:
  82. if string[idx] in template:
  83. return idx
  84. idx += 1
  85. return -1
  86. def htmlEscape(string): #@nocy
  87. #cdef str htmlEscape(str string): #@cy
  88. return html.escape(string, True)
  89. def stringBool(string, default): #@nocy
  90. #cdef _Bool stringBool(str string, _Bool default): #@cy
  91. #@cy cdef str s
  92. s = string.lower()
  93. if s in ("true", "yes", "on", "1"):
  94. return True
  95. if s in ("false", "no", "off", "0"):
  96. return False
  97. try:
  98. return bool(int(s))
  99. except ValueError:
  100. return default
  101. class StrCArray(object): #@nocy
  102. __slots__ = ( "_string", ) #@nocy
  103. def str2carray(carray, string, arrayLen): #@nocy
  104. if arrayLen > 0: #@nocy
  105. carray._string = string[:arrayLen-1] #@nocy
  106. def carray2str(carray, arrayLen): #@nocy
  107. if arrayLen > 0: #@nocy
  108. return carray._string #@nocy
  109. return "" #@nocy
  110. class FSHelpers(object): #+cdef
  111. def __init__(self):
  112. self.__pathSep = os.path.sep
  113. self.__os_stat = os.stat
  114. self.__os_listdir = os.listdir
  115. self.__stat_S_ISDIR = stat.S_ISDIR
  116. def __mkpath(self, path_elements): #@nocy
  117. #@cy cdef str __mkpath(self, tuple path_elements):
  118. # Do not use os.path.join, because it discards elements, if
  119. # one element begins with a separator (= is absolute).
  120. return self.__pathSep.join(path_elements)
  121. # Create a path string from path element strings.
  122. def mkpath(self, *path_elements):
  123. return self.__mkpath(path_elements)
  124. def __exists(self, path_elements): #@nocy
  125. #@cy cdef _Bool __exists(self, tuple path_elements):
  126. #@cy cdef str path
  127. try:
  128. path = self.__mkpath(path_elements)
  129. self.__os_stat(path.encode("UTF-8", "strict"))
  130. except OSError:
  131. return False
  132. except UnicodeError:
  133. raise CMSException(500, "Unicode decode error")
  134. return True
  135. def exists(self, *path_elements):
  136. return self.__exists(path_elements)
  137. def __exists_nonempty(self, path_elements): #@nocy
  138. #@cy cdef _Bool __exists_nonempty(self, tuple path_elements):
  139. if self.__exists(path_elements):
  140. return bool(self.__read(path_elements).strip())
  141. return False
  142. def exists_nonempty(self, *path_elements):
  143. return self.__exists_nonempty(path_elements)
  144. def __read(self, path_elements): #@nocy
  145. #@cy cdef str __read(self, tuple path_elements):
  146. #@cy cdef str path
  147. #@cy cdef bytes data
  148. try:
  149. path = self.__mkpath(path_elements)
  150. with open(path.encode("UTF-8", "strict"), "rb") as fd:
  151. data = fd.read()
  152. return data.decode("UTF-8", "strict")
  153. except IOError:
  154. return ""
  155. except UnicodeError:
  156. raise CMSException(500, "Unicode decode error")
  157. def read(self, *path_elements):
  158. return self.__read(path_elements)
  159. def __read_int(self, path_elements): #@nocy
  160. #@cy cdef object __read_int(self, tuple path_elements):
  161. #@cy cdef str data
  162. data = self.__read(path_elements)
  163. try:
  164. return int(data.strip())
  165. except ValueError:
  166. return None
  167. def read_int(self, *path_elements):
  168. return self.__read_int(path_elements)
  169. def __mtime(self, path_elements): #@nocy
  170. #@cy cdef object __mtime(self, tuple path_elements):
  171. try:
  172. path = self.__mkpath(path_elements).encode("UTF-8", "strict")
  173. return datetime.utcfromtimestamp(self.__os_stat(path).st_mtime)
  174. except OSError:
  175. raise CMSException(404)
  176. except UnicodeError:
  177. raise CMSException(500, "Unicode decode error")
  178. def mtime(self, *path_elements):
  179. return self.__mtime(path_elements)
  180. def __mtime_nofail(self, path_elements): #@nocy
  181. #@cy cdef object __mtime_nofail(self, tuple path_elements):
  182. try:
  183. return self.__mtime(path_elements)
  184. except CMSException:
  185. return datetime.now(dt_timezone.utc)
  186. def mtime_nofail(self, *path_elements):
  187. return self.__mtime_nofail(path_elements)
  188. def __subdirList(self, path_elements): #@nocy
  189. #@cy cdef list __subdirList(self, tuple path_elements):
  190. #@cy cdef str path
  191. #@cy cdef bytes bdentry
  192. #@cy cdef bytes bp
  193. #@cy cdef str dentry
  194. #@cy cdef list dirListing
  195. #@cy cdef list ret
  196. try:
  197. ret = []
  198. path = self.__mkpath(path_elements)
  199. dirListing = self.__os_listdir(path.encode("UTF-8", "strict"))
  200. S_ISDIR = self.__stat_S_ISDIR
  201. stat = self.__os_stat
  202. for bdentry in dirListing:
  203. dentry = bdentry.decode("UTF-8", "strict")
  204. if dentry.startswith("."):
  205. continue # Omit ".", ".." and hidden entries
  206. if dentry.startswith("__"):
  207. continue # Omit system folders/files.
  208. try:
  209. bp = self.__mkpath((path, dentry)).encode("UTF-8", "strict")
  210. if not S_ISDIR(stat(bp).st_mode):
  211. continue
  212. except OSError:
  213. continue
  214. except UnicodeError:
  215. raise CMSException(500, "Unicode decode error")
  216. ret.append(dentry)
  217. return ret
  218. except OSError:
  219. return []
  220. except UnicodeError:
  221. raise CMSException(500, "Unicode decode error")
  222. def subdirList(self, *path_elements):
  223. return self.__subdirList(path_elements)
  224. fs = FSHelpers() #+cdef-FSHelpers