Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

170 řádky
5.9 KiB

  1. #!/usr/bin/env python3
  2. import re
  3. import sys
  4. anchor = '###'
  5. min_entries_per_section = 3
  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. num_segments = 6
  17. errors = []
  18. title_links = []
  19. previous_links = []
  20. anchor_re = re.compile(anchor + '\s(.+)')
  21. section_title_re = re.compile('\*\s\[(.*)\]')
  22. def add_error(line_num, message):
  23. """adds an error to the dynamic error list"""
  24. err = '(L{:03d}) {}'.format(line_num + 1, message)
  25. errors.append(err)
  26. def check_alphabetical(lines):
  27. """
  28. checks if all entries per section are in alphabetical order based in entry title
  29. """
  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. def check_entry(line_num, segments):
  46. # START Title
  47. title = segments[index_title].upper()
  48. if title.endswith(' API'):
  49. add_error(line_num, 'Title should not contain "API"')
  50. # END Title
  51. # START Description
  52. # first character should be capitalized
  53. char = segments[index_desc][0]
  54. if char.upper() != char:
  55. add_error(line_num, "first character of description is not capitalized")
  56. # last character should not punctuation
  57. char = segments[index_desc][-1]
  58. if char in punctuation:
  59. add_error(line_num, "description should not end with {}".format(char))
  60. desc_length = len(segments[index_desc])
  61. if desc_length > 100:
  62. add_error(line_num, "description should not exceed 100 characters (currently {})".format(desc_length))
  63. # END Description
  64. # START Auth
  65. # values should conform to valid options only
  66. auth = segments[index_auth]
  67. if auth != 'No' and (not auth.startswith('`') or not auth.endswith('`')):
  68. add_error(line_num, "auth value is not enclosed with `backticks`")
  69. if auth.replace('`', '') not in auth_keys:
  70. add_error(line_num, "{} is not a valid Auth option".format(auth))
  71. # END Auth
  72. # START HTTPS
  73. # values should conform to valid options only
  74. https = segments[index_https]
  75. if https not in https_keys:
  76. add_error(line_num, "{} is not a valid HTTPS option".format(https))
  77. # END HTTPS
  78. # START CORS
  79. # values should conform to valid options only
  80. cors = segments[index_cors]
  81. if cors not in cors_keys:
  82. add_error(line_num, "{} is not a valid CORS option".format(cors))
  83. # END CORS
  84. # START Link
  85. # url should be wrapped in '[Go!]()' Markdown syntax
  86. link = segments[index_link]
  87. if not link.startswith('[Go!](http') or not link.endswith(')'):
  88. add_error(line_num, 'link syntax should be "[Go!](LINK)"')
  89. if link in previous_links:
  90. add_error(line_num, 'duplicate link - entries should only be included in one section')
  91. else:
  92. previous_links.append(link)
  93. # END Link
  94. def check_format(filename):
  95. """
  96. validates that each line is formatted correctly,
  97. appending to error list as needed
  98. """
  99. with open(filename) as fp:
  100. lines = list(line.rstrip() for line in fp)
  101. check_alphabetical(lines)
  102. # START Check Entries
  103. num_in_category = min_entries_per_section + 1
  104. category = ""
  105. category_line = 0
  106. for line_num, line in enumerate(lines):
  107. if section_title_re.match(line):
  108. title_links.append(section_title_re.match(line).group(1))
  109. # check each section for the minimum number of entries
  110. if line.startswith(anchor):
  111. match = anchor_re.match(line)
  112. if match:
  113. if match.group(1) not in title_links:
  114. add_error(line_num, "section header ({}) not added as a title link".format(match.group(1)))
  115. else:
  116. add_error(line_num, "section header is not formatted correctly")
  117. if num_in_category < min_entries_per_section:
  118. add_error(category_line, "{} section does not have the minimum {} entries (only has {})".format(
  119. category, min_entries_per_section, num_in_category))
  120. category = line.split(' ')[1]
  121. category_line = line_num
  122. num_in_category = 0
  123. continue
  124. # skips lines that we do not care about
  125. if not line.startswith('|') or line.startswith('|---'):
  126. continue
  127. num_in_category += 1
  128. segments = line.split('|')[1:-1]
  129. if len(segments) < num_segments:
  130. add_error(line_num, "entry does not have all the required sections (have {}, need {})".format(
  131. len(segments), num_segments))
  132. continue
  133. # START Global
  134. for segment in segments:
  135. # every line segment should start and end with exactly 1 space
  136. if len(segment) - len(segment.lstrip()) != 1 or len(segment) - len(segment.rstrip()) != 1:
  137. add_error(line_num, "each segment must start and end with exactly 1 space")
  138. # END Global
  139. segments = [seg.strip() for seg in segments]
  140. check_entry(line_num, segments)
  141. # END Check Entries
  142. def main():
  143. if len(sys.argv) < 2:
  144. print("No file passed (file should contain Markdown table syntax)")
  145. sys.exit(1)
  146. check_format(sys.argv[1])
  147. if len(errors) > 0:
  148. for err in errors:
  149. print(err)
  150. sys.exit(1)
  151. if __name__ == "__main__":
  152. main()