You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.7 KiB

  1. #!/usr/bin/env python3
  2. import json
  3. import re
  4. import string
  5. import sys
  6. anchor = '###'
  7. min_entries_per_section = 3
  8. auth_keys = ['apiKey', 'OAuth', 'X-Mashape-Key', 'No']
  9. punctuation = ['.', '?', '!']
  10. https_keys = ['Yes', 'No']
  11. cors_keys = ['Yes', 'No', 'Unknown']
  12. index_title = 0
  13. index_desc = 1
  14. index_auth = 2
  15. index_https = 3
  16. index_cors = 4
  17. index_link = 5
  18. errors = []
  19. def add_error(line_num, message):
  20. """adds an error to the dynamic error list"""
  21. err = '(L{:03d}) {}'.format(line_num+1, message)
  22. errors.append(err)
  23. def check_format(filename):
  24. """
  25. validates that each line is formatted correctly,
  26. appending to error list as needed
  27. """
  28. with open(filename) as fp:
  29. lines = list(line.rstrip() for line in fp)
  30. # START Alphabetical Order
  31. category = ""
  32. sections = {}
  33. section_line_num = {}
  34. for line_num, line in enumerate(lines):
  35. if line.startswith(anchor):
  36. category = line.split(anchor)[1].strip()
  37. sections[category] = []
  38. section_line_num[category] = line_num
  39. continue
  40. if not line.startswith('|') or line.startswith('|---'):
  41. continue
  42. title = [x.strip() for x in line.split('|')[1:-1]][0].upper()
  43. sections[category].append(title)
  44. for category, entries in sections.items():
  45. if sorted(entries) != entries:
  46. add_error(section_line_num[category], "{} section is not in alphabetical order".format(category))
  47. # END Alphabetical Order
  48. # START Check Entries
  49. num_in_category = min_entries_per_section + 1
  50. anchor_re = re.compile('###\s\S+')
  51. for line_num, line in enumerate(lines):
  52. # check each section for the minimum number of entries
  53. if line.startswith(anchor):
  54. if not anchor_re.match(line):
  55. add_error(line_num, "section header is not formatted correctly")
  56. if num_in_category < min_entries_per_section:
  57. add_error(category_line, "{} section does not have the minimum {} entries (only has {})".format(
  58. category, min_entries_per_section, num_in_category))
  59. category = line.split(' ')[1]
  60. category_line = line_num
  61. num_in_category = 0
  62. continue
  63. if not line.startswith('|') or line.startswith('|---'):
  64. continue
  65. num_in_category += 1
  66. segments = line.split('|')[1:-1]
  67. # START Global
  68. for segment in segments:
  69. # every line segment should start and end with exactly 1 space
  70. if len(segment) - len(segment.lstrip()) != 1 or len(segment) - len(segment.rstrip()) != 1:
  71. add_error(line_num, "each segment must start and end with exactly 1 space")
  72. # END Global
  73. segments = [seg.strip() for seg in segments]
  74. # START Title
  75. title = segments[index_title].upper()
  76. if title.endswith(' API'):
  77. add_error(line_num, 'Title should not contain "API"')
  78. # END Title
  79. # START Description
  80. # first character should be capitalized
  81. char = segments[index_desc][0]
  82. if char.upper() != char:
  83. add_error(line_num, "first character of description is not capitalized")
  84. # last character should not punctuation
  85. char = segments[index_desc][-1]
  86. if char in punctuation:
  87. add_error(line_num, "description should not end with {}".format(char))
  88. # END Description
  89. # START Auth
  90. # values should conform to valid options only
  91. auth = segments[index_auth].replace('`', '')
  92. if auth not in auth_keys:
  93. add_error(line_num, "{} is not a valid Auth option".format(auth))
  94. # END Auth
  95. # START HTTPS
  96. # values should conform to valid options only
  97. https = segments[index_https]
  98. if https not in https_keys:
  99. add_error(line_num, "{} is not a valid HTTPS option".format(https))
  100. # END HTTPS
  101. # START CORS
  102. # values should conform to valid options only
  103. cors = segments[index_cors]
  104. if cors not in cors_keys:
  105. add_error(line_num, "{} is not a valid CORS option".format(cors))
  106. # END CORS
  107. # START Link
  108. # url should be wrapped in '[Go!]()' Markdown syntax
  109. link = segments[index_link]
  110. if not link.startswith('[Go!](http') or not link.endswith(')'):
  111. add_error(line_num, 'link syntax should be "[Go!](LINK)"')
  112. # END Link
  113. # END Check Entries
  114. def main():
  115. num_args = len(sys.argv)
  116. if num_args < 2:
  117. print("No file passed (file should contain Markdown table syntax)")
  118. sys.exit(1)
  119. check_format(sys.argv[1])
  120. if len(errors) > 0:
  121. for err in errors:
  122. print(err)
  123. sys.exit(1)
  124. if __name__ == "__main__":
  125. main()