__init__.py
brancholdstable
changeset 8746 88c71ad83d47
parent 8486 399e916a97f3
child 8556 bbe0d6985e59
equal deleted inserted replaced
8507:0c111b232927 8746:88c71ad83d47
     1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of CubicWeb.
     4 # This file is part of CubicWeb.
     5 #
     5 #
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
    39 
    39 
    40 import sys, os, logging
    40 import sys, os, logging
    41 from StringIO import StringIO
    41 from StringIO import StringIO
    42 
    42 
    43 from logilab.common.logging_ext import set_log_methods
    43 from logilab.common.logging_ext import set_log_methods
       
    44 from yams.constraints import BASE_CONVERTERS
    44 
    45 
    45 
    46 
    46 if os.environ.get('APYCOT_ROOT'):
    47 if os.environ.get('APYCOT_ROOT'):
    47     logging.basicConfig(level=logging.CRITICAL)
    48     logging.basicConfig(level=logging.CRITICAL)
    48 else:
    49 else:
    53 
    54 
    54 set_log_methods(sys.modules[__name__], logging.getLogger('cubicweb'))
    55 set_log_methods(sys.modules[__name__], logging.getLogger('cubicweb'))
    55 
    56 
    56 # make all exceptions accessible from the package
    57 # make all exceptions accessible from the package
    57 from cubicweb._exceptions import *
    58 from cubicweb._exceptions import *
       
    59 from logilab.common.registry import ObjectNotFound, NoSelectableObject, RegistryNotFound
    58 
    60 
    59 # convert eid to the right type, raise ValueError if it's not a valid eid
    61 # convert eid to the right type, raise ValueError if it's not a valid eid
    60 typed_eid = int
    62 typed_eid = int
    61 
    63 
    62 
    64 
    75     def write(self, data):
    77     def write(self, data):
    76         assert isinstance(data, (str, buffer)), \
    78         assert isinstance(data, (str, buffer)), \
    77                "Binary objects must use raw strings, not %s" % data.__class__
    79                "Binary objects must use raw strings, not %s" % data.__class__
    78         StringIO.write(self, data)
    80         StringIO.write(self, data)
    79 
    81 
    80     def to_file(self, filename):
    82     def to_file(self, fobj):
    81         """write a binary to disk
    83         """write a binary to disk
    82 
    84 
    83         the writing is performed in a safe way for files stored on
    85         the writing is performed in a safe way for files stored on
    84         Windows SMB shares
    86         Windows SMB shares
    85         """
    87         """
    86         pos = self.tell()
    88         pos = self.tell()
    87         with open(filename, 'wb') as fobj:
    89         self.seek(0)
    88             self.seek(0)
    90         if sys.platform == 'win32':
    89             if sys.platform == 'win32':
    91             while True:
    90                 while True:
    92                 # the 16kB chunksize comes from the shutil module
    91                     # the 16kB chunksize comes from the shutil module
    93                 # in stdlib
    92                     # in stdlib
    94                 chunk = self.read(16*1024)
    93                     chunk = self.read(16*1024)
    95                 if not chunk:
    94                     if not chunk:
    96                     break
    95                         break
    97                 fobj.write(chunk)
    96                     fobj.write(chunk)
    98         else:
    97             else:
    99             fobj.write(self.read())
    98                 fobj.write(self.read())
       
    99         self.seek(pos)
   100         self.seek(pos)
   100 
   101 
   101     @staticmethod
   102     @staticmethod
   102     def from_file(filename):
   103     def from_file(filename):
   103         """read a file and returns its contents in a Binary
   104         """read a file and returns its contents in a Binary
   118             else:
   119             else:
   119                 binary.write(fobj.read())
   120                 binary.write(fobj.read())
   120         binary.seek(0)
   121         binary.seek(0)
   121         return binary
   122         return binary
   122 
   123 
       
   124 def str_or_binary(value):
       
   125     if isinstance(value, Binary):
       
   126         return value
       
   127     return str(value)
       
   128 BASE_CONVERTERS['Password'] = str_or_binary
       
   129 
       
   130 
   123 
   131 
   124 # use this dictionary to rename entity types while keeping bw compat
   132 # use this dictionary to rename entity types while keeping bw compat
   125 ETYPE_NAME_MAP = {}
   133 ETYPE_NAME_MAP = {}
   126 
   134 
   127 # XXX cubic web cube migration map. See if it's worth keeping this mecanism
   135 # XXX cubic web cube migration map. See if it's worth keeping this mecanism