|
| 1 | +import os |
| 2 | +from typing import Dict, List |
| 3 | + |
| 4 | + |
| 5 | +def count_lines_of_code(directory: str, extensions: Dict[str, str]) -> Dict[str, int]: |
| 6 | + """ |
| 7 | + Count the lines of code for each file extension in the specified directory. |
| 8 | +
|
| 9 | + Args: |
| 10 | + directory (str): The directory where the project is located. |
| 11 | + extensions (Dict[str, str]): A dictionary mapping file extensions to their programming languages. |
| 12 | +
|
| 13 | + Returns: |
| 14 | + Dict[str, int]: A dictionary mapping file extensions to the total lines of code counted. |
| 15 | + """ |
| 16 | + totalLines = {ext: 0 for ext in extensions} # Initialize with 0 for each extension |
| 17 | + for ext, language in extensions.items(): |
| 18 | + total = 0 |
| 19 | + for dirpath, _, filenames in os.walk(directory): |
| 20 | + for filename in filenames: |
| 21 | + if filename.endswith(ext): |
| 22 | + file_path = os.path.join(dirpath, filename) |
| 23 | + try: |
| 24 | + with open(file_path, encoding="utf-8") as file: |
| 25 | + lines = file.readlines() |
| 26 | + lines_count = len(lines) |
| 27 | + total += lines_count |
| 28 | + except (UnicodeDecodeError, FileNotFoundError): |
| 29 | + print(f"Could not read file: {file_path}") |
| 30 | + totalLines[ext] = total |
| 31 | + return totalLines |
| 32 | + |
| 33 | + |
| 34 | +def sum_lines_of_code_for_multiple_directories( |
| 35 | + directories: List[str], extensions: Dict[str, str] |
| 36 | +) -> Dict[str, Dict[str, int]]: |
| 37 | + """ |
| 38 | + Count the lines of code for each file extension across multiple project directories. |
| 39 | +
|
| 40 | + Args: |
| 41 | + directories (List[str]): A list of directories representing different projects. |
| 42 | + extensions (Dict[str, str]): A dictionary mapping file extensions to their programming languages. |
| 43 | +
|
| 44 | + Returns: |
| 45 | + Dict[str, Dict[str, int]]: A dictionary with project names as keys and |
| 46 | + another dictionary mapping file extensions to their total lines of code in each project. |
| 47 | + """ |
| 48 | + all_project_lines = {} |
| 49 | + total_lines_per_extension = {ext: 0 for ext in extensions} |
| 50 | + |
| 51 | + for directory in directories: |
| 52 | + project_name = os.path.basename(directory) |
| 53 | + lines_in_project = count_lines_of_code(directory, extensions) |
| 54 | + all_project_lines[project_name] = lines_in_project |
| 55 | + |
| 56 | + # Sum up total lines across all projects for each extension |
| 57 | + for ext, lines in lines_in_project.items(): |
| 58 | + total_lines_per_extension[ext] += lines |
| 59 | + |
| 60 | + all_project_lines["total"] = total_lines_per_extension |
| 61 | + return all_project_lines |
| 62 | + |
| 63 | + |
| 64 | +def generate_markdown_table_for_multiple_projects( |
| 65 | + project_lines: Dict[str, Dict[str, int]], extensions: Dict[str, str] |
| 66 | +) -> str: |
| 67 | + """ |
| 68 | + Generate a Markdown-formatted table showing lines of code for each file extension across multiple projects. |
| 69 | +
|
| 70 | + Args: |
| 71 | + project_lines (Dict[str, Dict[str, int]]): A dictionary mapping project names to |
| 72 | + another dictionary mapping file extensions to the total lines of code. |
| 73 | + extensions (Dict[str, str]): A dictionary mapping file extensions to their programming languages. |
| 74 | +
|
| 75 | + Returns: |
| 76 | + str: A Markdown-formatted table showing lines of code across multiple projects and their totals. |
| 77 | + """ |
| 78 | + # Create Markdown table header |
| 79 | + header_projects = " | ".join(project_lines.keys()) |
| 80 | + markdown_table = f"| File Extension | Language | {header_projects} |\n" |
| 81 | + |
| 82 | + # Create separator row |
| 83 | + separator = f"|{'-' * 15}|{'-' * 18}|{'-' * (len(header_projects) + 4)}|\n" |
| 84 | + markdown_table += separator |
| 85 | + |
| 86 | + # Populate table rows |
| 87 | + for ext, language in extensions.items(): |
| 88 | + row = f"| {ext.ljust(13)} | {language.ljust(16)} " |
| 89 | + for project in project_lines: |
| 90 | + row += f"| {str(project_lines[project].get(ext, 0)).rjust(6)} " |
| 91 | + row += "|\n" |
| 92 | + markdown_table += row |
| 93 | + |
| 94 | + return markdown_table |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + # Define file extensions and their corresponding programming languages |
| 99 | + extensions = { |
| 100 | + ".py": "Python", |
| 101 | + ".cpp": "C++", |
| 102 | + ".h": "C++", |
| 103 | + ".c": "C", |
| 104 | + ".cmake": "CMake", |
| 105 | + ".txt": "Text", |
| 106 | + ".rst": "RestructuredText", |
| 107 | + ".css": "CSS", |
| 108 | + } |
| 109 | + |
| 110 | + # Define the list of project directories |
| 111 | + project_directories = [ |
| 112 | + r"C:\itom\sources\itom", # itom core |
| 113 | + r"C:\itom\sources\plugins", # plugins |
| 114 | + r"C:\itom\sources\designerPlugins", # designer plugins |
| 115 | + ] |
| 116 | + |
| 117 | + # Count lines of code for all project directories |
| 118 | + total_lines = sum_lines_of_code_for_multiple_directories(project_directories, extensions) |
| 119 | + |
| 120 | + # Generate and print Markdown table for all projects |
| 121 | + markdown_output = generate_markdown_table_for_multiple_projects(total_lines, extensions) |
| 122 | + print(markdown_output) |
0 commit comments