示例#1
0
文件: memds.py 项目: saxix/pyslet
 def new_stream(self, src, sinfo=None, key=None):
     e = self.new_entity()
     if sinfo is None:
         sinfo = odata.StreamInfo()
     etag = e.ETagValues()
     if len(etag) == 1 and isinstance(etag[0], edm.BinaryValue):
         h = hashlib.sha256()
         etag = etag[0]
     else:
         h = None
     data = self._read_src(src, sinfo.size)
     if h is not None:
         h.update(data)
         etag.set_from_value(h.digest())
     if sinfo.created is None:
         sinfo.created = iso.TimePoint.from_now_utc()
     if sinfo.modified is None:
         sinfo.modified = sinfo.created
     sinfo.size = len(data)
     sinfo.md5 = hashlib.md5(data).digest()
     # we need the lock to ensure the entity and stream and updated
     # together
     with self.entity_store.container.lock:
         if key is None:
             e.auto_key()
         else:
             e.set_key(key)
         for i in xrange(1000):
             key = e.key()
             if not self.entity_store.test_key(key):
                 break
             e.auto_key()
         self.insert_entity(e)
         self.entity_store.update_entity_stream(key, data, sinfo)
     return e
示例#2
0
文件: memds.py 项目: saxix/pyslet
 def update_stream(self, src, key, sinfo=None):
     update = False
     e = self[key]
     old_data, oldinfo = self.entity_store.read_stream(key)
     if sinfo is None:
         sinfo = odata.StreamInfo()
     etag = e.ETagValues()
     if len(etag) == 1 and isinstance(etag[0], edm.BinaryValue):
         h = hashlib.sha256()
         etag = etag[0]
     else:
         h = None
     data = self._read_src(src, sinfo.size)
     if h is not None:
         h.update(data)
         etag.set_from_value(h.digest())
         update = True
     if sinfo.created is None:
         sinfo.created = oldinfo.created
     if sinfo.created is None:
         sinfo.created = iso.TimePoint.from_now_utc()
     if sinfo.modified is None:
         sinfo.modified = iso.TimePoint.from_now_utc()
     sinfo.size = len(data)
     sinfo.md5 = hashlib.md5(data).digest()
     # we need the lock to ensure the entity and stream and updated
     # together
     with self.entity_store.container.lock:
         if update:
             self.update_entity(e)
         self.entity_store.update_entity_stream(key, data, sinfo)
示例#3
0
 def _get_path_info(self, path):
     try:
         e = self[path]
         fspath = path_to_fspath(path)
         if os.path.isdir(fspath):
             # directories return zero-length data
             sinfo = odata.StreamInfo(type=params.PLAIN_TEXT, size=0)
         else:
             root, ext = os.path.splitext(fspath)
             type = map_extension(ext)
             modified = e['lastModified'].value
             if modified:
                 modified = modified.with_zone(0)
             sinfo = odata.StreamInfo(type=type,
                                      modified=modified,
                                      size=e['size'].value)
         return fspath, sinfo
     except ValueError:
         raise KeyError("No such path: %s" % path)
示例#4
0
文件: memds.py 项目: saxix/pyslet
    def read_stream(self, key):
        """Returns a tuple of the entity's media stream

        The return value is a tuple: (data, StreamInfo)."""
        with self.container.lock:
            if key not in self.data:
                raise KeyError
            if key in self.streams:
                stream, sinfo = self.streams[key]
                return stream, sinfo
            else:
                return '', odata.StreamInfo(size=0)