storages.py 6.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of CubicWeb.
#
# CubicWeb is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
12
13
14
15
16
17
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
18
19
20
"""custom storages for the system source"""
from os import unlink, path as osp

21
22
from yams.schema import role_name

23
from cubicweb import Binary
24
25
26
from cubicweb.server.hook import Operation

def set_attribute_storage(repo, etype, attr, storage):
27
    repo.system_source.set_storage(etype, attr, storage)
28

29
def unset_attribute_storage(repo, etype, attr):
30
    repo.system_source.unset_storage(etype, attr)
31
32

class Storage(object):
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    """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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
        """
        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
74
# * handle backup/restore
75

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def uniquify_path(dirpath, basename):
    """return a unique file name for `basename` in `dirpath`, or None
    if all attemps failed.

    XXX subject to race condition.
    """
    path = osp.join(dirpath, basename)
    if not osp.isfile(path):
        return path
    base, ext = osp.splitext(path)
    for i in xrange(1, 256):
        path = '%s%s%s' % (base, i, ext)
        if not osp.isfile(path):
            return path
    return None

92
93
class BytesFileSystemStorage(Storage):
    """store Bytes attribute value on the file system"""
94
    def __init__(self, defaultdir, fsencoding='utf-8'):
95
        self.default_directory = defaultdir
96
        self.fsencoding = fsencoding
97

98
    def callback(self, source, value):
99
100
101
        """sql generator callback when some attribute with a custom storage is
        accessed
        """
102
103
104
105
106
107
        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
108
109
110

    def entity_added(self, entity, attr):
        """an entity using this storage for attr has been added"""
111
112
113
114
115
116
117
118
119
120
        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
121
122
123

    def entity_updated(self, entity, attr):
        """an entity using this storage for attr has been updatded"""
124
125
126
127
128
129
130
131
132
133
        if entity._cw.transaction_data.get('fs_importing'):
            oldpath = self.current_fs_path(entity, attr)
            fpath = entity[attr].getvalue()
            if oldpath != fpath:
                DeleteFileOp(entity._cw, filepath=oldpath)
            binary = Binary(file(fpath).read())
        else:
            binary = entity.pop(attr)
            fpath = self.current_fs_path(entity, attr)
            UpdateFileOp(entity._cw, filepath=fpath, filedata=binary.getvalue())
134
        return binary
135
136
137
138
139
140

    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):
141
142
143
144
145
        # We try to get some hint about how to name the file using attribute's
        # name metadata, so we use the real file name and extension when
        # available. Keeping the extension is useful for example in the case of
        # PIL processing that use filename extension to detect content-type, as
        # well as providing more understandable file names on the fs.
146
147
148
        basename = [str(entity.eid), attr]
        name = entity.attr_metadata(attr, 'name')
        if name is not None:
149
            basename.append(name.encode(self.fsencoding))
150
151
152
        fspath = uniquify_path(self.default_directory, '_'.join(basename))
        if fspath is None:
            msg = entity._cw._('failed to uniquify path (%s, %s)') % (
153
                dirpath, '_'.join(basename))
154
            raise ValidationError(entity.eid, {role_name(attr, 'subject'): msg})
155
156
157
        return fspath

    def current_fs_path(self, entity, attr):
158
159
160
        sysource = entity._cw.pool.source('system')
        cu = sysource.doexec(entity._cw,
                             'SELECT cw_%s FROM cw_%s WHERE cw_eid=%s' % (
161
                             attr, entity.__regid__, entity.eid))
162
163
164
165
        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],
166
                                       binarywrap=str)
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186


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)
187
188
        except Exception, ex:
            self.exception(str(ex))