Exemplo n.º 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))
Exemplo n.º 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))