pyrat.src.utils

This file is part of the PyRat library. It is meant to be used as a library, and not to be executed directly.

Please import necessary elements using the following syntax:

from pyrat import

  1#####################################################################################################################################################
  2######################################################################## INFO #######################################################################
  3#####################################################################################################################################################
  4
  5"""
  6    This file is part of the PyRat library.
  7    It is meant to be used as a library, and not to be executed directly.
  8    Please import necessary elements using the following syntax:
  9        from pyrat import <element_name>
 10"""
 11
 12#####################################################################################################################################################
 13###################################################################### IMPORTS ######################################################################
 14#####################################################################################################################################################
 15
 16# External imports
 17from typing import *
 18from typing_extensions import *
 19from numbers import *
 20import os
 21import shutil
 22import inspect
 23import pdoc
 24import pathlib
 25import sys
 26
 27#####################################################################################################################################################
 28##################################################################### FUNCTIONS #####################################################################
 29#####################################################################################################################################################
 30
 31def create_workspace ( target_directory: str
 32                     ) ->                None:
 33
 34    """
 35        Creates all the directories for a clean student workspace.
 36        Also creates a few default programs to start with.
 37        In:
 38            * target_directory: The directory in which to create the workspace.
 39        Out:
 40            * None.
 41    """
 42
 43    # Debug
 44    assert isinstance(target_directory, str) # Type check for target_directory
 45
 46    # Copy the template workspace into the current directory if not already existing
 47    source_workspace = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "workspace")
 48    target_workspace = os.path.join(target_directory, "pyrat_workspace")
 49    shutil.copytree(source_workspace, target_workspace, ignore=shutil.ignore_patterns('__pycache__'))
 50
 51#####################################################################################################################################################
 52
 53def generate_documentation ( workspace_directory: str
 54                           ) ->                   None:
 55
 56    """
 57        Generates the documentation for the project.
 58        The function will parse the PyRat library, and all the subdirectories of the workspace directory.
 59        This will create a doc directory in the workspace directory, and fill it with the documentation.
 60        In:
 61            * workspace_directory: The directory in which the workspace is located.
 62        Out:
 63            * None.
 64    """
 65    
 66    # Debug
 67    assert isinstance(workspace_directory, str) # Type check for workspace_directory
 68
 69    # Process paths
 70    target_directory = pathlib.Path(os.path.join(workspace_directory, "doc"))
 71    workspace_subdirectories = [os.path.join(workspace_directory, directory) for directory in os.listdir(workspace_directory) if directory != "doc"]
 72    for d in workspace_subdirectories:
 73        sys.path.append(d)
 74    
 75    # Generate the documentation for PyRat, and for workspace subdirectories
 76    pdoc.render.configure(docformat="google")
 77    pdoc.pdoc("pyrat", *workspace_subdirectories, output_directory=target_directory)
 78
 79#####################################################################################################################################################
 80
 81def caller_file () -> str:
 82
 83    """
 84        Returns the name of the file from which the caller of this function was called.
 85        In:
 86            * None.
 87        Out:
 88            * caller: The name of the file from which the caller of this function was called.
 89    """
 90
 91    # Check stack to get the name
 92    caller = inspect.currentframe().f_back.f_back.f_code.co_filename
 93    return caller
 94
 95#####################################################################################################################################################
 96
 97def pyrat_files () -> List[str]:
 98
 99    """
100        Returns the list of all the files in the PyRat library.
101        In:
102            * None.
103        Out:
104            * files: The list of all the files in the PyRat library.
105    """
106
107    # Get the list of all the files in the PyRat library
108    pyrat_path = os.path.dirname(os.path.realpath(__file__))
109    files = [os.path.join(pyrat_path, file) for file in os.listdir(pyrat_path) if file.endswith(".py")]
110    return files
111
112#####################################################################################################################################################
113#####################################################################################################################################################
def create_workspace(target_directory: str) -> None:
32def create_workspace ( target_directory: str
33                     ) ->                None:
34
35    """
36        Creates all the directories for a clean student workspace.
37        Also creates a few default programs to start with.
38        In:
39            * target_directory: The directory in which to create the workspace.
40        Out:
41            * None.
42    """
43
44    # Debug
45    assert isinstance(target_directory, str) # Type check for target_directory
46
47    # Copy the template workspace into the current directory if not already existing
48    source_workspace = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "workspace")
49    target_workspace = os.path.join(target_directory, "pyrat_workspace")
50    shutil.copytree(source_workspace, target_workspace, ignore=shutil.ignore_patterns('__pycache__'))

Creates all the directories for a clean student workspace. Also creates a few default programs to start with.

In:
  • target_directory: The directory in which to create the workspace.
Out:
  • None.
def generate_documentation(workspace_directory: str) -> None:
54def generate_documentation ( workspace_directory: str
55                           ) ->                   None:
56
57    """
58        Generates the documentation for the project.
59        The function will parse the PyRat library, and all the subdirectories of the workspace directory.
60        This will create a doc directory in the workspace directory, and fill it with the documentation.
61        In:
62            * workspace_directory: The directory in which the workspace is located.
63        Out:
64            * None.
65    """
66    
67    # Debug
68    assert isinstance(workspace_directory, str) # Type check for workspace_directory
69
70    # Process paths
71    target_directory = pathlib.Path(os.path.join(workspace_directory, "doc"))
72    workspace_subdirectories = [os.path.join(workspace_directory, directory) for directory in os.listdir(workspace_directory) if directory != "doc"]
73    for d in workspace_subdirectories:
74        sys.path.append(d)
75    
76    # Generate the documentation for PyRat, and for workspace subdirectories
77    pdoc.render.configure(docformat="google")
78    pdoc.pdoc("pyrat", *workspace_subdirectories, output_directory=target_directory)

Generates the documentation for the project. The function will parse the PyRat library, and all the subdirectories of the workspace directory. This will create a doc directory in the workspace directory, and fill it with the documentation.

In:
  • workspace_directory: The directory in which the workspace is located.
Out:
  • None.
def caller_file() -> str:
82def caller_file () -> str:
83
84    """
85        Returns the name of the file from which the caller of this function was called.
86        In:
87            * None.
88        Out:
89            * caller: The name of the file from which the caller of this function was called.
90    """
91
92    # Check stack to get the name
93    caller = inspect.currentframe().f_back.f_back.f_code.co_filename
94    return caller

Returns the name of the file from which the caller of this function was called.

In:
  • None.
Out:
  • caller: The name of the file from which the caller of this function was called.
def pyrat_files() -> List[str]:
 98def pyrat_files () -> List[str]:
 99
100    """
101        Returns the list of all the files in the PyRat library.
102        In:
103            * None.
104        Out:
105            * files: The list of all the files in the PyRat library.
106    """
107
108    # Get the list of all the files in the PyRat library
109    pyrat_path = os.path.dirname(os.path.realpath(__file__))
110    files = [os.path.join(pyrat_path, file) for file in os.listdir(pyrat_path) if file.endswith(".py")]
111    return files

Returns the list of all the files in the PyRat library.

In:
  • None.
Out:
  • files: The list of all the files in the PyRat library.