- Timestamp:
- 4 Jun 2011, 11:17:28 (14 years ago)
- Location:
- main/waeup.sirp/trunk/src/waeup/sirp
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/interfaces.py
r6273 r6276 181 181 `logger` is the logger to use during import. 182 182 """ 183 184 class ISchemaTypeConverter(Interface):185 """A converter for zope.schema.types.186 """187 def convert(string):188 """Convert string to certain schema type.189 """190 191 183 192 184 class IUserAccount(IWAeUPObject): -
main/waeup.sirp/trunk/src/waeup/sirp/utils/batching.py
r6273 r6276 15 15 from zope.schema import getFields 16 16 from waeup.sirp.interfaces import ( 17 IBatchProcessor, ISchemaTypeConverter, FatalCSVError, DuplicationError, 18 IObjectConverter) 17 IBatchProcessor, FatalCSVError, DuplicationError, IObjectConverter) 19 18 20 19 class BatchProcessor(grok.GlobalUtility): … … 122 121 result[raw_header[num]] = field 123 122 return result 124 125 def getFieldConverters(self, fieldnames):126 """Get converters for fieldnames.127 """128 result = dict()129 for key, field in getFields(self.iface).items():130 if key not in fieldnames:131 continue132 converter = ISchemaTypeConverter(field)133 result[key] = converter134 return result135 136 def convertToTypes(self, row, converter_dict):137 """Convert values in given row to destination type.138 """139 if '--IGNORE--' in row.keys():140 del row['--IGNORE--']141 warnings = []142 for key, value in row.items():143 converter = converter_dict.get(key, None)144 if converter:145 try:146 row.update({key:converter_dict[key].fromString(value)})147 except:148 msg = "conversion error: field %s: %s %r" % (149 key, sys.exc_info()[0], sys.exc_info()[1])150 warnings.append(msg)151 return (row, warnings)152 123 153 124 def stringFromErrs(self, errors, inv_errors): … … 244 215 self.checkHeaders(headerfields, mode) 245 216 mapping = self.getMapping(path, headerfields, mode) 246 converters = self.getFieldConverters(headerfields)247 217 reader = csv.DictReader(open(path, 'rb')) 248 218 -
main/waeup.sirp/trunk/src/waeup/sirp/utils/converters.py
r6273 r6276 1 1 """Converters for zope.schema-based datatypes. 2 2 """ 3 import datetime4 3 import grok 5 from zope.component import getMultiAdapter6 from zope.publisher.browser import TestRequest7 try:8 from zope.app.form.browser.interfaces import ITerms9 except ImportError:10 from zope.browser.interfaces import ITerms11 from zope.schema.interfaces import IBool, IText, IInt, IChoice, IDate12 from waeup.sirp.interfaces import ISchemaTypeConverter13 14 # If a string has this value, it is considered as 'missing_value' or None.15 NONE_STRING_VALUE = ''16 17 class Converter(grok.Adapter):18 """Base for string-or-none to zope.schema field converters.19 """20 grok.baseclass()21 grok.provides(ISchemaTypeConverter)22 23 def __init__(self, context):24 """Create a converter with context, a zope.schema.Field, as context.25 """26 self.context = context27 28 def _convertValueFromString(self, string):29 """You must at least override this method to build a working30 converter.31 """32 raise NotImplementedError('method not implemented')33 34 def fromString(self, string=None, strict=True):35 """Convert ``string`` to value according to assigned field type.36 """37 result = None38 if string is NONE_STRING_VALUE:39 string = None40 if string is None:41 if self.context.required is True:42 result = self.context.default43 else:44 result = self.context.missing_value45 else:46 result = self._convertValueFromString(string)47 if strict:48 self.context.validate(result)49 return result50 51 def _convertValueToString(self, value):52 return str(value)53 54 def toString(self, value, strict=True):55 """Convert given `value` to string according to assigned field type.56 """57 if strict:58 self.context.validate(value)59 if value == self.context.missing_value:60 return None61 return self._convertValueToString(value)62 63 class BoolConverter(Converter):64 """A converter for zope.schema.Bool fields.65 """66 grok.context(IBool)67 grok.provides(ISchemaTypeConverter)68 69 def _convertValueFromString(self, string):70 if string is NONE_STRING_VALUE:71 string = None72 if string is None:73 return None74 if string.lower() in ['1', 'true', 'yes']:75 return True76 return False77 78 def _convertValueToString(self, value):79 if value is None:80 return None81 if value:82 return '1'83 return '0'84 85 class TextConverter(Converter):86 """A converter for zope.schema.interfaces.IText fields.87 """88 grok.context(IText)89 grok.provides(ISchemaTypeConverter)90 91 def _convertValueFromString(self, string):92 return unicode(string)93 94 class IntConverter(Converter):95 """A converter for zope.schema.Int fields.96 """97 grok.context(IInt)98 grok.provides(ISchemaTypeConverter)99 100 def _convertValueFromString(self, string):101 return int(string)102 103 class ChoiceConverter(Converter):104 """A converter for zope.schema.Choice fields.105 """106 grok.context(IChoice)107 grok.provides(ISchemaTypeConverter)108 109 tokens = None110 values = None111 112 def __init__(self, context):113 self.context = context114 if not hasattr(self.context.source, 'factory'):115 try:116 self.terms = getMultiAdapter(117 (self.context.source, TestRequest()), ITerms)118 except:119 self.terms = None120 121 return122 if not hasattr(self.context.source.factory, 'getToken'):123 return124 # For expensive token/key lookups we create a 'cache'125 # here. This speeds up mass operations with many conversions126 # by factor 10 or more.127 128 # Mapping token -> value129 self.tokens = dict([(self.context.source.factory.getToken(x), x)130 for x in self.context.source.factory.getValues()])131 # Mapping value -> token132 self.values = dict([(y,x) for x,y in self.tokens.items()])133 134 def _convertValueFromString(self, string):135 if self.tokens is not None:136 result = None137 try:138 result = self.tokens[string]139 except KeyError:140 # Be gentle...141 try:142 result = self.tokens[string.lower()]143 return result144 except KeyError:145 tokenlist = (','.join(self.tokens[:2]))146 raise ValueError(147 'The token %s is not valid. Use one of %s, ...' % (148 string, tokenlist))149 return result150 if self.terms is not None:151 return self.terms.getValue(string)152 result = self.context.source.getTermByToken(string).value153 return result154 155 def _convertValueToString(self, value):156 if self.values is not None:157 return self.values[value]158 if self.terms is not None:159 return self.terms.getTerm(value).token160 return str(value)161 162 def fromString(self, string=None, strict=False):163 """Convert ``string`` to value according to assigned field type.164 165 We change the default for ``strict``: this disables extra166 validation checks for Choice fields and saves lots of time. If167 a string/value is out of allowed range we get a value or key168 error anyway.169 """170 return super(ChoiceConverter, self).fromString(string=string,171 strict=strict)172 173 def toString(self, value, strict=False):174 """Convert ``value`` to string according to assigned field type.175 176 We change the default for ``strict``: this disables extra177 validation checks for Choice fields and saves lots of time. If178 a string/value is out of allowed range we get a value or key179 error anyway.180 """181 return super(ChoiceConverter, self).toString(value=value,182 strict=strict)183 184 class DateConverter(Converter):185 """A converter for zope.schema.IDate fields.186 187 Converts date to string and vice versa. Stringified dates are188 expected in format ``YYYY-MM-DD``.189 190 To support at least some other formats, we accept for conversion191 from string also the following formats:192 193 * ``YYYY/MM/DD``194 195 * ``DD/MM/YYYY``196 197 * ``D/M/YYYY``198 199 * ``DD.MM.YYYY``200 201 * ``D.M.YYYY``202 203 When converting to strings, always 'YYYY-MM-DD' is returned.204 205 For convenience, when converting from strings also string data206 separated by space is stripped before processing. Therefore207 strings like '1990-04-01 12:12:01 GMT +1' will also work.208 """209 grok.context(IDate)210 grok.provides(ISchemaTypeConverter)211 212 #: List of supported date formats in `strftime()` notation.213 formats = ['%Y-%m-%d', '%Y/%m/%d' , '%d/%m/%Y', '%D/%M/%Y',214 '%d.%m.%Y', '%D.%M.%Y']215 216 def _convertValueFromString(self, string):217 orig_string = string218 if string is NONE_STRING_VALUE:219 string = None220 if string is None:221 return None222 value = None223 if ' ' in string:224 string = string.split(' ')[0]225 for format in self.formats:226 try:227 value = datetime.datetime.strptime(string, format)228 break229 except ValueError:230 pass231 if value is None:232 raise ValueError(233 'Cannot convert to date: %s. Use YYYY-MM-DD.' %234 orig_string)235 value = value.date()236 return value237 238 def _convertValueToString(self, value):239 return datetime.date.strftime(value, '%Y-%m-%d')240 241 242 from zope import schema243 4 from zope.component import createObject 244 from zope.interface import Interface245 5 from zope.formlib import form 246 6 from zope.formlib.form import ( 247 _widgetKey, WidgetInputError, ValidationError, InputErrors, expandPrefix, 248 setUpInputWidgets) 7 _widgetKey, WidgetInputError, ValidationError, InputErrors, expandPrefix) 249 8 from zope.formlib.interfaces import IInputWidget 9 from zope.interface import Interface 250 10 from zope.publisher.browser import TestRequest 251 11 from waeup.sirp.interfaces import IObjectConverter -
main/waeup.sirp/trunk/src/waeup/sirp/utils/converters.txt
r5328 r6276 9 9 10 10 11 .. : doctest:12 .. : layer: waeup.sirp.testing.WAeUPSIRPUnitTestLayer11 .. :NOdoctest: 12 .. :NOlayer: waeup.sirp.testing.WAeUPSIRPUnitTestLayer 13 13 14 14
Note: See TracChangeset for help on using the changeset viewer.