Skip to content

Commit

Permalink
Update build scripts to pass flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
davemachado committed Jan 28, 2018
1 parent e53982f commit 9eaf189
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 57 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ notifications:
install:
- pip install -r build/requirements.txt
before_script:
# stop the build if there are Python syntax errors or undefined names
- flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
- flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- cd build
script:
- ./main.sh
Expand Down
5 changes: 3 additions & 2 deletions build/md2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def markdown_to_json(filename, anchor):
entries = []
with open(filename) as fp:
lines = (line.rstrip() for line in fp)
lines = list(line for line in lines if line \
and line.startswith(anchor) or line.startswith('| '))
lines = list(line for line in lines if line and
line.startswith(anchor) or line.startswith('| '))
for line in lines:
if line.startswith(anchor):
category = line.split(anchor)[1].strip()
Expand Down Expand Up @@ -45,5 +45,6 @@ def main():
anchor = sys.argv[2]
print(markdown_to_json(sys.argv[1], anchor))


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions build/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
flake8>=3.5.0
httplib2==0.9.2
108 changes: 57 additions & 51 deletions build/validate_format.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env python3

import json
import re
import string
import sys

anchor = '###'
Expand All @@ -24,20 +22,14 @@

def add_error(line_num, message):
"""adds an error to the dynamic error list"""
err = '(L{:03d}) {}'.format(line_num+1, message)
err = '(L{:03d}) {}'.format(line_num + 1, message)
errors.append(err)


def check_format(filename):
def check_alphabetical(lines):
"""
validates that each line is formatted correctly,
appending to error list as needed
checks if all entries per section are in alphabetical order based in entry title
"""
with open(filename) as fp:
lines = list(line.rstrip() for line in fp)

# START Alphabetical Order
category = ""
sections = {}
section_line_num = {}
for line_num, line in enumerate(lines):
Expand All @@ -54,10 +46,61 @@ def check_format(filename):
for category, entries in sections.items():
if sorted(entries) != entries:
add_error(section_line_num[category], "{} section is not in alphabetical order".format(category))
# END Alphabetical Order


def check_entry(line_num, segments):
# START Title
title = segments[index_title].upper()
if title.endswith(' API'):
add_error(line_num, 'Title should not contain "API"')
# END Title
# START Description
# first character should be capitalized
char = segments[index_desc][0]
if char.upper() != char:
add_error(line_num, "first character of description is not capitalized")
# last character should not punctuation
char = segments[index_desc][-1]
if char in punctuation:
add_error(line_num, "description should not end with {}".format(char))
# END Description
# START Auth
# values should conform to valid options only
auth = segments[index_auth].replace('`', '')
if auth not in auth_keys:
add_error(line_num, "{} is not a valid Auth option".format(auth))
# END Auth
# START HTTPS
# values should conform to valid options only
https = segments[index_https]
if https not in https_keys:
add_error(line_num, "{} is not a valid HTTPS option".format(https))
# END HTTPS
# START CORS
# values should conform to valid options only
cors = segments[index_cors]
if cors not in cors_keys:
add_error(line_num, "{} is not a valid CORS option".format(cors))
# END CORS
# START Link
# url should be wrapped in '[Go!]()' Markdown syntax
link = segments[index_link]
if not link.startswith('[Go!](http') or not link.endswith(')'):
add_error(line_num, 'link syntax should be "[Go!](LINK)"')
# END Link


def check_format(filename):
"""
validates that each line is formatted correctly,
appending to error list as needed
"""
with open(filename) as fp:
lines = list(line.rstrip() for line in fp)
check_alphabetical(lines)
# START Check Entries
num_in_category = min_entries_per_section + 1
category = ""
category_line = 0
anchor_re = re.compile('###\s\S+')
for line_num, line in enumerate(lines):
Expand All @@ -83,47 +126,10 @@ def check_format(filename):
add_error(line_num, "each segment must start and end with exactly 1 space")
# END Global
segments = [seg.strip() for seg in segments]
# START Title
title = segments[index_title].upper()
if title.endswith(' API'):
add_error(line_num, 'Title should not contain "API"')
# END Title
# START Description
# first character should be capitalized
char = segments[index_desc][0]
if char.upper() != char:
add_error(line_num, "first character of description is not capitalized")
# last character should not punctuation
char = segments[index_desc][-1]
if char in punctuation:
add_error(line_num, "description should not end with {}".format(char))
# END Description
# START Auth
# values should conform to valid options only
auth = segments[index_auth].replace('`', '')
if auth not in auth_keys:
add_error(line_num, "{} is not a valid Auth option".format(auth))
# END Auth
# START HTTPS
# values should conform to valid options only
https = segments[index_https]
if https not in https_keys:
add_error(line_num, "{} is not a valid HTTPS option".format(https))
# END HTTPS
# START CORS
# values should conform to valid options only
cors = segments[index_cors]
if cors not in cors_keys:
add_error(line_num, "{} is not a valid CORS option".format(cors))
# END CORS
# START Link
# url should be wrapped in '[Go!]()' Markdown syntax
link = segments[index_link]
if not link.startswith('[Go!](http') or not link.endswith(')'):
add_error(line_num, 'link syntax should be "[Go!](LINK)"')
# END Link
check_entry(line_num, segments)
# END Check Entries


def main():
num_args = len(sys.argv)
if num_args < 2:
Expand Down
9 changes: 5 additions & 4 deletions build/validate_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ def parse_links(filename):
"""Returns a list of URLs from text file"""
with open(filename) as fp:
data = fp.read()
raw_links = re.findall( \
'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', \
data)
raw_links = re.findall(
'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',
data)
links = [raw_link.replace(')', '') for raw_link in raw_links]
return links


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


if __name__ == "__main__":
num_args = len(sys.argv)
if num_args < 2:
Expand All @@ -44,4 +46,3 @@ def validate_links(links):
for err in errors:
print(err)
sys.exit(1)

0 comments on commit 9eaf189

Please sign in to comment.