Change str to str_data in parse(..), fixes #21

This commit is contained in:
Simeon Warner 2016-03-22 16:37:00 -07:00
parent 8faf4467e2
commit b2f2c5682b
6 changed files with 32 additions and 14 deletions

View File

@ -6,9 +6,11 @@ core specification version. Versions 1.0.x implement the v1.0
ResourceSync specification which was standardized as ANSI/NISO Z39.99-2014
<http://www.openarchives.org/rs/1.0/toc>.
v1.0.1 2016-01-30
v1.0.1 2016-03-22
* Improved test coverage
* Use defusedxml for safer XML parsing
* Changed parameter str to str_data in ListBase.parse(), str still supported
but deprecated. Issue pointed out by @uweschmitt
v1.0.0 2014-06-18
* Update for v1.0, Z39.99-2014 specification (http://www.openarchives.org/rs/1.0/)

2
README
View File

@ -2,7 +2,7 @@
resync
======
.. image:: https://travis-ci.org/resync/resync.png?branch=master
.. image:: https://travis-ci.org/resync/resync.png?branch=1.0.1
:target: https://travis-ci.org/resync/resync
:alt: Build status

View File

@ -82,22 +82,29 @@ class ListBase(ResourceContainer):
"""
self.parse(uri=uri)
def parse(self,uri=None,fh=None,str=None):
def parse(self,uri=None,fh=None,str_data=None,**kwargs):
"""Parse a single XML document for this list
Accepts either a uri (uri or default if parameter not specified),
or a filehandle (fh) or a string (str).
or a filehandle (fh) or a string (str_data). Note that this method
does not handle the case of a sitemapindex+sitemaps.
Does not handle the case of sitemapindex+sitemaps
LEGACY SUPPORT - the parameter str may be used in place of str_data
but is deprecated and will be removed in a later version.
"""
if (uri is not None):
try:
fh = URLopener().open(uri)
except IOError as e:
msg = __builtins__.str(e)
raise Exception("Failed to load sitemap/sitemapindex from %s (%s)" % (uri, e))
elif (str is not None):
fh=StringIO.StringIO(str)
raise Exception("Failed to load sitemap/sitemapindex from %s (%s)" % (uri,str(e)))
elif (str_data is not None):
fh=io.StringIO(str_data)
elif ('str' in kwargs):
# Legacy support for str argument, see
# https://github.com/resync/resync/pull/21
# One test for this in tests/test_list_base.py
self.logger.warn("Legacy parse(str=...), use parse(str_data=...) instead")
fh = io.StringIO(kwargs['str'])
if (fh is None):
raise Exception("Nothing to parse")
s = self.new_sitemap()

View File

@ -29,7 +29,7 @@ class TestCapabilityList(unittest.TestCase):
def test03_parse(self):
xml='<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:rs="http://www.openarchives.org/rs/terms/"><rs:md capability="capabilitylist" from="2013-02-07T22:39:00" /><url><loc>http://example.org/resourcelist.xml</loc><rs:md capability="resourcelist" /></url></urlset>'
cl=CapabilityList()
cl.parse(str=xml)
cl.parse(str_data=xml)
self.assertEqual( cl.capability, 'capabilitylist')
self.assertEqual( len(cl.resources), 1, 'got 1 resource')
[r] = cl.resources

View File

@ -47,9 +47,18 @@ class TestListBase(unittest.TestCase):
<url><loc>/tmp/rs_test/src/file_a</loc><lastmod>2012-03-14T18:37:36Z</lastmod><rs:md length="12" /></url>\
<url><loc>/tmp/rs_test/src/file_b</loc><lastmod>2012-03-14T18:37:36Z</lastmod><rs:md length="32" /></url>\
</urlset>'
lb=ListBase()
lb.parse(fh=io.StringIO(xml))
self.assertEqual( len(lb.resources), 2, 'got 2 resources')
# parse from string
lb1=ListBase()
lb1.parse(str_data=xml)
self.assertEqual( len(lb1.resources), 2, 'got 2 resources')
# parse from fh
lb2=ListBase()
lb2.parse(fh=io.StringIO(xml))
self.assertEqual( len(lb2.resources), 2, 'got 2 resources')
# parse from string (LEGACY parameter name, to be removed)
lb3=ListBase()
lb3.parse(str=xml)
self.assertEqual( len(lb3.resources), 2, 'got 2 resources')
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestListBase)

View File

@ -34,7 +34,7 @@ class TestSourceDescription(unittest.TestCase):
def test04_parse(self):
xml='<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:rs="http://www.openarchives.org/rs/terms/"><rs:ln href="http://example.org/about" rel="describedby" /><rs:md capability="description" /><url><loc>http://example.org/ds1/cl.xml</loc><rs:md capability="capabilitylist" /></url><url><loc>http://example.org/ds2/cl.xml</loc><rs:md capability="capabilitylist" /></url><url><loc>http://example.org/ds3/cl.xml</loc><rs:md capability="capabilitylist" /></url></urlset>'
sd=SourceDescription()
sd.parse(str=xml)
sd.parse(str_data=xml)
self.assertEqual( sd.link_href('describedby'), 'http://example.org/about',
'describedby link' )
self.assertEqual( sd.capability, 'description' )