示例#1
0
    def open(self, fp, zipType=None):
        '''
        For a supplied file path to a pickled stream, unpickle
        '''
        if isinstance(fp, pathlib.Path):
            fp = str(fp)  # TODO: reverse this... use Pathlib...

        if os.sep in fp:  # assume it's a complete path
            fp = fp
        else:
            directory = environLocal.getRootTempDir()
            fp = str(directory / fp)

        f = open(fp, 'rb')
        fileData = f.read()  # TODO: do not read entire file
        f.close()

        fmt = self.parseOpenFmt(fileData)
        if fmt == 'pickle':
            #environLocal.printDebug(['opening fp', fp])
            f = open(fp, 'rb')
            if zipType is None:
                storage = pickle.load(f)
            elif zipType == 'zlib':
                compressedString = f.read()
                uncompressed = zlib.decompress(compressedString)
                try:
                    storage = pickle.loads(uncompressed)
                except AttributeError as e:
                    raise FreezeThawException(
                        'Problem in decoding: {}'.format(e))
            else:
                raise FreezeThawException('Unknown zipType %s' % zipType)
            f.close()
        elif fmt == 'jsonpickle':
            f = open(fp, 'r')
            data = f.read()
            f.close()
            storage = jsonpickle.decode(data)
        else:
            raise FreezeThawException('bad StreamFreezer format: %s' % fmt)

        self.stream = self.unpackStream(storage)
示例#2
0
    def open(self, fp, zipType=None):
        '''
        For a supplied file path to a pickled stream, unpickle
        '''
        if isinstance(fp, pathlib.Path):
            fp = str(fp) # TODO: reverse this... use Pathlib...

        if os.sep in fp: # assume it's a complete path
            fp = fp
        else:
            directory = environLocal.getRootTempDir()
            fp = str(directory / fp)

        f = open(fp, 'rb')
        fileData = f.read() # TODO: do not read entire file
        f.close()

        fmt = self.parseOpenFmt(fileData)
        if fmt == 'pickle':
            # environLocal.printDebug(['opening fp', fp])
            f = open(fp, 'rb')
            if zipType is None:
                storage = pickle.load(f)
            elif zipType == 'zlib':
                compressedString = f.read()
                uncompressed = zlib.decompress(compressedString)
                try:
                    storage = pickle.loads(uncompressed)
                except AttributeError as e:
                    raise FreezeThawException('Problem in decoding: {}'.format(e))
            else:
                raise FreezeThawException('Unknown zipType %s' % zipType)
            f.close()
        elif fmt == 'jsonpickle':
            f = open(fp, 'r')
            data = f.read()
            f.close()
            storage = jsonpickle.decode(data)
        else:
            raise FreezeThawException('bad StreamFreezer format: %s' % fmt)

        self.stream = self.unpackStream(storage)
示例#3
0
    def openStr(self, fileData, pickleFormat=None):
        '''
        Take a string representing a Frozen(pickled/jsonpickled)
        Stream and convert it to a normal Stream.

        if format is None then the format is automatically
        determined from the string contents.
        '''
        if pickleFormat is not None:
            fmt = pickleFormat
        else:
            fmt = self.parseOpenFmt(fileData)

        if fmt == 'pickle':
            storage = pickle.loads(fileData)
        elif fmt == 'jsonpickle':
            storage = jsonpickle.decode(fileData)
        else:
            raise FreezeThawException('bad StreamFreezer format: %s' % fmt)
        environLocal.printDebug('StreamThawer:openStr: storage is: %s' % storage)
        self.stream = self.unpackStream(storage)
示例#4
0
    def openStr(self, fileData, pickleFormat=None):
        '''
        Take a string representing a Frozen(pickled/jsonpickled)
        Stream and convert it to a normal Stream.

        if format is None then the format is automatically
        determined from the string contents.
        '''
        if pickleFormat is not None:
            fmt = pickleFormat
        else:
            fmt = self.parseOpenFmt(fileData)

        if fmt == 'pickle':
            storage = pickle.loads(fileData)
        elif fmt == 'jsonpickle':
            storage = jsonpickle.decode(fileData)
        else:
            raise FreezeThawException('bad StreamFreezer format: %s' % fmt)
        environLocal.printDebug('StreamThawer:openStr: storage is: %s' % storage)
        self.stream = self.unpackStream(storage)