No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

153 líneas
5.1 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. desc_length = len(segments[index_desc])
  56. if desc_length > 100:
  57. add_error(line_num, "description should not exceed 100 characters (currently {})".format(desc_length))
  58. # END Description
  59. # START Auth
  60. # values should conform to valid options only
  61. auth = segments[index_auth]
  62. if auth != 'No' and (not auth.startswith('`') or not auth.endswith('`')):
  63. add_error(line_num, "auth value is not enclosed with `backticks`")
  64. if auth.replace('`', '') not in auth_keys:
  65. add_error(line_num, "{} is not a valid Auth option".format(auth))
  66. # END Auth
  67. # START HTTPS
  68. # values should conform to valid options only
  69. https = segments[index_https]
  70. if https not in https_keys:
  71. add_error(line_num, "{} is not a valid HTTPS option".format(https))
  72. # END HTTPS
  73. # START CORS
  74. # values should conform to valid options only
  75. cors = segments[index_cors]
  76. if cors not in cors_keys:
  77. add_error(line_num, "{} is not a valid CORS option".format(cors))
  78. # END CORS
  79. # START Link
  80. # url should be wrapped in '[Go!]()' Markdown syntax
  81. link = segments[index_link]
  82. if not link.startswith('[Go!](http') or not link.endswith(')'):
  83. add_error(line_num, 'link syntax should be "[Go!](LINK)"')
  84. # END Link
  85. def check_format(filename):
  86. """
  87. validates that each line is formatted correctly,
  88. appending to error list as needed
  89. """
  90. with open(filename) as fp:
  91. lines = list(line.rstrip() for line in fp)
  92. check_alphabetical(lines)
  93. # START Check Entries
  94. num_in_category = min_entries_per_section + 1
  95. category = ""
  96. category_line = 0
  97. anchor_re = re.compile('###\s\S+')
  98. for line_num, line in enumerate(lines):
  99. # check each section for the minimum number of entries
  100. if line.startswith(anchor):
  101. if not anchor_re.match(line):
  102. add_error(line_num, "section header is not formatted correctly")
  103. if num_in_category < min_entries_per_section:
  104. add_error(category_line, "{} section does not have the minimum {} entries (only has {})".format(
  105. category, min_entries_per_section, num_in_category))
  106. category = line.split(' ')[1]
  107. category_line = line_num
  108. num_in_category = 0
  109. continue
  110. if not line.startswith('|') or line.startswith('|---'):
  111. continue
  112. num_in_category += 1
  113. segments = line.split('|')[1:-1]
  114. # START Global
  115. for segment in segments:
  116. # every line segment should start and end with exactly 1 space
  117. if len(segment) - len(segment.lstrip()) != 1 or len(segment) - len(segment.rstrip()) != 1:
  118. add_error(line_num, "each segment must start and end with exactly 1 space")
  119. # END Global
  120. segments = [seg.strip() for seg in segments]
  121. check_entry(line_num, segments)
  122. # END Check Entries
  123. def main():
  124. num_args = len(sys.argv)
  125. if num_args < 2:
  126. print("No file passed (file should contain Markdown table syntax)")
  127. sys.exit(1)
  128. check_format(sys.argv[1])
  129. if len(errors) > 0:
  130. for err in errors:
  131. print(err)
  132. sys.exit(1)
  133. if __name__ == "__main__":
  134. main()