Ejemplo n.º 1
0
	def __init__(self, filler=None, config_file_path=None, load_old_config=True, path_base=None):
		def _get_name(prefix=''):
			if config_file_path:
				return ('%s.%s' % (prefix, get_file_name(config_file_path))).strip('.')
			elif prefix:
				return prefix
			return 'unnamed'

		try:
			config_dn = os.getcwd()
		except Exception:
			raise ConfigError('The current directory does not exist!')
		if config_file_path:
			config_dn = os.path.dirname(resolve_path(config_file_path,
				search_path_list=[os.getcwd()], exception_type=ConfigError))
		if path_base:
			config_dn = os.path.dirname(resolve_path(path_base, search_path_list=[os.getcwd()]))

		# Init config containers
		self._container_cur = ConfigContainer('current')
		if filler:  # Read in the current configuration ...
			filler.fill(self._container_cur)
		self._container_cur.resolve()  # resolve interpolations

		logging.getLogger('config.stored').propagate = False
		container_old = ConfigContainer('stored')
		container_old.enabled = False

		# Create config view and temporary config interface
		self._view = SimpleConfigView(_get_name(), container_old, self._container_cur)
		self._view.config_vault['path:search'] = UniqueList([os.getcwd(), config_dn])

		# Determine work directory using config interface with "global" scope
		tmp_config = SimpleConfigInterface(self._view.get_view(set_sections=['global']))
		work_dn_base = tmp_config.get_dn('workdir base', config_dn, must_exist=False)
		work_dn_default = os.path.join(work_dn_base, _get_name('work'))
		work_dn = tmp_config.get_dn('workdir', work_dn_default, must_exist=False)
		self._view.config_vault['path:work_dn'] = work_dn  # tmp_config still has undefinied
		# Set dynamic plugin search path
		sys.path.extend(tmp_config.get_dn_list('plugin paths', [os.getcwd()]))

		# Determine and load stored config settings
		self._config_path_min = os.path.join(work_dn, 'current.conf')  # Minimal config file
		self._config_path_old = os.path.join(work_dn, 'work.conf')  # Config file with saved settings
		if load_old_config:
			if os.path.exists(self._config_path_old):
				GeneralFileConfigFiller([self._config_path_old]).fill(container_old)
			old_setting_file = os.path.join(work_dn, 'task.dat')
			if os.path.exists(old_setting_file):
				ConfigFiller.create_instance('CompatConfigFiller', old_setting_file).fill(container_old)
			container_old.enabled = True
			container_old.protect()

		# Get persistent variables - only possible after container_old was enabled
		self._view.set_config_name(tmp_config.get('config id', _get_name(), persistent=True))
Ejemplo n.º 2
0
class ConfigFactory(object):
	# Main config interface
	def __init__(self, filler=None, config_file_path=None, load_old_config=True, path_base=None):
		def _get_name(prefix=''):
			if config_file_path:
				return ('%s.%s' % (prefix, get_file_name(config_file_path))).strip('.')
			elif prefix:
				return prefix
			return 'unnamed'

		try:
			config_dn = os.getcwd()
		except Exception:
			raise ConfigError('The current directory does not exist!')
		if config_file_path:
			config_dn = os.path.dirname(resolve_path(config_file_path,
				search_path_list=[os.getcwd()], exception_type=ConfigError))
		if path_base:
			config_dn = os.path.dirname(resolve_path(path_base, search_path_list=[os.getcwd()]))

		# Init config containers
		self._container_cur = ConfigContainer('current')
		if filler:  # Read in the current configuration ...
			filler.fill(self._container_cur)
		self._container_cur.resolve()  # resolve interpolations

		logging.getLogger('config.stored').propagate = False
		container_old = ConfigContainer('stored')
		container_old.enabled = False

		# Create config view and temporary config interface
		self._view = SimpleConfigView(_get_name(), container_old, self._container_cur)
		self._view.config_vault['path:search'] = UniqueList([os.getcwd(), config_dn])

		# Determine work directory using config interface with "global" scope
		tmp_config = SimpleConfigInterface(self._view.get_view(set_sections=['global']))
		work_dn_base = tmp_config.get_dn('workdir base', config_dn, must_exist=False)
		work_dn_default = os.path.join(work_dn_base, _get_name('work'))
		work_dn = tmp_config.get_dn('workdir', work_dn_default, must_exist=False)
		self._view.config_vault['path:work_dn'] = work_dn  # tmp_config still has undefinied
		# Set dynamic plugin search path
		sys.path.extend(tmp_config.get_dn_list('plugin paths', [os.getcwd()]))

		# Determine and load stored config settings
		self._config_path_min = os.path.join(work_dn, 'current.conf')  # Minimal config file
		self._config_path_old = os.path.join(work_dn, 'work.conf')  # Config file with saved settings
		if load_old_config:
			if os.path.exists(self._config_path_old):
				GeneralFileConfigFiller([self._config_path_old]).fill(container_old)
			old_setting_file = os.path.join(work_dn, 'task.dat')
			if os.path.exists(old_setting_file):
				ConfigFiller.create_instance('CompatConfigFiller', old_setting_file).fill(container_old)
			container_old.enabled = True
			container_old.protect()

		# Get persistent variables - only possible after container_old was enabled
		self._view.set_config_name(tmp_config.get('config id', _get_name(), persistent=True))

	def freeze(self, write_config=True, show_unused=True, raise_on_change=True):
		# Inform the user about unused options
		def _match_unused_entries(entry):
			return ('!' not in entry.section) and not entry.accessed
		self._container_cur.protect(raise_on_change)
		if show_unused:
			unused = lfilter(_match_unused_entries, self._view.iter_entries())
			log = logging.getLogger('config.freeze')
			if unused:
				log.log(logging.INFO1, 'There are %s unused config options!', len(unused))
			for entry in unused:
				log.log(logging.INFO1, '\t%s', entry.format(print_section=True))
		if write_config or not os.path.exists(self._config_path_old):
			ensure_dir_exists(os.path.dirname(self._config_path_old),
				'config storage directory', ConfigError)
			# Write user friendly, flat config file and config file with saved settings
			self._write_file(self._config_path_min, print_minimal=True, print_default=False,
				print_workdir=True, print_unused=False)
			self._write_file(self._config_path_old, print_minimal=True, print_default=True,
				print_source=True, print_unused=True,
				msg='; ==> DO NOT EDIT THIS FILE! <==\n; This file is used to find config changes!\n')

	def get_config(self):
		result = SimpleConfigInterface(self._view)
		result.factory = self
		return result

	def _write_file(self, fn, msg=None, **kwargs):
		def _write_msg_view(fp):
			if msg is not None:
				fp.write(msg)
			self._view.write(fp, **kwargs)
		with_file(SafeFile(fn, 'w'), _write_msg_view)