]>
git.frykholm.com Git - friends.git/blob - friends/rd/jrd.py
5 from rd
.core
import RD
, Attribute
, Element
, Link
, Property
, Title
9 for key
in list(d
.keys()):
16 def expires_handler(key
, val
, obj
):
17 obj
.expires
= isodate
.parse_datetime(val
)
19 def subject_handler(key
, val
, obj
):
22 def aliases_handler(key
, val
, obj
):
24 obj
.aliases
.append(alias
)
26 def properties_handler(key
, val
, obj
):
27 for ptype
, pvalue
in list(val
.items()):
28 obj
.properties
.append(Property(ptype
, pvalue
))
30 def titles_handler(key
, val
, obj
):
31 for tlang
, tvalue
in list(val
.items()):
32 if tlang
== 'default':
34 obj
.titles
.append(Title(tvalue
, tlang
))
36 def links_handler(key
, val
, obj
):
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)
44 titles_handler('title', link
['titles'], l
)
45 if 'properties' in link
:
46 properties_handler('property', link
['properties'], l
)
49 def namespace_handler(key
, val
, obj
):
51 ns
= list(namespace
.keys())[0]
52 ns_uri
= list(namespace
.values())[0]
53 obj
.attributes
.append(Attribute("xmlns:%s" % ns
, ns_uri
))
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
,
65 def unknown_handler(key
, val
, obj
):
67 (ns
, name
) = key
.split(':')
68 key
= "%s:%s" % (ns
, name
.capitalize())
69 obj
.elements
.append(Element(key
, val
))
71 doc
= json
.loads(content
)
75 for key
, value
in list(doc
.items()):
76 handler
= handlers
.get(key
, unknown_handler
)
77 handler(key
, value
, rd
)
92 #list_keys = doc.keys()
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
})
100 doc
['expires'] = xrd
.expires
.isoformat()
103 doc
['subject'] = xrd
.subject
105 for alias
in xrd
.aliases
:
106 doc
['aliases'].append(alias
)
108 for prop
in xrd
.properties
:
109 doc
['properties'][prop
.type] = prop
.value
111 for link
in xrd
.links
:
119 link_doc
['rel'] = link
.rel
122 link_doc
['type'] = link
.type
125 link_doc
['href'] = link
.href
128 link_doc
['template'] = link
.template
130 for prop
in link
.properties
:
131 link_doc
['properties'][prop
.type] = prop
.value
133 for title
in link
.titles
:
134 lang
= title
.lang
or "default"
135 link_doc
['titles'][lang
] = title
.value
137 _clean_dict(link_doc
)
139 doc
['links'].append(link_doc
)
141 for elem
in xrd
.elements
:
142 doc
[elem
.name
.lower()] = elem
.value
146 return json
.dumps(doc
)