123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- import json
- import isodate
- from rd.core import RD, Attribute, Element, Link, Property, Title
- def _clean_dict(d):
- for key in list(d.keys()):
- if not d[key]:
- del d[key]
- def loads(content):
- def expires_handler(key, val, obj):
- obj.expires = isodate.parse_datetime(val)
- def subject_handler(key, val, obj):
- obj.subject = val
- def aliases_handler(key, val, obj):
- for alias in val:
- obj.aliases.append(alias)
- def properties_handler(key, val, obj):
- for ptype, pvalue in list(val.items()):
- obj.properties.append(Property(ptype, pvalue))
- def titles_handler(key, val, obj):
- for tlang, tvalue in list(val.items()):
- if tlang == 'default':
- tlang = None
- obj.titles.append(Title(tvalue, tlang))
- def links_handler(key, val, obj):
- for link in val:
- l = Link()
- l.rel = link.get('rel', None)
- l.type = link.get('type', None)
- l.href = link.get('href', None)
- l.template = link.get('template', None)
- if 'titles' in link:
- titles_handler('title', link['titles'], l)
- if 'properties' in link:
- properties_handler('property', link['properties'], l)
- obj.links.append(l)
- def namespace_handler(key, val, obj):
- for namespace in val:
- ns = list(namespace.keys())[0]
- ns_uri = list(namespace.values())[0]
- obj.attributes.append(Attribute("xmlns:%s" % ns, ns_uri))
- handlers = {
- 'expires': expires_handler,
- 'subject': subject_handler,
- 'aliases': aliases_handler,
- 'properties': properties_handler,
- 'links': links_handler,
- 'titles': titles_handler,
- 'namespace': namespace_handler,
- }
- def unknown_handler(key, val, obj):
- if ':' in key:
- (ns, name) = key.split(':')
- key = "%s:%s" % (ns, name.capitalize())
- obj.elements.append(Element(key, val))
- doc = json.loads(content)
- rd = RD()
- for key, value in list(doc.items()):
- handler = handlers.get(key, unknown_handler)
- handler(key, value, rd)
- return rd
- def dumps(xrd):
- doc = {
- "aliases": [],
- "links": [],
- "namespace": [],
- "properties": {},
- "titles": [],
- }
- #list_keys = doc.keys()
- for attr in xrd.attributes:
- if attr.name.startswith("xmlns:"):
- ns = attr.name.split(":")[1]
- doc['namespace'].append({ns: attr.value})
- if xrd.expires:
- doc['expires'] = xrd.expires.isoformat()
- if xrd.subject:
- doc['subject'] = xrd.subject
- for alias in xrd.aliases:
- doc['aliases'].append(alias)
- for prop in xrd.properties:
- doc['properties'][prop.type] = prop.value
- for link in xrd.links:
- link_doc = {
- 'titles': {},
- 'properties': {},
- }
- if link.rel:
- link_doc['rel'] = link.rel
- if link.type:
- link_doc['type'] = link.type
- if link.href:
- link_doc['href'] = link.href
- if link.template:
- link_doc['template'] = link.template
- for prop in link.properties:
- link_doc['properties'][prop.type] = prop.value
- for title in link.titles:
- lang = title.lang or "default"
- link_doc['titles'][lang] = title.value
- _clean_dict(link_doc)
- doc['links'].append(link_doc)
- for elem in xrd.elements:
- doc[elem.name.lower()] = elem.value
- _clean_dict(doc)
- return json.dumps(doc)
|