Beispiel #1
0
class BindingsManager(object):
    """
    """
    _logger = dynamiclogger()

    def __init__(self):
        """
        """
        self._entities = EntityConfigurationManager()
        self._instances = InstanceManager(self)
        self._builder = EntityBuilder(self)

    @classmethod
    def load_from_filepath(cls, filepath):
        """"""
        manager = BindingsManager()
        manager.import_configuration(filepath)
        return manager

    def import_configuration(self, filepath):
        """"""
        bindings = self.__read_configuration(filepath)
        if not isinstance(bindings, list):
            raise BindingsConfigurationException("invalid configuration file \"%s\"" % filepath)

        for entity in bindings:
            self.add_entity(entity)

    def __read_configuration(self, filepath):
        """"""
        f = None
        try:
            try:
                f = open(filepath, "r")
                bindings = ConfigurationLoader(f).load()
                return bindings

            except IOError, e:
                raise BindingsConfigurationException("invalid filepath: %s" % filepath)

        finally:
            if f: f.close()

        return []

    def check(self, label):
        """
        Checks whether a label exists (without throwing an exception)
        """

    def resolve(self, label):
        """
        """
        self._logger.debug("resolve entity: %s" % label)

        entity_config = self._entities.resolve(label)

        if entity_config is None:
            raise BindingsResolutionException("no binding exists for label \"%s\"" % label)

        return self._instances.instantiate(label, entity_config, self._builder)

    def bind(self, label):
        """
        """
        self._logger.debug("creating dynamic binding for: %s" % label)
        return EntityBinding(self, label)

    def add_entity(self, entity):
        """
        :type entity: flxbindings.domain.BaseEntity
        """
        self._logger.debug("add entity: %s" % entity)
        self._entities.add_entity(entity)
Beispiel #2
0
 def __init__(self):
     """
     """
     self._entities = EntityConfigurationManager()
     self._instances = InstanceManager(self)
     self._builder = EntityBuilder(self)