jrd.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import json
  2. import isodate
  3. from rd.core import RD, Attribute, Element, Link, Property, Title
  4. def _clean_dict(d):
  5. for key in list(d.keys()):
  6. if not d[key]:
  7. del d[key]
  8. def loads(content):
  9. def expires_handler(key, val, obj):
  10. obj.expires = isodate.parse_datetime(val)
  11. def subject_handler(key, val, obj):
  12. obj.subject = val
  13. def aliases_handler(key, val, obj):
  14. for alias in val:
  15. obj.aliases.append(alias)
  16. def properties_handler(key, val, obj):
  17. for ptype, pvalue in list(val.items()):
  18. obj.properties.append(Property(ptype, pvalue))
  19. def titles_handler(key, val, obj):
  20. for tlang, tvalue in list(val.items()):
  21. if tlang == 'default':
  22. tlang = None
  23. obj.titles.append(Title(tvalue, tlang))
  24. def links_handler(key, val, obj):
  25. for link in val:
  26. l = Link()
  27. l.rel = link.get('rel', None)
  28. l.type = link.get('type', None)
  29. l.href = link.get('href', None)
  30. l.template = link.get('template', None)
  31. if 'titles' in link:
  32. titles_handler('title', link['titles'], l)
  33. if 'properties' in link:
  34. properties_handler('property', link['properties'], l)
  35. obj.links.append(l)
  36. def namespace_handler(key, val, obj):
  37. for namespace in val:
  38. ns = list(namespace.keys())[0]
  39. ns_uri = list(namespace.values())[0]
  40. obj.attributes.append(Attribute("xmlns:%s" % ns, ns_uri))
  41. handlers = {
  42. 'expires': expires_handler,
  43. 'subject': subject_handler,
  44. 'aliases': aliases_handler,
  45. 'properties': properties_handler,
  46. 'links': links_handler,
  47. 'titles': titles_handler,
  48. 'namespace': namespace_handler,
  49. }
  50. def unknown_handler(key, val, obj):
  51. if ':' in key:
  52. (ns, name) = key.split(':')
  53. key = "%s:%s" % (ns, name.capitalize())
  54. obj.elements.append(Element(key, val))
  55. doc = json.loads(content)
  56. rd = RD()
  57. for key, value in list(doc.items()):
  58. handler = handlers.get(key, unknown_handler)
  59. handler(key, value, rd)
  60. return rd
  61. def dumps(xrd):
  62. doc = {
  63. "aliases": [],
  64. "links": [],
  65. "namespace": [],
  66. "properties": {},
  67. "titles": [],
  68. }
  69. #list_keys = doc.keys()
  70. for attr in xrd.attributes:
  71. if attr.name.startswith("xmlns:"):
  72. ns = attr.name.split(":")[1]
  73. doc['namespace'].append({ns: attr.value})
  74. if xrd.expires:
  75. doc['expires'] = xrd.expires.isoformat()
  76. if xrd.subject:
  77. doc['subject'] = xrd.subject
  78. for alias in xrd.aliases:
  79. doc['aliases'].append(alias)
  80. for prop in xrd.properties:
  81. doc['properties'][prop.type] = prop.value
  82. for link in xrd.links:
  83. link_doc = {
  84. 'titles': {},
  85. 'properties': {},
  86. }
  87. if link.rel:
  88. link_doc['rel'] = link.rel
  89. if link.type:
  90. link_doc['type'] = link.type
  91. if link.href:
  92. link_doc['href'] = link.href
  93. if link.template:
  94. link_doc['template'] = link.template
  95. for prop in link.properties:
  96. link_doc['properties'][prop.type] = prop.value
  97. for title in link.titles:
  98. lang = title.lang or "default"
  99. link_doc['titles'][lang] = title.value
  100. _clean_dict(link_doc)
  101. doc['links'].append(link_doc)
  102. for elem in xrd.elements:
  103. doc[elem.name.lower()] = elem.value
  104. _clean_dict(doc)
  105. return json.dumps(doc)