adapters.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:charms.openstack 作者: openstack 项目源码 文件源码
def __init__(self, relations, options=None, options_instance=None,
                 charm_instance=None):
        """
        :param relations: List of instances of relation classes
        :param options: Configuration class to use (DEPRECATED)
        :param options_instance: Instance of Configuration class to use
        :param charm_instance: optional charm_instance that is captured as a
            weakref for use on the adapter.
        """
        self._charm_instance_weakref = None
        if charm_instance is not None:
            self._charm_instance_weakref = weakref.ref(charm_instance)
        self._relations = set()
        if options is not None:
            hookenv.log("The 'options' argument is deprecated please use "
                        "options_instance instead.", level=hookenv.WARNING)
            self.options = options()
        elif options_instance is not None:
            self.options = options_instance
        else:
            # create a default, customised ConfigurationAdapter if the
            # APIConfigurationAdapter is needed as a base, then it must be
            # passed as an instance on the options_instance First pull the
            # configuration class from the charm instance (if it's available).
            base_cls = None
            if self.charm_instance:
                base_cls = getattr(self.charm_instance, 'configuration_class',
                                   base_cls)
            self.options = make_default_options(base_cls, self.charm_instance)
        self._relations.add('options')
        # walk the mro() from object to this class to build up the _adapters
        # ensure that all of the relations' have their '-' turned into a '_' to
        # ensure that everything is consistent in the class.
        self._adapters = {}
        for cls in reversed(self.__class__.mro()):
            self._adapters.update(
                {k.replace('-', '_'): v
                 for k, v in getattr(cls, 'relation_adapters', {}).items()})
        # now we have to add in any customisations to those adapters
        for relation, properties in _custom_adapter_properties.items():
            relation = relation.replace('-', '_')
            try:
                cls = self._adapters[relation]
            except KeyError:
                cls = OpenStackRelationAdapter
            self._adapters[relation] = make_default_relation_adapter(
                cls, relation, properties)
        self.add_relations(relations)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号