Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 51 additions & 43 deletions JsonPreprocessor/CJsonPreprocessor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020-2022 Robert Bosch Car Multimedia GmbH
# Copyright 2020-2022 Robert Bosch GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -58,16 +58,21 @@ class CSyntaxType():
(re.VERBOSE | re.MULTILINE | re.DOTALL))

class CPythonJSONDecoder(json.JSONDecoder):
""" Add below python values when scanning json data
"""
Some keywords are different in Python and Json

.. table:: Python vs. Json keywords
:widths: auto

+---------------+-------------------+
| True | True |
+---------------+-------------------+
| False | False |
+---------------+-------------------+
| None | None |
+---------------+-------------------+
======= =======
Python Json
======= =======
True true
False false
None null
======= =======
"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.scan_once = self.custom_scan_once
Expand Down Expand Up @@ -123,24 +128,26 @@ def custom_scan_once(self, string, idx):

class CJsonPreprocessor():
'''
CJsonPreprocessor helps to handle configuration file as json format:
- Allow comment within json file
- Allow import json file within json file
CJsonPreprocessor helps to handle configuration file as json format:

- Allow comment within json file
- Allow import json file within json file
'''
def __init__(self, syntax=CSyntaxType.json, currentCfg={}):
'''
Method: __processImportFiles this is custom decorder of object_pairs_hook function.

This method helps to import json file which is provided in '[import]' keyword into current json file.

Returns:
Dictionary is parsed from json file.
'''
self.lImportedFiles = []
self.recursive_level = 0
self.syntax = syntax
self.currentCfg = currentCfg
self.lUpdatedParams = []


'''
Method: __processImportFiles this is custom decorder of object_pairs_hook function.
This method helps to import json file which is provided in '[import]' keyword into current json file.
Returns:
Dictionary is parsed from json file.
'''
def __processImportFiles(self, input_data):
out_dict = {}
for key, value in input_data:
Expand All @@ -161,13 +168,13 @@ def __processImportFiles(self, input_data):
out_dict[key] = value
return out_dict

'''
Method: __removeComments loads json config file which allows comments inside
Args:
jsonFile: string
Returns:
lJsonData: list, list of string data from jsonFile after removing comment(s).
'''
# '''
# Method: __removeComments loads json config file which allows comments inside
# Args:
# jsonFile: string
# Returns:
# lJsonData: list, list of string data from jsonFile after removing comment(s).
# '''
def __removeComments(self, jsonFile):
jsonPath = ''
if '/' in jsonFile:
Expand Down Expand Up @@ -205,14 +212,14 @@ def __removeComments(self, jsonFile):
lJsonData.append(line)
return lJsonData, jsonPath

'''
private __nestedParamHandler: This method handles the nested variable in param names or value
in updated json config file.
Args:
sInputStr: string - param name or value which contains nested variable
Returns:
sStrHandled: string
'''
# '''
# private __nestedParamHandler: This method handles the nested variable in param names or value
# in updated json config file.
# Args:
# sInputStr: string - param name or value which contains nested variable
# Returns:
# sStrHandled: string
# '''
def __nestedParamHandler(self, sInputStr):

#globals().update(currentCfg)
Expand Down Expand Up @@ -271,14 +278,14 @@ def __nestedParamHandler(self, sInputStr):
sStrHandled = fullVariable
return sStrHandled

'''
private __updateAndReplaceNestedParam: this method replaces all nested params in key and value of Json object
Args:
oJson: dict
currentCfg: dict
Returns:
oJsonOut: dict
'''
# '''
# private __updateAndReplaceNestedParam: this method replaces all nested params in key and value of Json object
# Args:
# oJson: dict
# currentCfg: dict
# Returns:
# oJsonOut: dict
# '''
def __updateAndReplaceNestedParam(self, oJson, recursive=False):

if bool(self.currentCfg) and not recursive:
Expand Down Expand Up @@ -359,6 +366,7 @@ def jsonLoad(self, jFile, masterFile=True):

Args:
jFile: string, json file input

Returns:
oJson: dict
'''
Expand Down Expand Up @@ -392,4 +400,4 @@ def jsonLoad(self, jFile, masterFile=True):
oJson = self.__updateAndReplaceNestedParam(oJson)
# oJson['JsonPath'] = jsonPath # is JsonPath required?

return oJson
return oJson
Binary file added JsonPreprocessor/JsonPreprocessor.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion JsonPreprocessor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020-2022 Robert Bosch Car Multimedia GmbH
# Copyright 2020-2022 Robert Bosch GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
23 changes: 23 additions & 0 deletions JsonPreprocessor/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# **************************************************************************************************************
#
# Copyright 2020-2022 Robert Bosch GmbH
#
# 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.
#
# **************************************************************************************************************
#
# Version and date of JsonPreprocessor
#
VERSION = "0.1.2"
VERSION_DATE = "01.07.2022"

2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ package documentation is located in 'doc/_build/'.
Package Documentation
---------------------

A detailed documentation of the Json Preprocessor's package can be found here: `Json-Preprocessor.pdf <https://github.com/test-fullautomation/python-jsonpreprocessor/blob/develop/doc/_build/latex/Json-Preprocessor.pdf>`_
A detailed documentation of the Json Preprocessor's package can be found here: `JsonPreprocessor.pdf <https://github.com/test-fullautomation/python-jsonpreprocessor/blob/develop/JsonPreprocessor/JsonPreprocessor.pdf>`_

Feedback
--------
Expand Down
Loading