1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
# Copyright (c) 2000 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
this module provides classes to format the native tree2tree output
"""
__revision__ = '$Id: format.py,v 1.29 2005-04-15 11:02:50 syt Exp $'
import types
try:
from xml.dom import EMPTY_NAMESPACE as NO_NS
except:
NO_NS = None
from xmldiff.objects import A_N1, A_N2, A_DESC, N_PARENT, caract, \
xml_print, f_xpath, XUPD_PREFIX, XUPD_URI, to_dom
from sys import stdout
def get_attrs_string(attrs):
""" extract and return a string corresponding to an attributes list """
attr_s = []
for attr_n, attr_v in attrs:
attr_s.append('%s="%s" '%(attr_n, attr_v))
return ' '.join(attr_s)
## XUPDATE FUNCTIONS ##########################################################
def open_xupdate_node(type, attrs, indent='', stream=stdout):
"""print opening xupdate tag"""
stream.write('<%s:%s %s>' % (XUPD_PREFIX,
type, get_attrs_string(attrs)))
def close_xupdate_node(action, indent='', stream=stdout):
"""print closing xupdate tag"""
stream.write('</%s:%s>\n' % (XUPD_PREFIX, action))
def write_xupdate_node(type, attrs, indent='', stream=stdout):
"""print single xupdate tag"""
stream.write('<%s:%s %s/>\n' % (XUPD_PREFIX, type,
get_attrs_string(attrs)))
## Formatter interface ########################################################
class AbstractFormatter:
"""
Formatter interface
"""
def init(self, stream=stdout):
""" method called before the begining of the tree 2 tree correction """
self.edit_s = []
self._stream = stream
def add_action(self, action):
""" method called when an action is added to the edit script """
self.edit_s.append(action)
def format_action(self, action):
""" method called by end() to format each action in the edit script
at least this method should be overridden
"""
raise NotImplementedError()
def end(self):
""" method called at the end of the tree 2 tree correction """
for action in self.edit_s:
self.format_action(action)
## Internal Formatter ##########################################################
class InternalPrinter(AbstractFormatter):
""" print actions in the internal format """
def add_action(self, action):
"""
See AbstractFormatter interface
"""
if len(action) > 2 and type(action[A_N2]) == types.ListType:
if type(action[A_N1]) == types.ListType:
#swap or move node
action[A_N1] = f_xpath(action[A_N1])
action[A_N2] = f_xpath(action[A_N2])
AbstractFormatter.add_action(self, action)
def format_action(self, action):
"""
See AbstractFormatter interface
"""
if len(action) > 2 and type(action[A_N2]) == types.ListType:
self._stream.write('[%s, %s,\n' % (action[A_DESC], action[A_N1]))
xml_print(action[A_N2])
self._stream.write("]\n")
elif len(action) > 2:
self._stream.write('[%s, %s, %s]\n' % (action[A_DESC],
action[A_N1],
action[A_N2]))
else:
self._stream.write('[%s, %s]\n' % (action[A_DESC], action[A_N1]))
## XUpdate Formatters (text / DOM) #############################################
class XUpdateMixIn:
""" XUpdate mixin to preprocess added actions """
def add_action(self, action):
"""
See AbstractFormatter interface
"""
if action[A_DESC] == 'move-first':
# replace move-first with remove and insert (sibling nodes)
self.edit_s.append(('remove', f_xpath(action[A_N1])))
self.edit_s.append(('append', f_xpath(action[A_N2]), action[A_N1]))
elif action[A_DESC] == 'move-after':
# replace move-after with remove and insert (sibling nodes)
self.edit_s.append(('remove', f_xpath(action[A_N1])))
self.edit_s.append(('insert-after',
f_xpath(action[A_N2]),
action[A_N1]))
elif action[A_DESC] == 'move-and-rename':
# replace move-and-rename with remove and append (attribute nodes)
self.edit_s.append(('remove', f_xpath(action[A_N1])))
self.edit_s.append(('append',
f_xpath(action[A_N2][N_PARENT]),
action[A_N2]))
elif action[A_DESC] == 'swap':
# replace swap with remove and insert (sibling nodes)
self.edit_s.append(('remove', f_xpath(action[A_N2])))
self.edit_s.append(('insert-after',
f_xpath(action[A_N1]),
action[A_N2]))
else:
self.edit_s.append(action)
class XUpdatePrinter(XUpdateMixIn, AbstractFormatter):
""" take the actions list in standard format and output it following
Xupdate xml specification
"""
def init(self, stream = stdout):
"""
See AbstractFormatter interface
"""
AbstractFormatter.init(self, stream)
self._stream.write('''<?xml version="1.0"?>
<xupdate:modifications version="1.0" xmlns:%s="%s">\n''' % (XUPD_PREFIX,
XUPD_URI))
def format_action(self, action, indent=' '):
"""
See AbstractFormatter interface
"""
if action[A_DESC] == 'remove':
write_xupdate_node(action[A_DESC],
(('select', action[A_N1]), ),
indent,
self._stream)
elif action[A_DESC] == 'append-last':
open_xupdate_node('append',
(('select', action[A_N1]),
('child', 'last()')),
indent,
self._stream)
xml_print(action[A_N2], indent, xupdate=1, stream=self._stream)
close_xupdate_node('append', indent, self._stream)
elif action[A_DESC] == 'append-first':
open_xupdate_node('append',
(('select', action[A_N1]),
('child', 'first()')),
indent,
self._stream)
xml_print(action[A_N2], indent, xupdate=1, stream=self._stream)
close_xupdate_node('append', indent, self._stream)
elif action[A_DESC] in ['append', 'insert-after']:
open_xupdate_node(action[A_DESC],
(('select', action[A_N1]), ),
indent, self._stream)
xml_print(action[A_N2], indent, xupdate=1, stream=self._stream)
close_xupdate_node(action[A_DESC], indent, self._stream)
elif action[A_DESC] == 'rename':
open_xupdate_node(action[A_DESC],
(('select', action[A_N1]), ),
indent, self._stream)
self._stream.write(action[A_N2])
close_xupdate_node(action[A_DESC], indent, self._stream)
else:
open_xupdate_node(action[A_DESC],
(('select', action[A_N1]), ),
indent, self._stream)
self._stream.write(action[A_N2])
close_xupdate_node(action[A_DESC], indent, self._stream)
self._stream.write('\n')
self._stream.flush()
def end(self):
"""
See AbstractFormatter interface
"""
AbstractFormatter.end(self)
self._stream.write('</%s:modifications>'%XUPD_PREFIX)
class DOMXUpdateFormatter(XUpdateMixIn, AbstractFormatter):
""" take the actions list in standard format and return a dom tree
which follow Xupdate xml specification (without xupdate namespace)
dom tree is append to doc (DOM Document node)
"""
def __init__(self, doc, encoding='UTF-8'):
"""
Instance attributes are doc and encoding
"""
self.doc = doc
self.encoding = encoding
def init(self):
"""
See AbstractFormatter interface
"""
AbstractFormatter.init(self)
output = self.doc.createElementNS(XUPD_URI,
'%s:modifications'%XUPD_PREFIX)
output.setAttributeNS(NO_NS, 'version', '1.0')
self.output = output
def format_action(self, action):
"""
See AbstractFormatter interface
"""
doc = self.doc
if action[A_DESC] == 'remove':
node = doc.createElementNS(XUPD_URI,
'%s:%s' % (XUPD_PREFIX, action[A_DESC]))
node.setAttributeNS(NO_NS, 'select', action[A_N1])
elif action[A_DESC] == 'append-first':
node = doc.createElementNS(XUPD_URI,
'%s:%s'% (XUPD_PREFIX, 'append'))
node.setAttributeNS(NO_NS, 'select', action[A_N1])
node.setAttributeNS(NO_NS, 'child', 'first()')
node.appendChild(to_dom(action[A_N2], doc, XUPD_URI, XUPD_PREFIX))
elif action[A_DESC] == 'append-last':
node = doc.createElementNS(XUPD_URI,
'%s:%s' % (XUPD_PREFIX, 'append'))
node.setAttributeNS(NO_NS, 'select', action[A_N1])
node.setAttributeNS(NO_NS, 'child', 'last()')
node.appendChild(to_dom(action[A_N2], doc, XUPD_URI, XUPD_PREFIX))
elif action[A_DESC] in ['append', 'insert-after', 'insert-before']:
node = doc.createElementNS(XUPD_URI,
'%s:%s' % (XUPD_PREFIX, action[A_DESC]))
node.setAttributeNS(NO_NS, 'select', action[A_N1])
node.appendChild(to_dom(action[A_N2], doc, XUPD_URI, XUPD_PREFIX))
elif action[A_DESC] == 'rename':
node = doc.createElementNS(XUPD_URI,
'%s:%s' %(XUPD_PREFIX, action[A_DESC]))
node.setAttributeNS(NO_NS, 'name', action[A_N1])
v = unicode(action[A_N2], self.encoding)
node.appendChild(doc.createTextNode(v))
else:
node = doc.createElementNS(XUPD_URI,
'%s:%s' % (XUPD_PREFIX, action[A_DESC]))
node.setAttributeNS(NO_NS, 'select', action[A_N1])
v = unicode(action[A_N2], self.encoding)
node.appendChild(doc.createTextNode(v))
# append xupdate node
self.output.appendChild(node)
|