21
21
import socket
22
22
import sys
23
23
24
+ try :
25
+ import webcolors
26
+ except ImportError :
27
+ webcolors = None
28
+
24
29
25
30
__version__ = "1.0.0-dev"
26
31
@@ -561,7 +566,7 @@ def conforms(self, instance, format):
561
566
@FormatChecker .cls_checks ("date-time" )
562
567
def is_date_time (instance ):
563
568
"""
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.
565
570
566
571
:argument str instance: the instance to check
567
572
:rtype: bool
@@ -585,7 +590,7 @@ def is_date_time(instance):
585
590
@FormatChecker .cls_checks ("date" )
586
591
def is_date (instance ):
587
592
"""
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.
589
594
590
595
:argument str instance: the instance to check
591
596
:rtype: bool
@@ -609,7 +614,7 @@ def is_date(instance):
609
614
@FormatChecker .cls_checks ("time" )
610
615
def is_time (instance ):
611
616
"""
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.
613
618
614
619
:argument str instance: the instance to check
615
620
:rtype: bool
@@ -776,111 +781,6 @@ def is_host_name(instance):
776
781
return True
777
782
778
783
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
-
884
784
@FormatChecker .cls_checks ("regex" )
885
785
def is_regex (instance ):
886
786
"""
@@ -895,13 +795,36 @@ def is_regex(instance):
895
795
False
896
796
897
797
"""
798
+
898
799
try :
899
800
re .compile (instance )
900
801
return True
901
802
except re .error :
902
803
return False
903
804
904
805
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
+
905
828
class RefResolver (object ):
906
829
"""
907
830
Resolve JSON References.
0 commit comments