cubicweb/__init__.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Thu, 21 Mar 2019 14:33:54 +0100
changeset 12530 9d88e1177c35
parent 12508 a8c1ea390400
child 12543 71aa20cb43f2
permissions -rw-r--r--
Remove Twisted web server Twisted web server is not used anymore and has been superseded by pyramid many years ago. Furthermore, our usage is not compatible with Python 3. So we drop the "etwist" sub-package. As a consequence, "all-in-one" configuration type gets dropped as it was Twisted-specific. We resurrect it in cubicweb/pyramid/config.py by only keeping options used by the "pyramid". Similarly, we introduce a AllInOneCreateHandler in cubicweb/pyramid/pyramidctl.py that is basically the one that lived in cubicweb/etwist/twctl.py and is used to create the "all-in-one" instance. Added a TODO here about "pyramid.ini" that could be generated at the end of bootstrap() method. In cubicweb/devtools/httptest.py, CubicWebServerTC is now equivalent to CubicWebWsgiTC and the latter is dropped.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11281
diff changeset
     1
# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5081
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5081
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5081
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5081
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5081
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5081
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5081
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5081
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5081
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5081
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5081
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5081
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5081
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5081
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5081
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
"""CubicWeb is a generic framework to quickly build applications which describes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
relations between entitites.
5881
57387070f612 [selectors] use before-registry-reset event to init is_instance cache: cleaner code and avoid reloading bug (making hooks test fail for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5451
diff changeset
    20
"""
11767
432f87a63057 flake8 and all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11473
diff changeset
    21
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
11457
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
    23
import imp
11249
0ff4c02a1871 [test] Fix devtools unittest_i18n when ran without pytest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11129
diff changeset
    24
import logging
0ff4c02a1871 [test] Fix devtools unittest_i18n when ran without pytest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11129
diff changeset
    25
import os
11281
2cb8b383a519 Add missing import, eaten by previous merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11279
diff changeset
    26
import pickle
11249
0ff4c02a1871 [test] Fix devtools unittest_i18n when ran without pytest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11129
diff changeset
    27
import sys
11920
f13799fbcfea [cwconfig] create a virtual "cubes" package
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11821
diff changeset
    28
import types
5451
7b2e9f774028 ignore annoying warnings about pygments already being imported and stuff being added to python path
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5424
diff changeset
    29
import warnings
9299
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8748
diff changeset
    30
import zlib
11279
e4f11ef1face backport 3.22 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11274 11249
diff changeset
    31
10665
79ff784cd8af [py3k] introduce cubicweb._ (related to #7589459)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10616
diff changeset
    32
from six import PY2, binary_type, text_type
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
11811
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
    34
from logilab.common.logging_ext import set_log_methods
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
    35
from yams.constraints import BASE_CONVERTERS, BASE_CHECKERS
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
    36
from yams.schema import role_name as rname
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
    37
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
    38
from cubicweb.__pkginfo__ import version as __version__   # noqa
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
    39
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
    40
# make all exceptions accessible from the package
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
    41
from logilab.common.registry import ObjectNotFound, NoSelectableObject, RegistryNotFound  # noqa
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
    42
from yams import ValidationError
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
    43
from cubicweb._exceptions import *  # noqa
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
    44
11274
d0f6fe008ec4 Test and fix "pickleability" of Binary objects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11102
diff changeset
    45
if PY2:
11102
cd1267c1243e Fix Binary on python < 2.7.4 (closes #10593811)
Julien Cristau <julien.cristau@logilab.fr>
parents: 10907
diff changeset
    46
    # http://bugs.python.org/issue10211
cd1267c1243e Fix Binary on python < 2.7.4 (closes #10593811)
Julien Cristau <julien.cristau@logilab.fr>
parents: 10907
diff changeset
    47
    from StringIO import StringIO as BytesIO
cd1267c1243e Fix Binary on python < 2.7.4 (closes #10593811)
Julien Cristau <julien.cristau@logilab.fr>
parents: 10907
diff changeset
    48
else:
cd1267c1243e Fix Binary on python < 2.7.4 (closes #10593811)
Julien Cristau <julien.cristau@logilab.fr>
parents: 10907
diff changeset
    49
    from io import BytesIO
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
11249
0ff4c02a1871 [test] Fix devtools unittest_i18n when ran without pytest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11129
diff changeset
    51
# ignore the pygments UserWarnings
0ff4c02a1871 [test] Fix devtools unittest_i18n when ran without pytest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11129
diff changeset
    52
warnings.filterwarnings('ignore', category=UserWarning,
0ff4c02a1871 [test] Fix devtools unittest_i18n when ran without pytest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11129
diff changeset
    53
                        message='.*was already imported',
0ff4c02a1871 [test] Fix devtools unittest_i18n when ran without pytest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11129
diff changeset
    54
                        module='.*pygments')
0ff4c02a1871 [test] Fix devtools unittest_i18n when ran without pytest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11129
diff changeset
    55
9891
3386fd89c914 remove references to global environment variable APYCOT_ROOT
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9402
diff changeset
    56
# pre python 2.7.2 safety
3386fd89c914 remove references to global environment variable APYCOT_ROOT
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9402
diff changeset
    57
logging.basicConfig()
11811
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
    58
set_log_methods(sys.modules[__name__], logging.getLogger('cubicweb'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
11249
0ff4c02a1871 [test] Fix devtools unittest_i18n when ran without pytest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11129
diff changeset
    60
# this is necessary for i18n devtools test where chdir is done while __path__ is relative, which
0ff4c02a1871 [test] Fix devtools unittest_i18n when ran without pytest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11129
diff changeset
    61
# breaks later imports
11811
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
    62
__path__[0] = os.path.abspath(__path__[0])  # noqa
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
    63
CW_SOFTWARE_ROOT = __path__[0]  # noqa
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
10665
79ff784cd8af [py3k] introduce cubicweb._ (related to #7589459)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10616
diff changeset
    65
79ff784cd8af [py3k] introduce cubicweb._ (related to #7589459)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10616
diff changeset
    66
# '_' is available to mark internationalized string but should not be used to
79ff784cd8af [py3k] introduce cubicweb._ (related to #7589459)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10616
diff changeset
    67
# do the actual translation
79ff784cd8af [py3k] introduce cubicweb._ (related to #7589459)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10616
diff changeset
    68
_ = text_type
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
10616
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents: 10602
diff changeset
    71
class Binary(BytesIO):
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents: 10602
diff changeset
    72
    """class to hold binary data. Use BytesIO to prevent use of unicode data"""
12355
c703dc95c82e Fix flake8 issues since release 3.6.0
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11955
diff changeset
    73
    _allowed_types = (binary_type, bytearray, buffer if PY2 else memoryview)  # noqa: F405
10616
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents: 10602
diff changeset
    74
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents: 10602
diff changeset
    75
    def __init__(self, buf=b''):
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents: 10602
diff changeset
    76
        assert isinstance(buf, self._allowed_types), \
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11281
diff changeset
    77
            "Binary objects must use bytes/buffer objects, not %s" % buf.__class__
11102
cd1267c1243e Fix Binary on python < 2.7.4 (closes #10593811)
Julien Cristau <julien.cristau@logilab.fr>
parents: 10907
diff changeset
    78
        # don't call super, BytesIO may be an old-style class (on python < 2.7.4)
cd1267c1243e Fix Binary on python < 2.7.4 (closes #10593811)
Julien Cristau <julien.cristau@logilab.fr>
parents: 10907
diff changeset
    79
        BytesIO.__init__(self, buf)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
    def write(self, data):
10616
f454404733c1 Port cw.Binary to io.BytesIO
Rémi Cardona <remi.cardona@free.fr>
parents: 10602
diff changeset
    82
        assert isinstance(data, self._allowed_types), \
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11281
diff changeset
    83
            "Binary objects must use bytes/buffer objects, not %s" % data.__class__
11102
cd1267c1243e Fix Binary on python < 2.7.4 (closes #10593811)
Julien Cristau <julien.cristau@logilab.fr>
parents: 10907
diff changeset
    84
        # don't call super, BytesIO may be an old-style class (on python < 2.7.4)
cd1267c1243e Fix Binary on python < 2.7.4 (closes #10593811)
Julien Cristau <julien.cristau@logilab.fr>
parents: 10907
diff changeset
    85
        BytesIO.write(self, data)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
8180
1f6ba9afb925 [storage] BFSS now create read only file (closes #2151672)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8132
diff changeset
    87
    def to_file(self, fobj):
8131
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
    88
        """write a binary to disk
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
    89
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
    90
        the writing is performed in a safe way for files stored on
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
    91
        Windows SMB shares
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
    92
        """
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
    93
        pos = self.tell()
8180
1f6ba9afb925 [storage] BFSS now create read only file (closes #2151672)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8132
diff changeset
    94
        self.seek(0)
1f6ba9afb925 [storage] BFSS now create read only file (closes #2151672)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8132
diff changeset
    95
        if sys.platform == 'win32':
1f6ba9afb925 [storage] BFSS now create read only file (closes #2151672)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8132
diff changeset
    96
            while True:
1f6ba9afb925 [storage] BFSS now create read only file (closes #2151672)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8132
diff changeset
    97
                # the 16kB chunksize comes from the shutil module
1f6ba9afb925 [storage] BFSS now create read only file (closes #2151672)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8132
diff changeset
    98
                # in stdlib
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11281
diff changeset
    99
                chunk = self.read(16 * 1024)
8180
1f6ba9afb925 [storage] BFSS now create read only file (closes #2151672)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8132
diff changeset
   100
                if not chunk:
1f6ba9afb925 [storage] BFSS now create read only file (closes #2151672)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8132
diff changeset
   101
                    break
1f6ba9afb925 [storage] BFSS now create read only file (closes #2151672)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8132
diff changeset
   102
                fobj.write(chunk)
1f6ba9afb925 [storage] BFSS now create read only file (closes #2151672)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8132
diff changeset
   103
        else:
1f6ba9afb925 [storage] BFSS now create read only file (closes #2151672)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8132
diff changeset
   104
            fobj.write(self.read())
8131
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   105
        self.seek(pos)
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   106
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   107
    @staticmethod
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   108
    def from_file(filename):
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   109
        """read a file and returns its contents in a Binary
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   110
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   111
        the reading is performed in a safe way for files stored on
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   112
        Windows SMB shares
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   113
        """
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   114
        binary = Binary()
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   115
        with open(filename, 'rb') as fobj:
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   116
            if sys.platform == 'win32':
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   117
                while True:
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   118
                    # the 16kB chunksize comes from the shutil module
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   119
                    # in stdlib
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11281
diff changeset
   120
                    chunk = fobj.read(16 * 1024)
8131
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   121
                    if not chunk:
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   122
                        break
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   123
                    binary.write(chunk)
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   124
            else:
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   125
                binary.write(fobj.read())
8234
83fd9ff90a9d [binary] use seek(0) in `from_file` Binary creation method (closes #2189183)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 8132
diff changeset
   126
        binary.seek(0)
8131
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   127
        return binary
a6654712ad50 fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7879
diff changeset
   128
9299
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8748
diff changeset
   129
    def __eq__(self, other):
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8748
diff changeset
   130
        if not isinstance(other, Binary):
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8748
diff changeset
   131
            return False
9912
a84922befb89 Fix Binary.__eq__ (closes #4172701)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9299
diff changeset
   132
        return self.getvalue() == other.getvalue()
9299
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8748
diff changeset
   133
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8748
diff changeset
   134
    # Binary helpers to store/fetch python objects
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8748
diff changeset
   135
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8748
diff changeset
   136
    @classmethod
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8748
diff changeset
   137
    def zpickle(cls, obj):
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8748
diff changeset
   138
        """ return a Binary containing a gzipped pickle of obj """
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8748
diff changeset
   139
        retval = cls()
10602
4845012cfc8e [py3k] import 'pickle' using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10272
diff changeset
   140
        retval.write(zlib.compress(pickle.dumps(obj, protocol=2)))
9299
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8748
diff changeset
   141
        return retval
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8748
diff changeset
   142
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8748
diff changeset
   143
    def unzpickle(self):
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8748
diff changeset
   144
        """ decompress and loads the stream before returning it """
10602
4845012cfc8e [py3k] import 'pickle' using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10272
diff changeset
   145
        return pickle.loads(zlib.decompress(self.getvalue()))
9299
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8748
diff changeset
   146
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8748
diff changeset
   147
10045
0a49d61c8cca Add custom checker for Password values
Julien Cristau <julien.cristau@logilab.fr>
parents: 9990
diff changeset
   148
def check_password(eschema, value):
10680
1b4df4b3cd9a [py3k] str → six.binary_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10665
diff changeset
   149
    return isinstance(value, (binary_type, Binary))
11821
7534b32c45e3 Fix (new) flake8 errors
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11811
diff changeset
   150
7534b32c45e3 Fix (new) flake8 errors
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11811
diff changeset
   151
10045
0a49d61c8cca Add custom checker for Password values
Julien Cristau <julien.cristau@logilab.fr>
parents: 9990
diff changeset
   152
BASE_CHECKERS['Password'] = check_password
0a49d61c8cca Add custom checker for Password values
Julien Cristau <julien.cristau@logilab.fr>
parents: 9990
diff changeset
   153
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11281
diff changeset
   154
8383
3f34d69e0c22 [yams] Binary instance passwords should not be given to str()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8236
diff changeset
   155
def str_or_binary(value):
3f34d69e0c22 [yams] Binary instance passwords should not be given to str()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8236
diff changeset
   156
    if isinstance(value, Binary):
3f34d69e0c22 [yams] Binary instance passwords should not be given to str()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8236
diff changeset
   157
        return value
10680
1b4df4b3cd9a [py3k] str → six.binary_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10665
diff changeset
   158
    return binary_type(value)
11821
7534b32c45e3 Fix (new) flake8 errors
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11811
diff changeset
   159
7534b32c45e3 Fix (new) flake8 errors
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11811
diff changeset
   160
8383
3f34d69e0c22 [yams] Binary instance passwords should not be given to str()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8236
diff changeset
   161
BASE_CONVERTERS['Password'] = str_or_binary
3f34d69e0c22 [yams] Binary instance passwords should not be given to str()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8236
diff changeset
   162
3f34d69e0c22 [yams] Binary instance passwords should not be given to str()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8236
diff changeset
   163
4475
37c413a07216 kill most pre 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   164
# use this dictionary to rename entity types while keeping bw compat
37c413a07216 kill most pre 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   165
ETYPE_NAME_MAP = {}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
4475
37c413a07216 kill most pre 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   167
# XXX cubic web cube migration map. See if it's worth keeping this mecanism
37c413a07216 kill most pre 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   168
#     to help in cube renaming
37c413a07216 kill most pre 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   169
CW_MIGRATION_MAP = {}
231
d740f5f55d30 some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   170
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11281
diff changeset
   171
231
d740f5f55d30 some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   172
def neg_role(role):
d740f5f55d30 some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   173
    if role == 'subject':
d740f5f55d30 some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   174
        return 'object'
d740f5f55d30 some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   175
    return 'subject'
d740f5f55d30 some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   176
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11281
diff changeset
   177
231
d740f5f55d30 some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   178
def role(obj):
d740f5f55d30 some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   179
    try:
d740f5f55d30 some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   180
        return obj.role
d740f5f55d30 some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   181
    except AttributeError:
d740f5f55d30 some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   182
        return neg_role(obj.target)
d740f5f55d30 some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   183
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11281
diff changeset
   184
231
d740f5f55d30 some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   185
def target(obj):
d740f5f55d30 some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   186
    try:
d740f5f55d30 some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   187
        return obj.target
d740f5f55d30 some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   188
    except AttributeError:
d740f5f55d30 some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   189
        return neg_role(obj.role)
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   190
2683
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   191
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   192
class CubicWebEventManager(object):
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   193
    """simple event / callback manager.
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   194
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   195
    Typical usage to register a callback::
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   196
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   197
      >>> from cubicweb import CW_EVENT_MANAGER
2705
30bcdbd92820 [events] renamed source-reload into registry-reload to avoid potential confusions with datasources
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2689
diff changeset
   198
      >>> CW_EVENT_MANAGER.bind('after-registry-reload', mycallback)
2683
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   199
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   200
    Typical usage to emit an event::
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   201
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   202
      >>> from cubicweb import CW_EVENT_MANAGER
2705
30bcdbd92820 [events] renamed source-reload into registry-reload to avoid potential confusions with datasources
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2689
diff changeset
   203
      >>> CW_EVENT_MANAGER.emit('after-registry-reload')
2683
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   204
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   205
    emit() accepts an additional context parameter that will be passed
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   206
    to the callback if specified (and only in that case)
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   207
    """
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   208
    def __init__(self):
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   209
        self.callbacks = {}
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   210
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   211
    def bind(self, event, callback, *args, **kwargs):
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11281
diff changeset
   212
        self.callbacks.setdefault(event, []).append((callback, args, kwargs))
2683
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   213
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   214
    def emit(self, event, context=None):
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   215
        for callback, args, kwargs in self.callbacks.get(event, ()):
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   216
            if context is None:
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   217
                callback(*args, **kwargs)
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   218
            else:
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   219
                callback(context, *args, **kwargs)
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   220
11821
7534b32c45e3 Fix (new) flake8 errors
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11811
diff changeset
   221
2683
52b1a86c1913 introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2647
diff changeset
   222
CW_EVENT_MANAGER = CubicWebEventManager()
2689
44f041222d0f [autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2683
diff changeset
   223
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11281
diff changeset
   224
5081
2ea98b8512dd [events manager] onevent decorator may be given extra arguments to give to em.bind
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4475
diff changeset
   225
def onevent(event, *args, **kwargs):
2689
44f041222d0f [autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2683
diff changeset
   226
    """decorator to ease event / callback binding
44f041222d0f [autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2683
diff changeset
   227
44f041222d0f [autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2683
diff changeset
   228
    >>> from cubicweb import onevent
2705
30bcdbd92820 [events] renamed source-reload into registry-reload to avoid potential confusions with datasources
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2689
diff changeset
   229
    >>> @onevent('before-registry-reload')
2689
44f041222d0f [autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2683
diff changeset
   230
    ... def mycallback():
44f041222d0f [autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2683
diff changeset
   231
    ...     print 'hello'
44f041222d0f [autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2683
diff changeset
   232
    ...
44f041222d0f [autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2683
diff changeset
   233
    >>>
44f041222d0f [autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2683
diff changeset
   234
    """
44f041222d0f [autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2683
diff changeset
   235
    def _decorator(func):
5081
2ea98b8512dd [events manager] onevent decorator may be given extra arguments to give to em.bind
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4475
diff changeset
   236
        CW_EVENT_MANAGER.bind(event, func, *args, **kwargs)
2689
44f041222d0f [autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2683
diff changeset
   237
        return func
44f041222d0f [autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2683
diff changeset
   238
    return _decorator
8556
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   239
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   240
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   241
def validation_error(entity, errors, substitutions=None, i18nvalues=None):
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   242
    """easy way to retrieve a :class:`cubicweb.ValidationError` for an entity or eid.
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   243
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   244
    You may also have 2-tuple as error keys, :func:`yams.role_name` will be
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   245
    called automatically for them.
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   246
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   247
    Messages in errors **should not be translated yet**, though marked for
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   248
    internationalization. You may give an additional substition dictionary that
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   249
    will be used for interpolation after the translation.
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   250
    """
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   251
    if substitutions is None:
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   252
        # set empty dict else translation won't be done for backward
8594
001159e2e4f3 [validation api] properly use yams 0.36 validation error api and update message catalog. Follows bbe0d6985e59
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
   253
        # compatibility reason (see ValidationError.translate method)
8556
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   254
        substitutions = {}
8696
0bb18407c053 [toward py3k] rewrite dict.keys() and dict.values() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
   255
    for key in list(errors):
8556
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   256
        if isinstance(key, tuple):
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   257
            errors[rname(*key)] = errors.pop(key)
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   258
    return ValidationError(getattr(entity, 'eid', entity), errors,
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8486
diff changeset
   259
                           substitutions, i18nvalues)
9046
6fb3f0106301 [dbapi] move ProgrammingError into cubicweb module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8748
diff changeset
   260
6fb3f0106301 [dbapi] move ProgrammingError into cubicweb module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8748
diff changeset
   261
6fb3f0106301 [dbapi] move ProgrammingError into cubicweb module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8748
diff changeset
   262
# exceptions ##################################################################
6fb3f0106301 [dbapi] move ProgrammingError into cubicweb module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8748
diff changeset
   263
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11281
diff changeset
   264
class ProgrammingError(Exception):
9046
6fb3f0106301 [dbapi] move ProgrammingError into cubicweb module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8748
diff changeset
   265
    """Exception raised for errors that are related to the database's operation
6fb3f0106301 [dbapi] move ProgrammingError into cubicweb module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8748
diff changeset
   266
    and not necessarily under the control of the programmer, e.g. an unexpected
6fb3f0106301 [dbapi] move ProgrammingError into cubicweb module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8748
diff changeset
   267
    disconnect occurs, the data source name is not found, a transaction could
6fb3f0106301 [dbapi] move ProgrammingError into cubicweb module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8748
diff changeset
   268
    not be processed, a memory allocation error occurred during processing,
6fb3f0106301 [dbapi] move ProgrammingError into cubicweb module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8748
diff changeset
   269
    etc.
6fb3f0106301 [dbapi] move ProgrammingError into cubicweb module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8748
diff changeset
   270
    """
11457
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   271
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   272
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   273
# Import hook for "legacy" cubes ##############################################
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   274
11955
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   275
class _CubesLoader(object):
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   276
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   277
    def __init__(self, *modinfo):
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   278
        self.modinfo = modinfo
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   279
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   280
    def load_module(self, fullname):
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   281
        try:
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   282
            # If there is an existing module object named 'fullname' in
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   283
            # sys.modules , the loader must use that existing module.
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   284
            # Otherwise, the reload() builtin will not work correctly.
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   285
            return sys.modules[fullname]
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   286
        except KeyError:
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   287
            pass
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   288
        if fullname == 'cubes':
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   289
            mod = sys.modules[fullname] = types.ModuleType(
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   290
                fullname, doc='CubicWeb cubes')
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   291
        else:
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   292
            modname, file, pathname, description = self.modinfo
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   293
            try:
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   294
                mod = sys.modules[fullname] = imp.load_module(
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   295
                    modname, file, pathname, description)
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   296
            finally:
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   297
                # https://docs.python.org/2/library/imp.html#imp.load_module
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   298
                # Important: the caller is responsible for closing the file
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   299
                # argument, if it was not None, even when an exception is
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   300
                # raised. This is best done using a try ... finally statement
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   301
                if file is not None:
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   302
                    file.close()
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   303
        return mod
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   304
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   305
11457
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   306
class _CubesImporter(object):
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   307
    """Module finder handling redirection of import of "cubes.<name>"
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   308
    to "cubicweb_<name>".
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   309
    """
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   310
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   311
    @classmethod
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   312
    def install(cls):
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   313
        if not any(isinstance(x, cls) for x in sys.meta_path):
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   314
            self = cls()
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   315
            sys.meta_path.append(self)
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   316
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   317
    def find_module(self, fullname, path=None):
11920
f13799fbcfea [cwconfig] create a virtual "cubes" package
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11821
diff changeset
   318
        if fullname == 'cubes':
11955
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   319
            return _CubesLoader()
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   320
        elif fullname.startswith('cubes.') and fullname.count('.') == 1:
11457
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   321
            modname = 'cubicweb_' + fullname.split('.', 1)[1]
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   322
            try:
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   323
                modinfo = imp.find_module(modname)
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   324
            except ImportError:
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   325
                return None
d404fd8499dd Add an import redirect hook from "cubes.<name>" to "cubicweb_<name>"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11417
diff changeset
   326
            else:
11955
f85ec84355db Fix possible double import of cubes modules
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11954
diff changeset
   327
                return _CubesLoader(modname, *modinfo)