Merge lp:~gz/bzr-xmloutput/relocate__escape_cdata into lp:bzr-xmloutput

Proposed by Martin Packman
Status: Merged
Approved by: Guillermo Gonzalez
Approved revision: 147
Merged at revision: 147
Proposed branch: lp:~gz/bzr-xmloutput/relocate__escape_cdata
Merge into: lp:bzr-xmloutput
Diff against target: 187 lines (+35/-21)
8 files modified
__init__.py (+1/-2)
annotatexml.py (+3/-2)
logxml.py (+4/-2)
lsxml.py (+4/-3)
statusxml.py (+5/-6)
versionxml.py (+2/-1)
writer.py (+10/-0)
xml_errors.py (+6/-5)
To merge this branch: bzr merge lp:~gz/bzr-xmloutput/relocate__escape_cdata
Reviewer Review Type Date Requested Status
Guillermo Gonzalez Approve
Review via email: mp+34640@code.launchpad.net

Commit message

Import xml escaping function through local module without relying on bzrlib elementtree monkey-patched version

Description of the change

In fixing bug 614522 I removed the monkey-patching artefact of a copy of the elementtree _escape_cdata function from bzrlib.xml_serializer, which bzr-xmloutput is relying on.

Despite being blasé about how easy this is to fix to Vincent, I got pretty stuck last night trying to create a branch that didn't change code all over the place.

It was sort of a choice between:
* Expand every import of _escape_cdata to look in all *three* locations elementtree may live.
* Stop using relative imports everywhere and just do the import dance in the base package, and import from there using bzrlib.plugins.xmloutput as the name.
* Write a new escaping function somewhere and use that, the elementtree one isn't really sufficient anyway.
* Import from elementtree via bzrlib.xml_serializer everywhere.
* Make a new module to put the import dance in and import from there.

In the end, this branch does the last two, plus pokes a few imports statements that I couldn't resist nearby.

There are some bits of code I had in this branch, but I have not included here:
* Fixing the Python 2.5ism in test_info_xml.
* Teasing out the needlessly lazy imports from the helpful ones in the base package.
* Importing from bzrlib.plugins.xmloutput rather than using relative imports.
* Some more import cleanup.
* Creating xml prolog function rather than having the same user encoding inserting logic in lots of places.
* Making the prolog use an encoding expat can actually parse.

I can put up branches for any of these that there's interest in landing, but what I'd really like to see, and haven't coded, is a sane abstraction for writing xml that actually ensures well-formedness.

Still unresolved, ElementTree 1.3 (used in Python 2.7) makes the encoding parameter non-optional, for good reason, so this wants changing to something better sooner rather than later.

To post a comment you must log in.
Revision history for this message
Guillermo Gonzalez (verterok) wrote :

Wow, this looks great.

Thanks a lot for putting this together.

Yes, there is interest in any improvement/fix/code cleanup you might have in mind (or already coded ;-))
And I agree with you on most of the exposed above, mostly on the need of a unified way to generate the xml (or json, or protobuf or <another format of your choice>).

I'm a bit short on time (as the commit log shows) :-(
But I hope to get back to work on bzr-xmloutput and bzr-eclipse in the short term again.

review: Approve
Revision history for this message
Guillermo Gonzalez (verterok) wrote :

Please set a commit message and I'll merge this in trunk.

Thanks!

Revision history for this message
Martin Packman (gz) wrote :

> Yes, there is interest in any improvement/fix/code cleanup you might have in
> mind (or already coded ;-))

I'll put up another branch or two with the less invasive bits.

> I'm a bit short on time (as the commit log shows) :-(

I understand completely...

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '__init__.py'
2--- __init__.py 2010-02-08 04:26:45 +0000
3+++ __init__.py 2010-09-05 18:09:43 +0000
4@@ -41,7 +41,6 @@
5 option,
6 log,
7 workingtree,
8- xml_serializer,
9 errors
10 )
11
12@@ -278,7 +277,7 @@
13 self.outf.write('<?xml version="1.0" encoding="%s"?>' % \
14 bzrlib.osutils.get_user_encoding())
15 self.outf.write('<plugins>')
16- from bzrlib.xml_serializer import _escape_cdata
17+ from writer import _escape_cdata
18 for name, plugin in bzrlib.plugin.plugins().items():
19 self.outf.write('<plugin>')
20 self.outf.write('<name>%s</name>' % name)
21
22=== modified file 'annotatexml.py'
23--- annotatexml.py 2009-09-23 03:29:47 +0000
24+++ annotatexml.py 2010-09-05 18:09:43 +0000
25@@ -35,11 +35,12 @@
26 # to support bzr < 1.8
27 from bzrlib.annotate import _annotate_file
28
29-
30 from bzrlib.annotate import _annotations
31-from bzrlib.xml_serializer import _escape_cdata
32 from bzrlib import osutils
33
34+from writer import _escape_cdata
35+
36+
37 empty_annotation = 'revno="" author="" date=""'
38
39 def annotate_file_xml(branch, rev_id, file_id, to_file=None,
40
41=== modified file 'logxml.py'
42--- logxml.py 2009-07-02 16:12:44 +0000
43+++ logxml.py 2010-09-05 18:09:43 +0000
44@@ -1,5 +1,7 @@
45 # -*- encoding: utf-8 -*-
46
47+import os
48+
49 from bzrlib.lazy_import import lazy_import
50 lazy_import(globals(), """
51 import bzrlib
52@@ -8,10 +10,10 @@
53 osutils,
54 log,
55 )
56-from bzrlib.xml_serializer import _escape_cdata
57-import os
58 """)
59
60+from writer import _escape_cdata
61+
62
63 class XMLLogFormatter(log.LogFormatter):
64 """ add a --xml format to 'bzr log'"""
65
66=== modified file 'lsxml.py'
67--- lsxml.py 2009-11-15 19:27:23 +0000
68+++ lsxml.py 2010-09-05 18:09:43 +0000
69@@ -19,14 +19,15 @@
70 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
71 #
72
73+import os
74
75 from bzrlib.lazy_import import lazy_import
76 lazy_import(globals(), """
77-import os
78-from bzrlib import bzrdir, errors, osutils, xml_serializer
79-from bzrlib.xml_serializer import _escape_cdata
80+from bzrlib import bzrdir, errors, osutils
81 """)
82
83+from writer import _escape_cdata
84+
85
86 def show_ls_xml(outf, revision=None, non_recursive=False,
87 from_root=False, unknown=False, versioned=False,
88
89=== modified file 'statusxml.py'
90--- statusxml.py 2009-07-05 19:59:00 +0000
91+++ statusxml.py 2010-09-05 18:09:43 +0000
92@@ -26,19 +26,18 @@
93 import os, sys, time
94 from bzrlib import (
95 diff,
96- trace,
97 errors,
98+ osutils,
99 revision as _mod_revision,
100 status,
101- osutils,
102+ trace,
103 )
104
105-from bzrlib.osutils import terminal_width
106-from bzrlib.xml_serializer import _escape_cdata
107-from bzrlib.trace import warning
108 import logxml
109 """)
110
111+from writer import _escape_cdata
112+
113
114 def show_tree_status_xml(wt, show_unchanged=None,
115 specific_files=None,
116@@ -119,7 +118,7 @@
117 new_is_working_tree = True
118 if revision is None:
119 if wt.last_revision() != wt.branch.last_revision():
120- warning("working tree is out of date, run 'bzr update'")
121+ trace.warning("working tree is out of date, run 'bzr update'")
122 new = wt
123 old = new.basis_tree()
124 elif len(revision) > 0:
125
126=== modified file 'versionxml.py'
127--- versionxml.py 2009-09-23 03:29:47 +0000
128+++ versionxml.py 2010-09-05 18:09:43 +0000
129@@ -35,7 +35,8 @@
130 trace,
131 )
132 from bzrlib.version import _get_bzr_source_tree
133-from bzrlib.xml_serializer import _escape_cdata
134+
135+from writer import _escape_cdata
136
137
138 def show_version_xml(show_config=True, show_copyright=True, to_file=None):
139
140=== added file 'writer.py'
141--- writer.py 1970-01-01 00:00:00 +0000
142+++ writer.py 2010-09-05 18:09:43 +0000
143@@ -0,0 +1,10 @@
144+"""Tools for writing xml
145+
146+Suggestion for this module: create a simple xml writer here and expurgate all
147+other modules of angle brackets entirely.
148+"""
149+
150+import bzrlib.xml_serializer
151+
152+# Use xml_serializer to avoid duplicating the elementtree location logic
153+_escape_cdata = bzrlib.xml_serializer.elementtree.ElementTree._escape_cdata
154
155=== modified file 'xml_errors.py'
156--- xml_errors.py 2009-09-23 03:29:47 +0000
157+++ xml_errors.py 2010-09-05 18:09:43 +0000
158@@ -1,15 +1,16 @@
159 """ XMLError handling module """
160+
161 from bzrlib.lazy_import import lazy_import
162 lazy_import(globals(), """
163-import bzrlib
164 from bzrlib import (
165 errors,
166- debug,
167+ osutils,
168+ trace,
169 )
170 """)
171
172-from bzrlib.xml_serializer import _escape_cdata
173-from bzrlib import trace
174+from writer import _escape_cdata
175+
176
177 class XMLError(errors.BzrError):
178 """ A class that wraps an BzrError and 'serialize' it as xml."""
179@@ -21,7 +22,7 @@
180 def __str__(self):
181 """__str__"""
182 xml = '<?xml version="1.0" encoding="%s"?>' % \
183- bzrlib.osutils.get_user_encoding()
184+ osutils.get_user_encoding()
185 try:
186 xml += '<error>%s</error>' % self.get_cause_xml()
187 except Exception, e:

Subscribers

People subscribed via source and target branches

to all changes: