# -*- coding: utf-8 -*-## This file is subject to the terms and conditions defined in# file 'LICENSE.txt', which is part of this source code package.#fromtypingimportOptional,TypefromtypesimportTracebackTypeimportosfromMuscat.Helpers.IO.TemporaryDirectoryimportTemporaryDirectory
[docs]classTemporalChdir():""" class to change directory for a local context. This function use os.chdir to change the current path. The user is responsible of ensuring the path exists and il has the correct permissions At exit the install will restore the path at the creation of the instance. with TemporalChdir("c:/my/local/working/path") : #do something """def__init__(self,targetPath:str)->None:super().__init__()self.originalPath=os.getcwd()+os.sepself.targetPath=targetPathdef__enter__(self)->None:"""Context manager enter function """os.chdir(self.targetPath)def__exit__(self,exc_type:Optional[Type[BaseException]],exc:Optional[BaseException],traceback:Optional[TracebackType])->None:"""Context manager exit function Parameters ---------- exc_type : Optional[Type[BaseException]] _description_ exc : Optional[BaseException] _description_ traceback : Optional[TracebackType] _description_ """os.chdir(self.originalPath)
[docs]classPathController():""" Class to do operation on path and filename. The idea is to store 2 path (for an application). This are class attributes (not instance attributes). the currentDirectory, defined initially with value of os.getcwd()+os.sep. intended to store the path from where the user execute the application the workingDirectory, defined initially with value of os.getcwd()+os.sep. intended to store the path of the treated file, The user can set the currentDirectory with SetCurrentDirectory or SetCurrentDirectoryUsingFile The user can set the workingDirectory with SetWorkingDirectory or SetWorkingDirectoryUsingFile Then different function are available to recover path, and full path filename on current/working directories. .. note:: This class DOES NOT change the working directory of python( value returned by os.getcwd()), this class is intender only for path manipulation. """currentDirectory:str=os.getcwd()+os.sepworkingDirectory:str=os.getcwd()+os.sep
[docs]@staticmethoddefSetCurrentDirectory(folder:str)->None:"""Set the current directory using a path. Parameters ---------- folder : str path to a folder """PathController.currentDirectory=os.path.abspath(os.path.expanduser(folder))+os.sep
[docs]@staticmethoddefSetCurrentDirectoryUsingFile(folder:str)->None:"""set the current directory using a file os.path Parameters ---------- folder : str path to a file """PathController.currentDirectory=os.path.dirname(os.path.abspath(os.path.expanduser(folder)))+os.sep
[docs]@staticmethoddefSetWorkingDirectory(folder:str)->None:"""Set the working directory using a path. Parameters ---------- folder : str path to a folder """PathController.workingDirectory=os.path.abspath(os.path.expanduser(folder))+os.sep
[docs]@staticmethoddefSetWorkingDirectoryUsingFile(file)->None:"""set the working directory using a file path Parameters ---------- folder : str path to a file """PathController.workingDirectory=os.path.abspath(os.path.dirname(os.path.expanduser(file)))+os.sep
[docs]@staticmethoddefGetCurrentDirectory()->str:"""Return the current directory"""returnPathController.currentDirectory
[docs]@staticmethoddefGetWorkingDirectory()->str:"""Return the working directory"""returnPathController.workingDirectory
[docs]@staticmethoddefGetFullFilenameCurrentDirectory(filename:str)->str:"""Return a absolution path of a filename in the currentDirectory Parameters ---------- filename : str Path to a file relative to Current Directory Returns ------- str the absolute path of filename """ifos.path.isabs(filename):returnfilenameelse:returnos.path.abspath(PathController.currentDirectory+filename)
[docs]@staticmethoddefGetFullFilenameWorkingDirectory(filename:str)->str:"""Return a absolution path of a filename in the workingDirectory Parameters ---------- filename : str Path to a file relative to Working Directory Returns ------- str the absolute path of filename """ifos.path.isabs(filename):returnfilenameelse:returnos.path.abspath(PathController.workingDirectory+filename)
[docs]@staticmethoddefGetFullPathCurrentDirectoryUsingFile(filename:str)->str:"""Return the absolute path of a filename relative to the current directory If current directory is "/my/current/directory", and filename is "./test1/output.toto" then: GetFullPathInCurrentDirectoryUsingFile(filename) -> "/my/current/directory/test1" if filename is an absolute path then this function return path Parameters ---------- filename : str filename with a relative or absolute path Returns ------- str an absolute path """file=PathController.GetFullFilenameCurrentDirectory(filename)returnos.path.abspath(os.path.dirname(os.path.expanduser(file)))+os.sep
[docs]@staticmethoddefGetFullPathWorkingDirectory(pathname:str)->str:"""Return the absolute path of a path relative to the working directory if pathname is an absolute path then this function return path Parameters ---------- pathname : str relative (to working directory) or absolute path Returns ------- str an absolute path """ifos.path.isabs(pathname):returnpathnameelse:returnos.path.abspath(PathController.workingDirectory+pathname)+os.sep