Programmatically determine config value based on the desired saltenv

def add_conf_overlay(cls, name):
        '''
        Programmatically determine config value based on the desired saltenv
        '''
        def _getconf(self, tgt_env='base'):
            strip_sep = lambda x: x.rstrip(os.sep) \
                if name in ('root', 'mountpoint') \
                else x
            if self.role != 'gitfs':
                return strip_sep(getattr(self, '_' + name))
            # Get saltenv-specific configuration
            saltenv_conf = self.saltenv.get(tgt_env, {})
            if name == 'ref':
                def _get_per_saltenv(tgt_env):
                    if name in saltenv_conf:
                        return saltenv_conf[name]
                    elif tgt_env in self.global_saltenv \
                            and name in self.global_saltenv[tgt_env]:
                        return self.global_saltenv[tgt_env][name]
                    else:
                        return None

                # Return the all_saltenvs branch/tag if it is configured
                per_saltenv_ref = _get_per_saltenv(tgt_env)
                try:
                    all_saltenvs_ref = self.all_saltenvs
                    if per_saltenv_ref and all_saltenvs_ref != per_saltenv_ref:
                        log.debug(
                            'The per-saltenv configuration has mapped the '
                            '\'%s\' branch/tag to saltenv \'%s\' for %s '
                            'remote \'%s\', but this remote has '
                            'all_saltenvs set to \'%s\'. The per-saltenv '
                            'mapping will be ignored in favor of \'%s\'.',
                            per_saltenv_ref, tgt_env, self.role, self.id,
                            all_saltenvs_ref, all_saltenvs_ref
                        )
                    return all_saltenvs_ref
                except AttributeError:
                    # all_saltenvs not configured for this remote
                    pass

                if tgt_env == 'base':
                    return self.base
                elif self.disable_saltenv_mapping:
                    if per_saltenv_ref is None:
                        log.debug(
                            'saltenv mapping is diabled for %s remote \'%s\' '
                            'and saltenv \'%s\' is not explicitly mapped',
                            self.role, self.id, tgt_env
                        )
                    return per_saltenv_ref
                else:
                    return per_saltenv_ref or tgt_env

            if name in saltenv_conf:
                return strip_sep(saltenv_conf[name])
            elif tgt_env in self.global_saltenv \
                    and name in self.global_saltenv[tgt_env]:
                return strip_sep(self.global_saltenv[tgt_env][name])
            else:
                return strip_sep(getattr(self, '_' + name))
        setattr(cls, name, _getconf)