cubicweb/__init__.py
changeset 12567 26744ad37953
parent 12543 71aa20cb43f2
equal deleted inserted replaced
12566:6b3523f81f42 12567:26744ad37953
    25 import pickle
    25 import pickle
    26 import sys
    26 import sys
    27 import warnings
    27 import warnings
    28 import zlib
    28 import zlib
    29 
    29 
    30 from six import PY2, binary_type, text_type
       
    31 
       
    32 from logilab.common.logging_ext import set_log_methods
    30 from logilab.common.logging_ext import set_log_methods
    33 from yams.constraints import BASE_CONVERTERS, BASE_CHECKERS
    31 from yams.constraints import BASE_CONVERTERS, BASE_CHECKERS
    34 from yams.schema import role_name as rname
    32 from yams.schema import role_name as rname
    35 
    33 
    36 from cubicweb.__pkginfo__ import version as __version__   # noqa
    34 from cubicweb.__pkginfo__ import version as __version__   # noqa
    38 # make all exceptions accessible from the package
    36 # make all exceptions accessible from the package
    39 from logilab.common.registry import ObjectNotFound, NoSelectableObject, RegistryNotFound  # noqa
    37 from logilab.common.registry import ObjectNotFound, NoSelectableObject, RegistryNotFound  # noqa
    40 from yams import ValidationError
    38 from yams import ValidationError
    41 from cubicweb._exceptions import *  # noqa
    39 from cubicweb._exceptions import *  # noqa
    42 
    40 
    43 if PY2:
    41 from io import BytesIO
    44     # http://bugs.python.org/issue10211
       
    45     from StringIO import StringIO as BytesIO
       
    46 else:
       
    47     from io import BytesIO
       
    48 
    42 
    49 # ignore the pygments UserWarnings
    43 # ignore the pygments UserWarnings
    50 warnings.filterwarnings('ignore', category=UserWarning,
    44 warnings.filterwarnings('ignore', category=UserWarning,
    51                         message='.*was already imported',
    45                         message='.*was already imported',
    52                         module='.*pygments')
    46                         module='.*pygments')
    61 CW_SOFTWARE_ROOT = __path__[0]  # noqa
    55 CW_SOFTWARE_ROOT = __path__[0]  # noqa
    62 
    56 
    63 
    57 
    64 # '_' is available to mark internationalized string but should not be used to
    58 # '_' is available to mark internationalized string but should not be used to
    65 # do the actual translation
    59 # do the actual translation
    66 _ = text_type
    60 _ = str
    67 
    61 
    68 
    62 
    69 class Binary(BytesIO):
    63 class Binary(BytesIO):
    70     """class to hold binary data. Use BytesIO to prevent use of unicode data"""
    64     """class to hold binary data. Use BytesIO to prevent use of unicode data"""
    71     _allowed_types = (binary_type, bytearray, buffer if PY2 else memoryview)  # noqa: F405
    65     _allowed_types = (bytes, bytearray, memoryview)
    72 
    66 
    73     def __init__(self, buf=b''):
    67     def __init__(self, buf=b''):
    74         assert isinstance(buf, self._allowed_types), \
    68         assert isinstance(buf, self._allowed_types), \
    75             "Binary objects must use bytes/buffer objects, not %s" % buf.__class__
    69             "Binary objects must use bytes/buffer objects, not %s" % buf.__class__
    76         # don't call super, BytesIO may be an old-style class (on python < 2.7.4)
    70         # don't call super, BytesIO may be an old-style class (on python < 2.7.4)
   142         """ decompress and loads the stream before returning it """
   136         """ decompress and loads the stream before returning it """
   143         return pickle.loads(zlib.decompress(self.getvalue()))
   137         return pickle.loads(zlib.decompress(self.getvalue()))
   144 
   138 
   145 
   139 
   146 def check_password(eschema, value):
   140 def check_password(eschema, value):
   147     return isinstance(value, (binary_type, Binary))
   141     return isinstance(value, (bytes, Binary))
   148 
   142 
   149 
   143 
   150 BASE_CHECKERS['Password'] = check_password
   144 BASE_CHECKERS['Password'] = check_password
   151 
   145 
   152 
   146 
   153 def str_or_binary(value):
   147 def str_or_binary(value):
   154     if isinstance(value, Binary):
   148     if isinstance(value, Binary):
   155         return value
   149         return value
   156     return binary_type(value)
   150     return bytes(value)
   157 
   151 
   158 
   152 
   159 BASE_CONVERTERS['Password'] = str_or_binary
   153 BASE_CONVERTERS['Password'] = str_or_binary
   160 
   154 
   161 
   155