Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

150 строки
4.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. 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_alphabetical(lines):
  22. """
  23. checks if all entries per section are in alphabetical order based in entry title
  24. """
  25. sections = {}
  26. section_line_num = {}
  27. for line_num, line in enumerate(lines):
  28. if line.startswith(anchor):
  29. category = line.split(anchor)[1].strip()
  30. sections[category] = []
  31. section_line_num[category] = line_num
  32. continue
  33. if not line.startswith('|') or line.startswith('|---'):
  34. continue
  35. title = [x.strip() for x in line.split('|')[1:-1]][0].upper()
  36. sections[category].append(title)
  37. for category, entries in sections.items():
  38. if sorted(entries) != entries:
  39. add_error(section_line_num[category], "{} section is not in alphabetical order".format(category))
  40. def check_entry(line_num, segments):
  41. # START Title
  42. title = segments[index_title].upper()
  43. if title.endswith(' API'):
  44. add_error(line_num, 'Title should not contain "API"')
  45. # END Title
  46. # START Description
  47. # first character should be capitalized
  48. char = segments[index_desc][0]
  49. if char.upper() != char:
  50. add_error(line_num, "first character of description is not capitalized")
  51. # last character should not punctuation
  52. char = segments[index_desc][-1]
  53. if char in punctuation:
  54. add_error(line_num, "description should not end with {}".format(char))
  55. # END Description
  56. # START Auth
  57. # values should conform to valid options only
  58. auth = segments[index_auth]
  59. if auth != 'No' and (not auth.startswith('`') or not auth.endswith('`')):
  60. add_error(line_num, "auth value is not enclosed with `backticks`")
  61. if auth.replace('`', '') not in auth_keys:
  62. add_error(line_num, "{} is not a valid Auth option".format(auth))
  63. # END Auth
  64. # START HTTPS
  65. # values should conform to valid options only
  66. https = segments[index_https]
  67. if https not in https_keys:
  68. add_error(line_num, "{} is not a valid HTTPS option".format(https))
  69. # END HTTPS
  70. # START CORS
  71. # values should conform to valid options only
  72. cors = segments[index_cors]
  73. if cors not in cors_keys:
  74. add_error(line_num, "{} is not a valid CORS option".format(cors))
  75. # END CORS
  76. # START Link
  77. # url should be wrapped in '[Go!]()' Markdown syntax
  78. link = segments[index_link]
  79. if not link.startswith('[Go!](http') or not link.endswith(')'):
  80. add_error(line_num, 'link syntax should be "[Go!](LINK)"')
  81. # END Link
  82. def check_format(filename):
  83. """
  84. validates that each line is formatted correctly,
  85. appending to error list as needed
  86. """
  87. with open(filename) as fp:
  88. lines = list(line.rstrip() for line in fp)
  89. check_alphabetical(lines)
  90. # START Check Entries
  91. num_in_category = min_entries_per_section + 1
  92. category = ""
  93. category_line = 0
  94. anchor_re = re.compile('###\s\S+')
  95. for line_num, line in enumerate(lines):
  96. # check each section for the minimum number of entries
  97. if line.startswith(anchor):
  98. if not anchor_re.match(line):
  99. add_error(line_num, "section header is not formatted correctly")
  100. if num_in_category < min_entries_per_section:
  101. add_error(category_line, "{} section does not have the minimum {} entries (only has {})".format(
  102. category, min_entries_per_section, num_in_category))
  103. category = line.split(' ')[1]
  104. category_line = line_num
  105. num_in_category = 0
  106. continue
  107. if not line.startswith('|') or line.startswith('|---'):
  108. continue
  109. num_in_category += 1
  110. segments = line.split('|')[1:-1]
  111. # START Global
  112. for segment in segments:
  113. # every line segment should start and end with exactly 1 space
  114. if len(segment) - len(segment.lstrip()) != 1 or len(segment) - len(segment.rstrip()) != 1:
  115. add_error(line_num, "each segment must start and end with exactly 1 space")
  116. # END Global
  117. segments = [seg.strip() for seg in segments]
  118. check_entry(line_num, segments)
  119. # END Check Entries
  120. def main():
  121. num_args = len(sys.argv)
  122. if num_args < 2:
  123. print("No file passed (file should contain Markdown table syntax)")
  124. sys.exit(1)
  125. check_format(sys.argv[1])
  126. if len(errors) > 0:
  127. for err in errors:
  128. print(err)
  129. sys.exit(1)
  130. if __name__ == "__main__":
  131. main()