Commit 9eaf1895 authored by davemachado's avatar davemachado
Browse files

Update build scripts to pass flake8

parent e53982f9
...@@ -5,6 +5,10 @@ notifications: ...@@ -5,6 +5,10 @@ notifications:
install: install:
- pip install -r build/requirements.txt - pip install -r build/requirements.txt
before_script: 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 - cd build
script: script:
- ./main.sh - ./main.sh
......
...@@ -10,8 +10,8 @@ def markdown_to_json(filename, anchor): ...@@ -10,8 +10,8 @@ def markdown_to_json(filename, anchor):
entries = [] entries = []
with open(filename) as fp: with open(filename) as fp:
lines = (line.rstrip() for line in fp) lines = (line.rstrip() for line in fp)
lines = list(line for line in lines if line \ lines = list(line for line in lines if line and
and line.startswith(anchor) or line.startswith('| ')) line.startswith(anchor) or line.startswith('| '))
for line in lines: for line in lines:
if line.startswith(anchor): if line.startswith(anchor):
category = line.split(anchor)[1].strip() category = line.split(anchor)[1].strip()
...@@ -45,5 +45,6 @@ def main(): ...@@ -45,5 +45,6 @@ def main():
anchor = sys.argv[2] anchor = sys.argv[2]
print(markdown_to_json(sys.argv[1], anchor)) print(markdown_to_json(sys.argv[1], anchor))
if __name__ == "__main__": if __name__ == "__main__":
main() main()
flake8>=3.5.0
httplib2==0.9.2 httplib2==0.9.2
#!/usr/bin/env python3 #!/usr/bin/env python3
import json
import re import re
import string
import sys import sys
anchor = '###' anchor = '###'
...@@ -24,20 +22,14 @@ errors = [] ...@@ -24,20 +22,14 @@ errors = []
def add_error(line_num, message): def add_error(line_num, message):
"""adds an error to the dynamic error list""" """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) errors.append(err)
def check_format(filename): def check_alphabetical(lines):
""" """
validates that each line is formatted correctly, checks if all entries per section are in alphabetical order based in entry title
appending to error list as needed
""" """
with open(filename) as fp:
lines = list(line.rstrip() for line in fp)
# START Alphabetical Order
category = ""
sections = {} sections = {}
section_line_num = {} section_line_num = {}
for line_num, line in enumerate(lines): for line_num, line in enumerate(lines):
...@@ -54,35 +46,9 @@ def check_format(filename): ...@@ -54,35 +46,9 @@ def check_format(filename):
for category, entries in sections.items(): for category, entries in sections.items():
if sorted(entries) != entries: if sorted(entries) != entries:
add_error(section_line_num[category], "{} section is not in alphabetical order".format(category)) add_error(section_line_num[category], "{} section is not in alphabetical order".format(category))
# END Alphabetical Order
# START Check Entries
num_in_category = min_entries_per_section + 1 def check_entry(line_num, segments):
category_line = 0
anchor_re = re.compile('###\s\S+')
for line_num, line in enumerate(lines):
# check each section for the minimum number of entries
if line.startswith(anchor):
if not anchor_re.match(line):
add_error(line_num, "section header is not formatted correctly")
if num_in_category < min_entries_per_section:
add_error(category_line, "{} section does not have the minimum {} entries (only has {})".format(
category, min_entries_per_section, num_in_category))
category = line.split(' ')[1]
category_line = line_num
num_in_category = 0
continue
if not line.startswith('|') or line.startswith('|---'):
continue
num_in_category += 1
segments = line.split('|')[1:-1]
# START Global
for segment in segments:
# every line segment should start and end with exactly 1 space
if len(segment) - len(segment.lstrip()) != 1 or len(segment) - len(segment.rstrip()) != 1:
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 # START Title
title = segments[index_title].upper() title = segments[index_title].upper()
if title.endswith(' API'): if title.endswith(' API'):
...@@ -122,8 +88,48 @@ def check_format(filename): ...@@ -122,8 +88,48 @@ def check_format(filename):
if not link.startswith('[Go!](http') or not link.endswith(')'): if not link.startswith('[Go!](http') or not link.endswith(')'):
add_error(line_num, 'link syntax should be "[Go!](LINK)"') add_error(line_num, 'link syntax should be "[Go!](LINK)"')
# END 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):
# check each section for the minimum number of entries
if line.startswith(anchor):
if not anchor_re.match(line):
add_error(line_num, "section header is not formatted correctly")
if num_in_category < min_entries_per_section:
add_error(category_line, "{} section does not have the minimum {} entries (only has {})".format(
category, min_entries_per_section, num_in_category))
category = line.split(' ')[1]
category_line = line_num
num_in_category = 0
continue
if not line.startswith('|') or line.startswith('|---'):
continue
num_in_category += 1
segments = line.split('|')[1:-1]
# START Global
for segment in segments:
# every line segment should start and end with exactly 1 space
if len(segment) - len(segment.lstrip()) != 1 or len(segment) - len(segment.rstrip()) != 1:
add_error(line_num, "each segment must start and end with exactly 1 space")
# END Global
segments = [seg.strip() for seg in segments]
check_entry(line_num, segments)
# END Check Entries # END Check Entries
def main(): def main():
num_args = len(sys.argv) num_args = len(sys.argv)
if num_args < 2: if num_args < 2:
......
...@@ -10,12 +10,13 @@ def parse_links(filename): ...@@ -10,12 +10,13 @@ def parse_links(filename):
"""Returns a list of URLs from text file""" """Returns a list of URLs from text file"""
with open(filename) as fp: with open(filename) as fp:
data = fp.read() data = fp.read()
raw_links = re.findall( \ raw_links = re.findall(
'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', \ 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',
data) data)
links = [raw_link.replace(')', '') for raw_link in raw_links] links = [raw_link.replace(')', '') for raw_link in raw_links]
return links return links
def validate_links(links): def validate_links(links):
"""Checks each entry in JSON file for live link""" """Checks each entry in JSON file for live link"""
print('Validating {} links...'.format(len(links))) print('Validating {} links...'.format(len(links)))
...@@ -34,6 +35,7 @@ def validate_links(links): ...@@ -34,6 +35,7 @@ def validate_links(links):
errors.append("SOC: {} : {}".format(socketerror, link)) errors.append("SOC: {} : {}".format(socketerror, link))
return errors return errors
if __name__ == "__main__": if __name__ == "__main__":
num_args = len(sys.argv) num_args = len(sys.argv)
if num_args < 2: if num_args < 2:
...@@ -44,4 +46,3 @@ if __name__ == "__main__": ...@@ -44,4 +46,3 @@ if __name__ == "__main__":
for err in errors: for err in errors:
print(err) print(err)
sys.exit(1) sys.exit(1)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment