annotate tincan.py @ 24:34d3cfcd37ef draft header-includes

First batch of work on getting a #include header. Unfinished.
author David Barts <n5jrn@me.com>
date Wed, 22 May 2019 07:47:16 -0700
parents e8b6ee7e5b6b
children e93e5e746cc5
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
17
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
18 import traceback
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
19 import urllib
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
20
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
21 import bottle
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
22
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
23 # E x c e p t i o n s
0
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 class TinCanException(Exception):
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 The parent class of all exceptions we raise.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
28 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
29 pass
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
30
24
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
31 class BaseSyntaxError(TinCanException):
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
32
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
33
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
34 class TemplateHeaderError(TinCanException):
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
35 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
36 Raised upon encountering a syntax error in the template headers.
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 __init__(self, message, line):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
39 super().__init__(message, line)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
40 self.message = message
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
41 self.line = line
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 def __str__(self):
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
44 return "line {0}: {1}".format(self.line, self.message)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
45
24
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
46 class IncludeError(TinCanException):
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
47 """
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
48 Raised when we run into problems #include'ing something, usually
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
49 because it doesn't exist.
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
50 """
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
51 def __init__(self, message, source):
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
52 super().__init__(message, source)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
53 self.message = message
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
54 self.source = source
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
55
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
56 def __str__(self):
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
57 return "{0}: {1}".format(self.source, self.message)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
58
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
59 class ForwardException(TinCanException):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
60 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
61 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
62 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
63 there seems to be no good alternative.
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 def __init__(self, target):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
66 self.target = target
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
67
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
68 class TinCanError(TinCanException):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
69 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
70 General-purpose exception thrown by TinCan when things go wrong, often
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
71 when attempting to launch webapps.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
72 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
73 pass
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
74
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
75 # T e m p l a t e s
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
76 #
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
77 # Template (.pspx) files. These are standard templates for a supported
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
78 # 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
79 # with '#'.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
80
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
81 class TemplateFile(object):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
82 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
83 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
84 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
85 same format regardless of the template body. The template body varies
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
86 depending on the selected templating engine. The body part has
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
87 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
88 line numbering when processing the body. The added newlines are normally
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
89 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
90 """
5
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
91 _END = "#end"
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
92 _LEND = len(_END)
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
93 _WS = set(whitespace)
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
94
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
95 def __init__(self, raw, encoding='utf-8'):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
96 if isinstance(raw, io.TextIOBase):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
97 self._do_init(raw)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
98 elif isinstance(raw, str):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
99 with open(raw, "r", encoding=encoding) as fp:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
100 self._do_init(fp)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
101 else:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
102 raise TypeError("Expecting a string or Text I/O object.")
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
103
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
104 def _do_init(self, fp):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
105 self._hbuf = []
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
106 self._bbuf = []
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
107 self._state = self._header
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
108 while True:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
109 line = fp.readline()
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
110 if line == '':
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
111 break
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
112 self._state(line)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
113 self.header = ''.join(self._hbuf)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
114 self.body = ''.join(self._bbuf)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
115
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
116 def _header(self, line):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
117 if not line.startswith('#'):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
118 self._state = self._body
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
119 self._state(line)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
120 return
5
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
121 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
122 self._state = self._body
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
123 self._hbuf.append(line)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
124 self._bbuf.append("\n")
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
125
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
126 def _body(self, line):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
127 self._bbuf.append(line)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
128
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
129 class TemplateHeader(object):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
130 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
131 Parses and represents a set of header lines.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
132 """
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
133 _NAMES = [ "errors", "forward", "methods", "python", "template" ]
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
134 _FNAMES = [ "hidden" ]
24
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
135 _ANAMES = [ "include" ]
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
136
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
137 def __init__(self, string):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
138 # Initialize our state
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
139 for i in self._NAMES:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
140 setattr(self, i, None)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
141 for i in self._FNAMES:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
142 setattr(self, i, False)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
143 # Parse the string
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
144 count = 0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
145 nameset = set(self._NAMES + self._FNAMES)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
146 seen = set()
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
147 lines = string.split("\n")
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
148 if lines and lines[-1] == "":
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
149 del lines[-1]
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
150 for line in lines:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
151 # Get line
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
152 count += 1
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
153 if not line.startswith("#"):
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
154 raise TemplateHeaderError("Does not start with '#'.", count)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
155 try:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
156 rna, rpa = line.split(maxsplit=1)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
157 except ValueError:
5
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
158 rna = line.rstrip()
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
159 rpa = None
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
160 # Get name, ignoring remarks.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
161 name = rna[1:]
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
162 if name == "rem":
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
163 continue
5
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
164 if name == "end":
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
165 break
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
166 if name not in nameset:
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
167 raise TemplateHeaderError("Invalid directive: {0!r}".format(rna), count)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
168 if name in seen:
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
169 raise TemplateHeaderError("Duplicate {0!r} directive.".format(rna), count)
24
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
170 if name not in self._ANAMES:
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
171 seen.add(name)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
172 # Flags
5
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
173 if name in self._FNAMES:
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
174 setattr(self, name, True)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
175 continue
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
176 # Get parameter
5
31bb8400e6e3 Add #end header, fix #errors.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
177 if rpa is None:
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
178 raise TemplateHeaderError("Missing parameter.", count)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
179 param = rpa.strip()
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
180 for i in [ "'", '"']:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
181 if param.startswith(i) and param.endswith(i):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
182 param = ast.literal_eval(param)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
183 break
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
184 # Update this object
24
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
185 if name in self._ANAMES:
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
186 getattr(self, name).append(param)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
187 else:
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
188 setattr(self, name, param)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
189
23
e8b6ee7e5b6b Well, *that* attempt at includes didn't work. Revert.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
190 # C h a m e l e o n
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
191 #
23
e8b6ee7e5b6b Well, *that* attempt at includes didn't work. Revert.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
192 # Support for Chameleon templates (the kind TinCan uses by default).
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
193
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
194 class ChameleonTemplate(bottle.BaseTemplate):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
195 def prepare(self, **options):
23
e8b6ee7e5b6b Well, *that* attempt at includes didn't work. Revert.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
196 from chameleon import PageTemplate, PageTemplateFile
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
197 if self.source:
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
198 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
199 **options)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
200 else:
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
201 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
202 search_path=self.lookup, **options)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
203
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
204 def render(self, *args, **kwargs):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
205 for dictarg in args:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
206 kwargs.update(dictarg)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
207 _defaults = self.defaults.copy()
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
208 _defaults.update(kwargs)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
209 return self.tpl.render(**_defaults)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
210
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
211 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
212 chameleon_view = functools.partial(bottle.view, template_adapter=ChameleonTemplate)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
213
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
214 # U t i l i t i e s
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
215
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
216 def _normpath(base, unsplit):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
217 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
218 Split, normalize and ensure a possibly relative path is absolute. First
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
219 argument is a list of directory names, defining a base. Second
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
220 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
221 absolute. Only '/' is supported as a separator.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
222 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
223 scratch = unsplit.strip('/').split('/')
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
224 if not unsplit.startswith('/'):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
225 scratch = base + scratch
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
226 ret = []
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
227 for i in scratch:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
228 if i == '.':
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
229 continue
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
230 if i == '..':
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
231 ret.pop() # may raise IndexError
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
232 continue
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
233 ret.append(i)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
234 return ret
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 def _mangle(string):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
237 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
238 Turn a possibly troublesome identifier into a mangled one.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
239 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
240 first = True
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
241 ret = []
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
242 for ch in string:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
243 if ch == '_' or not (ch if first else "x" + ch).isidentifier():
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
244 ret.append('_')
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
245 ret.append(b16encode(ch.encode("utf-8")).decode("us-ascii"))
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
246 else:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
247 ret.append(ch)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
248 first = False
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
249 return ''.join(ret)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
250
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
251 # 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
252 # the code-behind can call request.app.forward().
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
253
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
254 class TinCan(bottle.Bottle):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
255 def forward(self, target):
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 Forward this request to the specified target route.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
258 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
259 source = bottle.request.environ['PATH_INFO']
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
260 base = source.strip('/').split('/')[:-1]
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
261 if bottle.request.environ.get(_FTYPE, False):
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
262 raise TinCanError("{0}: forward from error page".format(source))
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
263 try:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
264 exc = ForwardException('/' + '/'.join(_normpath(base, target)))
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
265 except IndexError as e:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
266 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
267 raise exc
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
268
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
269 # C o d e B e h i n d
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
270 #
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
271 # 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
272 # course.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
273
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
274 class BasePage(object):
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
275 """
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
276 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
277 """
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
278 def handle(self):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
279 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
280 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
281 to be overridden.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
282 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
283 pass
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
284
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
285 def export(self):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
286 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
287 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
288 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
289 This method can be overridden if a different behavior is
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
290 desired. It should always return a dict or dict-like object.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
291 """
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
292 ret = { 'page': self }
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
293 for name in dir(self):
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
294 if name in self._HIDDEN or name.startswith('_'):
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
295 continue
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
296 value = getattr(self, name)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
297 if callable(value):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
298 continue
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
299 ret[name] = value
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
300 return ret
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
301
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
302 class Page(BasePage):
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
303 """
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
304 The code-behind for a normal page.
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
305 """
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
306 # 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
307 _HIDDEN = set([ "request", "response" ])
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
308
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
309 def __init__(self, req, resp):
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
310 """
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
311 Constructor. This is a lightweight operation.
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
312 """
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
313 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
314 self.response = resp
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
315
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
316 class ErrorPage(BasePage):
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
317 """
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
318 The code-behind for an error page.
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
319 """
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
320 _HIDDEN = set()
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
321
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
322 def __init__(self, req, err):
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
323 """
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
324 Constructor. This is a lightweight operation.
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
325 """
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
326 self.request = req
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
327 self.error = err
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
328
24
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
329 # I n c l u s i o n
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
330 #
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
331 # This is where the #include directives get processed
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
332
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
333 _IEXTEN = ".pt"
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
334
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
335 class _Includer(object):
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
336 def __init__(self, base, subdir, name, included=None, encoding="utf-8"):
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
337 self.base = base
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
338 self.subdir = subdir
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
339 self.name = name
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
340 self.included = set() if included is None else included
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
341 self.encoding = encoding
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
342 self._buf = []
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
343 self._tlib = os.path.join(base, _WINF, "tlib")
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
344
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
345 def render(self, includes, body):
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
346 for i in includes:
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
347 if i.startswith('<') and i.endswith('>'):
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
348 self._buf.append(self._render1(self._tlib, i[1:-1]))
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
349 else:
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
350 self._buf.append(self._render1(self.subdir, i))
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
351 self._buf.append(body)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
352 return ''.join(self._buf)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
353
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
354 def _render1(self, subdir, path):
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
355 # Reject bad file names
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
356 if not path.endswith(_IEXTEN) or path.endswith(_PEXTEN):
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
357 raise IncludeError(self.name, "#include files must end with {0} or {1}".format(_IEXTEN, _PEXTEN))
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
358 # Normalize
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
359 rawpath = _normpath(subdir, path)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
360 # Only include once
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
361 relpath = '/' + '/'.join(rawpath)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
362 if relpath in self.included:
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
363 return
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
364 self.included.add(relpath)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
365 # Do actual inclusion of a file
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
366 npath = os.path.join(self.base, *rawpath)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
367 nsubdir = rawpath[:-1]
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
368 try:
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
369 tf = TemplateFile(npath)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
370 except OSError as e:
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
371 raise IncludeError(self.name, str(e)) from e
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
372 try:
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
373 th = TemplateHeader(tf.headers)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
374 except TemplateHeaderError as e:
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
375 raise IncludeError(npath, str(e)) from e
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
376 # Reject unsupported crap (included files can only #include)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
377 for i in dir(th):
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
378 if i.startswith('_') or i == 'include':
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
379 continue
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
380 v = getattr(th, i)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
381 if callable(v):
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
382 continue
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
383 if v is not None and v is not False:
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
384 raise IncludeError(npath, "unsupported #{0}".format(i))
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
385 # Inclusion is recursive...
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
386 nested = _Includer(self.base, nsubdir, relpath,
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
387 included=self.included, encoding=self.encoding)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
388 return nested.render(th.includes, tf.body)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
389
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
390 # R o u t e s
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
391 #
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
392 # 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
393 # on the files it finds.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
394
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
395 _ERRMIN = 400
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
396 _ERRMAX = 599
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
397 _PEXTEN = ".py"
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
398 _TEXTEN = ".pspx"
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
399 _FLOOP = "tincan.forwards"
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
400 _FORIG = "tincan.origin"
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
401 _FTYPE = "tincan.iserror"
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
402
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
403 class _TinCanErrorRoute(object):
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
404 """
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
405 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
406 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
407 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
408 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
409 """
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
410 def __init__(self, template, klass):
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
411 self._template = template
23
e8b6ee7e5b6b Well, *that* attempt at includes didn't work. Revert.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
412 self._template.prepare()
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
413 self._class = klass
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
414
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
415 def __call__(self, e):
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
416 bottle.request.environ[_FTYPE] = True
17
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
417 try:
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
418 obj = self._class(bottle.request, e)
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
419 obj.handle()
23
e8b6ee7e5b6b Well, *that* attempt at includes didn't work. Revert.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
420 return self._template.render(obj.export()).lstrip('\n')
17
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
421 except bottle.HTTPResponse as e:
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
422 return e
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
423 except Exception as e:
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
424 traceback.print_exc()
18
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
425 # Bottle doesn't allow error handlers to themselves cause
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
426 # errors, most likely as a measure to prevent looping. So
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
427 # this will cause a "Critical error while processing request"
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
428 # page to be displayed, and any installed error pages to be
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
429 # ignored.
17
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
430 raise bottle.HTTPError(status=500, exception=e)
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
431
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
432 class _TinCanRoute(object):
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
433 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
434 A route created by the TinCan launcher.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
435 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
436 def __init__(self, launcher, name, subdir):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
437 self._fsroot = launcher.fsroot
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
438 self._urlroot = launcher.urlroot
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
439 self._name = name
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
440 self._python = name + _PEXTEN
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
441 self._fspath = os.path.join(launcher.fsroot, *subdir, name + _TEXTEN)
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
442 self._urlpath = self._urljoin(launcher.urlroot, *subdir, name + _TEXTEN)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
443 self._origin = self._urlpath
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
444 self._subdir = subdir
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
445 self._seen = set()
23
e8b6ee7e5b6b Well, *that* attempt at includes didn't work. Revert.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
446 self._tclass = launcher.tclass
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
447 self._app = launcher.app
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
448
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
449 def launch(self):
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
450 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
451 Launch a single page.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
452 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
453 # 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
454 oheader = None
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
455 while True:
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
456 try:
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
457 self._template = TemplateFile(self._fspath)
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
458 except IOError as e:
12
496d43d551d2 More redirecting fixes and improved error reportage.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
459 if oheader is not None:
496d43d551d2 More redirecting fixes and improved error reportage.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
460 note = "{0}: invalid #forward: ".format(self._origin)
496d43d551d2 More redirecting fixes and improved error reportage.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
461 else:
496d43d551d2 More redirecting fixes and improved error reportage.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
462 note = ""
496d43d551d2 More redirecting fixes and improved error reportage.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
463 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
464 try:
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
465 self._header = TemplateHeader(self._template.header)
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
466 except TemplateHeaderError as e:
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
467 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
468 if oheader is None:
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
469 oheader = self._header # save original header
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
470 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
471 raise TinCanError("{0}: invalid #forward".format(self._origin))
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
472 if self._header.forward is None:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
473 break
12
496d43d551d2 More redirecting fixes and improved error reportage.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
474 # print("forwarding from:", self._urlpath) # debug
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
475 self._redirect()
12
496d43d551d2 More redirecting fixes and improved error reportage.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
476 # 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
477 # 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
478 # don't get routes made for them.
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
479 if oheader.hidden and not oheader.errors:
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
480 return
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
481 # Get the code-behind #python
15
560c8fb55e4a Fix bugs in #python directive, make code-behind class loading simpler.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
482 if self._header.python is None:
560c8fb55e4a Fix bugs in #python directive, make code-behind class loading simpler.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
483 self._python_specified = False
560c8fb55e4a Fix bugs in #python directive, make code-behind class loading simpler.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
484 else:
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
485 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
486 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
487 self._python = self._header.python
15
560c8fb55e4a Fix bugs in #python directive, make code-behind class loading simpler.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
488 self._python_specified = True
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
489 # Obtain a class object by importing and introspecting a module.
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
490 self._getclass()
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
491 # Build body object (#template)
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
492 if self._header.template is not None:
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
493 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
494 raise TinCanError("{0}: #template files must end in {1}".format(self._urlpath, _TEXTEN))
16
448fc3d534f8 Improve error reportage.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
495 try:
24
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
496 rawpath = self._splitpath(self._header.template)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
497 tsubdir = rawpath[:-1]
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
498 tpath = os.path.normpath(os.path.join(self._fsroot, *rawpath))
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
499 turlpath = '/' + self._urljoin(rawpath)
16
448fc3d534f8 Improve error reportage.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
500 tfile = TemplateFile(tpath)
24
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
501 thead = TemplateHeader(tpath.header)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
502 except (OSError, TemplateHeaderError) as e:
16
448fc3d534f8 Improve error reportage.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
503 raise TinCanError("{0}: invalid #template: {1!s}".format(self._urlpath, e)) from e
448fc3d534f8 Improve error reportage.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
504 except IndexError as e:
448fc3d534f8 Improve error reportage.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
505 raise TinCanError("{0}: invalid #template".format(self._urlpath)) from e
24
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
506 includer = _Includer(self._fspath, tsubdir, turlpath)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
507 try:
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
508 self._body = self._tclass(source=includer.render(thead.include, tfile.body))
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
509 except IncludeError as e:
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
510 raise TinCanError("{0}: #include error: {1!s}".format(turlpath, e)) from e
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
511 else:
24
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
512 includer = _Includer(self._fspath, self._subdir, self._urlpath)
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
513 try:
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
514 self._body = self._tclass(
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
515 source=includer.render(self._header.include, self._template.body))
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
516 except IncludeError as e:
34d3cfcd37ef First batch of work on getting a #include header. Unfinished.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
517 raise TinCanError("{0}: #include error: {1!s}".format(self._urlpath, e)) from e
23
e8b6ee7e5b6b Well, *that* attempt at includes didn't work. Revert.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
518 self._body.prepare()
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
519 # 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
520 if oheader.errors is not None:
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
521 self._mkerror(oheader.errors)
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
522 return # this implies #hidden
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
523 # Get #methods for this route
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
524 if self._header.methods is None:
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
525 methods = [ 'GET' ]
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
526 else:
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
527 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
528 if not methods:
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
529 raise TinCanError("{0}: no #methods specified".format(self._urlpath))
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
530 # Register this thing with Bottle
6
a3823da7bb45 Minor tweaks.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
531 print("adding route:", self._origin, '('+','.join(methods)+')') # debug
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
532 self._app.route(self._origin, methods, self)
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
533
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
534 def _splitpath(self, unsplit):
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
535 return _normpath(self._subdir, unsplit)
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
536
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
537 def _mkerror(self, rerrors):
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
538 try:
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
539 errors = [ int(i) for i in rerrors.split() ]
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
540 except ValueError as e:
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
541 raise TinCanError("{0}: bad #errors line".format(self._urlpath)) from e
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
542 if not errors:
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
543 errors = range(_ERRMIN, _ERRMAX+1)
23
e8b6ee7e5b6b Well, *that* attempt at includes didn't work. Revert.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
544 route = _TinCanErrorRoute(self._tclass(source=self._template.body), self._class)
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
545 for error in errors:
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
546 if error < _ERRMIN or error > _ERRMAX:
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
547 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
548 self._app.error_handler[error] = route # XXX
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
549
7
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
550 def _gettime(self, path):
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
551 try:
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
552 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
553 except FileNotFoundError:
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
554 return 0
8
9aaa91247b14 Fix minor buglet.
David Barts <n5jrn@me.com>
parents: 7
diff changeset
555 except OSError as e:
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
556 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
557
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
558 def _getclass(self):
15
560c8fb55e4a Fix bugs in #python directive, make code-behind class loading simpler.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
559 try:
560c8fb55e4a Fix bugs in #python directive, make code-behind class loading simpler.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
560 pypath = os.path.normpath(os.path.join(self._fsroot, *self._splitpath(self._python)))
560c8fb55e4a Fix bugs in #python directive, make code-behind class loading simpler.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
561 except IndexError as e:
560c8fb55e4a Fix bugs in #python directive, make code-behind class loading simpler.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
562 raise TinCanError("{0}: invalid #python".format(self._urlpath)) from e
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
563 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
564 # 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
565 pytime = self._gettime(pypath)
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
566 if not pytime:
15
560c8fb55e4a Fix bugs in #python directive, make code-behind class loading simpler.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
567 if self._python_specified:
560c8fb55e4a Fix bugs in #python directive, make code-behind class loading simpler.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
568 raise TinCanError("{0}: #python file not found".format(self._urlpath))
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
569 self._class = klass
7
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
570 return
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
571 # Else load the code-behind from a .py file
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
572 pycpath = pypath + 'c'
7
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
573 pyctime = self._gettime(pycpath)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
574 try:
7
57ec65f527e9 Eliminate a stat() call, allow no code-behind on pages.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
575 if pyctime < pytime:
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
576 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
577 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
578 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
579 except Exception as e:
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
580 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
581 try:
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
582 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
583 mod = importlib.util.module_from_spec(spec)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
584 spec.loader.exec_module(mod)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
585 except Exception as e:
16
448fc3d534f8 Improve error reportage.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
586 raise TinCanError("{0}: error importing: {1!s}".format(pycpath, e)) from e
19
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
587 # Locate a suitable class. We look for the "deepest" class object
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
588 # we can find in the inheritance tree.
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
589 self._class = None
19
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
590 score = -1
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
591 ambig = False
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
592 for i in dir(mod):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
593 v = getattr(mod, i)
19
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
594 if not isclass(v):
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
595 continue
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
596 d = self._cldepth(klass, v)
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
597 if d > score:
15
560c8fb55e4a Fix bugs in #python directive, make code-behind class loading simpler.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
598 self._class = v
19
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
599 score = d
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
600 ambig = False
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
601 elif d == score:
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
602 ambig = True
3
c6902cded64d Corrections and reorg.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
603 if self._class is None:
9
75e375b1976a Error pages now can have code-behind.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
604 raise TinCanError("{0}: contains no {1} classes".format(pypath, klass.__name__))
19
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
605 if ambig:
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
606 raise TinCanError("{0}: contains ambiguous {1} classes".format(pypath, klass.__name__))
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
607
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
608 # This might fail for complex inheritance schemes from the classes of
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
609 # interest (so don't use them!).
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
610 def _cldepth(self, base, klass, count=0):
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
611 if klass is object:
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
612 # not found
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
613 return -1
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
614 elif klass is base:
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
615 # just found
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
616 return count
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
617 else:
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
618 # must recurse
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
619 for c in klass.__bases__:
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
620 result = self._cldepth(base, c, count=count+1)
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
621 if result > 0:
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
622 return result
5d9a1b82251a Return the "deepest" subclass; this allows subclassing tincan.Page and
David Barts <n5jrn@me.com>
parents: 18
diff changeset
623 return -1
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
624
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
625 def _redirect(self):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
626 try:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
627 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
628 forw = '/' + '/'.join(rlist)
11
8037bad7d5a8 Update documentation, fix some #forward bugs.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
629 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
630 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
631 self._seen.add(forw)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
632 rname = rlist.pop()
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
633 except IndexError as e:
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
634 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
635 name, ext = os.path.splitext(rname)
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
636 if ext != _TEXTEN:
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
637 raise TinCanError("{0}: invalid #forward".format(self._urlpath))
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
638 self._subdir = rlist
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
639 self._python = name + _PEXTEN
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
640 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
641 self._urlpath = '/' + self._urljoin(*self._subdir, rname)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
642
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
643 def _urljoin(self, *args):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
644 args = list(args)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
645 if args[0] == '/':
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
646 args[0] = ''
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
647 return '/'.join(args)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
648
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
649 def __call__(self):
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
650 """
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
651 This gets called by the framework AFTER the page is launched.
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
652 """
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
653 target = None
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
654 try:
17
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
655 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
656 obj.handle()
23
e8b6ee7e5b6b Well, *that* attempt at includes didn't work. Revert.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
657 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
658 except ForwardException as fwd:
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
659 target = fwd.target
17
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
660 except bottle.HTTPResponse as e:
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
661 return e
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
662 except Exception as e:
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
663 traceback.print_exc()
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
664 raise bottle.HTTPError(status=500, exception=e)
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
665 if target is None:
17
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
666 message = "{0}: unexpected null target".format(self._urlpath)
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
667 sys.stderr.write(message + '\n')
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
668 raise bottle.HTTPError(status=500, exception=TinCanError(message))
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
669 # 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
670 # forward.
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
671 environ = bottle.request.environ
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
672 if _FORIG not in environ:
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
673 environ[_FORIG] = self._urlpath
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
674 if _FLOOP not in environ:
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
675 environ[_FLOOP] = set([self._urlpath])
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
676 elif target in environ[_FLOOP]:
17
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
677 message = "{0}: forward loop detected".format(environ[_FORIG])
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
678 sys.stderr.write(message + '\n')
8186de188daf Improve the run-time error handling in code-behinds.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
679 raise bottle.HTTPError(status=500, exception=TinCanError(message))
1
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
680 environ[_FLOOP].add(target)
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
681 environ['bottle.raw_path'] = target
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
682 environ['PATH_INFO'] = urllib.parse.quote(target)
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
683 route, args = self._app.router.match(environ)
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
684 environ['route.handle'] = environ['bottle.route'] = route
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
685 environ['route.url_args'] = args
94b36e721500 Another check in to back stuff up.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
686 return route.call(**args)
0
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
687
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
688 def _mkdict(self, obj):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
689 ret = {}
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
690 for name in dir(obj):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
691 if name.startswith('_'):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
692 continue
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
693 value = getattr(obj, name)
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
694 if not callable(value):
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
695 ret[name] = value
e726fafcffac For backup purposes, UNFINISHED!!
David Barts <n5jrn@me.com>
parents:
diff changeset
696 return ret
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
697
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
698 # L a u n c h e r
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
699
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
700 _WINF = "WEB-INF"
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
701 _BANNED = set([_WINF])
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
702
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
703 class _Launcher(object):
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
704 """
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
705 Helper class for launching webapps.
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
706 """
23
e8b6ee7e5b6b Well, *that* attempt at includes didn't work. Revert.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
707 def __init__(self, fsroot, urlroot, tclass, logger):
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
708 """
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
709 Lightweight constructor. The real action happens in .launch() below.
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
710 """
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
711 self.fsroot = fsroot
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
712 self.urlroot = urlroot
23
e8b6ee7e5b6b Well, *that* attempt at includes didn't work. Revert.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
713 self.tclass = tclass
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
714 self.logger = logger
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
715 self.app = None
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
716 self.errors = 0
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
717 self.debug = False
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
718
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
719 def launch(self):
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
720 """
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
721 Does the actual work of launching something. XXX - modifies sys.path
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
722 and never un-modifies it.
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
723 """
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
724 # Sanity checks
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
725 if not self.urlroot.startswith("/"):
18
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
726 self.errors = 1
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
727 self.logger("urlroot not absolute: {0!r}".format(self.urlroot))
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
728 return self
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
729 if not os.path.isdir(self.fsroot):
18
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
730 self.errors = 1
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
731 self.logger("no such directory: {0!r}".format(self.fsroot))
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
732 return self
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
733 # Make any needed directories. Refuse to launch things that don't
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
734 # contain WEB-INF, to prevent accidental launches of undesired
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
735 # directory trees containing sensitive files.
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
736 winf = os.path.join(self.fsroot, _WINF)
18
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
737 if not os.path.isdir(winf):
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
738 self.errors = 1
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
739 self.logger("no WEB-INF directory in {0!r}".format(self.fsroot))
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
740 return self
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
741 lib = os.path.join(winf, "lib")
18
e88ab99914cf More improvements to the error reportage.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
742 for i in [ lib ]:
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
743 if not os.path.isdir(i):
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
744 os.mkdir(i)
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
745 # Add our private lib directory to sys.path
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
746 sys.path.insert(1, os.path.abspath(lib))
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
747 # Do what we gotta do
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
748 self.app = TinCan()
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
749 self._launch([])
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
750 return self
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
751
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
752 def _launch(self, subdir):
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
753 for entry in os.listdir(os.path.join(self.fsroot, *subdir)):
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
754 if not subdir and entry in _BANNED:
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
755 continue
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
756 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
757 if S_ISREG(etype):
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
758 ename, eext = os.path.splitext(entry)
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
759 if eext != _TEXTEN:
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
760 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
761 route = _TinCanRoute(self, ename, subdir)
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
762 try:
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
763 route.launch()
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
764 except TinCanError as e:
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
765 self.logger(str(e))
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
766 if self.debug:
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
767 while e.__cause__ != None:
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
768 e = e.__cause__
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
769 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
770 self.errors += 1
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
771 elif S_ISDIR(etype):
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
772 self._launch(subdir + [entry])
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
773
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
774 def _logger(message):
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
775 sys.stderr.write(message)
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
776 sys.stderr.write('\n')
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
777
23
e8b6ee7e5b6b Well, *that* attempt at includes didn't work. Revert.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
778 def launch(fsroot=None, urlroot='/', tclass=ChameleonTemplate, logger=_logger):
2
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
779 """
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
780 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
781 caller's responsibility to call app.run()
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
782 """
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
783 if fsroot is None:
ca6f8ca38cf2 Another backup commit.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
784 fsroot = os.getcwd()
23
e8b6ee7e5b6b Well, *that* attempt at includes didn't work. Revert.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
785 launcher = _Launcher(fsroot, urlroot, tclass, logger)
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
786 # launcher.debug = True
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
787 launcher.launch()
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
788 return launcher.app, launcher.errors
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
789
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
790 # 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
791 # launcher script for why.