From 19768ba7a36d8bb9df13fbf6b34af268bd17f2c1 Mon Sep 17 00:00:00 2001 From: Sylvain Corlay Date: Sat, 21 Mar 2015 01:05:01 -0400 Subject: [PATCH] Move color trait type to the widget package --- IPython/html/widgets/__init__.py | 2 ++ IPython/html/widgets/tests/test_traits.py | 20 ++++++++++++++++ IPython/html/widgets/trait_types.py | 29 +++++++++++++++++++++++ IPython/html/widgets/widget.py | 3 ++- IPython/html/widgets/widget_float.py | 3 ++- IPython/html/widgets/widget_int.py | 3 ++- 6 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 IPython/html/widgets/tests/test_traits.py create mode 100644 IPython/html/widgets/trait_types.py diff --git a/IPython/html/widgets/__init__.py b/IPython/html/widgets/__init__.py index 8b1944913..bd8d2378f 100644 --- a/IPython/html/widgets/__init__.py +++ b/IPython/html/widgets/__init__.py @@ -1,5 +1,7 @@ from .widget import Widget, DOMWidget, CallbackDispatcher, register +from .trait_types import Color + from .widget_bool import Checkbox, ToggleButton from .widget_button import Button from .widget_box import Box, FlexBox, HBox, VBox diff --git a/IPython/html/widgets/tests/test_traits.py b/IPython/html/widgets/tests/test_traits.py new file mode 100644 index 000000000..c25960a34 --- /dev/null +++ b/IPython/html/widgets/tests/test_traits.py @@ -0,0 +1,20 @@ +"""Test trait types of the widget packages.""" + +# Copyright (c) IPython Development Team. +# Distributed under the terms of the Modified BSD License. + +from unittest import TestCase +from IPython.utils.traitlets import HasTraits +from IPython.utils.tests.test_traitlets import TraitTestBase +from IPython.html.widgets import Color + + +class ColorTrait(HasTraits): + value = Color("black") + + +class TestColor(TraitTestBase): + obj = ColorTrait() + + _good_values = ["blue", "#AA0", "#FFFFFF"] + _bad_values = ["vanilla", "blues"] diff --git a/IPython/html/widgets/trait_types.py b/IPython/html/widgets/trait_types.py new file mode 100644 index 000000000..cef80db9d --- /dev/null +++ b/IPython/html/widgets/trait_types.py @@ -0,0 +1,29 @@ +# encoding: utf-8 +""" +Trait types for html widgets. +""" + +# Copyright (c) IPython Development Team. +# Distributed under the terms of the Modified BSD License. + +import re +from IPython.utils import traitlets + +#----------------------------------------------------------------------------- +# Utilities +#----------------------------------------------------------------------------- + +_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'] +_color_re = re.compile(r'#[a-fA-F0-9]{3}(?:[a-fA-F0-9]{3})?$') + + +class Color(traitlets._CoercedString): + """A string holding a valid HTML color such as 'blue', '#060482', '#A80'""" + + info_text = 'a valid HTML color' + + def validate(self, obj, value): + value = self._coerce_str(obj, value) + if value.lower() in _color_names or _color_re.match(value): + return value + self.error(obj, value) \ No newline at end of file diff --git a/IPython/html/widgets/widget.py b/IPython/html/widgets/widget.py index 694f592e4..456133d55 100644 --- a/IPython/html/widgets/widget.py +++ b/IPython/html/widgets/widget.py @@ -20,8 +20,9 @@ from IPython.kernel.comm import Comm from IPython.config import LoggingConfigurable from IPython.utils.importstring import import_item from IPython.utils.traitlets import Unicode, Dict, Instance, Bool, List, \ - CaselessStrEnum, Tuple, CUnicode, Int, Set, Color + CaselessStrEnum, Tuple, CUnicode, Int, Set from IPython.utils.py3compat import string_types +from .trait_types import Color #----------------------------------------------------------------------------- # Classes diff --git a/IPython/html/widgets/widget_float.py b/IPython/html/widgets/widget_float.py index 5a0a1909a..afc79365b 100644 --- a/IPython/html/widgets/widget_float.py +++ b/IPython/html/widgets/widget_float.py @@ -14,7 +14,8 @@ Represents an unbounded float using a widget. # Imports #----------------------------------------------------------------------------- from .widget import DOMWidget, register -from IPython.utils.traitlets import Unicode, CFloat, Bool, CaselessStrEnum, Tuple, Color +from .trait_types import Color +from IPython.utils.traitlets import Unicode, CFloat, Bool, CaselessStrEnum, Tuple from IPython.utils.warn import DeprecatedClass #----------------------------------------------------------------------------- diff --git a/IPython/html/widgets/widget_int.py b/IPython/html/widgets/widget_int.py index f1d141178..e006054d7 100644 --- a/IPython/html/widgets/widget_int.py +++ b/IPython/html/widgets/widget_int.py @@ -14,7 +14,8 @@ Represents an unbounded int using a widget. # Imports #----------------------------------------------------------------------------- from .widget import DOMWidget, register -from IPython.utils.traitlets import Unicode, CInt, Bool, CaselessStrEnum, Tuple, Color +from .trait_types import Color +from IPython.utils.traitlets import Unicode, CInt, Bool, CaselessStrEnum, Tuple from IPython.utils.warn import DeprecatedClass #-----------------------------------------------------------------------------