Merge lp:~benji/lazr.restful/faster-wadl-generation into lp:lazr.restful

Proposed by Benji York
Status: Merged
Approved by: Benji York
Approved revision: 157
Merged at revision: 156
Proposed branch: lp:~benji/lazr.restful/faster-wadl-generation
Merge into: lp:lazr.restful
Diff against target: 101 lines (+58/-3)
3 files modified
src/lazr/restful/NEWS.txt (+6/-0)
src/lazr/restful/tales.py (+51/-2)
src/lazr/restful/version.txt (+1/-1)
To merge this branch: bzr merge lp:~benji/lazr.restful/faster-wadl-generation
Reviewer Review Type Date Requested Status
Leonard Richardson (community) Approve
Review via email: mp+41232@code.launchpad.net

Description of the change

Added an optimization to the WADL docstring handling that results in a 30%
decrease in WADL generation time for large files.

To post a comment you must log in.
Revision history for this message
Leonard Richardson (leonardr) wrote :

The change itself looks fine. We should try to get something like this into epydoc, but this is a big enough improvement to justify some temporary messiness.

But, the code is much messier than it needs to be. It's bad to have lots of code outside classes and function definitions, since it's difficult to pinpoint exactly where that code "lives". Can you contain all that code in a class called _PydocParser, then instantiate it as a top-level object called _PYDOC_PARSER and invoke its parse_docstring() method as needed.

review: Approve
Revision history for this message
Benji York (benji) wrote :

On Thu, Nov 18, 2010 at 4:23 PM, Leonard Richardson
<email address hidden> wrote:
> Review: Approve
> The change itself looks fine. We should try to get something like this
> into epydoc, but this is a big enough improvement to justify some
> temporary messiness.

Agreed. I'll be contacting the epydoc project.

> But, the code is much messier than it needs to be. It's bad to have
> lots of code outside classes and function definitions, since it's
> difficult to pinpoint exactly where that code "lives". Can you contain
> all that code in a class called _PydocParser, then instantiate it as a
> top-level object called  _PYDOC_PARSER and invoke its
> parse_docstring() method as needed.

Done.
--
Benji York

157. By Benji York

encapsulate the various bits needed for the parse_docstring replacement

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/lazr/restful/NEWS.txt'
2--- src/lazr/restful/NEWS.txt 2010-10-25 16:31:09 +0000
3+++ src/lazr/restful/NEWS.txt 2010-11-18 22:14:26 +0000
4@@ -2,6 +2,12 @@
5 NEWS for lazr.restful
6 =====================
7
8+0.15.0 (unreleased)
9+===================
10+
11+Added an optimization to the WADL docstring handling that results in a 30%
12+decrease in WADL generation time for large files.
13+
14 0.14.1 (2010-10-24)
15 ===================
16
17
18=== modified file 'src/lazr/restful/tales.py'
19--- src/lazr/restful/tales.py 2010-08-24 18:57:50 +0000
20+++ src/lazr/restful/tales.py 2010-11-18 22:14:26 +0000
21@@ -12,7 +12,13 @@
22 import urllib
23
24 from epydoc.markup import DocstringLinker
25-from epydoc.markup.restructuredtext import parse_docstring
26+from epydoc.markup.restructuredtext import (
27+ _DocumentPseudoWriter,
28+ _EpydocReader,
29+ ParsedRstDocstring,
30+ )
31+from docutils import io
32+from docutils.core import Publisher
33
34 from zope.component import (
35 adapts, getGlobalSiteManager, getUtility, queryMultiAdapter)
36@@ -61,6 +67,49 @@
37 return indexterm
38
39
40+class _PydocParser:
41+ """Encapsulate the state/objects needed to parse docstrings."""
42+
43+ def __init__(self):
44+ # Set up the instance we'll be using to render docstrings.
45+ self.errors = []
46+ self.writer = _DocumentPseudoWriter()
47+ self.publisher = Publisher(_EpydocReader(self.errors),
48+ writer=self.writer,
49+ source_class=io.StringInput)
50+ self.publisher.set_components('standalone', 'restructuredtext',
51+ 'pseudoxml')
52+ settings_overrides={
53+ 'report_level':10000,
54+ 'halt_level':10000,
55+ 'warning_stream':None,
56+ }
57+ self.publisher.process_programmatic_settings(None,
58+ settings_overrides, None)
59+ self.publisher.set_destination()
60+
61+
62+ def parse_docstring(self, docstring, errors):
63+ """Parse a docstring for eventual transformation into HTML
64+
65+ This function is a replacement for parse_docstring from
66+ epydoc.markup.restructuredtext.parse_docstring. This function reuses
67+ the Publisher instance while the original did not. Using This
68+ function yields significantly faster WADL generation for complex
69+ systems.
70+ """
71+ # Clear any errors from previous calls.
72+ del self.errors[:]
73+ self.publisher.set_source(docstring, None)
74+ self.publisher.publish()
75+ # Move any errors into the caller-provided list.
76+ errors[:] = self.errors[:]
77+ return ParsedRstDocstring(self.writer.document)
78+
79+
80+_PYDOC_PARSER = _PydocParser()
81+
82+
83 WADL_DOC_TEMPLATE = (
84 '<wadl:doc xmlns="http://www.w3.org/1999/xhtml">\n%s\n</wadl:doc>')
85
86@@ -76,7 +125,7 @@
87 return None
88 doc = "%s\n%s" % (lines[0], textwrap.dedent("\n".join(lines[1:])))
89 errors = []
90- parsed = parse_docstring(doc, errors)
91+ parsed = _PYDOC_PARSER.parse_docstring(doc, errors)
92 if len(errors) > 0:
93 messages = [str(error) for error in errors]
94 raise AssertionError(
95
96=== modified file 'src/lazr/restful/version.txt'
97--- src/lazr/restful/version.txt 2010-10-25 16:31:09 +0000
98+++ src/lazr/restful/version.txt 2010-11-18 22:14:26 +0000
99@@ -1,1 +1,1 @@
100-0.14.1
101+0.15.0

Subscribers

People subscribed via source and target branches