Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
vidjil
vidjil
Commits
60471c5c
Commit
60471c5c
authored
Apr 13, 2015
by
Mikaël Salson
Committed by
Vidjil Team
Apr 13, 2015
Browse files
vidjil_utils: Search and traverse a JSON file
parent
1ced3f8f
Changes
1
Hide whitespace changes
Inline
Side-by-side
server/web2py/applications/vidjil/modules/vidjil_utils.py
View file @
60471c5c
import
math
import
re
import
defs
import
json
from
gluon
import
current
from
datetime
import
date
...
...
@@ -136,6 +137,53 @@ def search_first_regex_in_file(regex, filename, max_nb_line=None):
break
return
matched_keys
#### Utilities on JSON
def
extract_value_from_json_path
(
json_path
,
json
):
'''
Highly inspired from http://stackoverflow.com/a/7320664/1192742
Takes a path (for instance field1/field2/field3) and returns
the value at that path.
If the value doesn't exist None will be returned.
'''
elem
=
json
try
:
for
x
in
json_path
.
strip
(
"/"
).
split
(
"/"
):
elem
=
elem
.
get
(
x
)
except
:
pass
return
elem
def
extract_fields_from_json
(
json_fields
,
pos_in_list
,
filename
):
'''
Takes a map of JSON fields (the key is a common name
and the value is a path) and return a similar map
where the values are the values from the JSON filename.
If the value retrieved from a JSON is an array, we will
get only the item at position <pos_in_list>
'''
try
:
json_dict
=
json
.
loads
(
open
(
filename
).
read
())
except
IOError
:
json_dict
=
{}
matched_keys
=
{}
for
field
in
json_fields
:
value
=
extract_value_from_json_path
(
json_fields
[
field
],
json_dict
)
if
not
isinstance
(
value
,
basestring
):
matched_keys
[
field
]
=
value
[
pos_in_list
]
else
:
matched_keys
[
field
]
=
value
return
matched_keys
####
log_patient
=
re
.
compile
(
'\((\d+)\)'
)
log_config
=
re
.
compile
(
' c(\d+)'
)
log_task
=
re
.
compile
(
'\[(\d+)\]'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment