Example #1
0
class LocalFileProxy(FileProxy):
    def __init__(self, path, filename='', file=None):
        self.resolver = AssetResolver()
        super(LocalFileProxy, self).__init__(path, filename, file)

        if file is not None:
            if isinstance(file, LocalFileProxy):
                self.file = file.file
            else:
                self.open('w+')
                self.write(file.read())

    @property
    def abspath(self):
        assert self.filename, "{0}.filename must be set before calling open()".format(self.__class__.__name__)
        base = self.resolver.resolve(self.path).abspath()
        return os.path.join(base, self.filename)

    def open(self, mode='r'):
        self.file = open(self.abspath, mode)
        return self

    def remove(self):
        os.unlink(self.abspath)

    def __enter__(self):
        return self

    def __exit__(self, type, value, tb):
        self.close()
        return value is None
Example #2
0
    def test_local_storage_field(self):
        m = MyModel(name=u'storable')

        resolver = AssetResolver()
        path = resolver.resolve('batteries.tests:fixtures/test_image.png')
        m.attachment.filename = 'test_local_storage_field.png'

        with open(path.abspath(), 'r') as f:
            with m.attachment.open('w+') as a:
                a.write(f.read())

        self.session.add(m)
        self.session.flush()

        m = MyModel.query.one()
        with open(path.abspath(), 'r') as f:
            with m.attachment.open('r') as a:
                assert f.read() == a.read()

        MyModel.delete(m)
        self.session.flush()
        path = m.attachment.abspath

        assert not os.path.isfile(path)