Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

78 righe
2.0 KiB

  1. # -*- coding: utf-8 -*-
  2. import re
  3. from typing import List, Tuple, Dict
  4. anchor = '###'
  5. min_entries_per_section = 3
  6. auth_keys = ['apiKey', 'OAuth', 'X-Mashape-Key', 'User-Agent', '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 = 5
  17. errors = []
  18. title_links = []
  19. anchor_re = re.compile(anchor + '\s(.+)')
  20. section_title_re = re.compile('\*\s\[(.*)\]')
  21. link_re = re.compile('\[(.+)\]\((http.*)\)')
  22. # Type aliases
  23. APIList = List[str]
  24. Categories = Dict[str, APIList]
  25. CategoriesLineNumber = Dict[str, int]
  26. def error_message(line_number: int, message: str) -> str:
  27. line = line_number + 1
  28. return f'(L{line:03d}) {message}'
  29. def get_categories_content(contents: List[str]) -> Tuple[Categories, CategoriesLineNumber]:
  30. categories = {}
  31. category_line_num = {}
  32. for line_num, line_content in enumerate(contents):
  33. if line_content.startswith(anchor):
  34. category = line_content.split(anchor)[1].strip()
  35. categories[category] = []
  36. category_line_num[category] = line_num
  37. continue
  38. if not line_content.startswith('|') or line_content.startswith('|---'):
  39. continue
  40. raw_title = [
  41. raw_content.strip() for raw_content in line_content.split('|')[1:-1]
  42. ][0]
  43. title_match = link_re.match(raw_title)
  44. if title_match:
  45. title = title_match.group(1).upper()
  46. categories[category].append(title)
  47. return (categories, category_line_num)
  48. def check_alphabetical_order(lines: List[str]) -> None:
  49. categories, category_line_num = get_categories_content(contents=lines)
  50. for category, api_list in categories.items():
  51. if sorted(api_list) != api_list:
  52. message = error_message(
  53. category_line_num[category],
  54. f'{category} category is not alphabetical order'
  55. )
  56. errors.append(message)