Skip to content
Snippets Groups Projects
Commit 9e8a99075076 authored by Charlie Clark's avatar Charlie Clark
Browse files

Make context manager directly available and remove aliasing.

parent 69e8adf8ac37
No related branches found
No related tags found
No related merge requests found
from __future__ import absolute_import
from .xmlfile import xmlfile
\ No newline at end of file
......@@ -12,9 +12,10 @@
import tempfile, os, sys
from .common_imports import HelperTestCase, skipIf
from .. import xmlfile as etree
from et_xmlfile import xmlfile
from et_xmlfile.xmlfile import LxmlSyntaxError
import pytest
from .helper import compare_xml
import xml.etree.ElementTree
......@@ -16,11 +17,9 @@
import pytest
from .helper import compare_xml
import xml.etree.ElementTree
# _parse_file needs parse routine - take it from ElementTree
etree.parse = xml.etree.ElementTree.parse
from xml.etree.ElementTree import Element, parse
class _XmlFileTestCaseBase(HelperTestCase):
......@@ -30,9 +29,9 @@
self._file = BytesIO()
def test_element(self):
with etree.xmlfile(self._file) as xf:
with xmlfile(self._file) as xf:
with xf.element('test'):
pass
self.assertXml('<test></test>')
def test_element_write_text(self):
......@@ -34,11 +33,11 @@
with xf.element('test'):
pass
self.assertXml('<test></test>')
def test_element_write_text(self):
with etree.xmlfile(self._file) as xf:
with xmlfile(self._file) as xf:
with xf.element('test'):
xf.write('toast')
self.assertXml('<test>toast</test>')
def test_element_nested(self):
......@@ -40,9 +39,9 @@
with xf.element('test'):
xf.write('toast')
self.assertXml('<test>toast</test>')
def test_element_nested(self):
with etree.xmlfile(self._file) as xf:
with xmlfile(self._file) as xf:
with xf.element('test'):
with xf.element('toast'):
with xf.element('taste'):
......@@ -50,7 +49,7 @@
self.assertXml('<test><toast><taste>conTent</taste></toast></test>')
def test_element_nested_with_text(self):
with etree.xmlfile(self._file) as xf:
with xmlfile(self._file) as xf:
with xf.element('test'):
xf.write('con')
with xf.element('toast'):
......@@ -63,8 +62,8 @@
'tnet</toast>noc</test>')
def test_write_Element(self):
with etree.xmlfile(self._file) as xf:
xf.write(etree.Element('test'))
with xmlfile(self._file) as xf:
xf.write(Element('test'))
self.assertXml('<test/>')
def test_write_Element_repeatedly(self):
......@@ -68,8 +67,8 @@
self.assertXml('<test/>')
def test_write_Element_repeatedly(self):
element = etree.Element('test')
with etree.xmlfile(self._file) as xf:
element = Element('test')
with xmlfile(self._file) as xf:
with xf.element('test'):
for i in range(100):
xf.write(element)
......@@ -80,9 +79,9 @@
self.assertEqual(set(['test']), set(el.tag for el in tree.getroot()))
def test_namespace_nsmap(self):
with etree.xmlfile(self._file) as xf:
with xmlfile(self._file) as xf:
with xf.element('{nsURI}test', nsmap={'x': 'nsURI'}):
pass
self.assertXml('<x:test xmlns:x="nsURI"></x:test>')
def test_namespace_nested_nsmap(self):
......@@ -84,12 +83,12 @@
with xf.element('{nsURI}test', nsmap={'x': 'nsURI'}):
pass
self.assertXml('<x:test xmlns:x="nsURI"></x:test>')
def test_namespace_nested_nsmap(self):
with etree.xmlfile(self._file) as xf:
with xmlfile(self._file) as xf:
with xf.element('test', nsmap={'x': 'nsURI'}):
with xf.element('{nsURI}toast'):
pass
self.assertXml('<test xmlns:x="nsURI"><x:toast></x:toast></test>')
def test_anonymous_namespace(self):
......@@ -90,12 +89,12 @@
with xf.element('test', nsmap={'x': 'nsURI'}):
with xf.element('{nsURI}toast'):
pass
self.assertXml('<test xmlns:x="nsURI"><x:toast></x:toast></test>')
def test_anonymous_namespace(self):
with etree.xmlfile(self._file) as xf:
with xmlfile(self._file) as xf:
with xf.element('{nsURI}test'):
pass
self.assertXml('<ns0:test xmlns:ns0="nsURI"></ns0:test>')
def test_namespace_nested_anonymous(self):
......@@ -97,12 +96,12 @@
with xf.element('{nsURI}test'):
pass
self.assertXml('<ns0:test xmlns:ns0="nsURI"></ns0:test>')
def test_namespace_nested_anonymous(self):
with etree.xmlfile(self._file) as xf:
with xmlfile(self._file) as xf:
with xf.element('test'):
with xf.element('{nsURI}toast'):
pass
self.assertXml('<test><ns0:toast xmlns:ns0="nsURI"></ns0:toast></test>')
def test_default_namespace(self):
......@@ -103,12 +102,12 @@
with xf.element('test'):
with xf.element('{nsURI}toast'):
pass
self.assertXml('<test><ns0:toast xmlns:ns0="nsURI"></ns0:toast></test>')
def test_default_namespace(self):
with etree.xmlfile(self._file) as xf:
with xmlfile(self._file) as xf:
with xf.element('{nsURI}test', nsmap={None: 'nsURI'}):
pass
self.assertXml('<test xmlns="nsURI"></test>')
def test_nested_default_namespace(self):
......@@ -110,9 +109,9 @@
with xf.element('{nsURI}test', nsmap={None: 'nsURI'}):
pass
self.assertXml('<test xmlns="nsURI"></test>')
def test_nested_default_namespace(self):
with etree.xmlfile(self._file) as xf:
with xmlfile(self._file) as xf:
with xf.element('{nsURI}test', nsmap={None: 'nsURI'}):
with xf.element('{nsURI}toast'):
pass
......@@ -120,11 +119,12 @@
@pytest.mark.xfail
def test_pi(self):
with etree.xmlfile(self._file) as xf:
xf.write(etree.ProcessingInstruction('pypi'))
from et_xmlfile.xmlfile import ProcessingInstruction
with xmlfile(self._file) as xf:
xf.write(ProcessingInstruction('pypi'))
with xf.element('test'):
pass
self.assertXml('<?pypi ?><test></test>')
@pytest.mark.xfail
def test_comment(self):
......@@ -125,13 +125,13 @@
with xf.element('test'):
pass
self.assertXml('<?pypi ?><test></test>')
@pytest.mark.xfail
def test_comment(self):
with etree.xmlfile(self._file) as xf:
with xmlfile(self._file) as xf:
xf.write(etree.Comment('a comment'))
with xf.element('test'):
pass
self.assertXml('<!--a comment--><test></test>')
def test_attribute(self):
......@@ -132,12 +132,12 @@
xf.write(etree.Comment('a comment'))
with xf.element('test'):
pass
self.assertXml('<!--a comment--><test></test>')
def test_attribute(self):
with etree.xmlfile(self._file) as xf:
with xmlfile(self._file) as xf:
with xf.element('test', attrib={'k': 'v'}):
pass
self.assertXml('<test k="v"></test>')
def test_escaping(self):
......@@ -139,9 +139,9 @@
with xf.element('test', attrib={'k': 'v'}):
pass
self.assertXml('<test k="v"></test>')
def test_escaping(self):
with etree.xmlfile(self._file) as xf:
with xmlfile(self._file) as xf:
with xf.element('test'):
xf.write('Comments: <!-- text -->\n')
xf.write('Entities: &amp;')
......@@ -150,10 +150,10 @@
@pytest.mark.xfail
def test_encoding(self):
with etree.xmlfile(self._file, encoding='utf16') as xf:
with xmlfile(self._file, encoding='utf16') as xf:
with xf.element('test'):
xf.write('toast')
self.assertXml('<test>toast</test>', encoding='utf16')
@pytest.mark.xfail
def test_buffering(self):
......@@ -154,10 +154,10 @@
with xf.element('test'):
xf.write('toast')
self.assertXml('<test>toast</test>', encoding='utf16')
@pytest.mark.xfail
def test_buffering(self):
with etree.xmlfile(self._file, buffered=False) as xf:
with xmlfile(self._file, buffered=False) as xf:
with xf.element('test'):
self.assertXml("<test>")
xf.write('toast')
......@@ -174,7 +174,7 @@
@pytest.mark.xfail
def test_flush(self):
with etree.xmlfile(self._file, buffered=True) as xf:
with xmlfile(self._file, buffered=True) as xf:
with xf.element('test'):
self.assertXml("")
xf.write('toast')
......@@ -189,5 +189,5 @@
def test_failure_preceding_text(self):
try:
with etree.xmlfile(self._file) as xf:
with xmlfile(self._file) as xf:
xf.write('toast')
......@@ -193,7 +193,7 @@
xf.write('toast')
except etree.LxmlSyntaxError:
except LxmlSyntaxError:
self.assertTrue(True)
else:
self.assertTrue(False)
def test_failure_trailing_text(self):
......@@ -195,10 +195,10 @@
self.assertTrue(True)
else:
self.assertTrue(False)
def test_failure_trailing_text(self):
with etree.xmlfile(self._file) as xf:
with xmlfile(self._file) as xf:
with xf.element('test'):
pass
try:
xf.write('toast')
......@@ -201,10 +201,10 @@
with xf.element('test'):
pass
try:
xf.write('toast')
except etree.LxmlSyntaxError:
except LxmlSyntaxError:
self.assertTrue(True)
else:
self.assertTrue(False)
def test_failure_trailing_Element(self):
......@@ -206,9 +206,9 @@
self.assertTrue(True)
else:
self.assertTrue(False)
def test_failure_trailing_Element(self):
with etree.xmlfile(self._file) as xf:
with xmlfile(self._file) as xf:
with xf.element('test'):
pass
try:
......@@ -212,8 +212,8 @@
with xf.element('test'):
pass
try:
xf.write(etree.Element('test'))
except etree.LxmlSyntaxError:
xf.write(Element('test'))
except LxmlSyntaxError:
self.assertTrue(True)
else:
self.assertTrue(False)
......@@ -222,7 +222,7 @@
def test_closing_out_of_order_in_error_case(self):
cm_exit = None
try:
with etree.xmlfile(self._file) as xf:
with xmlfile(self._file) as xf:
x = xf.element('test')
cm_exit = x.__exit__
x.__enter__()
......@@ -231,7 +231,7 @@
self.assertTrue(cm_exit)
try:
cm_exit(ValueError, ValueError("huhu"), None)
except etree.LxmlSyntaxError:
except LxmlSyntaxError:
self.assertTrue(True)
else:
self.assertTrue(False)
......@@ -250,7 +250,7 @@
pos = self._file.tell()
self._file.seek(0)
try:
return etree.parse(self._file)
return parse(self._file)
finally:
self._file.seek(pos)
......@@ -268,7 +268,7 @@
self._file = BytesIO()
def test_filelike_close(self):
with etree.xmlfile(self._file, close=True) as xf:
with xmlfile(self._file, close=True) as xf:
with xf.element('test'):
pass
self.assertRaises(ValueError, self._file.getvalue)
......@@ -297,7 +297,7 @@
def _parse_file(self):
self._tmpfile.seek(0)
return etree.parse(self._tmpfile)
return parse(self._tmpfile)
@skipIf(True, "temp file behaviour is too platform specific here")
def test_buffering(self):
......@@ -333,8 +333,8 @@
pos = self._file.tell()
self._target.seek(0)
try:
return etree.parse(self._target)
return parse(self._target)
finally:
self._target.seek(pos)
def test_filelike_not_closing(self):
......@@ -337,10 +337,10 @@
finally:
self._target.seek(pos)
def test_filelike_not_closing(self):
with etree.xmlfile(self._file) as xf:
with xmlfile(self._file) as xf:
with xf.element('test'):
pass
self.assertFalse(self._file.closed)
def test_filelike_close(self):
......@@ -342,9 +342,9 @@
with xf.element('test'):
pass
self.assertFalse(self._file.closed)
def test_filelike_close(self):
with etree.xmlfile(self._file, close=True) as xf:
with xmlfile(self._file, close=True) as xf:
with xf.element('test'):
pass
self.assertTrue(self._file.closed)
......
......@@ -6,7 +6,6 @@
from contextlib import contextmanager
#from openpyxl.xml.functions import Element, tostring
from xml.etree.ElementTree import Element, tostring
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment