Package advene :: Package model :: Package serializers :: Module advene_zip
[hide private]
[frames] | no frames]

Source Code for Module advene.model.serializers.advene_zip

  1  """ 
  2  Unstable and experimental serializer implementation. 
  3   
  4  See `advene.model.serializers.advene_xml` for the reference implementation. 
  5  """ 
  6   
  7  from os import listdir, mkdir, path 
  8  from os.path import exists, isdir 
  9  from zipfile import ZipFile, ZIP_DEFLATED 
 10   
 11  from advene.model.consts import PACKAGED_ROOT 
 12  from advene.model.core.content import create_temporary_packaged_root 
 13  import advene.model.serializers.advene_xml as advene_xml 
 14   
 15  NAME = "Generic Advene Zipped Package" 
 16   
 17  EXTENSION = ".bzp" # Advene-2 Zipped Package 
 18   
 19  MIMETYPE = "application/x-advene-bzp" 
 20   
21 -def make_serializer(package, file_):
22 """Return a serializer that will serialize `package` to `file_`. 23 24 `file_` is a writable file-like object. It is the responsability of the 25 caller to close it. 26 """ 27 return _Serializer(package, file_)
28
29 -def serialize_to(package, file_):
30 """A shortcut for ``make_serializer(package, file).serialize()``. 31 32 See also `make_serializer`. 33 """ 34 return _Serializer(package, file_).serialize()
35
36 -class _Serializer(object):
37 38 _xml_serializer = advene_xml 39 mimetype = MIMETYPE 40
41 - def serialize(self):
42 """Perform the actual serialization.""" 43 #print "=== serializing directory", self.dir 44 f = open(path.join(self.dir, "content.xml"), "w") 45 self._xml_serializer.serialize_to(self.package, f) 46 f.close() 47 48 z = ZipFile(self.file, "w", self.compression) 49 _recurse(z, self.dir) 50 z.close()
51
52 - def __init__(self, package, file_, compression=None):
53 if compression is None: 54 self.compression = ZIP_DEFLATED 55 else: 56 self.compression = compression 57 self.dir = dir = package.get_meta(PACKAGED_ROOT, None) 58 if dir is None: 59 self.dir = dir = create_temporary_packaged_root(package) 60 61 if not exists(path.join(dir, "mimetype")): 62 f = open(path.join(dir, "mimetype"), "w") 63 f.write(self.mimetype) 64 f.close() 65 if not exists(path.join(dir, "data")): 66 mkdir(path.join(dir, "data")) 67 68 69 self.package = package 70 self.file = file_
71 72
73 -def _recurse(z, dir, base=""):
74 for f in listdir(dir): 75 abs = path.join(dir, f) 76 if isdir(abs): 77 _recurse(z, abs, path.join(base, f)) 78 else: 79 #print "=== zipping", path.join(base, f) 80 z.write(abs, path.join(base, f).encode('utf-8'))
81 82 83 if __name__ == "__main__": 84 # example of using ZIP serialization and parsing 85 from advene.model.core.package import Package 86 p = Package("file:/tmp/foo.bzp", create=True) 87 88 # prepare directories 89 if not exists("/tmp/foo"): mkdir("/tmp/foo") 90 if not exists("/tmp/foo/data"): mkdir("/tmp/foo/data") 91 92 # it is now safe to define the packaged-root of the package... 93 p.set_meta(PACKAGED_ROOT, "/tmp/foo") 94 # ... and to create packaged contents 95 r = p.create_resource("R1", "text/plain", None, "packaged:/data/R1.txt") 96 # this actually writes in /tmp/foo/data/R1.txt 97 r.content_data = "good moaning\n" 98 99 # let us serialize it... 100 f = open("/tmp/foo.bzp", "w") 101 serialize_to(p, f) 102 f.close() 103 # ... and parse it again 104 q = Package("file:/tmp/foo.bzp") 105 106 # note that advene_xml is smart enough to set the packaged-root if 107 # needed, so as to allow to open manually-unzipped packages 108 r = Package("file:/tmp/foo/content.xml") 109 print r.get_meta(PACKAGED_ROOT) 110