def define(self, *value_type, **metadata): default_value_type = -1 default_value = handler = clone = None if len(value_type) > 0: default_value = value_type[0] value_type = value_type[1:] if ((len(value_type) == 0) and (type(default_value) in SequenceTypes)): default_value, value_type = default_value[0], default_value if len(value_type) == 0: if isinstance(default_value, TraitFactory): default_value = trait_factory(default_value) if default_value in PythonTypes: handler = TraitType(default_value) default_value = DefaultValues.get(default_value) elif isinstance(default_value, CTrait): clone = default_value default_value_type, default_value = clone.default_value() metadata['type'] = clone.type elif isinstance(default_value, TraitHandler): handler = default_value default_value = None elif default_value is ThisClass: handler = ThisClass() default_value = None else: typeValue = type(default_value) if typeValue is StringType: string_options = self.extract(metadata, 'min_len', 'max_len', 'regex') if len(string_options) == 0: handler = TraitCastType(typeValue) else: handler = TraitString(**string_options) elif typeValue in TypeTypes: handler = TraitCastType(typeValue) else: handler = TraitInstance(default_value) if default_value is handler.aClass: default_value = DefaultValues.get(default_value) else: enum = [] other = [] map = {} self.do_list(value_type, enum, map, other) if (((len(enum) == 1) and (enum[0] is None)) and ((len(other) == 1) and isinstance(other[0], TraitInstance))): enum = [] other[0].allow_none() if len(enum) > 0: if (((len(map) + len(other)) == 0) and (default_value not in enum)): enum.insert(0, default_value) other.append(TraitEnum(enum)) if len(map) > 0: other.append(TraitMap(map)) if len(other) == 0: handler = TraitHandler() elif len(other) == 1: handler = other[0] if isinstance(handler, CTrait): clone, handler = handler, None metadata['type'] = clone.type elif isinstance(handler, TraitInstance): if default_value is None: handler.allow_none() elif isinstance(default_value, _InstanceArgs): default_value_type = 7 default_value = (handler.create_default_value, default_value.args, default_value.kw) elif (len(enum) == 0) and (len(map) == 0): aClass = handler.aClass typeValue = type(default_value) if typeValue is dict: default_value_type = 7 default_value = (aClass, (), default_value) elif not isinstance(default_value, aClass): if typeValue is not tuple: default_value = (default_value, ) default_value_type = 7 default_value = (aClass, default_value, None) else: for i, item in enumerate(other): if isinstance(item, CTrait): if item.type != 'trait': raise TraitError, ( "Cannot create a complex " "trait containing %s trait." % add_article(item.type)) handler = item.handler if handler is None: break other[i] = handler else: handler = TraitCompound(other) # Save the results: self.handler = handler self.clone = clone if default_value_type < 0: if isinstance(default_value, Default): default_value_type = 7 default_value = default_value.default_value else: if (handler is None) and (clone is not None): handler = clone.handler if handler is not None: default_value_type = handler.default_value_type if default_value_type >= 0: if hasattr(handler, 'default_value'): default_value = handler.default_value( default_value) else: try: default_value = handler.validate( None, '', default_value) except: pass if default_value_type < 0: default_value_type = _default_value_type(default_value) self.default_value_type = default_value_type self.default_value = default_value self.metadata = metadata.copy()
def CUnicode(value=u'', **metadata): if 'editor' not in metadata: metadata['editor'] = multi_line_text_editor return Trait(value, TraitCastType(unicode), **metadata)
def CBool(value=False, **metadata): return Trait(value, TraitCastType(bool), **metadata)
def CStr(value='', **metadata): if 'editor' not in metadata: metadata['editor'] = multi_line_text_editor return Trait(value, TraitCastType(str), **metadata)
def CComplex(value=0.0 + 0.0j, **metadata): return Trait(value, TraitCastType(complex), **metadata)
def CFloat(value=0.0, **metadata): return Trait(value, TraitCastType(float), **metadata)
def CInt(value=0, **metadata): return Trait(value, TraitCastType(int), **metadata)
Bool = TraitFactory(Bool) #--- 'Cast' traits ------------------------------------------------------------- def CInt(value=0, **metadata): return Trait(value, TraitCastType(int), **metadata) CInt = TraitFactory(CInt) def CLong(value=0L, **metadata): return Trait(value, TraitCastType(long), **metadata) CLong = TraitFactory(CLong) def CFloat(value=0.0, **metadata): return Trait(value, TraitCastType(float), **metadata) CFloat = TraitFactory(CFloat) def CComplex(value=0.0 + 0.0j, **metadata): return Trait(value, TraitCastType(complex), **metadata)