Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

validate_format.py 3.7 KiB

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