import re import datetime # Options default values STYLE_START_COMMENT = "/*!" START_COMMAND = "\\" AUTHOR_NAME = "John Doe" AUTHOR_EMAIL = "johndoe@email.com" DATE_FORMAT = "%d/%m/%Y" def getOptions(): """ Get options from options.txt file Returns ------- None """ global STYLE_START_COMMENT, START_COMMAND, AUTHOR_NAME, AUTHOR_EMAIL, DATE_FORMAT try: with open('options.ini', 'r') as file: file_content = file.read() start_command_match = re.search(r'(?<=start_command = ).+', file_content) style_start_comment_match = re.search(r'(?<=style_start_comment = ).+', file_content) author_name_match = re.search(r'(?<=author_name = )\w+\s?\w+', file_content) author_email_match = re.search(r'(?<=author_email = )\w+@\w+\.\w+', file_content) date_format_match = re.search(r'(?<=date_format = )\S+', file_content) except FileNotFoundError: start_command_match = None style_start_comment_match = None author_name_match = None author_email_match = None date_format_match = None if start_command_match: START_COMMAND = start_command_match.group() if style_start_comment_match: STYLE_START_COMMENT = style_start_comment_match.group() if author_name_match: AUTHOR_NAME = author_name_match.group() if author_email_match: AUTHOR_EMAIL = author_email_match.group() if date_format_match: DATE_FORMAT = date_format_match.group() def getFunctions(filename: str) -> list: """ Get the prototype of all functions in a file Parameters ---------- filename : str Name of the file to get the functions from Returns ------- list List of functions prototypes """ with open(filename, 'r') as file: content = file.read() pattern_function = re.compile(r'(?<=\n)(?!/\*\*|/\*!)\n+(\w+\s+\w+\s*\([^)]*\))') functions = re.findall(pattern_function, content) return functions def getArguments(function: str) -> list: """ Get the arguments from a function prototype Parameters ---------- function : str Prototype of the function Returns ------- list List of arguments """ pattern_args = re.compile(r'\b\w+\b(?=\s*[,)])') return re.findall(pattern_args, function) def getReturnType(function: str) -> str: """ Get the return type of a function Parameters ---------- function : str Prototype of the function Returns ------- str Return type of the function """ pattern_return = re.compile(r'\b\w+\b(?=\s+\w+\s*\([^)]*\))') return re.search(pattern_return, function).group() def writeFunctionCommentLines(content: list, index: int, function: str) -> list: """ Write the comment block for a function Parameters ---------- content : list Content of the file index : int Line index where the function is located function : str Prototype of the function to comment Returns ------- list Content of the file with the comment block added """ if content[index].strip().startswith('*/'): return content comment_lines = [ f'{STYLE_START_COMMENT} \n', f' * {START_COMMAND}fn {function} \n', f' * {START_COMMAND}author {AUTHOR_NAME} <{AUTHOR_EMAIL}> \n', f' * {START_COMMAND}version 1.0 \n', f' * {START_COMMAND}date {datetime.datetime.now().strftime(DATE_FORMAT)} \n', f' * {START_COMMAND}brief \n' ] args = getArguments(function) comment_lines.extend([f' * {START_COMMAND}param {arg} \n' for arg in args]) if getReturnType(function) != 'void': comment_lines.append(f' * {START_COMMAND}return \n') comment_lines.append(f' */\n') content[index:index] = comment_lines return content def writeFunctionComment(filename: str, function: str): """ Insert the comment block for a function in a file Parameters ---------- filename : str Name of the file to insert the comment block function : str Prototype of the function to comment Returns ------- None """ with open(filename, 'r') as file: content = file.readlines() for i, line in enumerate(content): if line.strip().startswith(function): content = writeFunctionCommentLines(content, i, function) break with open(filename, 'w') as file: file.writelines(content) def writeFileComment(filename: str) -> None: """ Write the comment block for the file Parameters ---------- filename : str Name of the file to insert the comment block Returns ------- None """ with open(filename, 'r+') as file: file_content = file.read() if re.search(r'(?:\/\*\*|\/\*!)\s*\*\s*(?:\\|@)file\s+(\w+)[\s\S]*?\*\/', file_content): return None file.seek(0) content = file.readlines() comment_block = [ f'{STYLE_START_COMMENT} \n', f' * {START_COMMAND}file {filename} \n', f' * {START_COMMAND}author {AUTHOR_NAME} <{AUTHOR_EMAIL}> \n', f' * {START_COMMAND}version 1.0 \n', f' * {START_COMMAND}date {datetime.datetime.now().strftime(DATE_FORMAT)} \n', f' * {START_COMMAND}brief \n', f' */\n\n\n' ] content = comment_block + content file.seek(0) file.writelines(content) file.truncate() def generateAllComments(filename: str): """ Write the comment block for all functions in a file and the file itself Parameters ---------- filename : str Name of the file to insert the comment blocks Returns ------- None """ getOptions() writeFileComment(filename) functions = getFunctions(filename) for function in functions: writeFunctionComment(filename, function)