util.py 7.0 KB

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