11#!/usr/bin/env python3
22
3- import json
43import re
5- import string
64import sys
75
86anchor = '###'
2422
2523def add_error (line_num , message ):
2624 """adds an error to the dynamic error list"""
27- err = '(L{:03d}) {}' .format (line_num + 1 , message )
25+ err = '(L{:03d}) {}' .format (line_num + 1 , message )
2826 errors .append (err )
2927
3028
31- def check_format ( filename ):
29+ def check_alphabetical ( lines ):
3230 """
33- validates that each line is formatted correctly,
34- appending to error list as needed
31+ checks if all entries per section are in alphabetical order based in entry title
3532 """
36- with open (filename ) as fp :
37- lines = list (line .rstrip () for line in fp )
38-
39- # START Alphabetical Order
40- category = ""
4133 sections = {}
4234 section_line_num = {}
4335 for line_num , line in enumerate (lines ):
@@ -54,10 +46,61 @@ def check_format(filename):
5446 for category , entries in sections .items ():
5547 if sorted (entries ) != entries :
5648 add_error (section_line_num [category ], "{} section is not in alphabetical order" .format (category ))
57- # END Alphabetical Order
5849
50+
51+ def check_entry (line_num , segments ):
52+ # START Title
53+ title = segments [index_title ].upper ()
54+ if title .endswith (' API' ):
55+ add_error (line_num , 'Title should not contain "API"' )
56+ # END Title
57+ # START Description
58+ # first character should be capitalized
59+ char = segments [index_desc ][0 ]
60+ if char .upper () != char :
61+ add_error (line_num , "first character of description is not capitalized" )
62+ # last character should not punctuation
63+ char = segments [index_desc ][- 1 ]
64+ if char in punctuation :
65+ add_error (line_num , "description should not end with {}" .format (char ))
66+ # END Description
67+ # START Auth
68+ # values should conform to valid options only
69+ auth = segments [index_auth ].replace ('`' , '' )
70+ if auth not in auth_keys :
71+ add_error (line_num , "{} is not a valid Auth option" .format (auth ))
72+ # END Auth
73+ # START HTTPS
74+ # values should conform to valid options only
75+ https = segments [index_https ]
76+ if https not in https_keys :
77+ add_error (line_num , "{} is not a valid HTTPS option" .format (https ))
78+ # END HTTPS
79+ # START CORS
80+ # values should conform to valid options only
81+ cors = segments [index_cors ]
82+ if cors not in cors_keys :
83+ add_error (line_num , "{} is not a valid CORS option" .format (cors ))
84+ # END CORS
85+ # START Link
86+ # url should be wrapped in '[Go!]()' Markdown syntax
87+ link = segments [index_link ]
88+ if not link .startswith ('[Go!](http' ) or not link .endswith (')' ):
89+ add_error (line_num , 'link syntax should be "[Go!](LINK)"' )
90+ # END Link
91+
92+
93+ def check_format (filename ):
94+ """
95+ validates that each line is formatted correctly,
96+ appending to error list as needed
97+ """
98+ with open (filename ) as fp :
99+ lines = list (line .rstrip () for line in fp )
100+ check_alphabetical (lines )
59101 # START Check Entries
60102 num_in_category = min_entries_per_section + 1
103+ category = ""
61104 category_line = 0
62105 anchor_re = re .compile ('###\s\S+' )
63106 for line_num , line in enumerate (lines ):
@@ -83,47 +126,10 @@ def check_format(filename):
83126 add_error (line_num , "each segment must start and end with exactly 1 space" )
84127 # END Global
85128 segments = [seg .strip () for seg in segments ]
86- # START Title
87- title = segments [index_title ].upper ()
88- if title .endswith (' API' ):
89- add_error (line_num , 'Title should not contain "API"' )
90- # END Title
91- # START Description
92- # first character should be capitalized
93- char = segments [index_desc ][0 ]
94- if char .upper () != char :
95- add_error (line_num , "first character of description is not capitalized" )
96- # last character should not punctuation
97- char = segments [index_desc ][- 1 ]
98- if char in punctuation :
99- add_error (line_num , "description should not end with {}" .format (char ))
100- # END Description
101- # START Auth
102- # values should conform to valid options only
103- auth = segments [index_auth ].replace ('`' , '' )
104- if auth not in auth_keys :
105- add_error (line_num , "{} is not a valid Auth option" .format (auth ))
106- # END Auth
107- # START HTTPS
108- # values should conform to valid options only
109- https = segments [index_https ]
110- if https not in https_keys :
111- add_error (line_num , "{} is not a valid HTTPS option" .format (https ))
112- # END HTTPS
113- # START CORS
114- # values should conform to valid options only
115- cors = segments [index_cors ]
116- if cors not in cors_keys :
117- add_error (line_num , "{} is not a valid CORS option" .format (cors ))
118- # END CORS
119- # START Link
120- # url should be wrapped in '[Go!]()' Markdown syntax
121- link = segments [index_link ]
122- if not link .startswith ('[Go!](http' ) or not link .endswith (')' ):
123- add_error (line_num , 'link syntax should be "[Go!](LINK)"' )
124- # END Link
129+ check_entry (line_num , segments )
125130 # END Check Entries
126131
132+
127133def main ():
128134 num_args = len (sys .argv )
129135 if num_args < 2 :
0 commit comments