Exemple #1
0
    def reset(cls):
        cls._RUN_ID = None
        cls._TODAY = None
        cls._RAW_CONFIG = copy.deepcopy(DEFAULTS)
        cls._CONFIG = copy.deepcopy(cls._RAW_CONFIG)
        cls._IM_CONFIG = afconfig.ImmutableConfigDict(cls._CONFIG)

        return cls
Exemple #2
0
    def reset(self):
        self._data._RUN_ID = None
        self._data._TODAY = None
        self._data._RAW_CONFIG = copy.deepcopy(DEFAULTS)
        self._data._CONFIG = copy.deepcopy(self._data._RAW_CONFIG)
        self._data._IM_CONFIG = afconfig.ImmutableConfigDict(
            self._data._CONFIG)

        return self
Exemple #3
0
    def merge(cls, config_dict):
        if config_dict:
            config_dict = to_lowercase_keys(config_dict)
            cls._RAW_CONFIG = afconfig.merge_configs(cls._RAW_CONFIG,
                                                     config_dict)
            cls._CONFIG = afconfig.merge_configs(
                cls._CONFIG,
                cls.replace_config_wildcards(copy.deepcopy(config_dict)))
            cls._IM_CONFIG = afconfig.ImmutableConfigDict(cls._CONFIG)

        return cls
Exemple #4
0
    def merge(self, config_dict):
        if config_dict:
            config_dict = to_lowercase_keys(config_dict)
            self._data._RAW_CONFIG = afconfig.merge_configs(
                self._data._RAW_CONFIG, config_dict)
            self._data._CONFIG = afconfig.merge_configs(
                self._data._CONFIG,
                self.replace_config_wildcards(copy.deepcopy(config_dict)))
            self._data._IM_CONFIG = afconfig.ImmutableConfigDict(
                self._data._CONFIG)

        return self
Exemple #5
0
    def set(cls, config_dict, *keys):
        config_dict = to_lowercase_keys(config_dict)
        if keys:
            keys = [k.lower() for k in keys]
            afconfig.set_config_value(cls._RAW_CONFIG,
                                      copy.deepcopy(config_dict), *keys)
            afconfig.set_config_value(
                cls._CONFIG,
                cls.replace_config_wildcards(copy.deepcopy(config_dict)),
                *keys)
            cls._IM_CONFIG = afconfig.ImmutableConfigDict(cls._CONFIG)

        else:
            cls._RAW_CONFIG = copy.deepcopy(DEFAULTS)
            cls._CONFIG = copy.deepcopy(cls._RAW_CONFIG)
            cls.merge(config_dict)

        return cls
Exemple #6
0
class Config:

    _RUN_ID = None
    _TODAY = None
    _RAW_CONFIG = copy.deepcopy(DEFAULTS)
    _CONFIG = copy.deepcopy(_RAW_CONFIG)
    _IM_CONFIG = afconfig.ImmutableConfigDict(_CONFIG)

    def __new__(cls):
        # Never instantiate. Always return class object
        return cls

    @classmethod
    def reset(cls):
        cls._RUN_ID = None
        cls._TODAY = None
        cls._RAW_CONFIG = copy.deepcopy(DEFAULTS)
        cls._CONFIG = copy.deepcopy(cls._RAW_CONFIG)
        cls._IM_CONFIG = afconfig.ImmutableConfigDict(cls._CONFIG)

        return cls

    @classmethod
    def merge(cls, config_dict):
        if config_dict:
            config_dict = to_lowercase_keys(config_dict)
            cls._RAW_CONFIG = afconfig.merge_configs(cls._RAW_CONFIG,
                                                     config_dict)
            cls._CONFIG = afconfig.merge_configs(
                cls._CONFIG,
                cls.replace_config_wildcards(copy.deepcopy(config_dict)))
            cls._IM_CONFIG = afconfig.ImmutableConfigDict(cls._CONFIG)

        return cls

    @classmethod
    def set(cls, config_dict, *keys):
        config_dict = to_lowercase_keys(config_dict)
        if keys:
            keys = [k.lower() for k in keys]
            afconfig.set_config_value(cls._RAW_CONFIG,
                                      copy.deepcopy(config_dict), *keys)
            afconfig.set_config_value(
                cls._CONFIG,
                cls.replace_config_wildcards(copy.deepcopy(config_dict)),
                *keys)
            cls._IM_CONFIG = afconfig.ImmutableConfigDict(cls._CONFIG)

        else:
            cls._RAW_CONFIG = copy.deepcopy(DEFAULTS)
            cls._CONFIG = copy.deepcopy(cls._RAW_CONFIG)
            cls.merge(config_dict)

        return cls

    @classmethod
    def set_today(cls, today):
        if today and cls._TODAY != today:
            cls._TODAY = today
            cls.set(cls._RAW_CONFIG)

    @classmethod
    def set_run_id(cls, run_id):
        if run_id and cls._RUN_ID != run_id:
            cls._RUN_ID = run_id
            cls.set(cls._RAW_CONFIG)

    @classmethod
    def get(cls, *keys, **kwargs):
        if 'default' in kwargs:
            raise DeprecationWarning("config defaults are specified in "
                                     "bluesky.config.defaults module")

        if keys:
            keys = [k.lower() for k in keys]
            # default behavior is to fail if key isn't in user's config
            # or in default config
            return afconfig.get_config_value(
                cls._IM_CONFIG,
                *keys,
                fail_on_missing_key=not kwargs.get('allow_missing'))

        else:
            return cls._IM_CONFIG

    @classmethod
    def replace_config_wildcards(cls, val):
        if isinstance(val, dict):
            for k in val:
                val[k] = cls.replace_config_wildcards(val[k])
        elif isinstance(val, list):
            val = [cls.replace_config_wildcards(v) for v in val]
        elif hasattr(val, 'lower'):  # i.e. it's a string
            if val:
                # first, fill in any datetime control codes or wildcards
                val = datetimeutils.fill_in_datetime_strings(val,
                                                             today=cls._TODAY)

                # then, see if the resulting string purely represents a datetime
                try:
                    val = datetimeutils.to_datetime(val, limit_range=True)
                except BlueSkyDatetimeValueError:
                    pass

                if hasattr(val, 'capitalize'):
                    # This gets rid of datetime parsing busters embedded
                    # in strings to prevent conversion to datetime object
                    val = val.replace('{datetime-parse-buster}', '')

                    if cls._RUN_ID:
                        val = val.replace('{run_id}', cls._RUN_ID)

                # TODO: any other replacements?

        return val