Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
LOFAR
Manage
Activity
Members
Labels
Plan
Issues
Wiki
Jira issues
Open Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RadioObservatory
LOFAR
Commits
cb8e95ec
Commit
cb8e95ec
authored
3 years ago
by
Jorrit Schaap
Browse files
Options
Downloads
Patches
Plain Diff
TMSS-717
: use caching
parent
7affe7f1
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!447
TMSS-717: merge commissioning fixes back to master
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
LCS/PyCommon/json_utils.py
+23
-10
23 additions, 10 deletions
LCS/PyCommon/json_utils.py
with
23 additions
and
10 deletions
LCS/PyCommon/json_utils.py
+
23
−
10
View file @
cb8e95ec
...
@@ -16,6 +16,8 @@
...
@@ -16,6 +16,8 @@
# with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
# with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
import
json
import
json
import
time
import
jsonschema
import
jsonschema
from
copy
import
deepcopy
from
copy
import
deepcopy
import
requests
import
requests
...
@@ -159,19 +161,31 @@ def get_referenced_subschema(ref_url, cache: dict=None, max_cache_age: timedelta
...
@@ -159,19 +161,31 @@ def get_referenced_subschema(ref_url, cache: dict=None, max_cache_age: timedelta
'''
fetch the schema given by the ref_url, and get the sub-schema given by the #/ path in the ref_url
'''
'''
fetch the schema given by the ref_url, and get the sub-schema given by the #/ path in the ref_url
'''
# deduct referred schema name and version from ref-value
# deduct referred schema name and version from ref-value
head
,
anchor
,
tail
=
ref_url
.
partition
(
'
#
'
)
head
,
anchor
,
tail
=
ref_url
.
partition
(
'
#
'
)
def
_fech_url_and_update_cache_entry_if_needed
():
# try to fetch the url a few time (jsonschema.org is down quite often, but only for a brief moment)
for
attempt_nr
in
range
(
5
):
try
:
response
=
requests
.
get
(
ref_url
)
if
response
.
status_code
==
200
:
referenced_schema
=
json
.
loads
(
response
.
text
)
if
isinstance
(
cache
,
dict
):
cache
[
head
]
=
referenced_schema
,
datetime
.
utcnow
()
return
referenced_schema
except
requests
.
exceptions
.
RequestException
as
e
:
time
.
sleep
(
2
)
# retry after a little sleep
raise
Exception
(
"
Could not get: %s
"
%
(
ref_url
,))
if
isinstance
(
cache
,
dict
)
and
head
in
cache
:
if
isinstance
(
cache
,
dict
)
and
head
in
cache
:
# use cached value
# use cached value
referenced_schema
,
last_update_timestamp
=
cache
[
head
]
referenced_schema
,
last_update_timestamp
=
cache
[
head
]
# refresh cache if outdated
# refresh cache if outdated
if
datetime
.
utcnow
()
-
last_update_timestamp
>
max_cache_age
:
if
datetime
.
utcnow
()
-
last_update_timestamp
>
max_cache_age
:
referenced_schema
=
json
.
loads
(
requests
.
get
(
ref_url
).
text
)
referenced_schema
=
_fech_url_and_update_cache_entry_if_needed
()
cache
[
head
]
=
referenced_schema
,
datetime
.
utcnow
()
else
:
else
:
# fetch url, and store in cache
# fetch url, and store in cache
referenced_schema
=
json
.
loads
(
requests
.
get
(
ref_url
).
text
)
referenced_schema
=
_fech_url_and_update_cache_entry_if_needed
()
if
isinstance
(
cache
,
dict
):
cache
[
head
]
=
referenced_schema
,
datetime
.
utcnow
()
# extract sub-schema
# extract sub-schema
tail
=
tail
.
strip
(
'
/
'
)
tail
=
tail
.
strip
(
'
/
'
)
...
@@ -222,13 +236,12 @@ def get_refs(schema) -> set:
...
@@ -222,13 +236,12 @@ def get_refs(schema) -> set:
return
refs
return
refs
def
validate_json_against_its_schema
(
json_object
:
dict
):
def
validate_json_against_its_schema
(
json_object
:
dict
,
cache
:
dict
=
None
,
max_cache_age
:
timedelta
=
DEFAULT_MAX_SCHEMA_CACHE_AGE
):
'''
validate the give json object against its own schema (the URI/URL that its propery $schema points to)
'''
'''
validate the give json object against its own schema (the URI/URL that its propery $schema points to)
'''
schema_url
=
json_object
[
'
$schema
'
]
schema_url
=
json_object
[
'
$schema
'
]
response
=
requests
.
get
(
schema_url
,
headers
=
{
"
Accept
"
:
"
application/json
"
})
schema_object
=
get_referenced_subschema
(
schema_url
,
cache
=
cache
,
max_cache_age
=
max_cache_age
)
if
response
.
status_code
==
200
:
return
validate_json_against_schema
(
json_object
,
schema_object
)
return
validate_json_against_schema
(
json_object
,
response
.
text
)
raise
jsonschema
.
exceptions
.
ValidationError
(
"
Could not get schema from
'
%s
'
\n
%s
"
%
(
schema_url
,
str
(
response
.
text
)))
def
validate_json_against_schema
(
json_string
:
str
,
schema
:
str
):
def
validate_json_against_schema
(
json_string
:
str
,
schema
:
str
):
'''
validate the given json_string against the given schema.
'''
validate the given json_string against the given schema.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment