comparison tincan.py @ 7:57ec65f527e9 draft

Eliminate a stat() call, allow no code-behind on pages.
author David Barts <n5jrn@me.com>
date Mon, 13 May 2019 16:00:11 -0700
parents a3823da7bb45
children 9aaa91247b14
comparison
equal deleted inserted replaced
6:a3823da7bb45 7:57ec65f527e9
396 for error in errors: 396 for error in errors:
397 if error < _ERRMIN or error > _ERRMAX: 397 if error < _ERRMIN or error > _ERRMAX:
398 raise TinCanError("{0}: bad #errors code".format(self._urlpath)) 398 raise TinCanError("{0}: bad #errors code".format(self._urlpath))
399 self._app.error_handler[error] = route # XXX 399 self._app.error_handler[error] = route # XXX
400 400
401 def _gettime(self, path):
402 try:
403 return os.stat(path).st_mtime
404 except FileNotFoundError:
405 return 0
406
401 def _getclass(self): 407 def _getclass(self):
402 pypath = os.path.normpath(os.path.join(self._fsroot, *self._splitpath(self._python))) 408 pypath = os.path.normpath(os.path.join(self._fsroot, *self._splitpath(self._python)))
409 # Give 'em a default code-behind if they don't furnish one
410 pytime = self._gettime(pypath)
411 if not pytime:
412 self._class = Page
413 return
414 # Else load the code-behind from a .py file
403 pycpath = pypath + 'c' 415 pycpath = pypath + 'c'
404 try: 416 pyctime = self._gettime(pycpath)
405 pyctime = os.stat(pycpath).st_mtime 417 try:
406 except OSError: 418 if pyctime < pytime:
407 pyctime = 0
408 try:
409 if pyctime < os.stat(pypath).st_mtime:
410 py_compile.compile(pypath, cfile=pycpath, doraise=True) 419 py_compile.compile(pypath, cfile=pycpath, doraise=True)
411 except py_compile.PyCompileError as e: 420 except py_compile.PyCompileError as e:
412 raise TinCanError(str(e)) from e 421 raise TinCanError(str(e)) from e
413 except Exception as e: 422 except Exception as e:
414 raise TinCanError("{0}: {1!s}".format(pypath, e)) from e 423 raise TinCanError("{0}: {1!s}".format(pypath, e)) from e