Skip to content
Merged
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
113 changes: 76 additions & 37 deletions JsonPreprocessor/CJsonPreprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ class CSyntaxType():

class CPythonJSONDecoder(json.JSONDecoder):
"""
**Method: PythonJSONDecoder**
**Class: PythonJSONDecoder**

Add python data types and syntax to json. ``True``, ``False`` and ``None`` will be a accepted as json syntax elements.

**Args:**

**json.JSONDecoder** (*object*)

Decoder object provided by ``json.loads``
"""

Expand Down Expand Up @@ -124,27 +127,27 @@ def custom_scan_once(self, string : str, idx : int) -> any:

class CJsonPreprocessor():
"""
CJsonPreprocessor extends the syntax of json.
**Class: CJsonPreprocessor**

Features are
CJsonPreprocessor extends the syntax of json.

- Allow c/c++-style comments within json files.
Features are

// single line or part of single line and /\* \*/ multline comments are possible
- Allow c/c++-style comments within json files.

- Allow to import json files into json files
// single line or part of single line and /\* \*/ multline comments are possible

``"[import]" : "relative/absolute path"``, imports another json file to exactly this location.
- Allow to import json files into json files

``%envariable%`` and ``${envariable}`` can be used, too.
``"[import]" : "relative/absolute path"``, imports another json file to exactly this location.

- Allow use of variables within json files
- Allow use of the defined paramaters within json files

In any place the syntax ``${basenode.subnode. ... nodename}`` allows to reference an already existing variable.
In any place the syntax ``${basenode.subnode. ... nodename}`` allows to reference an already existing parameter.

Example:
* Example:

.. code:: json
.. code::

{
"basenode" : {
Expand All @@ -154,23 +157,28 @@ class CJsonPreprocessor():

},

"myVar" : "${basenode.subnode.myparam}"
"myVar" : ${basenode.subnode.myparam}
}

- Allow python data types ``True``, ``False`` and ``None``
- Allow Python data types ``True``, ``False`` and ``None``
"""

def __init__(self, syntax: CSyntaxType = CSyntaxType.json , currentCfg : dict = {}) -> None:
"""
**Method: __init__**

Constructor

**Args:**

**syntax** (*CSyntaxType*) optional

default: `json` , `python`

If set to `python`, then python data types are allowed as part of json file.

**currentCfg** (*dict*) optional

Internally used to aggregate imported json files.
"""
self.lImportedFiles = []
Expand All @@ -184,26 +192,34 @@ def __init__(self, syntax: CSyntaxType = CSyntaxType.json , currentCfg : dict =
def __sNormalizePath(self, sPath : str) -> str:
"""
**Method: __sNormalizePath**
Python struggles with

- UNC paths
e.g. ``\\hi-z4939\ccstg\....``
- escape sequences in windows paths
e.g. ``c:\autotest\tuner \t`` will be interpreted as tab, the result
Python struggles with

- UNC paths

e.g. ``\\hi-z4939\ccstg\....``

- escape sequences in windows paths

e.g. ``c:\autotest\tuner \t`` will be interpreted as tab, the result
after processing it with an regexp would be ``c:\autotest uner``

In order to solve this problems any slash will be replaced from backslash
to slash, only the two UNC backslashes must be kept if contained.
In order to solve this problems any slash will be replaced from backslash
to slash, only the two UNC backslashes must be kept if contained.

**Args:**

**sPath** (*string*)

Absolute or relative path as input.

Allows environment variables with ``%variable%`` or ``${variable}`` syntax.

**Returns:**

**sPath** (*string*)
normalized path as string

Normalized path as string
"""
# make all backslashes to slash, but mask
# UNC indicator \\ before and restore after.
Expand Down Expand Up @@ -240,19 +256,22 @@ def __mkslash(sPath : str) -> str:
def __processImportFiles(self, input_data : dict) -> dict:
'''
**Method: __processImportFiles**
this is a custom decorder of ``json.loads object_pairs_hook`` function.

This is a custom decorder of ``json.loads object_pairs_hook`` function.

This method helps to import json files which are provided in ``"[import]"`` keyword into the current json file.

**Args:**

**input_data** (*dict*)
dictionary from json file as input

Dictionary from json file as input

**Returns:**

**out_dict** (*dict*)
dictionary as output

dictionary is extended if ``"[import]"`` found and properly imported.
Dictionary as output, dictionary is extended if ``"[import]"`` found and properly imported.
'''
out_dict = {}

Expand All @@ -279,18 +298,23 @@ def __processImportFiles(self, input_data : dict) -> dict:
def __load_and_removeComments(self, jsonFile : str) -> str:
"""
**Method: __load_and_removeComments**
loads a given json file and filters all C/C++ style comments.

Loads a given json file and filters all C/C++ style comments.

**Args:**

**jsonFile** (*string*)
path (absolute/relative/) file to be processed.

Path (absolute/relative/) file to be processed.
The path can contain windows/linux style environment variables.

!ATTENTION! This is dangerous

**Returns:**

**sContentCleaned** (*string*)
string version of json file after removing all comments.

String version of json file after removing all comments.
"""

def replacer(match):
Expand All @@ -313,15 +337,20 @@ def replacer(match):
def __nestedParamHandler(self, sInputStr : str) -> str:
'''
**Method: __nestedParamHandler**
This method handles nested variables in parameter names or values. Variable syntax is ${Variable_Name}.

This method handles nested variables in parameter names or values. Variable syntax is ${Variable_Name}.

**Args:**
**sInputStr** (*string*)
Parameter name or value which contains a nested variable.

**sInputStr** (*string*)

Parameter name or value which contains a nested variable.

**Returns:**
**sStrHandled** (*string*)
String which contains the resolved variable.

**sStrHandled** (*string*)

String which contains the resolved variable.
'''

#globals().update(currentCfg)
Expand Down Expand Up @@ -384,15 +413,20 @@ def __nestedParamHandler(self, sInputStr : str) -> str:
def __updateAndReplaceNestedParam(self, oJson : dict, recursive : bool = False):
'''
**Method: __updateAndReplaceNestedParam**

This method replaces all nested parameters in key and value of a json object .

**Args:**

**oJson** (*dict*)

Input Json object as dictionary. This dictionary will be searched for all ``${variable}`` occurences.
If found it will be replaced with it's current value.

**Returns:**

**oJsonOut** (*dict*)

Output Json object as dictionary with all variables resolved.
'''

Expand Down Expand Up @@ -472,19 +506,24 @@ def __updateAndReplaceNestedParam(self, oJson : dict, recursive : bool = False):
def jsonLoad(self, jFile : str, masterFile : bool = True):
'''
**Method: jsonLoad**

This function is the entry point of JsonPreprocessor.

It loads the json file, preprocesses it and returns the preprocessed result as data structure.

**Args:**

**jFile** (*string*)
relative/absolute path to main json file.

Relative/absolute path to main json file.

``%envvariable%`` and ``${envvariable}`` can be used, too in order to access environment variables.

**Returns:**
**Returns:**

**oJson** (*dict*)
preprocessed json file(s) as data structure

Preprocessed json file(s) as dictionary data structure
'''
jFile=jFile.strip()

Expand Down
Binary file modified JsonPreprocessor/JsonPreprocessor.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ After the build processes is completed, the package is located in

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)
[JsonPreprocessor.pdf](https://github.com/test-fullautomation/python-jsonpreprocessor/blob/develop/JsonPreprocessor/JsonPreprocessor.pdf)

## Feedback

Expand Down
Loading