Skip to content

Commit 69d1dab

Browse files
committed
Don't fall back if webcolors isn't present
1 parent cd08b02 commit 69d1dab

File tree

3 files changed

+59
-112
lines changed

3 files changed

+59
-112
lines changed

docs/validate.rst

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,6 @@ with.
232232

233233
.. autofunction:: is_regex
234234

235-
.. autofunction:: is_css21_color
236-
237-
.. autofunction:: is_css3_color
238-
239235
.. autofunction:: is_uri
240236

241237
.. autofunction:: is_email
@@ -245,3 +241,30 @@ with.
245241
.. autofunction:: is_ipv6
246242

247243
.. autofunction:: is_host_name
244+
245+
Additionally, if the webcolors_ library is present, some checkers related to
246+
CSS will be enabled:
247+
248+
.. function:: is_css21_color
249+
250+
Check if the instance is a valid CSS 2.1 color name or code.
251+
252+
>>> is_css21_color("fuchsia")
253+
True
254+
>>> is_css21_color("pink")
255+
False
256+
>>> is_css_color_code("#CC8899")
257+
True
258+
259+
.. function:: is_css3_color
260+
261+
Check if the instance is a valid CSS 3 color name or code.
262+
263+
>>> is_css3_color("pink")
264+
True
265+
>>> is_css3_color("puce")
266+
False
267+
>>> is_css_color_code("#CC8899")
268+
True
269+
270+
.. _webcolors: http://pypi.python.org/pypi/webcolors/

jsonschema.py

Lines changed: 31 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
import socket
2222
import sys
2323

24+
try:
25+
import webcolors
26+
except ImportError:
27+
webcolors = None
28+
2429

2530
__version__ = "1.0.0-dev"
2631

@@ -561,7 +566,7 @@ def conforms(self, instance, format):
561566
@FormatChecker.cls_checks("date-time")
562567
def is_date_time(instance):
563568
"""
564-
Check whether the instance is in ISO 8601 "YYYY-MM-DDThh:mm:ssZ" format.
569+
Check whether the instance is in ISO 8601 ``YYYY-MM-DDThh:mm:ssZ`` format.
565570
566571
:argument str instance: the instance to check
567572
:rtype: bool
@@ -585,7 +590,7 @@ def is_date_time(instance):
585590
@FormatChecker.cls_checks("date")
586591
def is_date(instance):
587592
"""
588-
Check whether the instance matches a date in "YYYY-MM-DD" format.
593+
Check whether the instance matches a date in ``YYYY-MM-DD`` format.
589594
590595
:argument str instance: the instance to check
591596
:rtype: bool
@@ -609,7 +614,7 @@ def is_date(instance):
609614
@FormatChecker.cls_checks("time")
610615
def is_time(instance):
611616
"""
612-
Check whether the instance matches a time in "hh:mm:ss" format.
617+
Check whether the instance matches a time in ``hh:mm:ss`` format.
613618
614619
:argument str instance: the instance to check
615620
:rtype: bool
@@ -776,111 +781,6 @@ def is_host_name(instance):
776781
return True
777782

778783

779-
def is_css_color_code(instance):
780-
"""
781-
Check if the instance is a valid CSS color code.
782-
783-
>>> is_css_color_code("#CC8899")
784-
True
785-
>>> is_css_color_code("#C89")
786-
True
787-
>>> is_css_color_code("#00332520")
788-
False
789-
790-
"""
791-
792-
pattern = r"^#([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6})$"
793-
return bool(re.match(pattern, instance))
794-
795-
796-
@FormatChecker.cls_checks("color")
797-
def is_css21_color(instance):
798-
"""
799-
Check for valid CSS 2.1 color names and well-formed CSS color codes.
800-
801-
Optionally uses the webcolors_ library.
802-
803-
>>> is_css21_color("fuchsia")
804-
True
805-
>>> is_css21_color("pink")
806-
False
807-
>>> is_css_color_code("#CC8899")
808-
True
809-
810-
.. _webcolors: http://pypi.python.org/pypi/webcolors/
811-
812-
"""
813-
814-
try:
815-
from webcolors import css21_names_to_hex as css21_colors
816-
except ImportError:
817-
css21_colors = (
818-
"aqua", "black", "blue", "fuchsia", "green", "grey", "lime",
819-
"maroon", "navy", "olive", "orange", "purple", "red", "silver",
820-
"teal", "white", "yellow")
821-
822-
if instance.lower() in css21_colors:
823-
return True
824-
return is_css_color_code(instance)
825-
826-
827-
def is_css3_color(instance):
828-
"""
829-
Check for valid CSS 3 color names and well-formed CSS color codes.
830-
831-
Optionally uses the webcolors_ library.
832-
833-
>>> is_css3_color("pink")
834-
True
835-
>>> is_css3_color("puce")
836-
False
837-
>>> is_css_color_code("#CC8899")
838-
True
839-
840-
.. _webcolors: http://pypi.python.org/pypi/webcolors/
841-
842-
"""
843-
844-
try:
845-
from webcolors import css3_names_to_hex as css3_colors
846-
except ImportError:
847-
css3_colors = (
848-
"aliceblue", "antiquewhite", "aqua", "aquamarine", "azure", "beige",
849-
"bisque", "black", "blanchedalmond", "blue", "blueviolet", "brown",
850-
"burlywood", "cadetblue", "chartreuse", "chocolate", "coral",
851-
"cornflowerblue", "cornsilk", "crimson", "cyan", "darkblue",
852-
"darkcyan", "darkgoldenrod", "darkgray", "darkgrey", "darkgreen",
853-
"darkkhaki", "darkmagenta", "darkolivegreen", "darkorange",
854-
"darkorchid", "darkred", "darksalmon", "darkseagreen",
855-
"darkslateblue", "darkslategray", "darkslategrey", "darkturquoise",
856-
"darkviolet", "deeppink", "deepskyblue", "dimgray", "dimgrey",
857-
"dodgerblue", "firebrick", "floralwhite", "forestgreen", "fuchsia",
858-
"gainsboro", "ghostwhite", "gold", "goldenrod", "gray", "grey",
859-
"green", "greenyellow", "honeydew", "hotpink", "indianred",
860-
"indigo", "ivory", "khaki", "lavender", "lavenderblush",
861-
"lawngreen", "lemonchiffon", "lightblue", "lightcoral",
862-
"lightcyan", "lightgoldenrodyellow", "lightgray", "lightgrey",
863-
"lightgreen", "lightpink", "lightsalmon", "lightseagreen",
864-
"lightskyblue", "lightslategray", "lightslategrey",
865-
"lightsteelblue", "lightyellow", "lime", "limegreen", "linen",
866-
"magenta", "maroon", "mediumaquamarine", "mediumblue",
867-
"mediumorchid", "mediumpurple", "mediumseagreen",
868-
"mediumslateblue", "mediumspringgreen", "mediumturquoise",
869-
"mediumvioletred", "midnightblue", "mintcream", "mistyrose",
870-
"moccasin", "navajowhite", "navy", "oldlace", "olive", "olivedrab",
871-
"orange", "orangered", "orchid", "palegoldenrod", "palegreen",
872-
"paleturquoise", "palevioletred", "papayawhip", "peachpuff",
873-
"peru", "pink", "plum", "powderblue", "purple", "red", "rosybrown",
874-
"royalblue", "saddlebrown", "salmon", "sandybrown", "seagreen",
875-
"seashell", "sienna", "silver", "skyblue", "slateblue",
876-
"slategray", "slategrey", "snow", "springgreen", "steelblue",
877-
"tan", "teal", "thistle", "tomato", "turquoise", "violet", "wheat",
878-
"white", "whitesmoke", "yellow", "yellowgreen")
879-
if instance.lower() in css3_colors:
880-
return True
881-
return is_css_color_code(instance)
882-
883-
884784
@FormatChecker.cls_checks("regex")
885785
def is_regex(instance):
886786
"""
@@ -895,13 +795,36 @@ def is_regex(instance):
895795
False
896796
897797
"""
798+
898799
try:
899800
re.compile(instance)
900801
return True
901802
except re.error:
902803
return False
903804

904805

806+
if webcolors is not None:
807+
def is_css_color_code(instance):
808+
try:
809+
webcolors.normalize_hex(instance)
810+
except (ValueError, TypeError):
811+
return False
812+
return True
813+
814+
815+
@FormatChecker.cls_checks("color")
816+
def is_css21_color(instance):
817+
if instance.lower() in webcolors.css21_names_to_hex:
818+
return True
819+
return is_css_color_code(instance)
820+
821+
822+
def is_css3_color(instance):
823+
if instance.lower() in webcolors.css3_names_to_hex:
824+
return True
825+
return is_css_color_code(instance)
826+
827+
905828
class RefResolver(object):
906829
"""
907830
Resolve JSON References.

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ deps =
1111
mock
1212
nose
1313
sphinx
14+
webcolors
1415

1516
[testenv:docs]
1617
basepython = python

0 commit comments

Comments
 (0)