Example #1
0
    def get(self, sourceItem):
        #TODO: take a serious look at this implementation...seems very holy (not in a religous sort of way)
        _cachedItem = self.factory()
        #_sqlInspecter = sqlalchemy.inspection.inspect(_cachedItem)
        for _cachedAttrKeyName in _cachedItem.__class__.__dict__.keys(): # iterate the actual cache object to make sure we don't miss any attributes
            if not isinstance(_cachedItem.__class__.__dict__[_cachedAttrKeyName], sqlalchemy.orm.attributes.InstrumentedAttribute): # these are the column assignments
                continue
            if _cachedAttrKeyName not in self.mapper:
                raise LookupError("expected to find cached object attribute in mapper keys: %s", _cachedAttrKeyName)
            
            _sourceAttrKey = self.mapper[_cachedAttrKeyName]
            #if queryAdapter(_sourceAttrKey, IManagedCachedItemMapperAttributeKeyWrapper):
            if IManagedCachedItemMapperAttributeKeyWrapper.providedBy(_sourceAttrKey):
                _sourceAttrValue = sourceItem.attributes[_sourceAttrKey()]
            else:
                _sourceAttrValue = sourceItem.attributes[_sourceAttrKey]
            _cachedAttrValue_new = None
            _sql_field_type_name = str(_cachedItem.__table__.c[_cachedAttrKeyName].type)
            
            if queryAdapter(_sourceAttrKey, IManagedCachedItemMapperAttribute): # MANAGED ATTRIBUTES
                _cachedAttrValue_new = None if not _sourceAttrValue else IManagedCachedItemMapperAttribute(_sourceAttrKey).manage(_sourceAttrValue)

            elif 'INT' in _sql_field_type_name.upper():
                try:
                    _cachedAttrValue_new = int(_sourceAttrValue)
                except ValueError:
                    _cachedAttrValue_new = None
            elif 'NCHAR' in _sql_field_type_name.upper():
                _cachedAttrValue_new = _sourceAttrValue.decode('utf8', 'replace') if _sourceAttrValue else None
            else:
                _cachedAttrValue_new = _sourceAttrValue
            
            _cachedItem.__dict__[_cachedAttrKeyName] = _cachedAttrValue_new
        logger.debug("generated cached item from source, values: %s", str(_cachedItem.getId()))
        return _cachedItem
Example #2
0
 def __init__(self, *args, **kwargs):
     _new_mapper = {}
     for mapper, attribute in self.mapper.iteritems():
         if IManagedCachedItemMapperAttributeKeyWrapper.providedBy(attribute): # managed attributes
             attribute.key = normalizedFieldNameCachableItemMixin.normalize(attribute.key)
             _new_mapper[mapper] = attribute
         else:
             _new_mapper[mapper] = normalizedFieldNameCachableItemMixin.normalize(attribute) # unmanaged attribute
     self.mapper = _new_mapper
     super(normalizedFieldNameSqlObjectMapperMixin, self).__init__(*args, **kwargs)
Example #3
0
 def __init__(self, *args, **kwargs):
     _new_mapper = {}
     for mapper, attribute in self.mapper.iteritems():
         if IManagedCachedItemMapperAttributeKeyWrapper.providedBy(
                 attribute):  # managed attributes
             attribute.key = normalizedFieldNameCachableItemMixin.normalize(
                 attribute.key)
             _new_mapper[mapper] = attribute
         else:
             _new_mapper[
                 mapper] = normalizedFieldNameCachableItemMixin.normalize(
                     attribute)  # unmanaged attribute
     self.mapper = _new_mapper
     super(normalizedFieldNameSqlObjectMapperMixin,
           self).__init__(*args, **kwargs)
Example #4
0
    def get(self, sourceItem):
        #TODO: take a serious look at this implementation...seems very holy (not in a religous sort of way)
        _cachedItem = self.factory()
        #_sqlInspecter = sqlalchemy.inspection.inspect(_cachedItem)
        for _cachedAttrKeyName in _cachedItem.__class__.__dict__.keys(
        ):  # iterate the actual cache object to make sure we don't miss any attributes
            if not isinstance(
                    _cachedItem.__class__.__dict__[_cachedAttrKeyName],
                    sqlalchemy.orm.attributes.InstrumentedAttribute
            ):  # these are the column assignments
                continue
            if _cachedAttrKeyName not in self.mapper:
                raise LookupError(
                    "expected to find cached object attribute in mapper keys: %s",
                    _cachedAttrKeyName)

            _sourceAttrKey = self.mapper[_cachedAttrKeyName]
            #if queryAdapter(_sourceAttrKey, IManagedCachedItemMapperAttributeKeyWrapper):
            if IManagedCachedItemMapperAttributeKeyWrapper.providedBy(
                    _sourceAttrKey):
                _sourceAttrValue = sourceItem.attributes[_sourceAttrKey()]
            else:
                _sourceAttrValue = sourceItem.attributes[_sourceAttrKey]
            _cachedAttrValue_new = None
            _sql_field_type_name = str(
                _cachedItem.__table__.c[_cachedAttrKeyName].type)

            if queryAdapter(
                    _sourceAttrKey,
                    IManagedCachedItemMapperAttribute):  # MANAGED ATTRIBUTES
                _cachedAttrValue_new = None if not _sourceAttrValue else IManagedCachedItemMapperAttribute(
                    _sourceAttrKey).manage(_sourceAttrValue)

            elif 'INT' in _sql_field_type_name.upper():
                try:
                    _cachedAttrValue_new = int(_sourceAttrValue)
                except ValueError:
                    _cachedAttrValue_new = None
            elif 'NCHAR' in _sql_field_type_name.upper():
                _cachedAttrValue_new = _sourceAttrValue.decode(
                    'utf8', 'replace') if _sourceAttrValue else None
            else:
                _cachedAttrValue_new = _sourceAttrValue

            _cachedItem.__dict__[_cachedAttrKeyName] = _cachedAttrValue_new
        logger.debug("generated cached item from source, values: %s",
                     str(_cachedItem.getId()))
        return _cachedItem