First pass at static file serving.

This commit is contained in:
Aldo Cortesi 2012-04-28 13:16:51 +12:00
parent b4105be21e
commit 2c8f17eae7
3 changed files with 56 additions and 11 deletions

View File

@ -1,4 +1,4 @@
import operator, string, random, sys, time
import operator, string, random, sys, time, mmap, os
import contrib.pyparsing as pp
import http, utils
import tornado.ioloop
@ -6,6 +6,7 @@ import tornado.ioloop
TESTING = False
class ParseException(Exception): pass
class ServerError(Exception): pass
DATATYPES = dict(
@ -81,6 +82,17 @@ class RandomGenerator:
class FileGenerator:
def __init__(self, path):
self.path = path
self.fp = file(path, "r")
self.map = mmap.mmap(self.fp.fileno(), 0, prot=mmap.PROT_READ)
def __len__(self):
return len(self.map)
def __getitem__(self, x):
return self.map.__getitem__(x)
def __getslice__(self, a, b):
return self.map.__getslice__(a, b)
class ValueLiteral:
@ -145,7 +157,13 @@ class ValueFile:
return e.setParseAction(lambda x: klass(*x))
def get_generator(self, settings):
raise NotImplementedError
sd = settings.get("staticdir")
if not sd:
raise ServerError("No static directory specified.")
path = os.path.join(sd, self.path)
if not os.path.exists(path):
raise ServerError("Static file does not exist: %s"%path)
return FileGenerator(path)
def __str__(self):
return "<%s"%(self.path)

6
pathod
View File

@ -1,6 +1,6 @@
#!/usr/bin/env python
import argparse
import libomnid
import libpathod
import tornado.ioloop
if __name__ == "__main__":
@ -12,8 +12,8 @@ if __name__ == "__main__":
)
args = parser.parse_args()
libomnid.application(staticdir=args.staticdir).listen(args.port)
print "omnid listening on port %s"%args.port
libpathod.application(staticdir=args.staticdir).listen(args.port)
print "pathod listening on port %s"%args.port
try:
tornado.ioloop.IOLoop.instance().start()
except KeyboardInterrupt:

View File

@ -1,4 +1,4 @@
import StringIO, sys
import StringIO, sys, os
import libpry
from libpathod import rparse
@ -24,11 +24,43 @@ class uMisc(libpry.AutoTree):
assert g[:] == "one"
assert g[1] == "n"
def test_filegenerator(self):
t = self.tmpdir()
path = os.path.join(t, "foo")
f = open(path, "w")
f.write("x"*10000)
f.close()
g = rparse.FileGenerator(path)
assert len(g) == 10000
assert g[0] == "x"
assert g[-1] == "x"
assert g[0:5] == "xxxxx"
def test_valueliteral(self):
v = rparse.ValueLiteral("foo")
assert v.expr()
assert str(v)
def test_file_value(self):
v = rparse.Value.parseString("<'one two'")[0]
assert v.path == "one two"
v = rparse.Value.parseString("<path")[0]
assert v.path == "path"
t = self.tmpdir()
p = os.path.join(t, "path")
f = open(p, "w")
f.write("x"*10000)
f.close()
assert v.get_generator(dict(staticdir=t))
v = rparse.Value.parseString("<path2")[0]
libpry.raises(rparse.ServerError, v.get_generator, dict(staticdir=t))
libpry.raises("no static directory", v.get_generator, dict())
def test_generated_value(self):
v = rparse.Value.parseString("!10b")[0]
assert v.usize == 10
@ -56,11 +88,6 @@ class uMisc(libpry.AutoTree):
assert rparse.Value.parseString('"val"')[0].val == "val"
assert rparse.Value.parseString('"\'val\'"')[0].val == "'val'"
v = rparse.Value.parseString("<path")[0]
assert v.path == "path"
v = rparse.Value.parseString("<'one two'")[0]
assert v.path == "one two"
def test_body(self):
e = rparse.Body.expr()
v = e.parseString("b:foo")[0]