-#!/usr/bin/python
+#!/usr/bin/python3
from distutils.core import setup, Extension
__all__ = ["wsgiwrap", "restart", "cookie", "formdata"]
-import proto
-from util import wsgiwrap, formparams, persession, sessiondata, autodirty, manudirty
-from dispatch import restart
-import cookie
-from form import formdata
+from . import proto
+from .util import wsgiwrap, stringwrap, formparams, persession, sessiondata, autodirty, manudirty
+from .dispatch import restart
+from . import cookie
+from .form import formdata
-import Cookie
+import http.cookies
__all__ = ["cookies", "get", "add"]
class cookiedict(object):
def __init__(self, req):
try:
- self.bk = Cookie.SimpleCookie(req.ihead.get("Cookie"))
- except Cookie.CookieError:
- self.bk = Cookie.SimpleCookie()
- self.codec = Cookie.SimpleCookie()
+ self.bk = http.cookies.SimpleCookie(req.ihead.get("Cookie"))
+ except http.cookies.CookieError:
+ self.bk = http.cookies.SimpleCookie()
+ self.codec = http.cookies.SimpleCookie()
req.oncommit(addcookies)
def __getitem__(self, name):
import sys, traceback
-import env
+from . import env
__all__ = ["restart"]
self.bk = real
self.bki = iter(real)
self._next = [None]
- self.next()
+ self.__next__()
def __iter__(self):
return self
- def next(self):
+ def __next__(self):
if self._next is None:
raise StopIteration()
ret = self._next[0]
try:
- self._next[:] = [self.bki.next()]
+ self._next[:] = [self.bki.__next__()]
except StopIteration:
self._next = None
return ret
self.bk.close()
def defaulterror(req, excinfo):
- import resp
+ from . import resp
traceback.print_exception(*excinfo)
raise resp.httperror(500)
try:
resp = iterproxy(handler(req))
break
- except restart, i:
+ except restart as i:
handler = i.handle
- except Exception, i:
+ except Exception as i:
if eh is None:
raise
handler = wraphandler(eh, sys.exc_info())
import os
-import resp
+from . import resp
pj = os.path.join
__all__ = ["filehandler"]
elif ext == "html":
ctype = "text/html"
req.ohead["Content-Type"] = ctype
- return open(path, "r")
+ return open(path, "rb")
def resolvefile(self, req, curpath, el):
if os.path.isfile(pj(curpath, el)):
import cgi
-import proto
+from . import proto
__all__ = ["formdata"]
return list(iter())
def keys(self):
- return self.cf.keys()
+ return list(self.cf.keys())
def values(self):
return [val for key, val in self.items()]
class formpart(object):
def __init__(self, form):
self.form = form
- self.buf = ""
+ self.buf = b""
self.eof = False
self.head = {}
def fillbuf(self, sz):
req = self.form.req
- mboundary = "\r\n--" + self.form.boundary + "\r\n"
- lboundary = "\r\n--" + self.form.boundary + "--\r\n"
+ mboundary = b"\r\n--" + self.form.boundary + b"\r\n"
+ lboundary = b"\r\n--" + self.form.boundary + b"--\r\n"
while not self.eof:
p = self.form.buf.find(mboundary)
if p >= 0:
def readline(self, limit = -1):
last = 0
while True:
- p = self.buf.find('\n', last)
+ p = self.buf.find(b'\n', last)
if p < 0:
if self.eof:
ret = self.buf
def __exit__(self, *excinfo):
return False
- def parsehead(self):
+ def parsehead(self, charset):
def headline():
ln = self.readline(256)
- if ln[-1] != '\n':
+ if ln[-1] != ord(b'\n'):
raise badmultipart("Too long header line in part")
- return ln.rstrip()
+ try:
+ return ln.decode(charset).rstrip()
+ except UnicodeError:
+ raise badmultipart("Form part header is not in assumed charset")
ln = headline()
while True:
raise badmultipart("Form part uses unexpected transfer encoding: %r" % encoding)
class multipart(object):
- def __init__(self, req):
+ def __init__(self, req, charset):
val, par = proto.pmimehead(req.ihead.get("Content-Type", ""))
if req.method != "POST" or val != "multipart/form-data":
raise badmultipart("Request is not a multipart form")
if "boundary" not in par:
raise badmultipart("Multipart form lacks boundary")
- self.boundary = par["boundary"]
+ try:
+ self.boundary = par["boundary"].encode("us-ascii")
+ except UnicodeError:
+ raise badmultipart("Multipart boundary must be ASCII string")
self.req = req
- self.buf = "\r\n"
+ self.buf = b"\r\n"
self.eof = False
+ self.headcs = charset
self.lastpart = formpart(self)
self.lastpart.close()
def __iter__(self):
return self
- def next(self):
+ def __next__(self):
if not self.lastpart.eof:
raise RuntimeError("All form parts must be read entirely")
if self.eof:
raise StopIteration()
self.lastpart = formpart(self)
- self.lastpart.parsehead()
+ self.lastpart.parsehead(self.headcs)
return self.lastpart
def formdata(req):
del self.dict[key.lower()]
def __iter__(self):
- return iter((list[0] for list in self.dict.itervalues()))
+ return iter((list[0] for list in self.dict.values()))
def get(self, key, default = ""):
if key.lower() in self.dict:
-import dispatch, proto, env
+from . import dispatch, proto, env
__all__ = ["skeleton", "skelfor", "setskel", "usererror"]
class usererror(dispatch.restart):
def __init__(self, message, detail):
- super(usererror, self).__init__()
+ super().__init__()
self.message = message
self.detail = detail
def handle(self, req):
- return [skelfor(req).error(self.message, self.detail)]
+ return [skelfor(req).error(self.message, self.detail).encode("utf-8")]
class message(dispatch.restart):
- def __init__(self, msg, detail):
- super(message, self).__init__()
- self.message = msg
+ def __init__(self, message, detail):
+ super().__init__()
+ self.message = message
self.detail = detail
def handle(self, req):
- return [skelfor(req).message(self.message, self.detail)]
+ return [skelfor(req).message(self.message, self.detail).encode("utf-8")]
class httperror(usererror):
def __init__(self, status, message = None, detail = None):
message = proto.statusinfo[status][0]
if detail is None:
detail = proto.statusinfo[status][1]
- super(httperror, self).__init__(message, detail)
+ super().__init__(message, detail)
self.status = status
def handle(self, req):
req.status(self.status, self.message)
- return super(httperror, self).handle(req)
+ return super().handle(req)
class notfound(httperror):
def __init__(self):
- return super(notfound, self).__init__(404)
+ return super().__init__(404)
class redirect(dispatch.restart):
def __init__(self, url, status = 303):
- super(redirect, self).__init__()
+ super().__init__()
self.url = url
self.status = status
import threading, time, pickle, random, os
-import cookie, env
+from . import cookie, env
__all__ = ["db", "get"]
def gennonce(length):
nonce = ""
- for i in xrange(length):
+ for i in range(length):
nonce += chr(random.randint(0, 255))
return nonce
now = int(time.time())
with self.lock:
dlist = []
- for sess in self.live.itervalues():
+ for sess in self.live.values():
if sess.atime + self.freezetime < now:
try:
if sess.dirty():
data = self.backdb[sessid]
try:
return pickle.loads(data)
- except Exception, e:
+ except:
raise KeyError()
def freeze(self, sess):
import inspect
-import req, dispatch, session, form
+from . import req, dispatch, session, form
def wsgiwrap(callable):
def wrapper(env, startreq):
return dispatch.handle(req.origrequest(env), startreq, callable)
return wrapper
+def stringwrap(charset):
+ def dec(callable):
+ def wrapper(*args, **kwargs):
+ bk = callable(*args, **kwargs)
+ for string in bk:
+ yield string.encode(charset)
+ return wrapper
+ return dec
+
def formparams(callable):
def wrapper(req):
data = form.formdata(req)
class autodirty(sessiondata):
@classmethod
def get(cls, req):
- ret = super(autodirty, cls).get(req)
+ ret = super().get(req)
if "_is_dirty" not in ret.__dict__:
ret.__dict__["_is_dirty"] = False
return ret
return self._is_dirty
def __setattr__(self, name, value):
- super(autodirty, self).__setattr__(name, value)
+ super().__setattr__(name, value)
if "_is_dirty" in self.__dict__:
self.__dict__["_is_dirty"] = True
def __delattr__(self, name):
- super(autodirty, self).__delattr__(name, value)
+ super().__delattr__(name, value)
if "_is_dirty" in self.__dict__:
self.__dict__["_is_dirty"] = True
class manudirty(object):
def __init__(self, *args, **kwargs):
- super(manudirty, self).__init__(*args, **kwargs)
+ super().__init__(*args, **kwargs)
self.__dirty = False
def sessfrozen(self):
import os, threading
from mako import template, lookup, filters
-import util, form, session
+from . import util, form, session
# It seems Mako isn't thread-safe.
makolock = threading.Lock()