pypu

pypudataSourceFileSourceClassUMLFileSourceFunctionSourceVariableSourceTypeutilsPackageToolsPlantEncoderModuleParserget_known_modules(directory:str, blacklist:list[str]) →list[str]get_source_files(directory:str, known_modules:list[str], blacklist:list[str]) →list[SourceFile]generate_uml(directory:str) →strSourceFileclasses:list[SourceClass]functions:list[SourceFunction]imports:list[str]name:strvariables:list[SourceVariable]__init__(self) →None__str__(self) →strget_connection_strings(self) →strSourceClassbases:list[str]methods:list[SourceFunction]name:strvariables:list[SourceVariable]__init__(self) →None__str__(self) →strget_dependencies(self) →set[str]UMLFilesource_files:list[SourceFile]__init__(self, source_files:list[SourceFile]) →None__str__(self) →strSourceFunctionname:strparams:list[SourceVariable]returns:SourceType__init__(self) →None__str__(self) →strget_dependencies(self) →set[str]SourceVariabledefault:str | Nonename:strstatic:booltype:SourceType__init__(self, name:str, data_type:SourceType, static:bool= False, default:str | None= None) →None__str__(self) →strget_dependencies(self) →set[str]SourceTypedependencies:set[str]readable:str__init__(self, readable:str= Any, dependencies:set[str] | None= None) →None__str__(self) →strget_matching_module(modules:list[str], snippet:str) →str | Noneencode(diagram:str, render_type:str) →strPLANTUML_ALPHABET:strBASE64_ALPHABET:strannotation_to_type(node:NodeNG | None) →SourceTypeget_function(node:FunctionDef) →SourceFunctionget_class(node:ClassDef) →SourceClassget_module_info(root:str, file_path:str, known_modules:list[str], with_external_dependencies:bool= False) →SourceFile

  1#!/usr/bin/env python3
  2"""
  3.. include:: ./example.svg
  4"""
  5
  6import argparse
  7import os
  8import sys
  9from argparse import _MutuallyExclusiveGroup
 10
 11import astroid
 12import requests
 13
 14from data.SourceFile import SourceFile
 15from data.UMLFile import UMLFile
 16from utils.ModuleParser import get_module_info
 17from utils.PlantEncoder import encode
 18
 19
 20def get_known_modules(directory: str, blacklist: list[str]) -> list[str]:
 21    """
 22    Gets a `list` of all modules in a directory and its subdirectories.
 23
 24    :param directory: The directory to process.
 25    :param blacklist: A `list` of forbidden substrings.
 26    :return: A `list` of known modules.
 27    """
 28    known_modules: list[str] = []
 29    for root, _, files in os.walk(directory):
 30        for file in files:
 31            if '.py' in file:
 32                file_path: str = os.path.join(root, file.split('.')[0])
 33                if not any(item in file_path for item in blacklist):
 34                    known_modules.append('.'.join([
 35                        x for x in file_path[len(directory):].split(os.sep) if x
 36                    ]))
 37    return known_modules
 38
 39
 40def get_source_files(
 41        directory: str,
 42        known_modules: list[str],
 43        blacklist: list[str]
 44) -> list[SourceFile]:
 45    """
 46    Gets a `list` of all source files in a directory and its subdirectories.
 47
 48    :param directory: The directory to process.
 49    :param known_modules: A `list` of known modules.
 50    :param blacklist: A `list` of forbidden substrings.
 51    :return: A `list` of source files.
 52    """
 53    source_files: list[SourceFile] = []
 54    for root, _, files in os.walk(directory):
 55        for file in files:
 56            if '.py' in file:
 57                file_path = os.path.join(root, file)
 58                if not any(item in file_path for item in blacklist):
 59                    try:
 60                        source_files.append(
 61                            get_module_info(directory, file_path, known_modules)
 62                        )
 63                    except astroid.AstroidBuildingException:
 64                        pass
 65    return source_files
 66
 67
 68def generate_uml(directory: str) -> str:
 69    """
 70    Generates a valid PlantUML string for all files in the given directory and its subdirectories.
 71
 72    :param directory: The directory to process.
 73    :return: A valid PlantUML string.
 74    """
 75    blacklist: list[str] = ['venv', '/.', '__']
 76    known_modules: list[str] = get_known_modules(directory, blacklist)
 77    source_files: list[SourceFile] = get_source_files(directory, known_modules, blacklist)
 78    return str(UMLFile(source_files))
 79
 80
 81if __name__ == '__main__':
 82    parser: argparse.ArgumentParser = argparse.ArgumentParser()
 83    parser.add_argument('-m', '--module', help='the module to analyze', required=True)
 84    group: _MutuallyExclusiveGroup = parser.add_mutually_exclusive_group()
 85    group.add_argument('-l', '--link', help='returns a link to the diagram in the specified format')
 86    group.add_argument('-f', '--format', help='returns the diagram in the specified format')
 87    args: argparse.Namespace = parser.parse_args()
 88
 89    uml: str = generate_uml(args.module)
 90    if args.link:
 91        print(encode(uml, args.link))
 92    elif args.format:
 93        encoded: str = encode(uml, args.format)
 94        result: requests.Response | None = None
 95        try:
 96            result = requests.get(encoded, timeout=3, allow_redirects=False)
 97        except requests.ReadTimeout:
 98            print(
 99                'The rendering server took too long to respond. Try visiting "' +
100                encoded +
101                '" in a browser.'
102            )
103        if result is not None and result.status_code == 200:
104            sys.stdout.buffer.write(result.content)
105    else:
106        print(uml)
def get_known_modules(directory: str, blacklist: list[str]) -> list[str]:
21def get_known_modules(directory: str, blacklist: list[str]) -> list[str]:
22    """
23    Gets a `list` of all modules in a directory and its subdirectories.
24
25    :param directory: The directory to process.
26    :param blacklist: A `list` of forbidden substrings.
27    :return: A `list` of known modules.
28    """
29    known_modules: list[str] = []
30    for root, _, files in os.walk(directory):
31        for file in files:
32            if '.py' in file:
33                file_path: str = os.path.join(root, file.split('.')[0])
34                if not any(item in file_path for item in blacklist):
35                    known_modules.append('.'.join([
36                        x for x in file_path[len(directory):].split(os.sep) if x
37                    ]))
38    return known_modules

Gets a list of all modules in a directory and its subdirectories.

Parameters
  • directory: The directory to process.
  • blacklist: A list of forbidden substrings.
Returns

A list of known modules.

def get_source_files( directory: str, known_modules: list[str], blacklist: list[str]) -> list[data.SourceFile.SourceFile]:
41def get_source_files(
42        directory: str,
43        known_modules: list[str],
44        blacklist: list[str]
45) -> list[SourceFile]:
46    """
47    Gets a `list` of all source files in a directory and its subdirectories.
48
49    :param directory: The directory to process.
50    :param known_modules: A `list` of known modules.
51    :param blacklist: A `list` of forbidden substrings.
52    :return: A `list` of source files.
53    """
54    source_files: list[SourceFile] = []
55    for root, _, files in os.walk(directory):
56        for file in files:
57            if '.py' in file:
58                file_path = os.path.join(root, file)
59                if not any(item in file_path for item in blacklist):
60                    try:
61                        source_files.append(
62                            get_module_info(directory, file_path, known_modules)
63                        )
64                    except astroid.AstroidBuildingException:
65                        pass
66    return source_files

Gets a list of all source files in a directory and its subdirectories.

Parameters
  • directory: The directory to process.
  • known_modules: A list of known modules.
  • blacklist: A list of forbidden substrings.
Returns

A list of source files.

def generate_uml(directory: str) -> str:
69def generate_uml(directory: str) -> str:
70    """
71    Generates a valid PlantUML string for all files in the given directory and its subdirectories.
72
73    :param directory: The directory to process.
74    :return: A valid PlantUML string.
75    """
76    blacklist: list[str] = ['venv', '/.', '__']
77    known_modules: list[str] = get_known_modules(directory, blacklist)
78    source_files: list[SourceFile] = get_source_files(directory, known_modules, blacklist)
79    return str(UMLFile(source_files))

Generates a valid PlantUML string for all files in the given directory and its subdirectories.

Parameters
  • directory: The directory to process.
Returns

A valid PlantUML string.