Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
E
erp5diff
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
erp5diff
Commits
0a8a5b1e
Commit
0a8a5b1e
authored
Jan 29, 2024
by
Jérome Perrin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support IO with bytes for easier usage on python3
parent
6dce2da5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
3 deletions
+75
-3
src/ERP5Diff/ERP5Diff.py
src/ERP5Diff/ERP5Diff.py
+14
-3
src/tests/test_erp5diff.py
src/tests/test_erp5diff.py
+61
-0
No files found.
src/ERP5Diff/ERP5Diff.py
View file @
0a8a5b1e
...
@@ -32,6 +32,7 @@ parser = etree.XMLParser(remove_blank_text=True)
...
@@ -32,6 +32,7 @@ parser = etree.XMLParser(remove_blank_text=True)
import
sys
import
sys
import
getopt
import
getopt
import
os
import
os
from
io
import
BytesIO
from
six
import
StringIO
from
six
import
StringIO
import
re
import
re
import
codecs
import
codecs
...
@@ -97,7 +98,7 @@ class ERP5Diff:
...
@@ -97,7 +98,7 @@ class ERP5Diff:
"""
"""
doc_list
=
[]
doc_list
=
[]
for
a
in
args
:
for
a
in
args
:
if
isinstance
(
a
,
s
tr
):
if
isinstance
(
a
,
s
ix
.
string_types
+
(
bytes
,
)
):
doc_list
.
append
(
etree
.
fromstring
(
a
,
parser
))
doc_list
.
append
(
etree
.
fromstring
(
a
,
parser
))
else
:
else
:
element_tree
=
etree
.
parse
(
a
,
parser
)
element_tree
=
etree
.
parse
(
a
,
parser
)
...
@@ -672,7 +673,7 @@ class ERP5Diff:
...
@@ -672,7 +673,7 @@ class ERP5Diff:
del
old_doc
del
old_doc
del
new_doc
del
new_doc
def
output
(
self
,
output_file
=
None
):
def
output
(
self
,
output_file
=
None
,
encoding
=
'unicode'
):
"""
"""
Output the result of parsing XML documents to 'output_file'.
Output the result of parsing XML documents to 'output_file'.
If it is not specified, stdout is assumed.
If it is not specified, stdout is assumed.
...
@@ -680,9 +681,19 @@ class ERP5Diff:
...
@@ -680,9 +681,19 @@ class ERP5Diff:
if
output_file
is
None
:
if
output_file
is
None
:
output_file
=
sys
.
stdout
output_file
=
sys
.
stdout
# stream
# stream
xml
=
etree
.
tostring
(
self
.
_result
,
encoding
=
'utf-8'
,
pretty_print
=
True
)
xml
=
etree
.
tostring
(
self
.
_result
,
encoding
=
encoding
,
pretty_print
=
True
)
output_file
.
write
(
xml
)
output_file
.
write
(
xml
)
def
outputBytes
(
self
,
encoding
=
'utf-8'
):
"""
Return the result as a bytes object.
"""
io
=
BytesIO
()
self
.
output
(
io
,
encoding
=
encoding
)
ret
=
io
.
getvalue
()
io
.
close
()
return
ret
def
outputString
(
self
):
def
outputString
(
self
):
"""
"""
Return the result as a string object.
Return the result as a string object.
...
...
src/tests/test_erp5diff.py
View file @
0a8a5b1e
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
from
__future__
import
absolute_import
from
__future__
import
absolute_import
import
doctest
import
doctest
import
io
import
unittest
import
unittest
import
lxml.doctestcompare
import
lxml.doctestcompare
from
lxml
import
etree
from
lxml
import
etree
...
@@ -1153,5 +1154,65 @@ class TestERP5Diff(unittest.TestCase):
...
@@ -1153,5 +1154,65 @@ class TestERP5Diff(unittest.TestCase):
"""
"""
self
.
_assertERP5DiffWorks
(
old_xml
,
new_xml
,
expected_result_string
)
self
.
_assertERP5DiffWorks
(
old_xml
,
new_xml
,
expected_result_string
)
class
TestIO
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
erp5diff
=
ERP5Diff
()
def
assertXupdate
(
self
):
expected_result_string
=
"""
<xupdate:modifications xmlns:xupdate="http://www.xmldb.org/xupdate" version="1.0">
<xupdate:update select="/xml/attribute::case">é</xupdate:update>
</xupdate:modifications>
"""
result_string
=
self
.
erp5diff
.
outputString
()
checker
=
lxml
.
doctestcompare
.
LXMLOutputChecker
()
if
not
checker
.
check_output
(
expected_result_string
,
result_string
,
0
):
self
.
fail
(
checker
.
output_difference
(
doctest
.
Example
(
""
,
expected_result_string
),
result_string
,
0
))
self
.
assertIn
(
u'é'
.
encode
(
'utf-8'
),
self
.
erp5diff
.
outputBytes
(
encoding
=
'utf-8'
))
out
=
io
.
BytesIO
()
self
.
erp5diff
.
output
(
out
,
encoding
=
'utf-8'
)
self
.
assertIn
(
u'é'
.
encode
(
'utf-8'
),
out
.
getvalue
())
self
.
assertIn
(
u'é'
.
encode
(
'iso-8859-1'
),
self
.
erp5diff
.
outputBytes
(
encoding
=
'iso-8859-1'
))
out
=
io
.
BytesIO
()
self
.
erp5diff
.
output
(
out
,
encoding
=
'iso-8859-1'
)
self
.
assertIn
(
u'é'
.
encode
(
'iso-8859-1'
),
out
.
getvalue
())
def
test_unicode
(
self
):
self
.
erp5diff
.
compare
(
u'<xml case="à"></xml>'
,
u'<xml case="é"></xml>'
)
self
.
assertXupdate
()
def
test_stringio
(
self
):
self
.
erp5diff
.
compare
(
io
.
StringIO
(
u'<xml case="à"></xml>'
),
io
.
StringIO
(
u'<xml case="é"></xml>'
))
self
.
assertXupdate
()
def
test_utf8_encoded_bytes
(
self
):
self
.
erp5diff
.
compare
(
u'<xml case="à"></xml>'
.
encode
(
'utf-8'
),
u'<xml case="é"></xml>'
.
encode
(
'utf-8'
))
self
.
assertXupdate
()
def
test_iso88591_encoded_bytes
(
self
):
self
.
erp5diff
.
compare
(
u'<?xml version="1.0" encoding="iso-8859-1"?><xml case="à"></xml>'
.
encode
(
'iso-8859-1'
),
u'<?xml version="1.0" encoding="iso-8859-1"?><xml case="é"></xml>'
.
encode
(
'iso-8859-1'
))
self
.
assertXupdate
()
def
test_utf8_encoded_byteesio
(
self
):
self
.
erp5diff
.
compare
(
io
.
BytesIO
(
u'<xml case="à"></xml>'
.
encode
(
'utf-8'
)),
io
.
BytesIO
(
u'<xml case="é"></xml>'
.
encode
(
'utf-8'
)))
self
.
assertXupdate
()
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment