aboutsummaryrefslogtreecommitdiffstats
path: root/FuseArchive/Serializer.py
blob: eb14cec4d928e3a7fbe3007f368d32e138cc739c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import gzip, FuseArchive, logging, cPickle

gzip_compress_level = 6

class Serializer:
    """This lets us experiment with different main file serializers"""
    @staticmethod
    def dump( f, obj ):
        out = open( f, "wb" )
        Serializer.dumpfh( obj, out ) # new file format
        out.close()

    @staticmethod
    def dumpfh( fh, obj ):
        logging.debug( "Going to serialize %s to %s" % ( obj, fh ) )
        fh.truncate( 0 )
        fh.seek( 0 )
        #f = gzip.GzipFile( None, "wb", gzip_compress_level, fh )
        f = fh
        f.write( Serializer.dumps( obj ) )
        #del f
        fh.flush()

    @staticmethod
    def dumps( obj ):
        return cPickle.dumps( obj, -1 )

    @staticmethod
    def load( f ):
        if FuseArchive.magic_profiling:
            return { 'size': 0, 'chunks': 0, 'chunk_size': 0 }

        inp = open( f, "rb" )
        magic = Serializer.loadfh( inp )
        inp.close()
        return magic

    @staticmethod
    def loadfh( fh ):
        logging.debug( "Going to load from %s" % fh )
        fh.seek( 0 )
        #f = gzip.GzipFile( None, "rb", gzip_compress_level, fh )
        f = fh
        magic = Serializer.loads( f.read() )
        return( magic )

    @staticmethod
    def loads( str ):
        return cPickle.loads( str )