This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
workspace_addjson.py
111 lines (95 loc) · 4.92 KB
/
workspace_addjson.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""
Copyright 2018 IBM Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import json, sys, os, os.path, argparse, codecs
from cfgCommons import Cfg
from wawCommons import setLoggerConfig, getScriptLogger, getRequiredParameter
import logging
logger = getScriptLogger(__file__)
try:
basestring # Python 2
except NameError:
basestring = (str, ) # Python 3
nothingFound = True
# function to find a desired key in complex json and add other part of json
def includeJson(nodeJSON, keyJSON, keySearch, includeJSON):
global nothingFound
logger.info(keyJSON)
# check if inputs are ok
if nodeJSON is None or keyJSON is None:
return
# at first call keyJSON does not exist
if isinstance(nodeJSON, dict) and keyJSON not in nodeJSON:
return
# at first call keyJSON is not dict or key is not valid
if isinstance(nodeJSON, list) and (not isinstance(keyJSON, int) or keyJSON not in range(len(nodeJSON))):
return
# set the includeJSON to the key
if keyJSON == keySearch:
nodeJSON[keyJSON] = includeJSON
nothingFound = False
# None
if nodeJSON[keyJSON] is None:
pass
# list
elif isinstance(nodeJSON[keyJSON], list):
for i in range(len(nodeJSON[keyJSON])):
includeJson(nodeJSON[keyJSON], i, keySearch, includeJSON)
# dict
elif isinstance(nodeJSON[keyJSON], dict):
for subKeyJSON in nodeJSON[keyJSON]:
includeJson(nodeJSON[keyJSON], subKeyJSON, keySearch, includeJSON)
def main(args):
logger.info('STARTING: ' + os.path.basename(__file__))
parser = argparse.ArgumentParser(description='This script takes a workspace JSON as one parameter and another JSON (i.e., piece of context data structure) and put the second one into desired place in the first one. This happens inplace.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# arguments
parser.add_argument('-c', '--common_configFilePaths', help='configuaration file', action='append')
parser.add_argument('-w','--common_outputs_workspace', required=False, help='filename of the original workspace JSON')
parser.add_argument('-d','--common_outputs_directory', required=False, help='directory, where the workspace is located')
parser.add_argument('-j','--includejsondata_jsonfile', required=False, help='file with JSON you want to include')
parser.add_argument('-t','--includejsondata_targetkey', required=False, help='the key, where you want to add your JSON, i.e., "data_structure" : null; where you want to replace null, you would put "data_strucute" as this parameter')
# optional arguments
parser.add_argument('-v','--verbose', required=False, help='verbosity', action='store_true')
#init the parameters
args = parser.parse_args(args)
config = Cfg(args)
VERBOSE = hasattr(config, 'common_verbose')
# get required parameters
# workspace
with codecs.open(os.path.join(getRequiredParameter(config, 'common_outputs_directory'), getRequiredParameter(config, 'common_outputs_workspace')), 'r', encoding='utf8') as inputpath:
try:
workspaceInput = json.load(inputpath)
except:
logger.error('Workspace JSON is not valid JSON: %s', os.path.join(getRequiredParameter(config, 'common_outputs_directory'), getRequiredParameter(config, 'common_outputs_workspace')))
exit(1)
# json to add
with codecs.open(os.path.join(getRequiredParameter(config, 'includejsondata_jsonfile')), 'r', encoding='utf8') as jsonincludepath:
try:
jsonInclude = json.load(jsonincludepath)
except:
logger.error('JSON to include is not valid JSON: %s', os.path.join(getRequiredParameter(config, 'includejsondata_jsonfile')))
exit(1)
# target element
targetKey = getRequiredParameter(config, 'includejsondata_targetkey')
# find the target key and add the json
includeJson(workspaceInput, "dialog_nodes", targetKey, jsonInclude)
# writing the file
with codecs.open(os.path.join(getattr(config, 'common_outputs_directory'), getattr(config, 'common_outputs_workspace')), 'w', encoding='utf8') as outfile:
json.dump(workspaceInput, outfile, indent=4)
if nothingFound is True:
logger.warning('Target key not found.')
else:
logger.info('Writing workspaces with added JSON successfull.')
logger.info('FINISHING: ' + os.path.basename(__file__))
if __name__ == '__main__':
setLoggerConfig()
main(sys.argv[1:])