From f5d5cc49887d3a54bc5edc7905b90e5912ae9e8a Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Fri, 5 Oct 2012 10:30:32 +1300 Subject: [PATCH] rparse.py -> language.py --- libpathod/app.py | 10 +- libpathod/{rparse.py => language.py} | 0 libpathod/pathoc.py | 18 +- libpathod/pathod.py | 22 +-- pathoc | 2 +- pathod | 4 +- test/{test_rparse.py => test_language.py} | 200 +++++++++++----------- 7 files changed, 128 insertions(+), 128 deletions(-) rename libpathod/{rparse.py => language.py} (100%) rename test/{test_rparse.py => test_language.py} (64%) diff --git a/libpathod/app.py b/libpathod/app.py index 0db31ae5a..396e45c2d 100644 --- a/libpathod/app.py +++ b/libpathod/app.py @@ -1,6 +1,6 @@ import logging, pprint, cStringIO from flask import Flask, jsonify, render_template, request, abort, make_response -import version, rparse, utils +import version, language, utils logging.basicConfig(level="DEBUG") app = Flask(__name__) @@ -116,14 +116,14 @@ def _preview(is_request): try: if is_request: - r = rparse.parse_request(app.config["pathod"].request_settings, spec) + r = language.parse_request(app.config["pathod"].request_settings, spec) else: - r = rparse.parse_response(app.config["pathod"].request_settings, spec) - except rparse.ParseException, v: + r = language.parse_response(app.config["pathod"].request_settings, spec) + except language.ParseException, v: args["syntaxerror"] = str(v) args["marked"] = v.marked() return render(template, False, **args) - except rparse.FileAccessDenied: + except language.FileAccessDenied: args["error"] = "File access is disabled." return render(template, False, **args) diff --git a/libpathod/rparse.py b/libpathod/language.py similarity index 100% rename from libpathod/rparse.py rename to libpathod/language.py diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index 7551c589f..bab568ca1 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -1,7 +1,7 @@ import sys, os from netlib import tcp, http import netlib.utils -import rparse, utils +import language, utils class PathocError(Exception): pass @@ -18,10 +18,10 @@ class Pathoc(tcp.TCPClient): """ Return an (httpversion, code, msg, headers, content) tuple. - May raise rparse.ParseException, netlib.http.HttpError or - rparse.FileAccessDenied. + May raise language.ParseException, netlib.http.HttpError or + language.FileAccessDenied. """ - r = rparse.parse_request(self.settings, spec) + r = language.parse_request(self.settings, spec) ret = r.serve(self.wfile, None, self.host) self.wfile.flush() return http.read_response(self.rfile, r.method, None) @@ -53,23 +53,23 @@ class Pathoc(tcp.TCPClient): Returns True if we have a non-ignored response. """ try: - r = rparse.parse_request(self.settings, spec) - except rparse.ParseException, v: + r = language.parse_request(self.settings, spec) + except language.ParseException, v: print >> fp, "Error parsing request spec: %s"%v.msg print >> fp, v.marked() return - except rparse.FileAccessDenied, v: + except language.FileAccessDenied, v: print >> fp, "File access error: %s"%v return resp, req = None, None if showreq: self.wfile.start_log() + if showresp: + self.rfile.start_log() try: req = r.serve(self.wfile, None, self.host) self.wfile.flush() - if showresp: - self.rfile.start_log() resp = http.read_response(self.rfile, r.method, None) except http.HttpError, v: print >> fp, "<< HTTP Error:", v.msg diff --git a/libpathod/pathod.py b/libpathod/pathod.py index 6afcf4bf0..4ce268fa2 100644 --- a/libpathod/pathod.py +++ b/libpathod/pathod.py @@ -1,7 +1,7 @@ import urllib, threading, re, logging, socket, sys from netlib import tcp, http, odict, wsgi import netlib.utils -import version, app, rparse +import version, app, language logger = logging.getLogger('pathod') @@ -30,7 +30,7 @@ class PathodHandler(tcp.BaseHandler): def handle_request(self): """ - Returns a (again, log) tuple. + Returns a (again, log) tuple. again: True if request handling should continue. log: A dictionary, or None @@ -76,26 +76,26 @@ class PathodHandler(tcp.BaseHandler): for i in self.server.anchors: if i[0].match(path): self.info("crafting anchor: %s"%path) - aresp = rparse.parse_response(self.server.request_settings, i[1]) + aresp = language.parse_response(self.server.request_settings, i[1]) return self.serve_crafted(aresp, request_log) if not self.server.nocraft and path.startswith(self.server.craftanchor): spec = urllib.unquote(path)[len(self.server.craftanchor):] self.info("crafting spec: %s"%spec) try: - crafted = rparse.parse_response(self.server.request_settings, spec) - except rparse.ParseException, v: + crafted = language.parse_response(self.server.request_settings, spec) + except language.ParseException, v: self.info("Parse error: %s"%v.msg) - crafted = rparse.PathodErrorResponse( + crafted = language.PathodErrorResponse( "Parse Error", "Error parsing response spec: %s\n"%v.msg + v.marked() ) - except rparse.FileAccessDenied: + except language.FileAccessDenied: self.info("File access denied") - crafted = rparse.PathodErrorResponse("Access Denied") + crafted = language.PathodErrorResponse("Access Denied") return self.serve_crafted(crafted, request_log) elif self.server.noweb: - crafted = rparse.PathodErrorResponse("Access Denied") + crafted = language.PathodErrorResponse("Access Denied") crafted.serve(self.wfile, self.server.check_policy) return False, dict(type = "error", msg="Access denied: web interface disabled") else: @@ -200,8 +200,8 @@ class Pathod(tcp.TCPServer): except re.error: raise PathodError("Invalid regex in anchor: %s"%i[0]) try: - aresp = rparse.parse_response(self.request_settings, i[1]) - except rparse.ParseException, v: + aresp = language.parse_response(self.request_settings, i[1]) + except language.ParseException, v: raise PathodError("Invalid page spec in anchor: '%s', %s"%(i[1], str(v))) self.anchors.append((arex, i[1])) diff --git a/pathoc b/pathoc index 5932d4a45..c9290587f 100755 --- a/pathoc +++ b/pathoc @@ -1,6 +1,6 @@ #!/usr/bin/env python import argparse, sys -from libpathod import pathoc, version, rparse +from libpathod import pathoc, version, language from netlib import tcp if __name__ == "__main__": diff --git a/pathod b/pathod index e692a935d..41b7578de 100755 --- a/pathod +++ b/pathod @@ -1,7 +1,7 @@ #!/usr/bin/env python import argparse, sys, logging, logging.handlers import os -from libpathod import pathod, utils, version, rparse +from libpathod import pathod, utils, version, language def daemonize (stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): @@ -96,7 +96,7 @@ def main(parser, args): ) except pathod.PathodError, v: parser.error(str(v)) - except rparse.FileAccessDenied, v: + except language.FileAccessDenied, v: parser.error("%s You probably want to a -d argument."%str(v)) try: diff --git a/test/test_rparse.py b/test/test_language.py similarity index 64% rename from test/test_rparse.py rename to test/test_language.py index 3cbec3351..0668fba23 100644 --- a/test/test_rparse.py +++ b/test/test_language.py @@ -1,18 +1,18 @@ import os, cStringIO -from libpathod import rparse, utils +from libpathod import language, utils import tutils -rparse.TESTING = True +language.TESTING = True class TestMisc: def test_generators(self): - v = rparse.Value.parseString("'val'")[0] + v = language.Value.parseString("'val'")[0] g = v.get_generator({}) assert g[:] == "val" def test_randomgenerator(self): - g = rparse.RandomGenerator("bytes", 100) + g = language.RandomGenerator("bytes", 100) assert repr(g) assert len(g[:10]) == 10 assert len(g[1:10]) == 9 @@ -21,7 +21,7 @@ class TestMisc: assert g[0] def test_literalgenerator(self): - g = rparse.LiteralGenerator("one") + g = language.LiteralGenerator("one") assert repr(g) assert g == "one" assert g[:] == "one" @@ -33,7 +33,7 @@ class TestMisc: f = open(path, "w") f.write("x"*10000) f.close() - g = rparse.FileGenerator(path) + g = language.FileGenerator(path) assert len(g) == 10000 assert g[0] == "x" assert g[-1] == "x" @@ -41,26 +41,26 @@ class TestMisc: assert repr(g) def test_valueliteral(self): - v = rparse.ValueLiteral("foo") + v = language.ValueLiteral("foo") assert v.expr() assert v.val == "foo" - v = rparse.ValueLiteral(r"foo\n") + v = language.ValueLiteral(r"foo\n") assert v.expr() assert v.val == "foo\n" assert repr(v) def test_valuenakedliteral(self): - v = rparse.ValueNakedLiteral("foo") + v = language.ValueNakedLiteral("foo") assert v.expr() assert repr(v) def test_file_value(self): - v = rparse.Value.parseString("<'one two'")[0] + v = language.Value.parseString("<'one two'")[0] assert str(v) assert v.path == "one two" - v = rparse.Value.parseString(" 100 x = [(1, 5), (0, 5)] - assert rparse.ready_actions(100, x) == sorted(x) + assert language.ready_actions(100, x) == sorted(x) class TestResponse: def dummy_response(self): - return rparse.parse_response({}, "400'msg'") + return language.parse_response({}, "400'msg'") def test_file(self): p = tutils.test_data.path("data") d = dict(staticdir=p) - r = rparse.parse_response(d, "+response") + r = language.parse_response(d, "+response") assert r.code == 202 def test_response(self): - r = rparse.parse_response({}, "400'msg'") + r = language.parse_response({}, "400'msg'") assert r.code == 400 assert r.msg == "msg" - r = rparse.parse_response({}, "400'msg':b@100b") + r = language.parse_response({}, "400'msg':b@100b") assert r.msg == "msg" assert r.body[:] assert str(r) def test_checkfunc(self): s = cStringIO.StringIO() - r = rparse.parse_response({}, "400:b@100k") + r = language.parse_response({}, "400:b@100k") def check(req, acts): return "errmsg" assert r.serve(s, check=check)["error"] == "errmsg" def test_render(self): s = cStringIO.StringIO() - r = rparse.parse_response({}, "400'msg'") + r = language.parse_response({}, "400'msg'") assert r.serve(s, None) def test_raw(self): s = cStringIO.StringIO() - r = rparse.parse_response({}, "400:b'foo'") + r = language.parse_response({}, "400:b'foo'") r.serve(s, None) v = s.getvalue() assert "Content-Length" in v assert "Date" in v s = cStringIO.StringIO() - r = rparse.parse_response({}, "400:b'foo':r") + r = language.parse_response({}, "400:b'foo':r") r.serve(s, None) v = s.getvalue() assert not "Content-Length" in v @@ -474,9 +474,9 @@ class TestResponse: s = cStringIO.StringIO() x.serve(s, None) assert x.length() == len(s.getvalue()) - testlen(rparse.parse_response({}, "400'msg'")) - testlen(rparse.parse_response({}, "400'msg':h'foo'='bar'")) - testlen(rparse.parse_response({}, "400'msg':h'foo'='bar':b@100b")) + testlen(language.parse_response({}, "400'msg'")) + testlen(language.parse_response({}, "400'msg':h'foo'='bar'")) + testlen(language.parse_response({}, "400'msg':h'foo'='bar':b@100b")) def test_effective_length(self): def testlen(x, actions): @@ -486,7 +486,7 @@ class TestResponse: actions = [ ] - r = rparse.parse_response({}, "400'msg':b@100") + r = language.parse_response({}, "400'msg':b@100") actions = [ (0, "disconnect"), @@ -508,7 +508,7 @@ class TestResponse: testlen(r, actions) def test_render(self): - r = rparse.parse_response({}, "400:p0,100:dr") + r = language.parse_response({}, "400:p0,100:dr") assert r.actions[0][1] == "pause" assert len(r.preview_safe()) == 1 assert not r.actions[0][1] == "pause" @@ -516,14 +516,14 @@ class TestResponse: def test_read_file(): - tutils.raises(rparse.FileAccessDenied, rparse.read_file, {}, "=/foo") + tutils.raises(language.FileAccessDenied, language.read_file, {}, "=/foo") p = tutils.test_data.path("data") d = dict(staticdir=p) - assert rparse.read_file(d, "+./file").strip() == "testfile" - assert rparse.read_file(d, "+file").strip() == "testfile" - tutils.raises(rparse.FileAccessDenied, rparse.read_file, d, "+./nonexistent") - tutils.raises(rparse.FileAccessDenied, rparse.read_file, d, "+/nonexistent") + assert language.read_file(d, "+./file").strip() == "testfile" + assert language.read_file(d, "+file").strip() == "testfile" + tutils.raises(language.FileAccessDenied, language.read_file, d, "+./nonexistent") + tutils.raises(language.FileAccessDenied, language.read_file, d, "+/nonexistent") - tutils.raises(rparse.FileAccessDenied, rparse.read_file, d, "+../test_rparse.py") + tutils.raises(language.FileAccessDenied, language.read_file, d, "+../test_language.py") d["unconstrained_file_access"] = True - assert rparse.read_file(d, "+../test_rparse.py") + assert language.read_file(d, "+../test_language.py")