storages.py 4.66 KB
Newer Older
1
2
3
"""custom storages for the system source"""
from os import unlink, path as osp

4
from cubicweb import Binary
5
6
7
from cubicweb.server.hook import Operation

def set_attribute_storage(repo, etype, attr, storage):
8
    repo.system_source.set_storage(etype, attr, storage)
9

10
def unset_attribute_storage(repo, etype, attr):
11
    repo.system_source.unset_storage(etype, attr)
12
13

class Storage(object):
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
    """abstract storage

    * If `source_callback` is true (by default), the callback will be run during
      query result process of fetched attribute's valu and should have the
      following prototype::

        callback(self, source, value)

      where `value` is the value actually stored in the backend. None values
      will be skipped (eg callback won't be called).

    * if `source_callback` is false, the callback will be run during sql
      generation when some attribute with a custom storage is accessed and
      should have the following prototype::

        callback(self, generator, relation, linkedvar)

      where `generator` is the sql generator, `relation` the current rql syntax
      tree relation and linkedvar the principal syntax tree variable holding the
      attribute.
    """
    is_source_callback = True

    def callback(self, *args):
        """see docstring for prototype, which vary according to is_source_callback
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
        """
        raise NotImplementedError()

    def entity_added(self, entity, attr):
        """an entity using this storage for attr has been added"""
        raise NotImplementedError()
    def entity_updated(self, entity, attr):
        """an entity using this storage for attr has been updatded"""
        raise NotImplementedError()
    def entity_deleted(self, entity, attr):
        """an entity using this storage for attr has been deleted"""
        raise NotImplementedError()

# TODO
# * make it configurable without code
# * better file path attribution
Sylvain Thénault's avatar
Sylvain Thénault committed
55
# * handle backup/restore
56
57
58
59
60
61

class BytesFileSystemStorage(Storage):
    """store Bytes attribute value on the file system"""
    def __init__(self, defaultdir):
        self.default_directory = defaultdir

62
    def callback(self, source, value):
63
64
65
        """sql generator callback when some attribute with a custom storage is
        accessed
        """
66
67
68
69
70
71
        fpath = source.binary_to_str(value)
        try:
            return Binary(file(fpath).read())
        except OSError, ex:
            source.critical("can't open %s: %s", value, ex)
            return None
72
73
74

    def entity_added(self, entity, attr):
        """an entity using this storage for attr has been added"""
75
76
77
78
79
80
81
82
83
84
        if entity._cw.transaction_data.get('fs_importing'):
            binary = Binary(file(entity[attr].getvalue()).read())
        else:
            binary = entity.pop(attr)
            fpath = self.new_fs_path(entity, attr)
            # bytes storage used to store file's path
            entity[attr] = Binary(fpath)
            file(fpath, 'w').write(binary.getvalue())
            AddFileOp(entity._cw, filepath=fpath)
        return binary
85
86
87

    def entity_updated(self, entity, attr):
        """an entity using this storage for attr has been updatded"""
88
89
90
91
        binary = entity.pop(attr)
        fpath = self.current_fs_path(entity, attr)
        UpdateFileOp(entity._cw, filepath=fpath, filedata=binary.getvalue())
        return binary
92
93
94
95
96
97

    def entity_deleted(self, entity, attr):
        """an entity using this storage for attr has been deleted"""
        DeleteFileOp(entity._cw, filepath=self.current_fs_path(entity, attr))

    def new_fs_path(self, entity, attr):
98
        fspath = osp.join(self.default_directory, '%s_%s' % (entity.eid, attr))
99
100
101
102
103
        while osp.exists(fspath):
            fspath = '_' + fspath
        return fspath

    def current_fs_path(self, entity, attr):
104
105
106
107
        sysource = entity._cw.pool.source('system')
        cu = sysource.doexec(entity._cw,
                             'SELECT cw_%s FROM cw_%s WHERE cw_eid=%s' % (
                                 attr, entity.__regid__, entity.eid))
108
109
110
111
        rawvalue = cu.fetchone()[0]
        if rawvalue is None: # no previous value
            return self.new_fs_path(entity, attr)
        return sysource._process_value(rawvalue, cu.description[0],
112
                                       binarywrap=str)
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132


class AddFileOp(Operation):
    def rollback_event(self):
        try:
            unlink(self.filepath)
        except:
            pass

class DeleteFileOp(Operation):
    def commit_event(self):
        try:
            unlink(self.filepath)
        except:
            pass

class UpdateFileOp(Operation):
    def precommit_event(self):
        try:
            file(self.filepath, 'w').write(self.filedata)
133
134
        except Exception, ex:
            self.exception(str(ex))