Skip to content

Commit 06564aa

Browse files
committed
Use json and lxml directly
1 parent d60c039 commit 06564aa

File tree

8 files changed

+40
-78
lines changed

8 files changed

+40
-78
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
zip_safe=False,
3737
install_requires=[
3838
"httplib2",
39+
"lxml",
3940
],
4041
# Following left in as a memory aid for later-
4142
#entry_points="""

sword2/atom_objects.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
Also provides Category, which is a convenience function to simplify reading in category information from an atom:entry
1010
"""
1111

12+
from datetime import datetime
13+
from lxml import etree
14+
1215
from .sword2_logging import logging
1316
from .implementation_info import __version__
1417
coll_l = logging.getLogger(__name__)
1518

16-
from .compatible_libs import etree
19+
1720
from .utils import NS, get_text
1821

19-
from datetime import datetime
2022

2123
class Category(object):
2224
"""Convenience class to aid in the intepreting of atom:category elements in XML. Currently, this is read-only.
@@ -263,11 +265,11 @@ def add_contributor(self, name, uri=None, email=None):
263265

264266
def __str__(self):
265267
"""Export the XML to a bytestring, ready for use"""
266-
xml_str = etree.tostring(self.entry)
268+
xml_str = etree.tounicode(self.entry)
267269
if not xml_str.startswith('<?xml version="1.0"?>'):
268270
xml_str = '<?xml version="1.0"?>' + xml_str
269271
return xml_str
270-
272+
271273
def pretty_print(self):
272274
"""A version of the XML document which should be slightly more readable on the command line."""
273275
return etree.tostring(self.entry, pretty_print=True)

sword2/collection.py

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
for the things they logically handle.
1414
1515
"""
16+
import json
1617

1718
from .sword2_logging import logging
1819
from .implementation_info import __version__
20+
1921
coll_l = logging.getLogger(__name__)
2022

21-
from .compatible_libs import etree
23+
from lxml import etree
2224
from .utils import NS, get_text
2325

2426
from .deposit_receipt import Deposit_Receipt
@@ -46,18 +48,19 @@ class SDCollection(object):
4648
>>> c.title
4749
"Thesis Deposit"
4850
"""
51+
4952
def __init__(self, title=None,
50-
href=None,
51-
accept=[],
52-
accept_multipart=[],
53-
categories=[],
54-
collectionPolicy=None,
55-
description = None,
56-
mediation=None,
57-
treatment=None,
58-
acceptPackaging=[],
59-
service=[],
60-
dom=None):
53+
href=None,
54+
accept=[],
55+
accept_multipart=[],
56+
categories=[],
57+
collectionPolicy=None,
58+
description=None,
59+
mediation=None,
60+
treatment=None,
61+
acceptPackaging=[],
62+
service=[],
63+
dom=None):
6164
"""
6265
Creates a `Collection` object - as used by `sword2.Service_Document`
6366
@@ -110,7 +113,7 @@ def __init__(self, title=None,
110113
Again, this step is done by the `sword2.Service_Document`, but if the above XML was in the `doc` variable:
111114
112115
# Get an etree-compatible library, such as from `lxml.etree`, `xml.etree` or `elementtree.ElementTree`
113-
>>> from sword2.compatible_libs import etree
116+
>>> from lxml import etree
114117
>>> from sword2 import SDCollection
115118
>>> dom = etree.fromstring(doc)
116119
@@ -224,31 +227,24 @@ def to_json(self):
224227
225228
NB this uses the attributes of the object, not the cached DOM object, so information can be altered/added
226229
on the fly."""
227-
from .compatible_libs import json
228-
if json:
229-
_j = {'title':self.title,
230-
'href':self.href,
231-
'description':self.description,
232-
'accept':self.accept,
233-
'accept_multipart':self.accept_multipart,
234-
'mediation':self.mediation,
235-
'treatment':self.treatment,
236-
'collectionPolicy':self.collectionPolicy,
237-
'acceptPackaging':self.acceptPackaging,
238-
'service':self.service,
239-
'categories':self.categories}
240-
return json.dumps(_j)
241-
else:
242-
coll_l.error("Could not return information about Collection '%s' as JSON" % self.title)
243-
return
230+
return json.dumps({'title': self.title,
231+
'href': self.href,
232+
'description': self.description,
233+
'accept': self.accept,
234+
'accept_multipart': self.accept_multipart,
235+
'mediation': self.mediation,
236+
'treatment': self.treatment,
237+
'collectionPolicy': self.collectionPolicy,
238+
'acceptPackaging': self.acceptPackaging,
239+
'service': self.service,
240+
'categories': self.categories})
241+
244242

245243
class Collection_Feed(object):
246244
"""Nothing to see here yet. Move along."""
245+
247246
def __init__(self, feed_iri=None, http_client=None, feed_xml=None):
248247
self.feed_xml = feed_xml
249248
self.feed_iri = feed_iri
250249
self._cached = []
251250
self.h = http_client
252-
253-
254-

sword2/compatible_libs.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

sword2/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from .statement import Atom_Sword_Statement, Ore_Sword_Statement
2222
from .exceptions import *
2323

24-
from .compatible_libs import etree
24+
from lxml import etree
2525

2626
# import httplib2
2727
from . import http_layer

sword2/deposit_receipt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from .atom_objects import Category
1717

18-
from .compatible_libs import etree
18+
from lxml import etree
1919
from .utils import NS, get_text
2020

2121
class Deposit_Receipt(object):

sword2/service_document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
from .collection import SDCollection
5757

58-
from .compatible_libs import etree
58+
from lxml import etree
5959
from .utils import NS, get_text
6060

6161
class ServiceDocument(object):

sword2/statement.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from .atom_objects import Category
44
from .deposit_receipt import Deposit_Receipt
55
from .sword2_logging import logging
6-
from .compatible_libs import etree
6+
from lxml import etree
77

88
s_l = logging.getLogger(__name__)
99

0 commit comments

Comments
 (0)