From a7e1d7d4bd8888a6ca994920910b75eb94550995 Mon Sep 17 00:00:00 2001 From: Alexander Loechel Date: Thu, 10 Aug 2017 23:07:54 +0200 Subject: [PATCH] starting to make some code convetion thingy and cleanup to be more readable --- lib/diazo/__init__.py | 4 + lib/diazo/compiler.py | 85 ++++-- lib/diazo/cssrules.py | 78 +++-- lib/diazo/defaults.xsl | 5 +- lib/diazo/rules.py | 132 ++++++--- lib/diazo/run.py | 101 ++++--- lib/diazo/runtrace.py | 77 +++-- lib/diazo/tests/__init__.py | 9 +- lib/diazo/tests/test_diazo.py | 29 +- lib/diazo/tests/test_trace.py | 90 ++++-- lib/diazo/tests/test_wsgi.py | 521 ++++++++++++++++++++++++---------- lib/diazo/utils.py | 164 +++++++---- lib/diazo/wsgi.py | 263 +++++++++++------ setup.cfg | 20 ++ setup.py | 54 ++-- tox.ini | 94 +++++- 16 files changed, 1215 insertions(+), 511 deletions(-) diff --git a/lib/diazo/__init__.py b/lib/diazo/__init__.py index df19bd5..f442168 100644 --- a/lib/diazo/__init__.py +++ b/lib/diazo/__init__.py @@ -1,2 +1,6 @@ +# -*- coding: utf-8 -*- + import logging + + logging.basicConfig() diff --git a/lib/diazo/compiler.py b/lib/diazo/compiler.py index e6b176f..f496a53 100644 --- a/lib/diazo/compiler.py +++ b/lib/diazo/compiler.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- """\ Usage: %prog [options] [-r] RULES [-t] THEME @@ -9,28 +10,38 @@ theme="//div[@id='page-content']"/> \ """ -import logging -import pkg_resources + +from diazo.rules import process_rules +from diazo.utils import _createOptionParser +from diazo.utils import CustomResolver +from diazo.utils import pkg_xsl +from diazo.utils import quote_param +from diazo.utils import split_params from lxml import etree from six import string_types -from diazo.rules import process_rules -from diazo.utils import pkg_xsl, _createOptionParser, CustomResolver -from diazo.utils import quote_param, split_params + +import logging +import pkg_resources + logger = logging.getLogger('diazo') usage = __doc__ def set_parser(stylesheet, parser, compiler_parser=None): - dummy_doc = etree.parse(open( - pkg_resources.resource_filename('diazo', 'dummy.html')), parser=parser) + dummy_doc = etree.parse( + open( + pkg_resources.resource_filename('diazo', 'dummy.html'), + ), + parser=parser, + ) name = 'file:///__diazo__' resolver = CustomResolver({name: stylesheet}) if compiler_parser is None: compiler_parser = etree.XMLParser() compiler_parser.resolvers.add(resolver) identity = pkg_xsl('identity.xsl', compiler_parser) - output_doc = identity(dummy_doc, docurl="'%s'" % name) + output_doc = identity(dummy_doc, docurl="'{name}'".format(name=name)) compiler_parser.resolvers.remove(resolver) return output_doc @@ -42,10 +53,13 @@ def build_xsl_params_document(xsl_params): xsl_params['path'] = '' known_params = etree.XML( '') + 'xmlns:xsl="http://www.w3.org/1999/XSL/Transform" />', + ) for param_name, param_value in xsl_params.items(): param_element = etree.SubElement( - known_params, "{http://www.w3.org/1999/XSL/Transform}param") + known_params, + '{http://www.w3.org/1999/XSL/Transform}param', + ) param_element.attrib['name'] = param_name if isinstance(param_value, string_types): param_element.text = param_value @@ -56,11 +70,25 @@ def build_xsl_params_document(xsl_params): return known_params -def compile_theme(rules, theme=None, extra=None, css=True, xinclude=True, - absolute_prefix=None, update=True, trace=False, - includemode=None, parser=None, compiler_parser=None, - rules_parser=None, access_control=None, read_network=False, - indent=None, xsl_params=None, runtrace=False): +def compile_theme( + rules, + theme=None, + extra=None, + css=True, + xinclude=True, + absolute_prefix=None, + update=True, + trace=False, + includemode=None, + parser=None, + compiler_parser=None, + rules_parser=None, + access_control=None, + read_network=False, + indent=None, + xsl_params=None, + runtrace=False, +): """Invoke the diazo compiler. * ``rules`` is the rules file @@ -120,7 +148,8 @@ def compile_theme(rules, theme=None, extra=None, css=True, xinclude=True, # Create a pseudo resolver for this known_params_url = 'file:///__diazo_known_params__' emit_stylesheet_resolver = CustomResolver({ - known_params_url: etree.tostring(known_params)}) + known_params_url: etree.tostring(known_params), + }) emit_stylesheet_parser = etree.XMLParser() emit_stylesheet_parser.resolvers.add(emit_stylesheet_resolver) @@ -133,10 +162,15 @@ def compile_theme(rules, theme=None, extra=None, css=True, xinclude=True, # Run the final stage compiler emit_stylesheet = pkg_xsl( - 'emit-stylesheet.xsl', parser=emit_stylesheet_parser) + 'emit-stylesheet.xsl', + parser=emit_stylesheet_parser, + ) compiled_doc = emit_stylesheet(rules_doc, **params) - compiled_doc = set_parser(etree.tostring(compiled_doc), parser, - compiler_parser) + compiled_doc = set_parser( + etree.tostring(compiled_doc), + parser, + compiler_parser, + ) return compiled_doc @@ -153,9 +187,9 @@ def main(): elif len(args) == 1: options.rules, = args else: - parser.error("Wrong number of arguments.") + parser.error('Wrong number of arguments.') elif args: - parser.error("Wrong number of arguments.") + parser.error('Wrong number of arguments.') if options.trace: logger.setLevel(logging.DEBUG) @@ -172,13 +206,16 @@ def main(): absolute_prefix=options.absolute_prefix, includemode=options.includemode, read_network=options.read_network, - xsl_params=xsl_params + xsl_params=xsl_params, ) root = output_xslt.getroot() if not root.tail: root.tail = '\n' - output_xslt.write(options.output, encoding='utf-8', - pretty_print=options.pretty_print) + output_xslt.write( + options.output, + encoding='utf-8', + pretty_print=options.pretty_print, + ) if __name__ == '__main__': diff --git a/lib/diazo/cssrules.py b/lib/diazo/cssrules.py index aae4d98..d352486 100644 --- a/lib/diazo/cssrules.py +++ b/lib/diazo/cssrules.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- """\ Usage: %prog RULES @@ -9,13 +10,16 @@ \ """ + from __future__ import absolute_import -from optparse import OptionParser -from lxml import etree from cssselect import GenericTranslator -from . import utils -import sys +from diazo import utils +from lxml import etree +from optparse import OptionParser + import logging +import sys + logger = logging.getLogger('diazo') usage = __doc__ @@ -34,28 +38,44 @@ def xpath_descendant_combinator(self, left, right): def convert_css_selectors(rules): """Convert css rules to xpath rules element tree in place """ - # XXX: There is a - # :root pseudo-class - http://www.w3.org/TR/css3-selectors/#root-pseudo + # XXX: There is a :root pseudo-class + # http://www.w3.org/TR/css3-selectors/#root-pseudo # We may wish to add support to lxml.cssselect for it some day. - for element in rules.xpath("//@*[namespace-uri()='%s']/.." % - utils.namespaces['css']): + for element in rules.xpath( + "//@*[namespace-uri()='{nsp}']/..".format(nsp=utils.namespaces['css']), + ): tag_namespace = utils.namespace(element.tag) - css_prefix = element.attrib.get(utils.fullname(utils.namespaces['css'], - 'prefix'), None) + css_prefix = element.attrib.get( + utils.fullname( + utils.namespaces['css'], + 'prefix', + ), + None, + ) for name, value in element.attrib.items(): - if not name.startswith('{%s}' % utils.namespaces['css']): + if not name.startswith( + '{%s}' % utils.namespaces['css'], + ): continue localname = utils.localname(name) if localname == 'prefix': continue if not value: - element.attrib[localname] = "" + element.attrib[localname] = '' continue - if (tag_namespace == utils.namespaces['diazo'] and - localname in ('content', 'content-children', 'if-content', - 'if-not-content') or - (tag_namespace == utils.namespaces['xsl'] and - localname in ('match',))): + if ( + tag_namespace == utils.namespaces['diazo'] and + localname in + ( + 'content', + 'content-children', + 'if-content', + 'if-not-content', + ) or ( + tag_namespace == utils.namespaces['xsl'] and + localname in ('match',) + ) + ): prefix = css_prefix or '//' tr = _location_path_translator else: @@ -70,16 +90,26 @@ def main(): """Called from console script """ parser = OptionParser(usage=usage) - parser.add_option("-o", "--output", metavar="output.html", - help="Output filename (instead of stdout)", - dest="output", default=sys.stdout) - parser.add_option("-p", "--pretty-print", action="store_true", - help="Pretty print output", - dest="pretty_print", default=False) + parser.add_option( + '-o', + '--output', + metavar='output.html', + help='Output filename (instead of stdout)', + dest='output', + default=sys.stdout, + ) + parser.add_option( + '-p', + '--pretty-print', + action='store_true', + help='Pretty print output', + dest='pretty_print', + default=False, + ) (options, args) = parser.parse_args() if len(args) != 1: - parser.error("Invalid number of arguments") + parser.error('Invalid number of arguments') rules = etree.parse(args[0]) convert_css_selectors(rules) rules.write(options.output, pretty_print=options.pretty_print) diff --git a/lib/diazo/defaults.xsl b/lib/diazo/defaults.xsl index bf5bc65..9d81b91 100644 --- a/lib/diazo/defaults.xsl +++ b/lib/diazo/defaults.xsl @@ -10,7 +10,10 @@ xmlns:xhtml="http://www.w3.org/1999/xhtml" exclude-result-prefixes="exsl str css dv dyn xhtml"> - / + + + / + nodes with the theme html. """ if absolute_prefix is None: @@ -115,13 +136,18 @@ def expand_themes(rules_doc, parser=None, absolute_prefix=None, base = rules_doc.docinfo.URL if parser is None: parser = etree.HTMLParser() - for element in rules_doc.xpath('//diazo:theme[@href]', - namespaces=namespaces): + for element in rules_doc.xpath( + '//diazo:theme[@href]', + namespaces=namespaces, + ): url = urljoin(base, element.get('href')) if not read_network and \ url.startswith(('ftp://', 'ftps://', 'http://', 'https://')): - raise ValueError("Supplied theme '%s', " - "but network access denied." % url) + raise ValueError( + "Supplied theme '{url}', but network access denied.".format( + url=url, + ), + ) elif read_network and \ url.startswith(('ftp://', 'ftps://', 'http://', 'https://')): theme = urlopen(url) @@ -174,19 +200,29 @@ def apply_absolute_prefix(theme_doc, absolute_prefix): def add_extra(rules_doc, extra): root = rules_doc.getroot() - extra_elements = extra.xpath('/xsl:stylesheet/xsl:*', - namespaces=namespaces) + extra_elements = extra.xpath( + '/xsl:stylesheet/xsl:*', + namespaces=namespaces, + ) root.extend(extra_elements) return rules_doc -def add_theme(rules_doc, theme, parser=None, absolute_prefix=None, - read_network=False): +def add_theme( + rules_doc, + theme, + parser=None, + absolute_prefix=None, + read_network=False, +): if not read_network and \ isinstance(theme, string_types) and \ theme[:6] in ('ftp://', 'http:/', 'https:'): - raise ValueError("Supplied theme '%s', " - "but network access denied." % theme) + raise ValueError( + "Supplied theme '{theme}', but network access denied.".format( + theme=theme, + ), + ) if absolute_prefix is None: absolute_prefix = '' if parser is None: @@ -206,14 +242,27 @@ def fixup_theme_comment_selectors(rules): """ for element in rules.xpath("//@theme[contains(., 'comment()')]/.."): element.attrib['theme'] = element.attrib['theme'].replace( - 'comment()', 'xsl:comment') + 'comment()', + 'xsl:comment', + ) return rules -def process_rules(rules, theme=None, extra=None, trace=None, css=True, - xinclude=True, absolute_prefix=None, includemode=None, - update=True, parser=None, rules_parser=None, - read_network=False, stop=None): +def process_rules( + rules, + theme=None, + extra=None, + trace=None, + css=True, + xinclude=True, + absolute_prefix=None, + includemode=None, + update=True, + parser=None, + rules_parser=None, + read_network=False, + stop=None, +): if trace: trace = '1' else: @@ -247,8 +296,13 @@ def process_rules(rules, theme=None, extra=None, trace=None, css=True, return rules_doc rules_doc = expand_themes(rules_doc, parser, absolute_prefix, read_network) if theme is not None: - rules_doc = add_theme(rules_doc, theme, parser, absolute_prefix, - read_network) + rules_doc = add_theme( + rules_doc, + theme, + parser, + absolute_prefix, + read_network, + ) if stop == 6: return rules_doc if includemode is None: @@ -283,9 +337,15 @@ def main(): """Called from console script """ parser = _createOptionParser(usage=usage) - parser.add_option("-s", "--stop", metavar="n", type="int", - help="Stop preprocessing at stage n", - dest="stop", default=None) + parser.add_option( + '-s', + '--stop', + metavar='n', + type='int', + help='Stop preprocessing at stage n', + dest='stop', + default=None, + ) (options, args) = parser.parse_args() if options.rules is None: @@ -294,9 +354,9 @@ def main(): elif len(args) == 1: options.rules, = args else: - parser.error("Wrong number of arguments.") + parser.error('Wrong number of arguments.') elif args: - parser.error("Wrong number of arguments.") + parser.error('Wrong number of arguments.') if options.trace: logger.setLevel(logging.DEBUG) diff --git a/lib/diazo/run.py b/lib/diazo/run.py index 10b685e..62264a7 100644 --- a/lib/diazo/run.py +++ b/lib/diazo/run.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- """\ Usage: %prog -x TRANSFORM CONTENT @@ -7,15 +8,21 @@ Usage: %prog -r RULES [options] CONTENT """ -import logging -import sys -import os.path + +from diazo.compiler import compile_theme +from diazo.utils import _createOptionParser +from diazo.utils import AC_READ_FILE +from diazo.utils import AC_READ_NET +from diazo.utils import quote_param +from diazo.utils import split_params from lxml import etree from six import string_types -from diazo.compiler import compile_theme -from diazo.utils import AC_READ_NET, AC_READ_FILE, _createOptionParser -from diazo.utils import split_params, quote_param + import diazo.runtrace +import logging +import os.path +import sys + logger = logging.getLogger('diazo') usage = __doc__ @@ -39,38 +46,49 @@ def main(): """Called from console script """ op = _createOptionParser(usage=usage) - op.add_option("-x", "--xsl", - metavar="transform.xsl", - help="XSL transform", - dest="xsl", - default=None) - op.add_option("--path", - metavar="PATH", - help="URI path", - dest="path", - default=None) - op.add_option("--parameters", - metavar="param1=val1,param2=val2", - help="Set the values of arbitrary parameters", - dest="parameters", - default=None) - op.add_option("--runtrace-xml", - metavar="runtrace.xml", - help="Write an xml format runtrace to file", - dest="runtrace_xml", - default=None) - op.add_option("--runtrace-html", - metavar="runtrace.html", - help="Write an html format runtrace to file", - dest="runtrace_html", - default=None) + op.add_option( + '-x', + '--xsl', + metavar='transform.xsl', + help='XSL transform', + dest='xsl', + default=None, + ) + op.add_option( + '--path', + metavar='PATH', + help='URI path', + dest='path', + default=None, + ) + op.add_option( + '--parameters', + metavar='param1=val1,param2=val2', + help='Set the values of arbitrary parameters', + dest='parameters', + default=None, + ) + op.add_option( + '--runtrace-xml', + metavar='runtrace.xml', + help='Write an xml format runtrace to file', + dest='runtrace_xml', + default=None, + ) + op.add_option( + '--runtrace-html', + metavar='runtrace.html', + help='Write an html format runtrace to file', + dest='runtrace_html', + default=None, + ) (options, args) = op.parse_args() if len(args) > 2: - op.error("Wrong number of arguments.") + op.error('Wrong number of arguments.') elif len(args) == 2: if options.xsl or options.rules: - op.error("Wrong number of arguments.") + op.error('Wrong number of arguments.') path, content = args if path.lower().endswith('.xsl'): options.xsl = path @@ -79,9 +97,9 @@ def main(): elif len(args) == 1: content, = args else: - op.error("Wrong number of arguments.") + op.error('Wrong number of arguments.') if options.rules is None and options.xsl is None: - op.error("Must supply either options or rules") + op.error('Must supply either options or rules') if options.trace: logger.setLevel(logging.DEBUG) @@ -96,7 +114,6 @@ def main(): if options.xsl is not None: output_xslt = etree.parse(options.xsl) else: - xsl_params = None if options.xsl_params: xsl_params = split_params(options.xsl_params) @@ -126,7 +143,7 @@ def main(): content_doc = etree.parse(content, parser=parser) params = {} if options.path is not None: - params['path'] = "'%s'" % options.path + params['path'] = "'{path}'".format(path=options.path) if options.parameters: for key, value in split_params(options.parameters).items(): @@ -142,14 +159,18 @@ def main(): if runtrace: runtrace_doc = diazo.runtrace.generate_runtrace( rules=options.rules, - error_log=transform.error_log) + error_log=transform.error_log, + ) if options.runtrace_xml: if options.runtrace_xml == '-': out = sys.stdout else: out = open(options.runtrace_xml, 'wt') - runtrace_doc.write(out, encoding='utf-8', - pretty_print=options.pretty_print) + runtrace_doc.write( + out, + encoding='utf-8', + pretty_print=options.pretty_print, + ) if options.runtrace_html: if options.runtrace_html == '-': out = sys.stdout diff --git a/lib/diazo/runtrace.py b/lib/diazo/runtrace.py index bdf58a5..30686fa 100644 --- a/lib/diazo/runtrace.py +++ b/lib/diazo/runtrace.py @@ -1,11 +1,13 @@ #!/usr/bin/env python -import logging +# -*- coding: utf-8 -*- from diazo.rules import process_rules from diazo.utils import pkg_xsl - from lxml import etree +import logging + + logger = logging.getLogger('diazo') _runtrace_to_html = pkg_xsl('runtrace_to_html.xsl') @@ -14,8 +16,12 @@ def log_to_xml_string(error_log): return """ %s - """ % "".join(l.message for l in error_log - if l.message.startswith('<rules""", html_string) + b"""title="Matches: if-content:true "><rules""", + html_string, + ) # HTML comments are included and escaped - self.assertIn(b"""<!-- Rules, lots of rules -->""", html_string) + self.assertIn( + b"""<!-- Rules, lots of rules -->""", + html_string, + ) # Rules tag has children - self.assertIn(b"""<rules """ - b"""xml:id="r4">""", - html_string) + self.assertIn( + b"""<rules """ + b"""xml:id="r4">""", + html_string, + ) # Theme tag has no conditions, is a singleton - self.assertIn(b"""<theme href="index.html" xml:id="r1"/>""", - html_string) + self.assertIn( + b"""<theme href="index.html" xml:id="r1"/>""", + html_string, + ) # Whitespace is preserved - self.assertIn(b"""xml:id=\"r4\">\n <!-- Rules, lots of rules """ - b"""-->""", html_string) + self.assertIn( + b"""xml:id=\"r4\">\n <!-- Rules, lots of rules """ + b"""-->""", + html_string, + ) # Neither theme or content matched - self.assertIn(b"""<copy """ - b"""xml:id="r5"""", html_string) + self.assertIn( + b"""<copy """ + b"""xml:id="r5"""", + html_string, + ) # Just content matched, still not good enough - self.assertIn(b"""<copy """ - b"""xml:id="r6"""", html_string) + self.assertIn( + b"""<copy """ + b"""xml:id="r6"""", + html_string, + ) # Full match - self.assertIn(b"""<copy """ - b"""xml:id="r7"""", html_string) + self.assertIn( + b"""<copy """ + b"""xml:id="r7"""", + html_string, + ) # More than one match still fine - self.assertIn(b"""<copy """ - b"""xml:id="r8"""", html_string) + self.assertIn( + b"""<copy """ + b"""xml:id="r8"""", + html_string, + ) def assertXPath(self, doc, xpath, expected): self.assertEqual( doc.xpath( xpath, namespaces=(dict(d="http://namespaces.plone.org/diazo")))[0], - expected + expected, ) diff --git a/lib/diazo/tests/test_wsgi.py b/lib/diazo/tests/test_wsgi.py index 83dd773..a1a287f 100644 --- a/lib/diazo/tests/test_wsgi.py +++ b/lib/diazo/tests/test_wsgi.py @@ -1,5 +1,8 @@ -import sys +# -*- coding: utf-8 -*- + import os.path +import sys + try: import unittest2 as unittest @@ -11,8 +14,13 @@ def testfile(filename): - return '/'.join(('file://',) + os.path.split(os.path.abspath( - os.path.dirname(__file__))) + ('test_wsgi_files', filename,)) + return '/'.join( + ('file://',) + os.path.split( + os.path.abspath( + os.path.dirname(__file__), + ), + ) + ('test_wsgi_files', filename,), + ) HTML = b"""\ @@ -166,16 +174,23 @@ def application(environ, start_response): start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application, {}, filename=filename) + app = XSLTMiddleware( + application, + {}, + filename=filename, + ) os.unlink(filename) request = Request.blank('/') response = request.get_response(app) - self.assertEqual(response.headers['Content-Type'], - 'text/html; charset=UTF-8') + self.assertEqual( + response.headers['Content-Type'], + 'text/html; charset=UTF-8', + ) self.assertTrue( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) def test_transform_tree(self): @@ -190,15 +205,22 @@ def application(environ, start_response): start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application, {}, tree=etree.fromstring(XSLT)) + app = XSLTMiddleware( + application, + {}, + tree=etree.fromstring(XSLT), + ) request = Request.blank('/') response = request.get_response(app) - self.assertEqual(response.headers['Content-Type'], - 'text/html; charset=UTF-8') + self.assertEqual( + response.headers['Content-Type'], + 'text/html; charset=UTF-8', + ) self.assertTrue( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) def test_head_request(self): @@ -209,12 +231,18 @@ def test_head_request(self): def application(environ, start_response): status = '200 OK' - response_headers = [('Content-Type', 'text/html'), - ('Content-Length', str(len(HTML)))] + response_headers = [ + ('Content-Type', 'text/html'), + ('Content-Length', str(len(HTML))), + ] start_response(status, response_headers) return [''] # Empty response for HEAD request - app = XSLTMiddleware(application, {}, tree=etree.fromstring(XSLT)) + app = XSLTMiddleware( + application, + {}, + tree=etree.fromstring(XSLT), + ) env = dict(REQUEST_METHOD='HEAD') request = Request.blank('/', environ=env) @@ -233,13 +261,19 @@ def test_update_content_length(self): def application(environ, start_response): status = '200 OK' - response_headers = [('Content-Type', 'text/html'), - ('Content-Length', str(len(HTML)))] + response_headers = [ + ('Content-Type', 'text/html'), + ('Content-Length', str(len(HTML))), + ] start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application, {}, tree=etree.fromstring(XSLT), - update_content_length=True) + app = XSLTMiddleware( + application, + {}, + tree=etree.fromstring(XSLT), + update_content_length=True, + ) request = Request.blank('/') response = request.get_response(app) @@ -254,12 +288,18 @@ def test_dont_update_content_length(self): def application(environ, start_response): status = '200 OK' - response_headers = [('Content-Type', 'text/html'), - ('Content-Length', '1')] + response_headers = [ + ('Content-Type', 'text/html'), + ('Content-Length', '1'), + ] start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application, {}, tree=etree.fromstring(XSLT)) + app = XSLTMiddleware( + application, + {}, + tree=etree.fromstring(XSLT), + ) request = Request.blank('/') response = request.get_response(app) @@ -274,13 +314,19 @@ def test_content_length_zero(self): def application(environ, start_response): status = '200 OK' - response_headers = [('Content-Type', 'text/html'), - ('Content-Length', '0')] + response_headers = [ + ('Content-Type', 'text/html'), + ('Content-Length', '0'), + ] start_response(status, response_headers) return [''] - app = XSLTMiddleware(application, {}, tree=etree.fromstring(XSLT), - update_content_length=True) + app = XSLTMiddleware( + application, + {}, + tree=etree.fromstring(XSLT), + update_content_length=True, + ) request = Request.blank('/') response = request.get_response(app) @@ -295,20 +341,27 @@ def test_content_empty(self): def application(environ, start_response): status = '200 OK' - response_headers = [('Content-Type', 'text/html'), - ('Content-MD5', - 'd41d8cd98f00b204e9800998ecf8427e')] + response_headers = [ + ('Content-Type', 'text/html'), + ('Content-MD5', 'd41d8cd98f00b204e9800998ecf8427e'), + ] start_response(status, response_headers) return [b''] - app = XSLTMiddleware(application, {}, tree=etree.fromstring(XSLT), - update_content_length=True) + app = XSLTMiddleware( + application, + {}, + tree=etree.fromstring(XSLT), + update_content_length=True, + ) request = Request.blank('/') response = request.get_response(app) - self.assertEqual(response.headers['Content-MD5'], - 'd41d8cd98f00b204e9800998ecf8427e') + self.assertEqual( + response.headers['Content-MD5'], + 'd41d8cd98f00b204e9800998ecf8427e', + ) def test_content_range(self): from lxml import etree @@ -319,16 +372,24 @@ def test_content_range(self): def application(environ, start_response): status = '200 OK' content_length = len(HTML) - content_range = 'bytes %d-%d/%d' % (0, - content_length - 1, - content_length) - response_headers = [('Content-Type', 'text/html'), - ('Content-Range', content_range), - ('Content-Length', str(content_length))] + content_range = 'bytes %d-%d/%d' % ( + 0, + content_length - 1, + content_length, + ) + response_headers = [ + ('Content-Type', 'text/html'), + ('Content-Range', content_range), + ('Content-Length', str(content_length)), + ] start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application, {}, tree=etree.fromstring(XSLT)) + app = XSLTMiddleware( + application, + {}, + tree=etree.fromstring(XSLT), + ) request = Request.blank('/') response = request.get_response(app) @@ -347,8 +408,12 @@ def application(environ, start_response): start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application, {}, tree=etree.fromstring(XSLT), - set_content_length=False) + app = XSLTMiddleware( + application, + {}, + tree=etree.fromstring(XSLT), + set_content_length=False, + ) request = Request.blank('/') response = request.get_response(app) @@ -367,13 +432,19 @@ def application(environ, start_response): start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application, {}, tree=etree.fromstring(XSLT)) + app = XSLTMiddleware( + application, + {}, + tree=etree.fromstring(XSLT), + ) request = Request.blank('/') response = request.get_response(app) - self.assertEqual(response.headers['Content-Type'], - 'text/html; charset=UTF-8') + self.assertEqual( + response.headers['Content-Type'], + 'text/html; charset=UTF-8', + ) def test_doctype_xhtml(self): from lxml import etree @@ -387,14 +458,19 @@ def application(environ, start_response): start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application, {}, - tree=etree.fromstring(XSLT_XHTML)) + app = XSLTMiddleware( + application, + {}, + tree=etree.fromstring(XSLT_XHTML), + ) request = Request.blank('/') response = request.get_response(app) - self.assertEqual(response.headers['Content-Type'], - 'application/xhtml+xml; charset=UTF-8') + self.assertEqual( + response.headers['Content-Type'], + 'application/xhtml+xml; charset=UTF-8', + ) def test_doctype_html5(self): from lxml import etree @@ -408,14 +484,17 @@ def application(environ, start_response): start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application, {}, - tree=etree.fromstring(XSLT_XHTML), - doctype="") + app = XSLTMiddleware( + application, + {}, + tree=etree.fromstring(XSLT_XHTML), + doctype='', + ) request = Request.blank('/') response = request.get_response(app) - self.assertTrue(response.body.startswith(b"\n\nContent content' in response.body) + b'
Content content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) def test_diazo_off_request_header(self): @@ -456,7 +540,11 @@ def application(environ, start_response): start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application, {}, tree=etree.fromstring(XSLT)) + app = XSLTMiddleware( + application, + {}, + tree=etree.fromstring(XSLT), + ) request = Request.blank('/') request.headers['X-Diazo-Off'] = 'yes' @@ -469,7 +557,8 @@ def application(environ, start_response): response = request.get_response(app) self.assertTrue( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) def test_diazo_off_response_header(self): @@ -480,8 +569,10 @@ def test_diazo_off_response_header(self): def application1(environ, start_response): status = '200 OK' - response_headers = [('Content-Type', 'text/html'), - ('X-Diazo-Off', 'yes')] + response_headers = [ + ('Content-Type', 'text/html'), + ('X-Diazo-Off', 'yes'), + ] start_response(status, response_headers) return [HTML] @@ -494,18 +585,25 @@ def application1(environ, start_response): def application2(environ, start_response): status = '200 OK' - response_headers = [('Content-Type', 'text/html'), - ('X-Diazo-Off', 'no')] + response_headers = [ + ('Content-Type', 'text/html'), + ('X-Diazo-Off', 'no'), + ] start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application2, {}, tree=etree.fromstring(XSLT)) + app = XSLTMiddleware( + application2, + {}, + tree=etree.fromstring(XSLT), + ) request = Request.blank('/') response = request.get_response(app) self.assertTrue( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) def test_non_html_content_type(self): @@ -520,7 +618,11 @@ def application1(environ, start_response): start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application1, {}, tree=etree.fromstring(XSLT)) + app = XSLTMiddleware( + application1, + {}, + tree=etree.fromstring(XSLT), + ) request = Request.blank('/') response = request.get_response(app) @@ -533,13 +635,18 @@ def application2(environ, start_response): start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application2, {}, tree=etree.fromstring(XSLT)) + app = XSLTMiddleware( + application2, + {}, + tree=etree.fromstring(XSLT), + ) request = Request.blank('/') response = request.get_response(app) self.assertTrue( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) def test_content_encoding(self): @@ -550,8 +657,10 @@ def test_content_encoding(self): def application1(environ, start_response): status = '200 OK' - response_headers = [('Content-Type', 'text/html'), - ('Content-Encoding', 'zip')] + response_headers = [ + ('Content-Type', 'text/html'), + ('Content-Encoding', 'zip'), + ] start_response(status, response_headers) return [HTML] @@ -568,13 +677,18 @@ def application2(environ, start_response): start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application2, {}, tree=etree.fromstring(XSLT)) + app = XSLTMiddleware( + application2, + {}, + tree=etree.fromstring(XSLT), + ) request = Request.blank('/') response = request.get_response(app) self.assertTrue( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) def test_301(self): @@ -589,7 +703,11 @@ def application1(environ, start_response): start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application1, {}, tree=etree.fromstring(XSLT)) + app = XSLTMiddleware( + application1, + {}, + tree=etree.fromstring(XSLT), + ) request = Request.blank('/') response = request.get_response(app) @@ -602,13 +720,18 @@ def application2(environ, start_response): start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application2, {}, tree=etree.fromstring(XSLT)) + app = XSLTMiddleware( + application2, + {}, + tree=etree.fromstring(XSLT), + ) request = Request.blank('/') response = request.get_response(app) self.assertTrue( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) def test_302(self): @@ -623,7 +746,11 @@ def application1(environ, start_response): start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application1, {}, tree=etree.fromstring(XSLT)) + app = XSLTMiddleware( + application1, + {}, + tree=etree.fromstring(XSLT), + ) request = Request.blank('/') response = request.get_response(app) @@ -636,13 +763,18 @@ def application2(environ, start_response): start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application2, {}, tree=etree.fromstring(XSLT)) + app = XSLTMiddleware( + application2, + {}, + tree=etree.fromstring(XSLT), + ) request = Request.blank('/') response = request.get_response(app) self.assertTrue( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) def test_304(self): @@ -676,7 +808,8 @@ def application2(environ, start_response): response = request.get_response(app) self.assertTrue( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) def test_204(self): @@ -710,7 +843,8 @@ def application2(environ, start_response): response = request.get_response(app) self.assertTrue( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) def test_401(self): @@ -725,7 +859,11 @@ def application1(environ, start_response): start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application1, {}, tree=etree.fromstring(XSLT)) + app = XSLTMiddleware( + application1, + {}, + tree=etree.fromstring(XSLT), + ) request = Request.blank('/') response = request.get_response(app) @@ -738,13 +876,18 @@ def application2(environ, start_response): start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application2, {}, tree=etree.fromstring(XSLT)) + app = XSLTMiddleware( + application2, + {}, + tree=etree.fromstring(XSLT), + ) request = Request.blank('/') response = request.get_response(app) self.assertTrue( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) def test_html_serialization(self): @@ -766,11 +909,15 @@ def application(environ, start_response): # HTML serialisation self.assertTrue( b'' in response.body) + b'"http://www.w3.org/TR/html4/strict.dtd">' in response.body, + ) self.assertTrue(b'
' in response.body) - app = XSLTMiddleware(application, {}, - tree=etree.fromstring(XSLT_XHTML)) + app = XSLTMiddleware( + application, + {}, + tree=etree.fromstring(XSLT_XHTML), + ) request = Request.blank('/') response = request.get_response(app) @@ -778,11 +925,15 @@ def application(environ, start_response): self.assertTrue( b'' - in response.body) + in response.body, + ) self.assertTrue(b'
' in response.body) - app = XSLTMiddleware(application, {}, - tree=etree.fromstring(XSLT_HTML5)) + app = XSLTMiddleware( + application, + {}, + tree=etree.fromstring(XSLT_HTML5), + ) request = Request.blank('/') response = request.get_response(app) @@ -803,8 +954,11 @@ def application(environ, start_response): return [HTML] app = XSLTMiddleware( - application, {}, tree=etree.fromstring(XSLT_PARAM), - environ_param_map={'test.param1': 'someparam'}) + application, + {}, + tree=etree.fromstring(XSLT_PARAM), + environ_param_map={'test.param1': 'someparam'}, + ) request = Request.blank('/') response = request.get_response(app) @@ -829,16 +983,22 @@ def application(environ, start_response): start_response(status, response_headers) return [HTML] - app = XSLTMiddleware(application, {}, - tree=etree.fromstring(XSLT_PARAM)) + app = XSLTMiddleware( + application, + {}, + tree=etree.fromstring(XSLT_PARAM), + ) request = Request.blank('/') response = request.get_response(app) self.assertTrue(b'

defaultvalue

' in response.body) - app = XSLTMiddleware(application, {}, - tree=etree.fromstring(XSLT_PARAM), - someparam='value1') + app = XSLTMiddleware( + application, + {}, + tree=etree.fromstring(XSLT_PARAM), + someparam='value1', + ) request = Request.blank('/') response = request.get_response(app) @@ -857,15 +1017,20 @@ def application(environ, start_response): start_response(status, response_headers) return [HTML] - app = DiazoMiddleware(application, {}, - testfile('simple_transform.xml')) + app = DiazoMiddleware( + application, + {}, + testfile('simple_transform.xml'), + ) request = Request.blank('/') response = request.get_response(app) self.assertTrue( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertFalse( - b'
Theme content
' in response.body) + b'
Theme content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) def test_doctype_html5(self): @@ -878,13 +1043,16 @@ def application(environ, start_response): start_response(status, response_headers) return [HTML] - app = DiazoMiddleware(application, {}, - testfile('simple_transform.xml'), - doctype="") + app = DiazoMiddleware( + application, + {}, + testfile('simple_transform.xml'), + doctype='', + ) request = Request.blank('/') response = request.get_response(app) - self.assertTrue(response.body.startswith(b"\n\nContent content' in response.body) + b'
Content content
' in response.body, + ) self.assertFalse( - b'
Theme content
' in response.body) + b'
Theme content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) def test_absolute_prefix(self): @@ -917,33 +1091,44 @@ def application(environ, start_response): start_response(status, response_headers) return [HTML] - app = DiazoMiddleware(application, {}, - testfile('simple_transform.xml')) + app = DiazoMiddleware( + application, + {}, + testfile('simple_transform.xml'), + ) request = Request.blank('/') response = request.get_response(app) self.assertTrue( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertFalse( - b'
Theme content
' in response.body) + b'
Theme content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) self.assertTrue( - b'' in response.body) - - app = DiazoMiddleware(application, {}, - testfile('simple_transform.xml'), - prefix='/static') + b'' in response.body, + ) + + app = DiazoMiddleware( + application, {}, + testfile('simple_transform.xml'), + prefix='/static', + ) request = Request.blank('/') response = request.get_response(app) self.assertTrue( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertFalse( - b'
Theme content
' in response.body) + b'
Theme content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) self.assertTrue( b'' - in response.body) + in response.body, + ) def test_path_param(self): from diazo.wsgi import DiazoMiddleware @@ -960,18 +1145,22 @@ def application(environ, start_response): response = request.get_response(app) self.assertFalse( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertTrue( - b'
Theme content
' in response.body) + b'
Theme content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) request = Request.blank('/index.html') response = request.get_response(app) self.assertTrue( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertFalse( - b'
Theme content
' in response.body) + b'
Theme content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) def test_custom_environ_param(self): @@ -984,16 +1173,22 @@ def application(environ, start_response): start_response(status, response_headers) return [HTML] - app = DiazoMiddleware(application, {}, testfile('custom_param.xml'), - environ_param_map={'test.param1': 'someparam'}) + app = DiazoMiddleware( + application, + {}, + testfile('custom_param.xml'), + environ_param_map={'test.param1': 'someparam'}, + ) request = Request.blank('/') response = request.get_response(app) self.assertFalse( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertTrue( - b'
Theme content
' in response.body) + b'
Theme content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) request = Request.blank('/') @@ -1001,9 +1196,11 @@ def application(environ, start_response): response = request.get_response(app) self.assertTrue( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertFalse( - b'
Theme content
' in response.body) + b'
Theme content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) request = Request.blank('/') @@ -1011,9 +1208,11 @@ def application(environ, start_response): response = request.get_response(app) self.assertFalse( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertTrue( - b'
Theme content
' in response.body) + b'
Theme content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) def test_custom_param(self): @@ -1026,26 +1225,38 @@ def application(environ, start_response): start_response(status, response_headers) return [HTML] - app = DiazoMiddleware(application, {}, testfile('custom_param.xml'), - someparam='value1') + app = DiazoMiddleware( + application, + {}, + testfile('custom_param.xml'), + someparam='value1', + ) request = Request.blank('/') response = request.get_response(app) self.assertTrue( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertFalse( - b'
Theme content
' in response.body) + b'
Theme content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) - app = DiazoMiddleware(application, {}, testfile('custom_param.xml'), - someparam='value2') + app = DiazoMiddleware( + application, + {}, + testfile('custom_param.xml'), + someparam='value2', + ) request = Request.blank('/') response = request.get_response(app) self.assertFalse( - b'
Content content
' in response.body) + b'
Content content
' in response.body, + ) self.assertTrue( - b'
Theme content
' in response.body) + b'
Theme content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) def test_subrequest(self): @@ -1068,9 +1279,11 @@ def application(environ, start_response): response = request.get_response(app) self.assertTrue( - b'
Alternative content
' in response.body) + b'
Alternative content
' in response.body, + ) self.assertFalse( - b'
Theme content
' in response.body) + b'
Theme content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) def test_esi(self): @@ -1088,25 +1301,35 @@ def application(environ, start_response): else: return [HTML] - app = DiazoMiddleware(application, {}, testfile('esi.xml'), - filter_xpath=True) + app = DiazoMiddleware( + application, + {}, + testfile('esi.xml'), + filter_xpath=True, + ) request = Request.blank('/') response = request.get_response(app) - self.assertTrue(b'''''' - b'''''' in response.body) + self.assertTrue( + b"""""" + b"""""" in response.body, + ) self.assertFalse( - b'
Theme content
' in response.body) + b'
Theme content
' in response.body, + ) self.assertTrue(b'Transformed' in response.body) request = Request.blank( - '''/other.html?;filter_xpath=//*[@id%20=%20'content']''') + """/other.html?;filter_xpath=//*[@id%20=%20'content']""", + ) response = request.get_response(app) # Strip response body in this test due too # https://bugzilla.gnome.org/show_bug.cgi?id=652766 - self.assertEqual(b'
Alternative content
', - response.body.strip()) + self.assertEqual( + b'
Alternative content
', + response.body.strip(), + ) def test_suite(): diff --git a/lib/diazo/utils.py b/lib/diazo/utils.py index deb7d35..c159120 100644 --- a/lib/diazo/utils.py +++ b/lib/diazo/utils.py @@ -1,10 +1,15 @@ +# -*- coding: utf-8 -*- + +from lxml import etree +from optparse import OptionParser +from six import integer_types +from six import PY3 +from six import string_types + import logging import pkg_resources import sys -from lxml import etree -from optparse import OptionParser -from six import string_types, integer_types, PY3 if PY3: stdout = sys.stdout.buffer @@ -16,14 +21,14 @@ logger = logging.getLogger('diazo') namespaces = dict( - diazo="http://namespaces.plone.org/diazo", - css="http://namespaces.plone.org/diazo/css", - old1="http://openplans.org/deliverance", - old2="http://namespaces.plone.org/xdv", - oldcss1="http://namespaces.plone.org/xdv+css", - oldcss2="http://namespaces.plone.org/diazo+css", - xml="http://www.w3.org/XML/1998/namespace", - xsl="http://www.w3.org/1999/XSL/Transform", + diazo='http://namespaces.plone.org/diazo', + css='http://namespaces.plone.org/diazo/css', + old1='http://openplans.org/deliverance', + old2='http://namespaces.plone.org/xdv', + oldcss1='http://namespaces.plone.org/xdv+css', + oldcss2='http://namespaces.plone.org/diazo+css', + xml='http://www.w3.org/XML/1998/namespace', + xsl='http://www.w3.org/1999/XSL/Transform', ) @@ -40,11 +45,19 @@ def fullname(namespace, name): AC_READ_FILE = etree.XSLTAccessControl( - read_file=True, write_file=False, create_dir=False, read_network=False, - write_network=False) + read_file=True, + write_file=False, + create_dir=False, + read_network=False, + write_network=False, +) AC_READ_NET = etree.XSLTAccessControl( - read_file=True, write_file=False, create_dir=False, read_network=True, - write_network=False) + read_file=True, + write_file=False, + create_dir=False, + read_network=True, + write_network=False, +) class CustomResolver(etree.Resolver): @@ -96,7 +109,7 @@ def quote_param(value): elif value is None: return '/..' else: - raise ValueError("Cannot convert %s", value) + raise ValueError('Cannot convert %s', value) def split_params(s): @@ -112,40 +125,87 @@ def split_params(s): def _createOptionParser(usage): parser = OptionParser(usage=usage) - parser.add_option("-o", "--output", metavar="output.xsl", - help="Output filename (instead of stdout)", - dest="output", default=stdout) - parser.add_option("-p", "--pretty-print", action="store_true", - help="Pretty print output (may alter rendering in " - "browser)", - dest="pretty_print", default=False) - parser.add_option("--trace", action="store_true", - help="Compiler trace logging", - dest="trace", default=False) - parser.add_option("-a", "--absolute-prefix", metavar="/", - help="relative urls in the theme file will be made into " - "absolute links with this prefix.", - dest="absolute_prefix", default=None) - parser.add_option("-i", "--includemode", metavar="INC", - help="include mode (document, ssi, ssiwait or esi)", - dest="includemode", default=None) - parser.add_option("-n", "--network", action="store_true", - help="Allow reads to the network to fetch resources", - dest="read_network", default=False) - parser.add_option("-t", "--theme", metavar="theme.html", - help="Theme file", - dest="theme", default=None) - parser.add_option("-r", "--rules", metavar="rules.xml", - help="Diazo rules file", - dest="rules", default=None) - parser.add_option("-c", "--custom-parameters", - metavar="param1,param2=defaultval", - help="Comma-separated list of custom parameter names " - "with optional default values that the compiled " - "theme will be able accept when run", - dest="xsl_params", default=None) - parser.add_option("-e", "--extra", metavar="extra.xsl", - help="Extra XSL to be included in the transform " - "(depracated, use inline xsl in the rules instead)", - dest="extra", default=None) + parser.add_option( + '-o', + '--output', + metavar='output.xsl', + help='Output filename (instead of stdout)', + dest='output', + default=stdout, + ) + parser.add_option( + '-p', + '--pretty-print', + action='store_true', + help='Pretty print output (may alter rendering in browser)', + dest='pretty_print', + default=False, + ) + parser.add_option( + '--trace', + action='store_true', + help='Compiler trace logging', + dest='trace', + default=False, + ) + parser.add_option( + '-a', + '--absolute-prefix', + metavar='/', + help='relative urls in the theme file will be made into absolute ' + 'links with this prefix.', + dest='absolute_prefix', + default=None, + ) + parser.add_option( + '-i', + '--includemode', + metavar='INC', + help='include mode (document, ssi, ssiwait or esi)', + dest='includemode', + default=None, + ) + parser.add_option( + '-n', + '--network', + action='store_true', + help='Allow reads to the network to fetch resources', + dest='read_network', + default=False, + ) + parser.add_option( + '-t', + '--theme', + metavar='theme.html', + help='Theme file', + dest='theme', + default=None, + ) + parser.add_option( + '-r', + '--rules', + metavar='rules.xml', + help='Diazo rules file', + dest='rules', + default=None, + ) + parser.add_option( + '-c', + '--custom-parameters', + metavar='param1,param2=defaultval', + help='Comma-separated list of custom parameter names with optional ' + 'default values that the compiled theme will be able accept ' + 'when run', + dest='xsl_params', + default=None, + ) + parser.add_option( + '-e', + '--extra', + metavar='extra.xsl', + help='Extra XSL to be included in the transform ' + '(depracated, use inline xsl in the rules instead)', + dest='extra', + default=None, + ) return parser diff --git a/lib/diazo/wsgi.py b/lib/diazo/wsgi.py index eb696f1..604dcc4 100644 --- a/lib/diazo/wsgi.py +++ b/lib/diazo/wsgi.py @@ -1,21 +1,19 @@ -import re -import pkg_resources -import os.path +# -*- coding: utf-8 -*- +from diazo.compiler import compile_theme +from diazo.utils import pkg_parse +from diazo.utils import quote_param from future.moves.urllib.parse import unquote_plus - -from webob import Request - from lxml import etree - -from six import string_types - from repoze.xmliter.serializer import XMLSerializer from repoze.xmliter.utils import getHTMLSerializer +from six import string_types +from webob import Request + +import os.path +import pkg_resources +import re -from diazo.compiler import compile_theme -from diazo.utils import pkg_parse -from diazo.utils import quote_param DIAZO_OFF_HEADER = 'X-Diazo-Off' @@ -23,9 +21,9 @@ def asbool(value): if isinstance(value, string_types): value = value.strip().lower() - if value in ('true', 'yes', 'on', 'y', 't', '1',): + if value in ('true', 'yes', 'on', 'y', 't', '1', ): return True - elif value in ('false', 'no', 'off', 'n', 'f', '0'): + elif value in ('false', 'no', 'off', 'n', 'f', '0', ): return False else: raise ValueError("String is not true/false: %r" % value) @@ -96,8 +94,10 @@ def resolve(self, system_url, public_id, context): result = response.text - if response.content_type in ('text/javascript', - 'application/x-javascript'): + if response.content_type in ( + 'text/javascript', + 'application/x-javascript', + ): result = u''.join([ u'