DiffSK / configobj

Python 3+ compatible port of the configobj library

Home Page:https://configobj.readthedocs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

String interpolation of values from a list in the same section

namanskumar opened this issue · comments

@jhermann @VamsikrishnaNallabothu
I am new to ConfigObj and am trying something as shown below
ngood = 4, 5, 6 good_directory = godown_%(ngood[0])s, godown_%(ngood[1])s, godown_%(ngood[2])s

This gives an error:
File "<string>", line unknown MissingInterpolationOption: missing option "ngood[0]" in interpolation.

Please let me know the way to correctly obtain respective values

"""
# ---------------------------- Config File reference automation ------------------------------- #
"""

def find_reference_vals(self, st):
	"""
	st is a string that may contain multiple references
	:param st: <string>
	:return: list
	"""
	strng_replacement = 0
	refs_in_str = re.findall(r'\${.+?}', st)
	if refs_in_str == []:
		return [st]
	new_refs_in_str = []
	for ref in refs_in_str:
		ref1 = ref[2:-1]
		ref_keys = ref1.split('.')
		max_depth = len(ref_keys)
		pre_text = self.config
		if max_depth == 1:
			new_refs_in_str.append(self.config[ref1])
		for i in ref_keys:
				if type(pre_text) == dict or 'configobj' in str(type(pre_text)):
					if i in pre_text.keys():
						pre_text = pre_text[i]
				elif type(pre_text) == list:
					try:
						if int(i) in range(len(pre_text)):
							pre_text = pre_text[int(i)]
					except Exception as e:
						self.logger.error("the list key should have integers as extension")
						self.logger.error(e)
						sys.exit(1)
				else:
					self.logger.error("Malformed references spotted in the config file")
					self.logger.error(ref)
					sys.exit(1)
		if ref != st and ref in st:
			if type(pre_text)== str:
				st = st.replace(ref, pre_text, 1)
				strng_replacement += 1
			else:
				error_warn = "Type of the Value {} for the reference_key {} can not be " \
				             "concatenated with string: {}".format(pre_text, ref, st)
				self.logger.error(error_warn)
				sys.exit(1)
		else:
			new_refs_in_str.append(pre_text)
	if strng_replacement !=0:
		new_refs_in_str.append(st)
	return new_refs_in_str

def search_and_realize_references(self, config):
	"""
	takes in a config file
	finds if there are any references ... which has format: ${references}
	:param config:
	:return:
	"""
	if isinstance(config, dict):
		for k, v in config.iteritems():
			config[k] = self.search_and_realize_references(v)
	elif isinstance(config, list):
		for elem in config:
			self.search_and_realize_references(elem)
	else:
		config = self.find_reference_vals(st=config)[0]
	# self.logger.info("All references mapped to the actual values in the Config File")
	return config

I use the above two functions like this:
self.config = self.get_config("config_file.cfg")
self.config = self.search_and_realize_references(config=self.config)

References would look like this:
Config File:

[Networks]
       [[MANAGEMENT_NETWORK]]
                NAME = "CVBFDSDF"
                 [[[[POOL]]]
                        X = Y,Z
                         L = M
 MANAGEMENT_NETWORK = ${NETWORKS.MANAGEMENT_NETWORK}  
 BACKPLANE_NETWORK = ${NETWORKS.BACKPLANE_NETWORK}

closing this as both "it's 3 years ago" and it appears as if there was a code fix for the specific issue