Update format.py

corrected indents and hyphens
This commit is contained in:
Тарасов СЕРГЕЙ
2022-06-09 22:22:18 +03:00
committed by GitHub
parent 1dce8ae6e2
commit b2fc445f58

View File

@@ -5,6 +5,7 @@ import sys
from string import punctuation from string import punctuation
from typing import List, Tuple, Dict from typing import List, Tuple, Dict
# Temporary replacement # Temporary replacement
# The descriptions that contain () at the end must adapt to the new policy later # The descriptions that contain () at the end must adapt to the new policy later
punctuation = punctuation.replace('()', '') punctuation = punctuation.replace('()', '')
@@ -40,56 +41,37 @@ def error_message(line_number: int, message: str) -> str:
def get_categories_content(contents: List[str]) -> Tuple[Categories, CategoriesLineNumber]: def get_categories_content(contents: List[str]) -> Tuple[Categories, CategoriesLineNumber]:
categories = {} categories = {}
category_line_num = {} category_line_num = {}
for line_num, line_content in enumerate(contents): for line_num, line_content in enumerate(contents):
if line_content.startswith(anchor): if line_content.startswith(anchor):
category = line_content.split(anchor)[1].strip() category = line_content.split(anchor)[1].strip()
categories[category] = [] categories[category] = []
category_line_num[category] = line_num category_line_num[category] = line_num
continue continue
if not line_content.startswith('|') or line_content.startswith('|---'): if not line_content.startswith('|') or line_content.startswith('|---'):
continue continue
raw_title = [raw_content.strip() for raw_content in line_content.split('|')[1:-1]][0]
raw_title = [
raw_content.strip() for raw_content in line_content.split('|')[1:-1]
][0]
title_match = link_re.match(raw_title) title_match = link_re.match(raw_title)
if title_match: if title_match:
title = title_match.group(1).upper() title = title_match.group(1).upper()
categories[category].append(title) categories[category].append(title)
return (categories, category_line_num) return (categories, category_line_num)
def check_alphabetical_order(lines: List[str]) -> List[str]: def check_alphabetical_order(lines: List[str]) -> List[str]:
err_msgs = [] err_msgs = []
categories, category_line_num = get_categories_content(contents=lines) categories, category_line_num = get_categories_content(contents=lines)
for category, api_list in categories.items(): for category, api_list in categories.items():
if sorted(api_list) != api_list: if sorted(api_list) != api_list:
err_msg = error_message( err_msg = error_message(category_line_num[category], f'{category} category is not alphabetical order')
category_line_num[category],
f'{category} category is not alphabetical order'
)
err_msgs.append(err_msg) err_msgs.append(err_msg)
return err_msgs return err_msgs
def check_title(line_num: int, raw_title: str) -> List[str]: def check_title(line_num: int, raw_title: str) -> List[str]:
err_msgs = [] err_msgs = []
title_match = link_re.match(raw_title) title_match = link_re.match(raw_title)
# url should be wrapped in "[TITLE](LINK)" Markdown syntax # url should be wrapped in "[TITLE](LINK)" Markdown syntax
if not title_match: if not title_match:
err_msg = error_message(line_num, 'Title syntax should be "[TITLE](LINK)"') err_msg = error_message(line_num, 'Title syntax should be "[TITLE](LINK)"')
@@ -100,113 +82,85 @@ def check_title(line_num: int, raw_title: str) -> List[str]:
if title.upper().endswith(' API'): if title.upper().endswith(' API'):
err_msg = error_message(line_num, 'Title should not end with "... API". Every entry is an API here!') err_msg = error_message(line_num, 'Title should not end with "... API". Every entry is an API here!')
err_msgs.append(err_msg) err_msgs.append(err_msg)
return err_msgs return err_msgs
def check_description(line_num: int, description: str) -> List[str]: def check_description(line_num: int, description: str) -> List[str]:
err_msgs = [] err_msgs = []
first_char = description[0] first_char = description[0]
if first_char.upper() != first_char: if first_char.upper() != first_char:
err_msg = error_message(line_num, 'first character of description is not capitalized') err_msg = error_message(line_num, 'first character of description is not capitalized')
err_msgs.append(err_msg) err_msgs.append(err_msg)
last_char = description[-1] last_char = description[-1]
if last_char in punctuation: if last_char in punctuation:
err_msg = error_message(line_num, f'description should not end with {last_char}') err_msg = error_message(line_num, f'description should not end with {last_char}')
err_msgs.append(err_msg) err_msgs.append(err_msg)
desc_length = len(description) desc_length = len(description)
if desc_length > max_description_length: if desc_length > max_description_length:
err_msg = error_message(line_num, f'description should not exceed {max_description_length} characters (currently {desc_length})') err_msg = error_message(line_num, f'description should not exceed {max_description_length} characters (currently {desc_length})')
err_msgs.append(err_msg) err_msgs.append(err_msg)
return err_msgs return err_msgs
def check_auth(line_num: int, auth: str) -> List[str]: def check_auth(line_num: int, auth: str) -> List[str]:
err_msgs = [] err_msgs = []
backtick = '`' backtick = '`'
if auth != 'No' and (not auth.startswith(backtick) or not auth.endswith(backtick)): if auth != 'No' and (not auth.startswith(backtick) or not auth.endswith(backtick)):
err_msg = error_message(line_num, 'auth value is not enclosed with `backticks`') err_msg = error_message(line_num, 'auth value is not enclosed with `backticks`')
err_msgs.append(err_msg) err_msgs.append(err_msg)
if auth.replace(backtick, '') not in auth_keys: if auth.replace(backtick, '') not in auth_keys:
err_msg = error_message(line_num, f'{auth} is not a valid Auth option') err_msg = error_message(line_num, f'{auth} is not a valid Auth option')
err_msgs.append(err_msg) err_msgs.append(err_msg)
return err_msgs return err_msgs
def check_https(line_num: int, https: str) -> List[str]: def check_https(line_num: int, https: str) -> List[str]:
err_msgs = [] err_msgs = []
if https not in https_keys: if https not in https_keys:
err_msg = error_message(line_num, f'{https} is not a valid HTTPS option') err_msg = error_message(line_num, f'{https} is not a valid HTTPS option')
err_msgs.append(err_msg) err_msgs.append(err_msg)
return err_msgs return err_msgs
def check_cors(line_num: int, cors: str) -> List[str]: def check_cors(line_num: int, cors: str) -> List[str]:
err_msgs = [] err_msgs = []
if cors not in cors_keys: if cors not in cors_keys:
err_msg = error_message(line_num, f'{cors} is not a valid CORS option') err_msg = error_message(line_num, f'{cors} is not a valid CORS option')
err_msgs.append(err_msg) err_msgs.append(err_msg)
return err_msgs return err_msgs
def check_entry(line_num: int, segments: List[str]) -> List[str]: def check_entry(line_num: int, segments: List[str]) -> List[str]:
raw_title = segments[index_title] raw_title = segments[index_title]
description = segments[index_desc] description = segments[index_desc]
auth = segments[index_auth] auth = segments[index_auth]
https = segments[index_https] https = segments[index_https]
cors = segments[index_cors] cors = segments[index_cors]
title_err_msgs = check_title(line_num, raw_title) title_err_msgs = check_title(line_num, raw_title)
desc_err_msgs = check_description(line_num, description) desc_err_msgs = check_description(line_num, description)
auth_err_msgs = check_auth(line_num, auth) auth_err_msgs = check_auth(line_num, auth)
https_err_msgs = check_https(line_num, https) https_err_msgs = check_https(line_num, https)
cors_err_msgs = check_cors(line_num, cors) cors_err_msgs = check_cors(line_num, cors)
err_msgs = [*title_err_msgs,
err_msgs = [ *desc_err_msgs,
*title_err_msgs, *auth_err_msgs,
*desc_err_msgs, *https_err_msgs,
*auth_err_msgs, *cors_err_msgs]
*https_err_msgs,
*cors_err_msgs
]
return err_msgs return err_msgs
def check_file_format(lines: List[str]) -> List[str]: def check_file_format(lines: List[str]) -> List[str]:
err_msgs = [] err_msgs = []
category_title_in_index = [] category_title_in_index = []
alphabetical_err_msgs = check_alphabetical_order(lines) alphabetical_err_msgs = check_alphabetical_order(lines)
err_msgs.extend(alphabetical_err_msgs) err_msgs.extend(alphabetical_err_msgs)
num_in_category = min_entries_per_category + 1 num_in_category = min_entries_per_category + 1
category = '' category = ''
category_line = 0 category_line = 0
for line_num, line_content in enumerate(lines): for line_num, line_content in enumerate(lines):
category_title_match = category_title_in_index_re.match(line_content) category_title_match = category_title_in_index_re.match(line_content)
if category_title_match: if category_title_match:
category_title_in_index.append(category_title_match.group(1)) category_title_in_index.append(category_title_match.group(1))
# check each category for the minimum number of entries # check each category for the minimum number of entries
if line_content.startswith(anchor): if line_content.startswith(anchor):
category_match = anchor_re.match(line_content) category_match = anchor_re.match(line_content)
@@ -217,47 +171,38 @@ def check_file_format(lines: List[str]) -> List[str]:
else: else:
err_msg = error_message(line_num, 'category header is not formatted correctly') err_msg = error_message(line_num, 'category header is not formatted correctly')
err_msgs.append(err_msg) err_msgs.append(err_msg)
if num_in_category < min_entries_per_category: if num_in_category < min_entries_per_category:
err_msg = error_message(category_line, f'{category} category does not have the minimum {min_entries_per_category} entries (only has {num_in_category})') err_msg = error_message(category_line,
f'{category} category does not have the minimum {min_entries_per_category} entries (only has {num_in_category})')
err_msgs.append(err_msg) err_msgs.append(err_msg)
category = line_content.split(' ')[1] category = line_content.split(' ')[1]
category_line = line_num category_line = line_num
num_in_category = 0 num_in_category = 0
continue continue
# skips lines that we do not care about # skips lines that we do not care about
if not line_content.startswith('|') or line_content.startswith('|---'): if not line_content.startswith('|') or line_content.startswith('|---'):
continue continue
num_in_category += 1 num_in_category += 1
segments = line_content.split('|')[1:-1] segments = line_content.split('|')[1:-1]
if len(segments) < num_segments: if len(segments) < num_segments:
err_msg = error_message(line_num, f'entry does not have all the required columns (have {len(segments)}, need {num_segments})') err_msg = error_message(line_num, f'entry does not have all the required columns (have {len(segments)}, need {num_segments})')
err_msgs.append(err_msg) err_msgs.append(err_msg)
continue continue
for segment in segments: for segment in segments:
# every line segment should start and end with exactly 1 space # every line segment should start and end with exactly 1 space
if len(segment) - len(segment.lstrip()) != 1 or len(segment) - len(segment.rstrip()) != 1: if len(segment) - len(segment.lstrip()) != 1 or len(segment) - len(segment.rstrip()) != 1:
err_msg = error_message(line_num, 'each segment must start and end with exactly 1 space') err_msg = error_message(line_num, 'each segment must start and end with exactly 1 space')
err_msgs.append(err_msg) err_msgs.append(err_msg)
segments = [segment.strip() for segment in segments] segments = [segment.strip() for segment in segments]
entry_err_msgs = check_entry(line_num, segments) entry_err_msgs = check_entry(line_num, segments)
err_msgs.extend(entry_err_msgs) err_msgs.extend(entry_err_msgs)
return err_msgs return err_msgs
def main(filename: str) -> None: def main(filename: str) -> None:
with open(filename, mode='r', encoding='utf-8') as file: with open(filename, mode='r', encoding='utf-8') as file:
lines = list(line.rstrip() for line in file) lines = list(line.rstrip() for line in file)
file_format_err_msgs = check_file_format(lines) file_format_err_msgs = check_file_format(lines)
if file_format_err_msgs: if file_format_err_msgs:
for err_msg in file_format_err_msgs: for err_msg in file_format_err_msgs:
print(err_msg) print(err_msg)
@@ -265,13 +210,9 @@ def main(filename: str) -> None:
if __name__ == '__main__': if __name__ == '__main__':
num_args = len(sys.argv) num_args = len(sys.argv)
if num_args < 2: if num_args < 2:
print('No .md file passed (file should contain Markdown table syntax)') print('No .md file passed (file should contain Markdown table syntax)')
sys.exit(1) sys.exit(1)
filename = sys.argv[1] filename = sys.argv[1]
main(filename) main(filename)