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