changeset 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
files tincan.py
diffstat 1 files changed, 14 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/tincan.py	Mon May 13 15:18:02 2019 -0700
+++ b/tincan.py	Mon May 13 16:00:11 2019 -0700
@@ -398,15 +398,24 @@
                 raise TinCanError("{0}: bad #errors code".format(self._urlpath))
             self._app.error_handler[error] = route  # XXX
 
+    def _gettime(self, path):
+        try:
+            return os.stat(path).st_mtime
+        except FileNotFoundError:
+            return 0
+
     def _getclass(self):
         pypath = os.path.normpath(os.path.join(self._fsroot, *self._splitpath(self._python)))
+        # Give 'em a default code-behind if they don't furnish one
+        pytime = self._gettime(pypath)
+        if not pytime:
+            self._class = Page
+            return
+        # Else load the code-behind from a .py file
         pycpath = pypath + 'c'
+        pyctime = self._gettime(pycpath)
         try:
-            pyctime = os.stat(pycpath).st_mtime
-        except OSError:
-            pyctime = 0
-        try:
-            if pyctime < os.stat(pypath).st_mtime:
+            if pyctime < pytime:
                 py_compile.compile(pypath, cfile=pycpath, doraise=True)
         except py_compile.PyCompileError as e:
             raise TinCanError(str(e)) from e