server/sources/storages.py
changeset 6401 d7f5d873e1b8
parent 6142 8bc6eac1fac1
parent 6383 19ebe0b994d6
child 6426 541659c39f6a
equal deleted inserted replaced
6400:21468682f688 6401:d7f5d873e1b8
    16 # You should have received a copy of the GNU Lesser General Public License along
    16 # You should have received a copy of the GNU Lesser General Public License along
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 """custom storages for the system source"""
    18 """custom storages for the system source"""
    19 
    19 
    20 from os import unlink, path as osp
    20 from os import unlink, path as osp
       
    21 from contextlib import contextmanager
    21 
    22 
    22 from yams.schema import role_name
    23 from yams.schema import role_name
    23 
    24 
    24 from cubicweb import Binary, ValidationError
    25 from cubicweb import Binary, ValidationError
    25 from cubicweb.server import hook
    26 from cubicweb.server import hook
    93     for i in xrange(1, 256):
    94     for i in xrange(1, 256):
    94         path = '%s%s%s' % (base, i, ext)
    95         path = '%s%s%s' % (base, i, ext)
    95         if not osp.isfile(path):
    96         if not osp.isfile(path):
    96             return path
    97             return path
    97     return None
    98     return None
       
    99 
       
   100 @contextmanager
       
   101 def fsimport(session):
       
   102     present = 'fs_importing' in session.transaction_data
       
   103     old_value = session.transaction_data.get('fs_importing')
       
   104     session.transaction_data['fs_importing'] = True
       
   105     yield
       
   106     if present:
       
   107         session.transaction_data['fs_importing'] = old_value
       
   108     else:
       
   109         del session.transaction_data['fs_importing']
    98 
   110 
    99 
   111 
   100 class BytesFileSystemStorage(Storage):
   112 class BytesFileSystemStorage(Storage):
   101     """store Bytes attribute value on the file system"""
   113     """store Bytes attribute value on the file system"""
   102     def __init__(self, defaultdir, fsencoding='utf-8'):
   114     def __init__(self, defaultdir, fsencoding='utf-8'):