Skip to content

Commit 9eaf189

Browse files
committed
Update build scripts to pass flake8
1 parent e53982f commit 9eaf189

5 files changed

Lines changed: 70 additions & 57 deletions

File tree

โ€Ž.travis.ymlโ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ notifications:
55
install:
66
- pip install -r build/requirements.txt
77
before_script:
8+
# stop the build if there are Python syntax errors or undefined names
9+
- flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics
10+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
11+
- flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
812
- cd build
913
script:
1014
- ./main.sh

โ€Žbuild/md2json.pyโ€Ž

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ def markdown_to_json(filename, anchor):
1010
entries = []
1111
with open(filename) as fp:
1212
lines = (line.rstrip() for line in fp)
13-
lines = list(line for line in lines if line \
14-
and line.startswith(anchor) or line.startswith('| '))
13+
lines = list(line for line in lines if line and
14+
line.startswith(anchor) or line.startswith('| '))
1515
for line in lines:
1616
if line.startswith(anchor):
1717
category = line.split(anchor)[1].strip()
@@ -45,5 +45,6 @@ def main():
4545
anchor = sys.argv[2]
4646
print(markdown_to_json(sys.argv[1], anchor))
4747

48+
4849
if __name__ == "__main__":
4950
main()

โ€Žbuild/requirements.txtโ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
flake8>=3.5.0
12
httplib2==0.9.2

โ€Žbuild/validate_format.pyโ€Ž

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#!/usr/bin/env python3
22

3-
import json
43
import re
5-
import string
64
import sys
75

86
anchor = '###'
@@ -24,20 +22,14 @@
2422

2523
def 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+
127133
def main():
128134
num_args = len(sys.argv)
129135
if num_args < 2:

โ€Žbuild/validate_links.pyโ€Ž

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ def parse_links(filename):
1010
"""Returns a list of URLs from text file"""
1111
with open(filename) as fp:
1212
data = fp.read()
13-
raw_links = re.findall( \
14-
'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', \
15-
data)
13+
raw_links = re.findall(
14+
'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',
15+
data)
1616
links = [raw_link.replace(')', '') for raw_link in raw_links]
1717
return links
1818

19+
1920
def validate_links(links):
2021
"""Checks each entry in JSON file for live link"""
2122
print('Validating {} links...'.format(len(links)))
@@ -34,6 +35,7 @@ def validate_links(links):
3435
errors.append("SOC: {} : {}".format(socketerror, link))
3536
return errors
3637

38+
3739
if __name__ == "__main__":
3840
num_args = len(sys.argv)
3941
if num_args < 2:
@@ -44,4 +46,3 @@ def validate_links(links):
4446
for err in errors:
4547
print(err)
4648
sys.exit(1)
47-

0 commit comments

Comments
ย (0)