##// END OF EJS Templates
Adding the color trait type and a test
Sylvain Corlay -
Show More
@@ -17,7 +17,7 b' from nose import SkipTest'
17
17
18 from IPython.utils.traitlets import (
18 from IPython.utils.traitlets import (
19 HasTraits, MetaHasTraits, TraitType, Any, Bool, CBytes, Dict, Enum,
19 HasTraits, MetaHasTraits, TraitType, Any, Bool, CBytes, Dict, Enum,
20 Int, Long, Integer, Float, Complex, Bytes, Unicode, TraitError,
20 Int, Long, Integer, Float, Complex, Bytes, Unicode, Color, TraitError,
21 Union, Undefined, Type, This, Instance, TCPAddress, List, Tuple,
21 Union, Undefined, Type, This, Instance, TCPAddress, List, Tuple,
22 ObjectName, DottedObjectName, CRegExp, link, directional_link,
22 ObjectName, DottedObjectName, CRegExp, link, directional_link,
23 EventfulList, EventfulDict, ForwardDeclaredType, ForwardDeclaredInstance,
23 EventfulList, EventfulDict, ForwardDeclaredType, ForwardDeclaredInstance,
@@ -918,8 +918,17 b' class TestDottedObjectName(TraitTestBase):'
918 _good_values.append(u"t.ΓΎ")
918 _good_values.append(u"t.ΓΎ")
919
919
920
920
921 class TCPAddressTrait(HasTraits):
921 class ColorTrait(HasTraits):
922 value = Color("black")
923
924 class TestColor(TraitTestBase):
925 obj = ColorTrait()
926
927 _good_values = ["blue", "#AA0", "#FFFFFF"]
928 _bad_values = ["vanilla", "blues"]
922
929
930
931 class TCPAddressTrait(HasTraits):
923 value = TCPAddress()
932 value = TCPAddress()
924
933
925 class TestTCPAddress(TraitTestBase):
934 class TestTCPAddress(TraitTestBase):
@@ -979,6 +988,19 b' class TestInstanceList(TraitTestBase):'
979 _good_values = [[Foo(), Foo(), None], []]
988 _good_values = [[Foo(), Foo(), None], []]
980 _bad_values = [['1', 2,], '1', [Foo], None]
989 _bad_values = [['1', 2,], '1', [Foo], None]
981
990
991 class UnionListTrait(HasTraits):
992
993 value = List(Int() | Bool())
994
995 class TestUnionListTrait(HasTraits):
996
997 obj = UnionListTrait()
998
999 _default_value = []
1000 _good_values = [[True, 1], [False, True]]
1001 _bad_values = [[1, 'True'], False]
1002
1003
982 class LenListTrait(HasTraits):
1004 class LenListTrait(HasTraits):
983
1005
984 value = List(Int, [0], minlen=1, maxlen=2)
1006 value = List(Int, [0], minlen=1, maxlen=2)
@@ -1288,19 +1288,14 b' class CUnicode(Unicode):'
1288 self.error(obj, value)
1288 self.error(obj, value)
1289
1289
1290
1290
1291 class ObjectName(TraitType):
1291 class _CoercedString(TraitType):
1292 """A string holding a valid object name in this version of Python.
1293
1294 This does not check that the name exists in any scope."""
1295 info_text = "a valid object identifier in Python"
1296
1292
1297 if py3compat.PY3:
1293 if py3compat.PY3:
1298 # Python 3:
1294 # Python 3:
1299 coerce_str = staticmethod(lambda _,s: s)
1295 _coerce_str = staticmethod(lambda _,s: s)
1300
1301 else:
1296 else:
1302 # Python 2:
1297 # Python 2:
1303 def coerce_str(self, obj, value):
1298 def _coerce_str(self, obj, value):
1304 "In Python 2, coerce ascii-only unicode to str"
1299 "In Python 2, coerce ascii-only unicode to str"
1305 if isinstance(value, unicode):
1300 if isinstance(value, unicode):
1306 try:
1301 try:
@@ -1309,23 +1304,49 b' class ObjectName(TraitType):'
1309 self.error(obj, value)
1304 self.error(obj, value)
1310 return value
1305 return value
1311
1306
1307
1308 class ObjectName(_CoercedString):
1309 """A string holding a valid object name in this version of Python.
1310
1311 This does not check that the name exists in any scope."""
1312
1313 info_text = "a valid object identifier in Python"
1314
1312 def validate(self, obj, value):
1315 def validate(self, obj, value):
1313 value = self.coerce_str(obj, value)
1316 value = self._coerce_str(obj, value)
1314
1317
1315 if isinstance(value, string_types) and py3compat.isidentifier(value):
1318 if isinstance(value, string_types) and py3compat.isidentifier(value):
1316 return value
1319 return value
1317 self.error(obj, value)
1320 self.error(obj, value)
1318
1321
1322
1319 class DottedObjectName(ObjectName):
1323 class DottedObjectName(ObjectName):
1320 """A string holding a valid dotted object name in Python, such as A.b3._c"""
1324 """A string holding a valid dotted object name in Python, such as A.b3._c"""
1325
1321 def validate(self, obj, value):
1326 def validate(self, obj, value):
1322 value = self.coerce_str(obj, value)
1327 value = self._coerce_str(obj, value)
1323
1328
1324 if isinstance(value, string_types) and py3compat.isidentifier(value, dotted=True):
1329 if isinstance(value, string_types) and py3compat.isidentifier(value, dotted=True):
1325 return value
1330 return value
1326 self.error(obj, value)
1331 self.error(obj, value)
1327
1332
1328
1333
1334 _color_names = ['aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'beige', 'bisque', 'black', 'blanchedalmond', 'blue', 'blueviolet', 'brown', 'burlywood', 'cadetblue', 'chartreuse', 'chocolate', 'coral', 'cornflowerblue', 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan', 'darkgoldenrod', 'darkgray', 'darkgreen', 'darkkhaki', 'darkmagenta', 'darkolivegreen', 'darkorange', 'darkorchid', 'darkred', 'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray', 'darkturquoise', 'darkviolet', 'deeppink', 'deepskyblue', 'dimgray', 'dodgerblue', 'firebrick', 'floralwhite', 'forestgreen', 'fuchsia', 'gainsboro', 'ghostwhite', 'gold', 'goldenrod', 'gray', 'green', 'greenyellow', 'honeydew', 'hotpink', 'indianred ', 'indigo ', 'ivory', 'khaki', 'lavender', 'lavenderblush', 'lawngreen', 'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan', 'lightgoldenrodyellow', 'lightgray', 'lightgreen', 'lightpink', 'lightsalmon', 'lightseagreen', 'lightskyblue', 'lightslategray', 'lightsteelblue', 'lightyellow', 'lime', 'limegreen', 'linen', 'magenta', 'maroon', 'mediumaquamarine', 'mediumblue', 'mediumorchid', 'mediumpurple', 'mediumseagreen', 'mediumslateblue', 'mediumspringgreen', 'mediumturquoise', 'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose', 'moccasin', 'navajowhite', 'navy', 'oldlace', 'olive', 'olivedrab', 'orange', 'orangered', 'orchid', 'palegoldenrod', 'palegreen', 'paleturquoise', 'palevioletred', 'papayawhip', 'peachpuff', 'peru', 'pink', 'plum', 'powderblue', 'purple', 'rebeccapurple', 'red', 'rosybrown', 'royalblue', 'saddlebrown', 'salmon', 'sandybrown', 'seagreen', 'seashell', 'sienna', 'silver', 'skyblue', 'slateblue', 'slategray', 'snow', 'springgreen', 'steelblue', 'tan', 'teal', 'thistle', 'tomato', 'turquoise', 'violet', 'wheat', 'white', 'whitesmoke', 'yellow', 'yellowgreen']
1335 _color_re = re.compile(r'#[a-fA-F0-9]{3}(?:[a-fA-F0-9]{3})?$')
1336
1337
1338 class Color(_CoercedString):
1339 """A string holding a valid HTML color such as 'blue', '#060482', '#A80'"""
1340
1341 info_text = 'a valid HTML color'
1342
1343 def validate(self, obj, value):
1344 value = self._coerce_str(obj, value)
1345 if value.lower() in _color_names or _color_re.match(value):
1346 return value
1347 self.error(obj, value)
1348
1349
1329 class Bool(TraitType):
1350 class Bool(TraitType):
1330 """A boolean (True, False) trait."""
1351 """A boolean (True, False) trait."""
1331
1352
General Comments 0
You need to be logged in to leave comments. Login now