Example #1
0
    def _unpack(self, name, attrib, load_collections):
        "Helper method to unpack SQLalchemy objects"
        if attrib is None:
            return

        if name == "metatags":
            return self._unpack_metatags(attrib)

        #log.d("name:", name, "attrib:", attrib)
        # beware lots of recursion
        if db.is_instanced(attrib):
            msg_obj = None

            exclude = (db.NameMixin.__name__, )

            for cls_name, cls_obj in self._db_clsmembers:
                if cls_name not in exclude:
                    if isinstance(attrib, cls_obj):
                        if cls_name in self._clsmembers:
                            msg_obj = self._clsmembers[cls_name](attrib)
                            break

            if not msg_obj:
                if isinstance(attrib, db.NameMixin):
                    msg_obj = NameMixin(attrib, name)
                else:
                    raise NotImplementedError(
                        "Message encapsulation for this database object does not exist ({})"
                        .format(type(attrib)))

            return msg_obj.data() if msg_obj else None

        elif db.is_list(attrib) or isinstance(attrib, list):
            return [self._unpack(name, x, load_collections) for x in attrib]

        elif db.is_query(attrib):
            if load_collections:
                return [
                    self._unpack(name, x, load_collections)
                    for x in attrib.all()
                ]
            else:
                return []

        elif isinstance(attrib, enum.Enum):
            return attrib.name

        elif isinstance(attrib, datetime):
            return attrib.timestamp()

        elif isinstance(attrib, arrow.Arrow):
            return attrib.timestamp

        elif isinstance(attrib, (bool, int, float, str, dict)):
            return attrib
        else:
            raise NotImplementedError(
                "Unpacking method for this attribute does not exist ({})".
                format(type(attrib)))
Example #2
0
 def __init__(self, key, db_item):
     super().__init__(key)
     assert isinstance(db_item, db.Base)
     assert db.is_instanced(db_item), "must be instanced database object"
     self.item = db_item
     DatabaseMessage._clsmembers = {
         x: globals()[x]
         for x in globals() if inspect.isclass(globals()[x])
     }
Example #3
0
    def _unpack(self, name, attrib, load_values, load_collections, propagate_bypass):
        "Helper method to unpack SQLalchemy objects"
        if attrib is None:
            return

        if name == "metatags":
            return self._unpack_metatags(attrib)

        #log.d("name:", name, "attrib:", attrib)
        # beware lots of recursion
        if db.is_instanced(attrib):
            msg_obj = self._get_message_object(self, name, attrib)
            if msg_obj is False:  # recursive checked
                return None

            msg_obj._indirect = True
            msg_obj._properties['force_value_load'] = utils.dict_merge(
                msg_obj._properties['force_value_load'], self._properties['force_value_load'].get(name, {}))
            msg_obj._properties['exclusions'] = self._properties['exclusions'].get(name, {})
            if self._detached:
                msg_obj._detached = self._detached
            msg_obj._msg_path = self._msg_path.copy()
            # note: don't pass load_collections here. use the force_value_load param
            return msg_obj.data(load_values=load_values, bypass_exclusions=propagate_bypass,
                                propagate_bypass=propagate_bypass) if msg_obj else None

        elif db.is_list(attrib) or isinstance(attrib, list):
            return [self._unpack(name, x, load_values, load_collections, propagate_bypass) for x in attrib]

        elif db.is_query(attrib):
            can_load = False
            if name in self._properties['force_value_load']:
                can_load = True
            if can_load or (load_collections and not self._detached):
                return [self._unpack(name, x, load_values, load_collections, propagate_bypass)
                        for x in attrib.all()]
            else:
                raise DatabaseMessage.NoUnpack

        elif isinstance(attrib, enum.Enum):
            return attrib.name

        elif isinstance(attrib, datetime):
            return attrib.timestamp()

        elif isinstance(attrib, arrow.Arrow):
            return attrib.timestamp

        elif isinstance(attrib, (bool, int, float, str, dict)):
            return attrib
        else:
            raise NotImplementedError(
                "Unpacking method for this attribute does not exist ({})".format(
                    type(attrib)))
Example #4
0
 def __init__(self, key, db_item):
     super().__init__(key)
     assert db.is_instanced(db_item), f"must be instanced database object not '{db_item}'"
     assert isinstance(db_item, self.db_type) if self.db_type else isinstance(
         db_item, db.Base), f"a {self.db_type} is required not '{type(db_item)}'"
     self.item = db_item
     self._recursive_depth = 2
     self._indirect = False
     self._msg_path = []
     self._sess = None
     self._detached = db.is_detached(db_item)
     self._properties = self.properties()
     DatabaseMessage._clsmembers = {
         x: globals()[x] for x in globals() if inspect.isclass(
             globals()[x])}