Back to index

plone3  3.1.7
datatype.py
Go to the documentation of this file.
00001 #  ATContentTypes http://plone.org/products/atcontenttypes/
00002 #  Archetypes reimplementation of the CMF core types
00003 #  Copyright (c) 2003-2006 AT Content Types development team
00004 #
00005 #  This program is free software; you can redistribute it and/or modify
00006 #  it under the terms of the GNU General Public License as published by
00007 #  the Free Software Foundation; either version 2 of the License, or
00008 #  (at your option) any later version.
00009 #
00010 #  This program is distributed in the hope that it will be useful,
00011 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 #  GNU General Public License for more details.
00014 #
00015 #  You should have received a copy of the GNU General Public License
00016 #  along with this program; if not, write to the Free Software
00017 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 #
00019 """ATCT ZConfig datatypes
00020 
00021 """
00022 __author__  = 'Christian Heimes <tiran@cheimes.de>'
00023 __docformat__ = 'restructuredtext'
00024 
00025 from Products.CMFCore import permissions as CMFCorePermissions
00026 from AccessControl import Permissions as ZopePermissions
00027 from ZConfig.datatypes import IdentifierConversion
00028 from ZConfig.datatypes import stock_datatypes
00029 
00030 _marker = object()
00031 
00032 def _getValueFromModule(module, key):
00033     var = getattr(module, key, _marker)
00034     if key is _marker:
00035         raise ValueError, "%s doesn't have an attribute %s" % (module, key)
00036     return var
00037 
00038 def _getValueFromDottedName(dotted_name):
00039     parts = dotted_name.split('.')
00040     module_name = '.'.join(parts[:-1])
00041     key = parts[-1]
00042     try:
00043         module = __import__(module_name, globals(), locals(), [key])
00044     except ImportError, msg:
00045         raise ValueError, str(msg)
00046     return _getValueFromModule(module, key)
00047 
00048 def permission_handler(value):
00049     """Parse a permission
00050     
00051     Valid value are:
00052         cmf.NAME - Products.CMFCore.permissions
00053         zope.NAME - AccessControl.Permissions
00054         aDottedName
00055     """
00056     if value.startswith('cmf.'):
00057         permission = _getValueFromModule(CMFCorePermissions, value[4:])
00058     elif value.startswith('zope.'):
00059         permission = _getValueFromModule(ZopePermissions, value[5:])
00060     else:
00061         permission = _getValueFromDottedName(value)
00062     if not isinstance(permission, basestring):
00063         raise ValueError, 'Permission %s is not a string: %s' % (permission,
00064             type(permission))
00065     return permission
00066 
00067 def identifier_none(value):
00068     if value == 'None':
00069         return None
00070     return IdentifierConversion()(value)
00071 
00072 def byte_size_in_mb(value):
00073     """Byte size handler for max size validator
00074     """
00075     if value.lower() == 'no':
00076         return 0.0
00077     byte_size = stock_datatypes["byte-size"]
00078     v = byte_size(value)
00079     return float(v) / (1024.0**2)
00080 
00081 def image_dimension(value):
00082     """Image dimension data type
00083     
00084     Splits a value of "200, 400" into two ints of (200, 400)
00085     """    
00086     if value.count(',') != 1:
00087         raise ValueError, "Width and height must be seperated by a comma"
00088     w, h = value.split(',')
00089     w = int(w)
00090     h = int(h)
00091     return (w, h)
00092 
00093 def image_dimension_or_no(value):
00094     """Image dimension data type with support for no
00095     
00096     Either "no" or (0, 0) results into None
00097     """
00098     if value.lower() == 'no':
00099         return None
00100     w, h = image_dimension(value)
00101     if (w, h) == (0, 0):
00102         return None
00103     return (w, h)
00104     
00105 def pil_algo(value):
00106     """Get PIL image filter algo from PIL.Image
00107     """
00108     try:
00109         import PIL.Image
00110     except ImportError:
00111         return None
00112     
00113     value = value.upper()
00114     available = ('NEAREST', 'BILINEAR', 'BICUBIC', 'ANTIALIAS')
00115     if value not in available:
00116         raise ValueError, "unknown algo %s" % value
00117     import PIL.Image
00118     return getattr(PIL.Image, value)
00119 
00120 class BaseFactory(object):
00121     """Basic factory
00122     """
00123     
00124     def __init__(self, section):
00125         self.name = section.getSectionName()
00126         #self._parsed = False
00127         self._section = section
00128         self._names = {}
00129         self._parse()
00130         
00131     #def __call__(self):
00132     #    if not self._parsed:
00133     #        self._parse()
00134     #    return self
00135         
00136     def set(self, name, value):
00137         self._names[name] = 1
00138         setattr(self, name, value)
00139         
00140     def _parse(self):
00141         raise NotImplementedError
00142         
00143 class MxTidy(BaseFactory):
00144     """data handler for mx tidy settings
00145     
00146     sets enable and options
00147     """
00148     
00149     def _parse(self):
00150         sec = self._section
00151         self.set('enable', sec.enable)
00152         cfg = {}
00153         for id in ('char_encoding', 'drop_empty_paras', 'drop_font_tags',
00154           'indent_spaces', 'input_xml', 'output_xhtml', 'quiet', 'show_warnings',
00155           'tab_size', 'word_2000', 'wrap'):
00156             cfg[id] = getattr(sec, id)
00157         self.set('options' , cfg)
00158 
00159 class Archetype(BaseFactory):
00160     """data handler for an archetype option
00161     """
00162     
00163     def _parse(self):
00164         sec = self._section
00165         self.set('max_file_size', sec.max_file_size)
00166         self.set('max_image_dimension', sec.max_image_dimension)
00167         self.set('allow_document_upload', sec.allow_document_upload)
00168         
00169         ct = sec.contenttypes
00170         if ct is not None:
00171             allowed = tuple(ct.allowed_content_types)
00172             default = ct.default_content_type
00173         
00174             if default not in allowed:
00175                 raise ValueError, "Default %s is not in %s" % (default, ct)
00176         
00177             self.set('default_content_type', default)
00178             self.set('allowed_content_types', allowed)
00179         
00180 class Feature(BaseFactory):
00181     """data handler for a feature
00182     """
00183     
00184     def _parse(self):
00185         sec = self._section
00186         self.set('enable', sec.enable)