8
8
# A dictionary of jsonld context types mapped to the type name
9
9
JSONLD_TYPES = {}
10
10
11
+ KEY_CONTEXT = '@context'
12
+ KEY_TYPE = '@type'
13
+ KEY_ID = '@id'
14
+
11
15
def register_jsonld_endpoints ():
12
16
manifest = app_qpylib .get_manifest_json ()
13
17
@@ -35,9 +39,10 @@ def register_jsonld_endpoints():
35
39
register_jsonld_type_from_context (jsonld_context )
36
40
37
41
def _extract_jsonld_context (argument , mime_id , context_id ):
38
- if mime_id in argument .keys () and context_id in argument .keys ():
39
- if argument [mime_id ] == 'application/json+ld' :
40
- return argument [context_id ]
42
+ if (mime_id in argument .keys () and
43
+ context_id in argument .keys () and
44
+ argument [mime_id ] == 'application/json+ld' ):
45
+ return argument [context_id ]
41
46
return None
42
47
43
48
def register_jsonld_type_from_context (context ):
@@ -57,12 +62,12 @@ def get_jsonld_type(jsonld_type):
57
62
58
63
def _extract_type (argument ):
59
64
type_id = None
60
- if '@context' in argument .keys ():
61
- context = argument ['@context' ]
62
- if '@type' in context .keys ():
63
- type_id = context ['@type' ]
64
- if type_id == '@id' and '@id' in context .keys ():
65
- type_id = context ['@id' ]
65
+ if KEY_CONTEXT in argument .keys ():
66
+ context = argument [KEY_CONTEXT ]
67
+ if KEY_TYPE in context .keys ():
68
+ type_id = context [KEY_TYPE ]
69
+ if type_id == KEY_ID and KEY_ID in context .keys ():
70
+ type_id = context [KEY_ID ]
66
71
return type_id
67
72
68
73
def render_json_ld_type (jld_type , data , jld_id = None ):
@@ -71,16 +76,16 @@ def render_json_ld_type(jld_type, data, jld_id=None):
71
76
for json_key in data :
72
77
json_dict [json_key ] = data [json_key ]
73
78
74
- json_dict ['@context' ] = jld_context ['@context' ]
75
- json_dict ['@type' ] = jld_type
79
+ json_dict [KEY_CONTEXT ] = jld_context [KEY_CONTEXT ]
80
+ json_dict [KEY_TYPE ] = jld_type
76
81
if jld_id is not None :
77
- json_dict ['@type' ] = jld_type
82
+ json_dict [KEY_TYPE ] = jld_type
78
83
79
84
return json .dumps (json_dict , sort_keys = True )
80
85
81
86
# pylint: disable=too-many-arguments
82
87
def json_ld (jld_context , jld_id , jld_type , name , description , data ):
83
- return json .dumps ({'@context' : jld_context , '@id' : jld_id , '@type' : jld_type , 'name' : name ,
88
+ return json .dumps ({KEY_CONTEXT : jld_context , KEY_ID : jld_id , KEY_TYPE : jld_type , 'name' : name ,
84
89
'description' : description , 'data' : data }, sort_keys = True )
85
90
86
91
def json_html (html ):
0 commit comments