Пример #1
0
    def __init__(self, filler=None, configFilePath=None):
        def getName(prefix=''):
            if configFilePath:
                return ('%s.%s' %
                        (prefix, utils.getRootName(configFilePath))).strip('.')
            elif prefix:
                return prefix
            return 'unnamed'

        pathMain = os.getcwd()
        if configFilePath:
            pathMain = os.path.dirname(
                utils.resolvePath(configFilePath,
                                  searchPaths=[os.getcwd()],
                                  ErrorClass=ConfigError))

        # Init config containers
        self._curContainer = ConfigContainer('current')
        if filler:  # Read in the current configuration ...
            filler.fill(self._curContainer)
        logging.getLogger('config.stored').propagate = False
        oldContainer = ConfigContainer('stored')
        oldContainer.enabled = False

        # Create config view and temporary config interface
        self._view = SimpleConfigView(getName(), oldContainer,
                                      self._curContainer)
        self._view.pathDict['search_paths'] = UniqueList(
            [os.getcwd(), pathMain])

        # Determine work directory using config interface with "global" scope
        tmpInterface = SimpleConfigInterface(
            self._view.getView(setSections=['global']))
        wdBase = tmpInterface.getPath('workdir base',
                                      pathMain,
                                      mustExist=False)
        pathWork = tmpInterface.getPath('workdir',
                                        os.path.join(wdBase, getName('work')),
                                        mustExist=False)
        self._view.pathDict[
            '<WORKDIR>'] = pathWork  # tmpInterface still has undefinied
        # Set dynamic plugin search path
        sys.path.extend(tmpInterface.getPaths('plugin paths', [os.getcwd()]))

        # Determine and load stored config settings
        self._flatCfgPath = os.path.join(pathWork,
                                         'current.conf')  # Minimal config file
        self._oldCfgPath = os.path.join(
            pathWork, 'work.conf')  # Config file with saved settings
        if os.path.exists(self._oldCfgPath):
            GeneralFileConfigFiller([self._oldCfgPath]).fill(oldContainer)
            CompatConfigFiller(os.path.join(pathWork,
                                            'task.dat')).fill(oldContainer)
            oldContainer.enabled = True
            oldContainer.setReadOnly()

        # Get persistent variables - only possible after oldContainer was enabled
        self._view.setConfigName(
            tmpInterface.get('config id', getName(), persistent=True))
Пример #2
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))
Пример #3
0
	def __init__(self, filler = None, configFilePath = None):
		def getName(prefix = ''):
			if configFilePath:
				return ('%s.%s' % (prefix, utils.getRootName(configFilePath))).strip('.')
			elif prefix:
				return prefix
			return 'unnamed'

		try:
			pathMain = os.getcwd()
		except Exception:
			raise ConfigError('The current directory does not exist!')
		if configFilePath:
			pathMain = os.path.dirname(utils.resolvePath(configFilePath,
				searchPaths = [os.getcwd()], ErrorClass = ConfigError))

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

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

		# Create config view and temporary config interface
		self._view = SimpleConfigView(getName(), oldContainer, self._curContainer)
		self._view.pathDict['search_paths'] = UniqueList([os.getcwd(), pathMain])

		# Determine work directory using config interface with "global" scope
		tmp_config = SimpleConfigInterface(self._view.getView(setSections = ['global']))
		wdBase = tmp_config.getPath('workdir base', pathMain, mustExist = False)
		pathWork = tmp_config.getPath('workdir', os.path.join(wdBase, getName('work')), mustExist = False)
		self._view.pathDict['<WORKDIR>'] = pathWork # tmp_config still has undefinied
		# Set dynamic plugin search path
		sys.path.extend(tmp_config.getPaths('plugin paths', [os.getcwd()]))

		# Determine and load stored config settings
		self._flatCfgPath = os.path.join(pathWork, 'current.conf') # Minimal config file
		self._oldCfgPath = os.path.join(pathWork, 'work.conf') # Config file with saved settings
		if os.path.exists(self._oldCfgPath):
			GeneralFileConfigFiller([self._oldCfgPath]).fill(oldContainer)
			CompatConfigFiller(os.path.join(pathWork, 'task.dat')).fill(oldContainer)
			oldContainer.enabled = True
			oldContainer.setReadOnly()

		# Get persistent variables - only possible after oldContainer was enabled
		self._view.setConfigName(tmp_config.get('config id', getName(), persistent = True))
Пример #4
0
class ConfigFactory(object):
	def __init__(self, filler = None, configFilePath = None):
		def getName(prefix = ''):
			if configFilePath:
				return ('%s.%s' % (prefix, utils.getRootName(configFilePath))).strip('.')
			elif prefix:
				return prefix
			return 'unnamed'

		try:
			pathMain = os.getcwd()
		except Exception:
			raise ConfigError('The current directory does not exist!')
		if configFilePath:
			pathMain = os.path.dirname(utils.resolvePath(configFilePath,
				searchPaths = [os.getcwd()], ErrorClass = ConfigError))

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

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

		# Create config view and temporary config interface
		self._view = SimpleConfigView(getName(), oldContainer, self._curContainer)
		self._view.pathDict['search_paths'] = UniqueList([os.getcwd(), pathMain])

		# Determine work directory using config interface with "global" scope
		tmp_config = SimpleConfigInterface(self._view.getView(setSections = ['global']))
		wdBase = tmp_config.getPath('workdir base', pathMain, mustExist = False)
		pathWork = tmp_config.getPath('workdir', os.path.join(wdBase, getName('work')), mustExist = False)
		self._view.pathDict['<WORKDIR>'] = pathWork # tmp_config still has undefinied
		# Set dynamic plugin search path
		sys.path.extend(tmp_config.getPaths('plugin paths', [os.getcwd()]))

		# Determine and load stored config settings
		self._flatCfgPath = os.path.join(pathWork, 'current.conf') # Minimal config file
		self._oldCfgPath = os.path.join(pathWork, 'work.conf') # Config file with saved settings
		if os.path.exists(self._oldCfgPath):
			GeneralFileConfigFiller([self._oldCfgPath]).fill(oldContainer)
			CompatConfigFiller(os.path.join(pathWork, 'task.dat')).fill(oldContainer)
			oldContainer.enabled = True
			oldContainer.setReadOnly()

		# Get persistent variables - only possible after oldContainer was enabled
		self._view.setConfigName(tmp_config.get('config id', getName(), persistent = True))


	def getConfig(self, **kwargs):
		result = SimpleConfigInterface(self._view)
		if kwargs:
			result = result.changeView(**kwargs)
		result.factory = self
		return result


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


	def freezeConfig(self, writeConfig = True):
		self._curContainer.setReadOnly()
		# Inform the user about unused options
		unused = lfilter(lambda entry: ('!' not in entry.section) and not entry.accessed, self._view.iterContent())
		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(printSection = True))
		if writeConfig or not os.path.exists(self._oldCfgPath):
			if not os.path.exists(os.path.dirname(self._oldCfgPath)):
				os.makedirs(os.path.dirname(self._oldCfgPath))
			# Write user friendly, flat config file and config file with saved settings
			self._write_file(self._flatCfgPath, printDefault = False, printUnused = False, printMinimal = True)
			self._write_file(self._oldCfgPath,  printDefault = True,  printUnused = True,  printMinimal = True, printSource = True,
				message = '; ==> DO NOT EDIT THIS FILE! <==\n; This file is used to find config changes!\n')
Пример #5
0
class ConfigFactory(object):
    def __init__(self, filler=None, configFilePath=None):
        def getName(prefix=''):
            if configFilePath:
                return ('%s.%s' %
                        (prefix, utils.getRootName(configFilePath))).strip('.')
            elif prefix:
                return prefix
            return 'unnamed'

        pathMain = os.getcwd()
        if configFilePath:
            pathMain = os.path.dirname(
                utils.resolvePath(configFilePath,
                                  searchPaths=[os.getcwd()],
                                  ErrorClass=ConfigError))

        # Init config containers
        self._curContainer = ConfigContainer('current')
        if filler:  # Read in the current configuration ...
            filler.fill(self._curContainer)
        logging.getLogger('config.stored').propagate = False
        oldContainer = ConfigContainer('stored')
        oldContainer.enabled = False

        # Create config view and temporary config interface
        self._view = SimpleConfigView(getName(), oldContainer,
                                      self._curContainer)
        self._view.pathDict['search_paths'] = UniqueList(
            [os.getcwd(), pathMain])

        # Determine work directory using config interface with "global" scope
        tmpInterface = SimpleConfigInterface(
            self._view.getView(setSections=['global']))
        wdBase = tmpInterface.getPath('workdir base',
                                      pathMain,
                                      mustExist=False)
        pathWork = tmpInterface.getPath('workdir',
                                        os.path.join(wdBase, getName('work')),
                                        mustExist=False)
        self._view.pathDict[
            '<WORKDIR>'] = pathWork  # tmpInterface still has undefinied
        # Set dynamic plugin search path
        sys.path.extend(tmpInterface.getPaths('plugin paths', [os.getcwd()]))

        # Determine and load stored config settings
        self._flatCfgPath = os.path.join(pathWork,
                                         'current.conf')  # Minimal config file
        self._oldCfgPath = os.path.join(
            pathWork, 'work.conf')  # Config file with saved settings
        if os.path.exists(self._oldCfgPath):
            GeneralFileConfigFiller([self._oldCfgPath]).fill(oldContainer)
            CompatConfigFiller(os.path.join(pathWork,
                                            'task.dat')).fill(oldContainer)
            oldContainer.enabled = True
            oldContainer.setReadOnly()

        # Get persistent variables - only possible after oldContainer was enabled
        self._view.setConfigName(
            tmpInterface.get('config id', getName(), persistent=True))

    def getConfig(self, **kwargs):
        result = SimpleConfigInterface(self._view)
        if kwargs:
            result = result.changeView(**kwargs)
        result.factory = self
        return result

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

    def freezeConfig(self, writeConfig=True):
        self._curContainer.setReadOnly()
        # Inform the user about unused options
        unused = lfilter(
            lambda entry: ('!' not in entry.section) and not entry.accessed,
            self._view.iterContent())
        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(printSection=True))
        if writeConfig or not os.path.exists(self._oldCfgPath):
            if not os.path.exists(os.path.dirname(self._oldCfgPath)):
                os.makedirs(os.path.dirname(self._oldCfgPath))
            # Write user friendly, flat config file and config file with saved settings
            self._write_file(self._flatCfgPath,
                             printDefault=False,
                             printUnused=False,
                             printMinimal=True)
            self._write_file(
                self._oldCfgPath,
                printDefault=True,
                printUnused=True,
                printMinimal=True,
                printSource=True,
                message=
                '; ==> DO NOT EDIT THIS FILE! <==\n; This file is used to find config changes!\n'
            )
Пример #6
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)