Changeset 4834 for waeup/branches/ulif-importers/src
- Timestamp:
- 17 Jan 2010, 14:13:51 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
waeup/branches/ulif-importers/src/waeup/utils/converters.py
r4814 r4834 10 10 from zope.schema.interfaces import IBool, IText, IInt, IChoice 11 11 from waeup.interfaces import ISchemaTypeConverter 12 13 # If a string has this value, it is considered as 'missing_value' or None. 14 NONE_STRING_VALUE = '' 12 15 13 16 class Converter(grok.Adapter): … … 32 35 """ 33 36 result = None 37 if string is NONE_STRING_VALUE: 38 string = None 34 39 if string is None: 35 40 if self.context.required is True: … … 54 59 55 60 class BoolConverter(Converter): 61 """A converter for zope.schema.Bool fields. 62 """ 56 63 grok.context(IBool) 57 64 grok.provides(ISchemaTypeConverter) 58 65 59 66 def _convertValueFromString(self, string): 67 if string is NONE_STRING_VALUE: 68 string = None 60 69 if string is None: 61 70 return None … … 72 81 73 82 class TextConverter(Converter): 83 """A converter for zope.schema.interfaces.IText fields. 84 """ 74 85 grok.context(IText) 75 86 grok.provides(ISchemaTypeConverter) … … 79 90 80 91 class IntConverter(Converter): 92 """A converter for zope.schema.Int fields. 93 """ 81 94 grok.context(IInt) 82 95 grok.provides(ISchemaTypeConverter) … … 86 99 87 100 class ChoiceConverter(Converter): 101 """A converter for zope.schema.Choice fields. 102 """ 88 103 grok.context(IChoice) 89 104 grok.provides(ISchemaTypeConverter) 90 105 106 tokens = None 107 values = None 108 91 109 def __init__(self, context): 92 110 self.context = context 93 try: 94 self.terms = getMultiAdapter( 95 (self.context.source, TestRequest()), ITerms) 96 except: 97 self.terms = None 111 if not hasattr(self.context.source, 'factory'): 112 try: 113 self.terms = getMultiAdapter( 114 (self.context.source, TestRequest()), ITerms) 115 except: 116 self.terms = None 117 118 return 119 if not hasattr(self.context.source.factory, 'getToken'): 120 return 121 # For expensive token/key lookups we create a 'cache' 122 # here. This speeds up mass operations with many conversions 123 # by factor 10 or more. 124 125 # Mapping token -> value 126 self.tokens = dict([(self.context.source.factory.getToken(x), x) 127 for x in self.context.source.factory.getValues()]) 128 # Mapping value -> token 129 self.values = dict([(y,x) for x,y in self.tokens.items()]) 98 130 99 131 def _convertValueFromString(self, string): 132 if self.tokens is not None: 133 return self.tokens[string] 100 134 if self.terms is not None: 101 135 return self.terms.getValue(string) … … 103 137 104 138 def _convertValueToString(self, value): 139 if self.values is not None: 140 return self.values[value] 105 141 if self.terms is not None: 106 142 return self.terms.getTerm(value).token 107 143 return str(value) 144 145 def fromString(self, string=None, strict=False): 146 """Convert ``string`` to value according to assigned field type. 147 148 We change the default for ``strict``: this disables extra 149 validation checks for Choice fields and saves lots of time. If 150 a string/value is out of allowed range we get a value or key 151 error anyway. 152 """ 153 return super(ChoiceConverter, self).fromString(string=string, 154 strict=strict) 155 156 def toString(self, value, strict=False): 157 """Convert ``value`` to string according to assigned field type. 158 159 We change the default for ``strict``: this disables extra 160 validation checks for Choice fields and saves lots of time. If 161 a string/value is out of allowed range we get a value or key 162 error anyway. 163 """ 164 return super(ChoiceConverter, self).toString(value=value, 165 strict=strict)
Note: See TracChangeset for help on using the changeset viewer.