Revision: 3bad63341e46
Author: Janne Härkönen <[email protected]>
Date: Mon Nov 28 01:06:41 2011
Log: Removed utils.domwrapper
http://code.google.com/p/robotframework/source/detail?r=3bad63341e46
Deleted:
/src/robot/utils/domwrapper.py
/utest/utils/test_domwrapper.py
Modified:
/src/robot/utils/__init__.py
/utest/utils/test_xmlwriter.py
=======================================
--- /src/robot/utils/domwrapper.py Sun Feb 6 01:24:10 2011
+++ /dev/null
@@ -1,105 +0,0 @@
-# Copyright 2008-2011 Nokia Siemens Networks Oyj
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-import etreewrapper
-
-
-class DomWrapper(object):
-
- def __init__(self, path=None, string=None, node=None):
- """Initialize by giving 'path' to an xml file or xml as a 'string'.
-
- Alternative initialization by giving dom 'node' ment to be used
only
- internally. 'path' may actually also be an already opened file
object
- (or anything accepted by ElementTree.parse).
- """
- node = etreewrapper.get_root(path, string, node)
- self.source = path
- self.name = node.tag
- self.attrs = dict(node.items())
- self.text = node.text or ''
- self.children = [DomWrapper(path, node=child) for child in
list(node)]
-
- def get_nodes(self, path):
- """Returns a list of descendants matching given 'path'.
-
- Path must be a string in format 'child_name/grandchild_name/etc'.
No
- slash is allowed at the beginning or end of the path string.
Returns an
- empty list if no matching descendants found and raises
AttributeError
- if path is invalid.
- """
- if not isinstance(path, basestring) \
- or path == '' or path[0] == '/' or path[-1] == '/':
- raise AttributeError("Invalid path '%s'" % path)
- matches = []
- for child in self.children:
- matches += child._get_matching_elements(path.split('/'))
- return matches
-
- def _get_matching_elements(self, tokens):
- if self.name != tokens[0]:
- return []
- elif len(tokens) == 1:
- return [self]
- else:
- matches = []
- for child in self.children:
- matches += child._get_matching_elements(tokens[1:])
- return matches
-
- def get_node(self, path):
- """Similar as get_nodes but checks that exactly one node is found.
-
- Node is returned as is (i.e. not in a list) and AttributeError
risen if
- no match or more than one match found.
- """
- nodes = self.get_nodes(path)
- if len(nodes) == 0:
- raise AttributeError("No nodes matching path '%s' found" %
path)
- if len(nodes) > 1:
- raise AttributeError("Multiple nodes matching path '%s'
found" % path)
- return nodes[0]
-
- def get_attr(self, name, default=None):
- """Helper for getting node's attributes.
-
- Otherwise equivalent to 'node.attrs.get(name, default)' but raises
- an AttributeError if no value found and no default given.
- """
- ret = self.attrs.get(name, default)
- if ret is None:
- raise AttributeError("No attribute '%s' found" % name)
- return ret
-
- def __getattr__(self, name):
- """Syntactic sugar for get_nodes (e.g. dom.elem[0].subelem).
-
- Differs from get_nodes so that if not matching nodes are found an
- AttributeError is risen instead of returning an empty list.
- """
- nodes = self.get_nodes(name)
- if not nodes:
- raise AttributeError("No nodes matching path '%s' found" %
name)
- return nodes
-
- def __getitem__(self, name):
- """Syntactic sugar for get_node (e.g. dom['elem/subelem'])"""
- try:
- return self.get_node(name)
- except AttributeError:
- raise IndexError("No node '%s' found" % name)
-
- def __repr__(self):
- """Return node name. Mainly for debugging purposes."""
- return self.name
=======================================
--- /utest/utils/test_domwrapper.py Thu Nov 18 08:34:58 2010
+++ /dev/null
@@ -1,85 +0,0 @@
-import sys
-import unittest
-import StringIO
-
-from robot.utils.asserts import assert_equals, assert_raises
-from robot.utils import DomWrapper
-
-
-XML_STR = '''<?xml version="1.0" encoding="UTF-8"?>
-<!-- Comment before root - all comments (and doctypes) should be ignored
-->
-<root>
- <!-- Comment inside root -->
- <elem a1="v1" a2="v2">
- <txt>hello</txt>
- <!-- Comment again -->
- </elem>
- <elem/>
- <!--
- Multiline
- comment
- <elem><txt>still in comment</txt></elem>
- -->
- <elem a="v">
- <elem>
- <txt>hi</txt>
- </elem>
- <elem>
- <txt/>
- </elem>
- </elem>
-</root>
-<!-- Comment after root -->
-'''
-
-# Tests only read from dom so we can create it only once
-DOM = DomWrapper(string=XML_STR)
-
-
-class TestDomWrapper(unittest.TestCase):
-
- def test_root(self):
- assert_equals(DOM.name, 'root')
- assert_equals(DOM.attrs, {})
-
- def test_elem(self):
- e1 = DOM.elem[0] # using __getattr__ sugar
- e2 = DOM.elem[1]
- e3 = DOM.elem[2]
- e31 = e3.elem[0]
- e32 = e3.elem[1]
- for elem, exp_attrs in [ (e1,{'a1':'v1','a2':'v2'}), (e2,{}),
- (e3,{'a':'v'}), (e31,{}), (e32,{}) ]:
- assert_equals(elem.name, 'elem')
- assert_equals(elem.attrs, exp_attrs)
-
- def test_text(self):
- t1 = DOM.elem[0].txt[0]
- t2 = DOM.elem[2].elem[0].txt[0]
- t3 = DOM.elem[2].elem[1].txt[0]
- for elem, exp_text in [ (t1,'hello'), (t2,'hi'), (t3,'') ]:
- assert_equals(elem.name, 'txt')
- assert_equals(elem.text, exp_text)
- assert_equals(elem.attrs, {})
- assert_equals(elem.children, [])
-
- def test_get_nodes(self):
- nodes = DOM.get_nodes('elem/txt')
- assert_equals(len(nodes), 1)
- assert_equals(nodes[0].text, 'hello')
- nodes = DOM.get_nodes('elem/elem/txt')
- assert_equals(len(nodes), 2)
- assert_equals(nodes[0].text, 'hi')
- assert_equals(nodes[1].text, '')
-
- def test_sugar(self):
- node = DOM.get_nodes('elem')[0].get_node('txt')
- assert_equals(DOM.elem[0].txt[0], node)
- assert_equals(DOM['elem/txt'], node)
- assert_raises(AttributeError, DOM.__getattr__, 'nonexisting')
- assert_raises(IndexError, DOM.__getitem__, 'nonexisting')
- assert_raises(IndexError, DOM.__getitem__, 2)
-
-
-if __name__ == "__main__":
- unittest.main()
=======================================
--- /src/robot/utils/__init__.py Fri Nov 18 04:07:46 2011
+++ /src/robot/utils/__init__.py Mon Nov 28 01:06:41 2011
@@ -15,7 +15,6 @@
from argumentparser import ArgumentParser
from compress import compress_text
from connectioncache import ConnectionCache
-from domwrapper import DomWrapper
from encoding import decode_output, encode_output, decode_from_file_system
from error import (get_error_message, get_error_details, ErrorDetails,
RERAISED_EXCEPTIONS)
=======================================
--- /utest/utils/test_xmlwriter.py Thu Nov 18 06:45:17 2010
+++ /utest/utils/test_xmlwriter.py Mon Nov 28 01:06:41 2011
@@ -11,12 +11,12 @@
def setUp(self):
self.writer = utils.XmlWriter(PATH)
-
+
def tearDown(self):
self.writer.close()
os.remove(PATH)
-
- def test_write_element_in_pieces(self):
+
+ def test_write_element_in_pieces(self):
self.writer.start('name', {'attr': 'value'}, True)
self.writer.content('Some content here!!')
self.writer.end('name', True)
@@ -31,13 +31,13 @@
self.writer.content('\tMy name is John')
self.writer.end('robot-log')
self.writer.close()
- self._verify_node(None, 'robot-log',
+ self._verify_node(None, 'robot-log',
'Hello world!\nHi again!\tMy name is John')
-
+
def test_write_element(self):
self.writer.element('foo', 'Node\n content',
{'a1':'attr1', 'a2':'attr2'})
self.writer.close()
- self._verify_node(None, 'foo', 'Node\n content',
+ self._verify_node(None, 'foo', 'Node\n content',
{'a1': 'attr1', 'a2': 'attr2'})
def test_write_many_elements(self):
@@ -49,22 +49,22 @@
self.writer.element('child2', attributes={'class': 'foo'})
self.writer.end('root')
self.writer.close()
- root = utils.DomWrapper(PATH)
+ root = utils.ET.parse(PATH).getroot()
self._verify_node(root, 'root', attrs={'version': 'test'})
- self._verify_node(root.get_node('child1'), 'child1',
attrs={'my-attr': 'my value'})
- self._verify_node(root.get_node('child1/leaf1.1'), 'leaf1.1',
+ self._verify_node(root.find('child1'), 'child1',
attrs={'my-attr': 'my value'})
+ self._verify_node(root.find('child1/leaf1.1'), 'leaf1.1',
'leaf content', {'type': 'kw'})
- self._verify_node(root.get_node('child1/leaf1.2'), 'leaf1.2')
- self._verify_node(root.get_node('child2'), 'child2',
attrs={'class': 'foo'})
+ self._verify_node(root.find('child1/leaf1.2'), 'leaf1.2')
+ self._verify_node(root.find('child2'), 'child2',
attrs={'class': 'foo'})
def test_newline_insertion(self):
self.writer.start('root')
self.writer.start('suite', {'type': 'directory_suite'})
- self.writer.element('test', attributes={'name': 'my_test'},
+ self.writer.element('test', attributes={'name': 'my_test'},
newline=False)
self.writer.element('test', attributes={'name': 'my_2nd_test'})
self.writer.end('suite', False)
- self.writer.start('suite', {'name': 'another suite'},
+ self.writer.start('suite', {'name': 'another suite'},
newline=False)
self.writer.content('Suite 2 content')
self.writer.end('suite')
@@ -79,7 +79,7 @@
self.writer.element(u'robot-log', None)
self.writer.close()
self._verify_node(None, 'robot-log')
-
+
def test_content_with_invalid_command_char(self):
self.writer.element('robot-log', '\033[31m\033[32m\033[33m\033[m')
self.writer.close()
@@ -96,11 +96,11 @@
self.writer.element(u'f', u'Hyv\u00E4\u00E4 \u00FC\u00F6t\u00E4')
self.writer.end('root')
self.writer.close()
- root = utils.DomWrapper(PATH)
- self._verify_node(root.get_node('e'), 'e', u'Circle is 360\u00B0')
- self._verify_node(root.get_node('f'), 'f',
+ root = utils.ET.parse(PATH).getroot()
+ self._verify_node(root.find('e'), 'e', u'Circle is 360\u00B0')
+ self._verify_node(root.find('f'), 'f',
u'Hyv\u00E4\u00E4 \u00FC\u00F6t\u00E4')
-
+
def test_content_with_entities(self):
self.writer.element(u'robot-log', 'Me, Myself & I > you')
@@ -116,12 +116,12 @@
def _verify_node(self, node, name, text=None, attrs={}):
if node is None:
- node = utils.DomWrapper(PATH)
- assert_equals(node.name, name)
+ node = utils.ET.parse(PATH).getroot()
+ assert_equals(node.tag, name)
if text is not None:
assert_equals(node.text, text)
- assert_equals(node.attrs, attrs)
-
+ assert_equals(node.attrib, attrs)
+
if __name__ == '__main__':
unittest.main()