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.

125 lines
4.0 KiB

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