############################################################################################################################################################################################################################# INFO ############################################################################################################################################################################################################################# 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 <element_name>"""This module provides utility functions for the PyRat library.It includes mainly a function to create a workspace, which is meant to be called just at the beginning of a PyRat project."""########################################################################################################################################################################################################################### IMPORTS ############################################################################################################################################################################################################################ External importsimportpyfakefs.fake_filesystem_unittestimportosimportshutilimportpathlibimportsysimportpyfakefsimportsiteimportsysconfig########################################################################################################################################################################################################################## FUNCTIONS ##########################################################################################################################################################################################################################
[docs]definit_workspace(target_directory:str="pyrat_workspace")->None:""" Creates all the directories for a clean student workspace. Also creates a few default programs to start with. This function also takes care of adding the workspace to the Python path so that it can be used directly. If the workspace already exists, it is not modified but we setup the Python path anyway to use it. Args: target_directory: The directory in which to create the workspace. """# Debugassertisinstance(target_directory,str),"Argument 'target_directory' must be a string"assertis_valid_directory(target_directory),"Workspace directory cannot be created"# Copy the template workspace into the target directory if not already existingsource_workspace=os.path.join(os.path.dirname(os.path.realpath(__file__)),"..","workspace")target_workspace=os.path.abspath(target_directory)ifnotos.path.exists(target_workspace):shutil.copytree(source_workspace,target_workspace,ignore=shutil.ignore_patterns('__pycache__'))print(f"Workspace created in {target_workspace}",file=sys.stderr)# Add the workspace to pathsite_packages=sysconfig.get_paths()["purelib"]pth_file=os.path.join(site_packages,"pyrat_workspace_path.pth")withopen(pth_file,"w")asf:f.write(target_workspace+"\n")site.addsitedir(site_packages)print(f"Workspace added to Python path",file=sys.stderr)# Confirmationprint(f"Your workspace is ready! You can now start coding your players and run games.",file=sys.stderr)
[docs]defis_valid_directory(directory:str)->bool:""" Checks if a directory exists or can be created, without actually creating it. Args: directory: The directory to check. Returns: ``True`` if the directory can be created, ``False`` otherwise. """# Debugassertisinstance(directory,str),"Argument 'directory' must be a string"# Initialize the fake filesystemvalid=Falsewithpyfakefs.fake_filesystem_unittest.Patcher()aspatcher:fs=patcher.fsdirectory_path=pathlib.Path(directory)# Try to create the directory in the fake filesystemtry:fs.makedirs(directory_path,exist_ok=True)valid=Trueexcept:pass# Donereturnvalid