annotate tincan.py @ 12:496d43d551d2 draft

More redirecting fixes and improved error reportage.
author David Barts <n5jrn@me.com>
date Wed, 15 May 2019 00:16:06 -0700
parents 8037bad7d5a8
children 6de828de4409
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
1 #!/usr/bin/env python3
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
3 # As with Bottle, it's all in one big, ugly file. For now.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
4
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
5 # I m p o r t s
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
6
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
7 import os, sys
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
8 import ast
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
9 import binascii
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
10 from base64 import b16encode, b16decode
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
11 import functools
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
12 import importlib
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
13 from inspect import isclass
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
14 import io
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
15 import py_compile
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
16 from stat import S_ISDIR, S_ISREG
5
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
17 from string import whitespace
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
18
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
19 import bottle
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
20
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
21 # E x c e p t i o n s
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
22
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
23 class TinCanException(Exception):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
24 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
25 The parent class of all exceptions we raise.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
26 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
27 pass
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
28
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
29 class TemplateHeaderError(TinCanException):
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
30 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
31 Raised upon encountering a syntax error in the template headers.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
32 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
33 def __init__(self, message, line):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
34 super().__init__(message, line)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
35 self.message = message
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
36 self.line = line
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
37
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
38 def __str__(self):
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
39 return "line {0}: {1}".format(self.line, self.message)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
40
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
41 class ForwardException(TinCanException):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
42 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
43 Raised to effect the flow control needed to do a forward (server-side
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
44 redirect). It is ugly to do this, but other Python frameworks do and
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
45 there seems to be no good alternative.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
46 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
47 def __init__(self, target):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
48 self.target = target
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
49
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
50 class TinCanError(TinCanException):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
51 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
52 General-purpose exception thrown by TinCan when things go wrong, often
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
53 when attempting to launch webapps.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
54 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
55 pass
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
56
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
57 # T e m p l a t e s
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
58 #
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
59 # Template (.pspx) files. These are standard templates for a supported
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
60 # template engine, but with an optional set of header lines that begin
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
61 # with '#'.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
62
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
63 class TemplateFile(object):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
64 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
65 Parse a template file into a header part and the body part. The header
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
66 is always a leading set of lines, each starting with '#', that is of the
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
67 same format regardless of the template body. The template body varies
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
68 depending on the selected templating engine. The body part has
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
69 each header line replaced by a blank line. This preserves the overall
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
70 line numbering when processing the body. The added newlines are normally
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
71 stripped out before the rendered page is sent back to the client.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
72 """
5
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
73 _END = "#end"
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
74 _LEND = len(_END)
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
75 _WS = set(whitespace)
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
76
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
77 def __init__(self, raw, encoding='utf-8'):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
78 if isinstance(raw, io.TextIOBase):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
79 self._do_init(raw)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
80 elif isinstance(raw, str):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
81 with open(raw, "r", encoding=encoding) as fp:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
82 self._do_init(fp)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
83 else:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
84 raise TypeError("Expecting a string or Text I/O object.")
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
85
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
86 def _do_init(self, fp):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
87 self._hbuf = []
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
88 self._bbuf = []
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
89 self._state = self._header
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
90 while True:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
91 line = fp.readline()
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
92 if line == '':
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
93 break
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
94 self._state(line)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
95 self.header = ''.join(self._hbuf)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
96 self.body = ''.join(self._bbuf)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
97
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
98 def _header(self, line):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
99 if not line.startswith('#'):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
100 self._state = self._body
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
101 self._state(line)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
102 return
5
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
103 if line.startswith(self._END) and (len(line) == self._LEND or line[self._LEND] in self._WS):
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
104 self._state = self._body
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
105 self._hbuf.append(line)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
106 self._bbuf.append("\n")
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
107
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
108 def _body(self, line):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
109 self._bbuf.append(line)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
110
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
111 class TemplateHeader(object):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
112 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
113 Parses and represents a set of header lines.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
114 """
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
115 _NAMES = [ "errors", "forward", "methods", "python", "template" ]
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
116 _FNAMES = [ "hidden" ]
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
117
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
118 def __init__(self, string):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
119 # Initialize our state
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
120 for i in self._NAMES:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
121 setattr(self, i, None)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
122 for i in self._FNAMES:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
123 setattr(self, i, False)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
124 # Parse the string
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
125 count = 0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
126 nameset = set(self._NAMES + self._FNAMES)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
127 seen = set()
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
128 lines = string.split("\n")
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
129 if lines and lines[-1] == "":
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
130 del lines[-1]
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
131 for line in lines:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
132 # Get line
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
133 count += 1
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
134 if not line.startswith("#"):
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
135 raise TemplateHeaderError("Does not start with '#'.", count)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
136 try:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
137 rna, rpa = line.split(maxsplit=1)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
138 except ValueError:
5
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
139 rna = line.rstrip()
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
140 rpa = None
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
141 # Get name, ignoring remarks.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
142 name = rna[1:]
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
143 if name == "rem":
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
144 continue
5
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
145 if name == "end":
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
146 break
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
147 if name not in nameset:
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
148 raise TemplateHeaderError("Invalid directive: {0!r}".format(rna), count)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
149 if name in seen:
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
150 raise TemplateHeaderError("Duplicate {0!r} directive.".format(rna), count)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
151 seen.add(name)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
152 # Flags
5
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
153 if name in self._FNAMES:
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
154 setattr(self, name, True)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
155 continue
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
156 # Get parameter
5
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
157 if rpa is None:
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
158 raise TemplateHeaderError("Missing parameter.", count)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
159 param = rpa.strip()
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
160 for i in [ "'", '"']:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
161 if param.startswith(i) and param.endswith(i):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
162 param = ast.literal_eval(param)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
163 break
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
164 # Update this object
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
165 setattr(self, name, param)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
166
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
167 # C h a m e l e o n
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
168 #
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
169 # Support for Chameleon templates (the kind TinCan uses by default).
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
170
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
171 class ChameleonTemplate(bottle.BaseTemplate):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
172 def prepare(self, **options):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
173 from chameleon import PageTemplate, PageTemplateFile
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
174 if self.source:
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
175 self.tpl = PageTemplate(self.source, encoding=self.encoding,
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
176 **options)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
177 else:
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
178 self.tpl = PageTemplateFile(self.filename, encoding=self.encoding,
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
179 search_path=self.lookup, **options)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
180
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
181 def render(self, *args, **kwargs):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
182 for dictarg in args:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
183 kwargs.update(dictarg)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
184 _defaults = self.defaults.copy()
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
185 _defaults.update(kwargs)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
186 return self.tpl.render(**_defaults)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
187
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
188 chameleon_template = functools.partial(bottle.template, template_adapter=ChameleonTemplate)
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
189 chameleon_view = functools.partial(bottle.view, template_adapter=ChameleonTemplate)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
190
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
191 # U t i l i t i e s
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
192
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
193 def _normpath(base, unsplit):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
194 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
195 Split, normalize and ensure a possibly relative path is absolute. First
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
196 argument is a list of directory names, defining a base. Second
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
197 argument is a string, which may either be relative to that base, or
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
198 absolute. Only '/' is supported as a separator.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
199 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
200 scratch = unsplit.strip('/').split('/')
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
201 if not unsplit.startswith('/'):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
202 scratch = base + scratch
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
203 ret = []
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
204 for i in scratch:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
205 if i == '.':
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
206 continue
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
207 if i == '..':
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
208 ret.pop() # may raise IndexError
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
209 continue
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
210 ret.append(i)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
211 return ret
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
212
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
213 def _mangle(string):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
214 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
215 Turn a possibly troublesome identifier into a mangled one.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
216 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
217 first = True
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
218 ret = []
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
219 for ch in string:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
220 if ch == '_' or not (ch if first else "x" + ch).isidentifier():
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
221 ret.append('_')
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
222 ret.append(b16encode(ch.encode("utf-8")).decode("us-ascii"))
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
223 else:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
224 ret.append(ch)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
225 first = False
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
226 return ''.join(ret)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
227
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
228 # The TinCan class. Simply a Bottle webapp that contains a forward method, so
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
229 # the code-behind can call request.app.forward().
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
230
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
231 class TinCan(bottle.Bottle):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
232 def forward(self, target):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
233 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
234 Forward this request to the specified target route.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
235 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
236 source = bottle.request.environ['PATH_INFO']
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
237 base = source.strip('/').split('/')[:-1]
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
238 if bottle.request.environ.get(_FTYPE, False):
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
239 raise TinCanError("{0}: forward from error page".format(source))
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
240 try:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
241 exc = ForwardException('/' + '/'.join(_normpath(base, target)))
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
242 except IndexError as e:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
243 raise TinCanError("{0}: invalid forward to {1!r}".format(source, target)) from e
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
244 raise exc
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
245
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
246 # C o d e B e h i n d
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
247 #
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
248 # Represents the code-behind of one of our pages. This gets subclassed, of
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
249 # course.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
250
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
251 class BasePage(object):
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
252 """
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
253 The parent class of both error and normal pages' code-behind.
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
254 """
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
255 def handle(self):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
256 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
257 This is the entry point for the code-behind logic. It is intended
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
258 to be overridden.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
259 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
260 pass
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
261
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
262 def export(self):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
263 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
264 Export template variables. The default behavior is to export all
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
265 non-hidden non-callables that don't start with an underscore.
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
266 This method can be overridden if a different behavior is
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
267 desired. It should always return a dict or dict-like object.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
268 """
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
269 ret = { 'page': self }
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
270 for name in dir(self):
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
271 if name in self._HIDDEN or name.startswith('_'):
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
272 continue
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
273 value = getattr(self, name)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
274 if callable(value):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
275 continue
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
276 ret[name] = value
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
277 return ret
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
278
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
279 class Page(BasePage):
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
280 """
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
281 The code-behind for a normal page.
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
282 """
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
283 # Non-private things we refuse to export anyhow.
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
284 _HIDDEN = set([ "request", "response" ])
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
285
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
286 def __init__(self, req, resp):
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
287 """
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
288 Constructor. This is a lightweight operation.
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
289 """
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
290 self.request = req # app context is request.app in Bottle
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
291 self.response = resp
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
292
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
293 class ErrorPage(BasePage):
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
294 """
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
295 The code-behind for an error page.
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
296 """
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
297 _HIDDEN = set()
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
298
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
299 def __init__(self, req, err):
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
300 """
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
301 Constructor. This is a lightweight operation.
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
302 """
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
303 self.request = req
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
304 self.error = err
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
305
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
306 # R o u t e s
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
307 #
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
308 # Represents a route in TinCan. Our launcher creates these on-the-fly based
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
309 # on the files it finds.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
310
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
311 _ERRMIN = 400
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
312 _ERRMAX = 599
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
313 _PEXTEN = ".py"
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
314 _TEXTEN = ".pspx"
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
315 _FLOOP = "tincan.forwards"
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
316 _FORIG = "tincan.origin"
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
317 _FTYPE = "tincan.iserror"
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
318
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
319 class _TinCanErrorRoute(object):
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
320 """
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
321 A route to an error page. These don't get routes created for them,
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
322 and are only reached if an error routes them there. Unless you create
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
323 custom code-behind, only two variables are available to your template:
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
324 request (bottle.Request) and error (bottle.HTTPError).
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
325 """
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
326 def __init__(self, template, klass):
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
327 self._template = template
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
328 self._template.prepare()
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
329 self._class = klass
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
330
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
331 def __call__(self, e):
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
332 bottle.request.environ[_FTYPE] = True
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
333 obj = self._class(bottle.request, e)
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
334 obj.handle()
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
335 return self._template.render(obj.export()).lstrip('\n')
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
336
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
337 class _TinCanRoute(object):
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
338 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
339 A route created by the TinCan launcher.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
340 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
341 def __init__(self, launcher, name, subdir):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
342 self._fsroot = launcher.fsroot
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
343 self._urlroot = launcher.urlroot
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
344 self._name = name
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
345 self._python = name + _PEXTEN
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
346 self._fspath = os.path.join(launcher.fsroot, *subdir, name + _TEXTEN)
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
347 self._urlpath = self._urljoin(launcher.urlroot, *subdir, name + _TEXTEN)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
348 self._origin = self._urlpath
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
349 self._subdir = subdir
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
350 self._seen = set()
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
351 self._tclass = launcher.tclass
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
352 self._app = launcher.app
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
353
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
354 def launch(self):
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
355 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
356 Launch a single page.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
357 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
358 # Build master and header objects, process #forward directives
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
359 oheader = None
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
360 while True:
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
361 try:
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
362 self._template = TemplateFile(self._fspath)
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
363 except IOError as e:
12
496d43d551d2 More redirecting fixes and improved error reportage.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
364 if oheader is not None:
496d43d551d2 More redirecting fixes and improved error reportage.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
365 note = "{0}: invalid #forward: ".format(self._origin)
496d43d551d2 More redirecting fixes and improved error reportage.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
366 else:
496d43d551d2 More redirecting fixes and improved error reportage.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
367 note = ""
496d43d551d2 More redirecting fixes and improved error reportage.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
368 raise TinCanError("{0}{1!s}".format(note, e)) from e
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
369 try:
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
370 self._header = TemplateHeader(self._template.header)
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
371 except TemplateHeaderError as e:
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
372 raise TinCanError("{0}: {1!s}".format(self._fspath, e)) from e
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
373 if oheader is None:
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
374 oheader = self._header # save original header
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
375 elif (oheader.errors is None) != (self._header.errors is None):
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
376 raise TinCanError("{0}: invalid #forward".format(self._origin))
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
377 if self._header.forward is None:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
378 break
12
496d43d551d2 More redirecting fixes and improved error reportage.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
379 # print("forwarding from:", self._urlpath) # debug
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
380 self._redirect()
12
496d43d551d2 More redirecting fixes and improved error reportage.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
381 # print("forwarded to:", self._urlpath) # debug
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
382 # If this is a #hidden page, we ignore it for now, since hidden pages
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
383 # don't get routes made for them.
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
384 if oheader.hidden and not oheader.errors:
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
385 return
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
386 # Get the code-behind #python
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
387 if self._header.python is not None:
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
388 if not self._header.python.endswith(_PEXTEN):
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
389 raise TinCanError("{0}: #python files must end in {1}".format(self._urlpath, _PEXTEN))
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
390 self._python = self._header.python
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
391 # Obtain a class object by importing and introspecting a module.
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
392 self._getclass()
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
393 # Build body object (#template)
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
394 if self._header.template is not None:
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
395 if not self._header.template.endswith(_TEXTEN):
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
396 raise TinCanError("{0}: #template files must end in {1}".format(self._urlpath, _TEXTEN))
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
397 tpath = os.path.normpath(os.path.join(self._fsroot, *self._splitpath(self._header.template)))
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
398 tfile = TemplateFile(tpath)
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
399 self._body = self._tclass(source=tfile.body)
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
400 else:
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
401 self._body = self._tclass(source=self._template.body)
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
402 self._body.prepare()
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
403 # If this is an #errors page, register it as such.
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
404 if oheader.errors is not None:
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
405 self._mkerror(oheader.errors)
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
406 return # this implies #hidden
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
407 # Get #methods for this route
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
408 if self._header.methods is None:
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
409 methods = [ 'GET' ]
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
410 else:
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
411 methods = [ i.upper() for i in self._header.methods.split() ]
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
412 if not methods:
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
413 raise TinCanError("{0}: no #methods specified".format(self._urlpath))
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
414 # Register this thing with Bottle
6
a3823da7bb45 Minor tweaks.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
415 print("adding route:", self._origin, '('+','.join(methods)+')') # debug
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
416 self._app.route(self._origin, methods, self)
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
417
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
418 def _splitpath(self, unsplit):
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
419 return _normpath(self._subdir, unsplit)
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
420
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
421 def _mkerror(self, rerrors):
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
422 try:
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
423 errors = [ int(i) for i in rerrors.split() ]
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
424 except ValueError as e:
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
425 raise TinCanError("{0}: bad #errors line".format(self._urlpath)) from e
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
426 if not errors:
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
427 errors = range(_ERRMIN, _ERRMAX+1)
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
428 route = _TinCanErrorRoute(self._tclass(source=self._template.body), self._class)
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
429 for error in errors:
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
430 if error < _ERRMIN or error > _ERRMAX:
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
431 raise TinCanError("{0}: bad #errors code".format(self._urlpath))
5
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
432 self._app.error_handler[error] = route # XXX
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
433
7
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
434 def _gettime(self, path):
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
435 try:
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
436 return os.stat(path).st_mtime
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
437 except FileNotFoundError:
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
438 return 0
8
9aaa91247b14 Fix minor buglet.
David Barts <n5jrn@me.com>
parents: 7
diff changeset
439 except OSError as e:
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
440 raise TinCanError(str(e)) from e
7
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
441
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
442 def _getclass(self):
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
443 pypath = os.path.normpath(os.path.join(self._fsroot, *self._splitpath(self._python)))
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
444 klass = ErrorPage if self._header.errors else Page
7
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
445 # Give 'em a default code-behind if they don't furnish one
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
446 pytime = self._gettime(pypath)
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
447 if not pytime:
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
448 self._class = klass
7
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
449 return
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
450 # Else load the code-behind from a .py file
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
451 pycpath = pypath + 'c'
7
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
452 pyctime = self._gettime(pycpath)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
453 try:
7
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
454 if pyctime < pytime:
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
455 py_compile.compile(pypath, cfile=pycpath, doraise=True)
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
456 except py_compile.PyCompileError as e:
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
457 raise TinCanError(str(e)) from e
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
458 except Exception as e:
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
459 raise TinCanError("{0}: {1!s}".format(pypath, e)) from e
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
460 try:
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
461 spec = importlib.util.spec_from_file_location(_mangle(self._name), pycpath)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
462 mod = importlib.util.module_from_spec(spec)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
463 spec.loader.exec_module(mod)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
464 except Exception as e:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
465 raise TinCanError("{0}: error importing".format(pycpath)) from e
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
466 self._class = None
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
467 for i in dir(mod):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
468 v = getattr(mod, i)
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
469 if isclass(v) and issubclass(v, klass):
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
470 if self._class is not None:
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
471 raise TinCanError("{0}: contains multiple {1} classes".format(pypath, klass.__name__))
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
472 self._class = v
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
473 if self._class is None:
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
474 raise TinCanError("{0}: contains no {1} classes".format(pypath, klass.__name__))
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
475
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
476 def _redirect(self):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
477 try:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
478 rlist = self._splitpath(self._header.forward)
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
479 forw = '/' + '/'.join(rlist)
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
480 if forw in self._seen:
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
481 raise TinCanError("{0}: #forward loop".format(self._origin))
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
482 self._seen.add(forw)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
483 rname = rlist.pop()
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
484 except IndexError as e:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
485 raise TinCanError("{0}: invalid #forward".format(self._urlpath)) from e
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
486 name, ext = os.path.splitext(rname)
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
487 if ext != _TEXTEN:
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
488 raise TinCanError("{0}: invalid #forward".format(self._urlpath))
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
489 self._subdir = rlist
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
490 self._python = name + _PEXTEN
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
491 self._fspath = os.path.join(self._fsroot, *self._subdir, rname)
12
496d43d551d2 More redirecting fixes and improved error reportage.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
492 self._urlpath = '/' + self._urljoin(*self._subdir, rname)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
493
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
494 def _urljoin(self, *args):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
495 args = list(args)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
496 if args[0] == '/':
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
497 args[0] = ''
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
498 return '/'.join(args)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
499
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
500 def __call__(self):
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
501 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
502 This gets called by the framework AFTER the page is launched.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
503 """
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
504 target = None
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
505 obj = self._class(bottle.request, bottle.response)
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
506 try:
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
507 obj.handle()
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
508 return self._body.render(obj.export()).lstrip('\n')
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
509 except ForwardException as fwd:
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
510 target = fwd.target
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
511 if target is None:
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
512 raise TinCanError("{0}: unexpected null target".format(self._urlpath))
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
513 # We get here if we are doing a server-side programmatic
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
514 # forward.
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
515 environ = bottle.request.environ
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
516 if _FORIG not in environ:
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
517 environ[_FORIG] = self._urlpath
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
518 if _FLOOP not in environ:
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
519 environ[_FLOOP] = set([self._urlpath])
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
520 elif target in environ[_FLOOP]:
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
521 raise TinCanError("{0}: forward loop detected".format(environ[_FORIG]))
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
522 environ[_FLOOP].add(target)
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
523 environ['bottle.raw_path'] = target
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
524 environ['PATH_INFO'] = urllib.parse.quote(target)
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
525 route, args = self._app.router.match(environ)
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
526 environ['route.handle'] = environ['bottle.route'] = route
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
527 environ['route.url_args'] = args
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
528 return route.call(**args)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
529
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
530 def _mkdict(self, obj):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
531 ret = {}
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
532 for name in dir(obj):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
533 if name.startswith('_'):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
534 continue
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
535 value = getattr(obj, name)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
536 if not callable(value):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
537 ret[name] = value
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
538 return ret
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
539
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
540 # L a u n c h e r
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
541
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
542 _WINF = "WEB-INF"
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
543 _BANNED = set([_WINF])
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
544
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
545 class _Launcher(object):
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
546 """
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
547 Helper class for launching webapps.
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
548 """
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
549 def __init__(self, fsroot, urlroot, tclass, logger):
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
550 """
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
551 Lightweight constructor. The real action happens in .launch() below.
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
552 """
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
553 self.fsroot = fsroot
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
554 self.urlroot = urlroot
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
555 self.tclass = tclass
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
556 self.logger = logger
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
557 self.app = None
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
558 self.errors = 0
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
559 self.debug = False
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
560
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
561 def launch(self):
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
562 """
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
563 Does the actual work of launching something. XXX - modifies sys.path
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
564 and never un-modifies it.
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
565 """
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
566 # Sanity checks
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
567 if not self.urlroot.startswith("/"):
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
568 raise TinCanError("urlroot must be absolute")
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
569 if not os.path.isdir(self.fsroot):
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
570 raise TinCanError("no such directory: {0!r}".format(self.fsroot))
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
571 # Make WEB-INF, if needed
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
572 winf = os.path.join(self.fsroot, _WINF)
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
573 lib = os.path.join(winf, "lib")
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
574 for i in [ winf, lib ]:
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
575 if not os.path.isdir(i):
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
576 os.mkdir(i)
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
577 # Add our private lib directory to sys.path
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
578 sys.path.insert(1, os.path.abspath(lib))
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
579 # Do what we gotta do
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
580 self.app = TinCan()
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
581 self._launch([])
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
582 return self
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
583
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
584 def _launch(self, subdir):
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
585 for entry in os.listdir(os.path.join(self.fsroot, *subdir)):
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
586 if not subdir and entry in _BANNED:
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
587 continue
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
588 etype = os.stat(os.path.join(self.fsroot, *subdir, entry)).st_mode
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
589 if S_ISREG(etype):
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
590 ename, eext = os.path.splitext(entry)
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
591 if eext != _TEXTEN:
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
592 continue # only look at interesting files
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
593 route = _TinCanRoute(self, ename, subdir)
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
594 try:
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
595 route.launch()
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
596 except TinCanError as e:
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
597 self.logger(str(e))
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
598 if self.debug:
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
599 while e.__cause__ != None:
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
600 e = e.__cause__
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
601 self.logger("\t{0}: {1!s}".format(e.__class__.__name__, e))
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
602 self.errors += 1
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
603 elif S_ISDIR(etype):
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
604 self._launch(subdir + [entry])
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
605
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
606 def _logger(message):
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
607 sys.stderr.write(message)
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
608 sys.stderr.write('\n')
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
609
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
610 def launch(fsroot=None, urlroot='/', tclass=ChameleonTemplate, logger=_logger):
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
611 """
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
612 Launch and return a TinCan webapp. Does not run the app; it is the
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
613 caller's responsibility to call app.run()
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
614 """
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
615 if fsroot is None:
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
616 fsroot = os.getcwd()
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
617 launcher = _Launcher(fsroot, urlroot, tclass, logger)
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
618 # launcher.debug = True
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
619 launcher.launch()
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
620 return launcher.app, launcher.errors
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
621
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
622 # XXX - We cannot implement a command-line launcher here; see the
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
623 # launcher script for why.