aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libpathod/rparse.py22
-rwxr-xr-xpathod6
-rw-r--r--test/test_rparse.py39
3 files changed, 56 insertions, 11 deletions
diff --git a/libpathod/rparse.py b/libpathod/rparse.py
index f3985d04..d1714fb8 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 ab8cc93f..4340e2d1 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 b872b1f3..0ee3aae4 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("<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]