Example #1
0
def read_config(resources, path):
	""" build and validate a ConfigObj using config file found at given filesystem path """

	_locate_config_files(path)

	# grab contents of config file
	with open(os.path.join(path, "mediarover.conf"), "r") as f:
		file = f.readlines()

	version_check = re.compile("__version__")

	# TODO the next time the user is required to regenerate the config file this code will no longer
	# be needed.  Between version 1 and version 2, __version__ was moved into the __SYSTEM__ subsection
	version = 0
	if version_check.search(file[0]):
		value = file[0]
	elif version_check.search(file[-1]):
		value = file[-1]
	
	if value:
		(left, sep, right) = value.partition("=")
		version = right.strip(" \n")

	# check if users config file is current
	if version > 0:
		if int(version) < int(__config_version__.get('min', __config_version__['version'])):
			raise ConfigurationError("Configuration file is out of date and needs to be regenerated! See `python mediarover.py write-configs --help` for instructions")
	else:
		raise ConfigurationError("Out of date or corrupt configuration file! See `python mediarover.py write-configs --help` for instructions")

	spec = os.path.join(resources, "config.spec")
	config = ConfigObj(file, configspec=spec)

	# validate config options
	results = config.validate(_get_validator(), preserve_errors=True)
	if results != True:
		results = flatten_errors(config, results)
		message = ["ERROR: Encountered the following configuration error(s):"]
		for error in results:
			level = 1
			section = []
			dict = config
			for key in error[0]:
				section.append(("[" * level) + key + ("]" * level))
				dict = dict[key]
				level += 1
			message.append(" %s %s = %s" % (" ".join(section), error[1], error[2]))
		raise ConfigurationError("Invalid Data in configuration file\n\n%s\n" % "\n".join(message))

	return config
Example #2
0
def build_mediarover_config(resources, path):
	""" build and validate config object using mediarover.conf at given path """

	# grab contents of config file
	with open(os.path.join(path, "mediarover.conf"), "r") as f:
		file = f.readlines()

	config = ConfigObj(file, configspec=os.path.join(resources, "config.spec"))

	# validate config object
	_validate_config(config, _get_validator(), "mediarover.conf")

	# check if users config file is current
	if '__SYSTEM__' in config and '__version__' in config['__SYSTEM__']:
		version = config['__SYSTEM__']['__version__']
		if int(version) < int(__config_version__.get('min', __config_version__['version'])):
			raise ConfigurationError("Configuration file is out of date and needs to be regenerated! See `python mediarover.py write-configs --help` for instructions")
	else:
		raise ConfigurationError("Out of date or corrupt configuration file! See `python mediarover.py write-configs --help` for instructions")

	return config