diff --git a/libpathod/rparse.py b/libpathod/rparse.py index f3985d042..d1714fb8c 100644 --- a/libpathod/rparse.py +++ b/libpathod/rparse.py @@ -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) diff --git a/pathod b/pathod index ab8cc93fa..4340e2d1b 100755 --- a/pathod +++ b/pathod @@ -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: diff --git a/test/test_rparse.py b/test/test_rparse.py index b872b1f34..0ee3aae46 100644 --- a/test/test_rparse.py +++ b/test/test_rparse.py @@ -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("