##// END OF EJS Templates
inherit from Unicode
Sylvain Corlay -
Show More
@@ -17,13 +17,12 b" _color_names = ['aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'bei"
17 _color_re = re.compile(r'#[a-fA-F0-9]{3}(?:[a-fA-F0-9]{3})?$')
17 _color_re = re.compile(r'#[a-fA-F0-9]{3}(?:[a-fA-F0-9]{3})?$')
18
18
19
19
20 class Color(traitlets._CoercedString):
20 class Color(traitlets.Unicode):
21 """A string holding a valid HTML color such as 'blue', '#060482', '#A80'"""
21 """A string holding a valid HTML color such as 'blue', '#060482', '#A80'"""
22
22
23 info_text = 'a valid HTML color'
23 info_text = 'a valid HTML color'
24
24
25 def validate(self, obj, value):
25 def validate(self, obj, value):
26 value = self._coerce_str(obj, value)
27 if value.lower() in _color_names or _color_re.match(value):
26 if value.lower() in _color_names or _color_re.match(value):
28 return value
27 return value
29 self.error(obj, value) No newline at end of file
28 self.error(obj, value)
@@ -1277,14 +1277,19 b' class CUnicode(Unicode):'
1277 self.error(obj, value)
1277 self.error(obj, value)
1278
1278
1279
1279
1280 class _CoercedString(TraitType):
1280 class ObjectName(TraitType):
1281 """A string holding a valid object name in this version of Python.
1282
1283 This does not check that the name exists in any scope."""
1284 info_text = "a valid object identifier in Python"
1281
1285
1282 if py3compat.PY3:
1286 if py3compat.PY3:
1283 # Python 3:
1287 # Python 3:
1284 _coerce_str = staticmethod(lambda _,s: s)
1288 coerce_str = staticmethod(lambda _,s: s)
1289
1285 else:
1290 else:
1286 # Python 2:
1291 # Python 2:
1287 def _coerce_str(self, obj, value):
1292 def coerce_str(self, obj, value):
1288 "In Python 2, coerce ascii-only unicode to str"
1293 "In Python 2, coerce ascii-only unicode to str"
1289 if isinstance(value, unicode):
1294 if isinstance(value, unicode):
1290 try:
1295 try:
@@ -1293,27 +1298,17 b' class _CoercedString(TraitType):'
1293 self.error(obj, value)
1298 self.error(obj, value)
1294 return value
1299 return value
1295
1300
1296
1297 class ObjectName(_CoercedString):
1298 """A string holding a valid object name in this version of Python.
1299
1300 This does not check that the name exists in any scope."""
1301
1302 info_text = "a valid object identifier in Python"
1303
1304 def validate(self, obj, value):
1301 def validate(self, obj, value):
1305 value = self._coerce_str(obj, value)
1302 value = self.coerce_str(obj, value)
1306
1303
1307 if isinstance(value, string_types) and py3compat.isidentifier(value):
1304 if isinstance(value, string_types) and py3compat.isidentifier(value):
1308 return value
1305 return value
1309 self.error(obj, value)
1306 self.error(obj, value)
1310
1307
1311
1312 class DottedObjectName(ObjectName):
1308 class DottedObjectName(ObjectName):
1313 """A string holding a valid dotted object name in Python, such as A.b3._c"""
1309 """A string holding a valid dotted object name in Python, such as A.b3._c"""
1314
1315 def validate(self, obj, value):
1310 def validate(self, obj, value):
1316 value = self._coerce_str(obj, value)
1311 value = self.coerce_str(obj, value)
1317
1312
1318 if isinstance(value, string_types) and py3compat.isidentifier(value, dotted=True):
1313 if isinstance(value, string_types) and py3compat.isidentifier(value, dotted=True):
1319 return value
1314 return value
General Comments 0
You need to be logged in to leave comments. Login now