X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=mc-core%2Fpackage%2Fj2src.py;fp=mc-core%2Fpackage%2Fj2src.py;h=22ecb043972ae43aa9ae2eb5c5e5f31192892b9c;hb=3d4b88135c1a1429d8cecb7380cfc2f8c67d04ef;hp=0000000000000000000000000000000000000000;hpb=79eb47c2cfd81b81e911e69b67b99c965990f9a2;p=ric-app%2Fmc.git diff --git a/mc-core/package/j2src.py b/mc-core/package/j2src.py new file mode 100755 index 0000000..22ecb04 --- /dev/null +++ b/mc-core/package/j2src.py @@ -0,0 +1,152 @@ +#!/usr/bin/env python3 +# vi: et ts=4 sw=4 : + +#---------------------------------------------------------------------------------- +# +# Copyright (c) 2021 AT&T Intellectual Property. +# +# 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. +# +#--------------------------------------------------------------------------------- + + +# Abstract: A config file (json) parser that looks for one or more "descriptions" and outputs +# shell styled variable assignments that can be sourced by a script. Descriptions +# are of the form: +# :[:...:{*|shell_var_name} +# +# The description "xapp_name:*" will find the field xapp_name at the top level +# and generate the variable assignment using the field name. The description +# "xapp_name:xname" would find the same field but generate "xname=value" in the +# output. +# +# It may be necssary to pull a field from an array of objects where a second field +# in the object has a desired value. As an example, in the messaging section there +# is an expected array of ports with each port having a name. To exctact a field +# like this, the final field in the list may have the form: +# []=@ +# +# For "messaging:port[]name=rmr-data@port" +# The messaging object is first located, and each element in the port array is examined. +# when the element which contains the field "name:, with a value of "rmr-data", +# the value of "port" is assigned to the output shell variable name. +# +# Limitations: This only allows one array to be traversed, and it is assumed to be the +# last field. In other words, nested object arrays are not supported. +# +# Usage: j2src [...] +# +# Date: 29 Janurary 2021 +# Author: E. Scott Daniels +# ------------------------------------------------------------------------------------------------------ +import sys +import json + +# Parse the description (see above) and return name and value to the caller. None,None is returned +# when we have an error, or cannot find the field. Debug strings MUST start with # so that they don't +# affect shell parsing of output. +# +def parse( pj, description, debug=False ) : + tokens = description.split( ":" ) # split fields, last is the output name or * + out_name = tokens[-1] + value = None + + if len( tokens ) < 2 : + print( "## ERR ## badly formed description: %s" % description ) + return None, None + + for i in range( len( tokens ) - 1 ) : + atoks = tokens[i].split( "[]" ) + if len( atoks ) > 1 : # array; [0] is the name, [1] is name=value@desired-name + if atoks[0] in pj : + nv = atoks[1].split( "=" ) + name = nv[0] + sv = nv[1].split( "@" ) + if len( sv ) < 2 : + if( debug ) : + print( "## ERR ## badly formed capture string: missing 'value