o |nh@sdZddlmZddlZddlZddlZddlmZddlmZm Z ddl m Z Gddde Z d&d'ddZd(ddZd)ddZd*ddZd*ddZd+d,d"d#Zd$ejvr^eejd$ZdSed%ZdS)-zCommon File System Utilities.) annotationsN)datetime)AnyOptional) TracebackTypec@sHeZdZUdZded<ded<ddd Zdd d Zd d ZdddZdS)Tmpa Provides creation and cleanup of a separate temporary directory, and file. Keyword Args: cleanup (bool): Whether or not to delete the temporary directory and file on exit (when used with the ``with`` operator). suffix (str): The suffix that the directory and file will end with. Default: *no suffix* prefix (str): The prefix that the directory and file will start with. Default: *no prefix* dir (str): The parent directory path that the temp directory and file will be created in. Default: *system default temporary path* Example: .. code-block:: python from cement.utils import fs with fs.Tmp() as tmp: # do something with a temporary directory os.path.listdir(tmp.dir) # do something with a temporary file with open(tmp.file, 'w') as f: f.write('some data') strdirfilekwargsreturnNonecKs^|dd|_|dd}|dd}|dd}tj|||d|_tj|||d\}|_dS) NcleanupTsuffixprefixtmpr )rrr )getrtempfilemkdtempr mkstempr )selfr rrr _rG/usr/local/bin/dhwp/env/lib/python3.10/site-packages/cement/utils/fs.py__init__0s   z Tmp.__init__cCsJ|jdur!tj|jrt|jtj|jr#t|jdSdSdS)zs Remove the temporary directory (and file) if it exists, and ``self.cleanup`` is ``True``. TN) rospathexistsr shutilrmtreer removerrrrr!=s  z Tmp.removecCs|SNrr"rrr __enter__Hsz Tmp.__enter___Tmp__exc_typetype[BaseException] | None_Tmp__exc_valueBaseException | None_Tmp__exc_tracebackTracebackType | NonecCs |dSr#)r!)rr%r'r)rrr__exit__Ks z Tmp.__exit__N)r rr r )r r )r%r&r'r(r)r*r r ) __name__ __module__ __qualname____doc____annotations__rr!r$r+rrrrrs    rTrrstrip_trailing_slashboolr cCstjtj|S)a Return an absolute path, while also expanding the ``~`` user directory shortcut. Args: path (str): The original path to expand. Returns: str: The fully expanded, absolute path to the given ``path`` Example: .. code-block:: python from cement.utils import fs fs.abspath('~/some/path') fs.abspath('./some.file') )rrabspath expanduser)rr1rrrr3Rsr3argsr rcOs0t|}t|d}tjj|g|Ri|S)a Return a complete, joined path, by first calling ``abspath()`` on the first item to ensure the final path is complete. Args: paths (list): A list of paths to join together. Returns: list: The complete and absolute joined path. Example: .. code-block:: python from cement.utils import fs fs.join('~/some/path', 'some/other/relevant/paht') r)listr3poprrjoin)r5r paths first_pathrrrr8ksr8r9tuple[str, bool]cGst|}|tj|fS)a Wrapper around ``os.path.join()``, ``os.path.abspath()``, and ``os.path.exists()``. Args: paths (list): List of paths to join, and then return ``True`` if that path exists, or ``False`` if it does not. Returns: tuple: First item is the fully joined absolute path, and the second is ``bool`` (whether that path exists or not). )r8rrr)r9rrrr join_existss r<r cCsNt|}tj|rtj|std|dtj|s%t|dSdS)a Ensure the directory ``path`` exists, and if not create it. Args: path (str): The filesystem path of a directory. Raises: AssertionError: If the directory ``path`` exists, but is not a directory. Returns: None zPath `z ` exists but is not a directory!N)r3rrrisdirAssertionErrormakedirs)rrrrensure_dir_existss  r@cCstjt|}t|S)z Ensure the parent directory of ``path`` (file, or directory) exists, and if not create it. Args: path (str): The filesystem path of a file or directory. Returns: None )rrdirnamer3r@)r parent_dirrrrensure_parent_dir_existss rC.bakr Optional[str]cKsd}d}t|}|dddur$|dd}t|}d||f} tj|rh|dkr6||}n ||d |}tj|rJ|d 7}q$tj |rYt || |Stj |ret || |S |S) a Rename a file or directory safely without overwriting an existing backup of the same name. Args: path (str): The path to the file or directory to make a backup of. suffix (str): The suffix to rename files with. Keyword Args: timestamp(bool): whether to add a timestamp to the backup suffix timestamp_format(str): Date-Time format in python datetime.datetime notation. default '%Y-%m-%d-%H:%M:%S' Returns: str: The new path of backed up file/directory Example: .. code-block:: python from cement.utils import fs fs.backup('/path/to/original/file') N timestampFTtimestamp_formatz%Y-%m-%d-%H:%M:%S-.)r3rrnowstrftimer8rrrisfilercopyr=copytree)rrr countnew_pathrHrGrrrbackups2      rSHOME~)T)rrr1r2r r)r5rr rr r)r9rr r;)rrr r )rD)rrrrr rr rE)r/ __future__rrrrrtypingrrtypesrobjectrr3r8r<r@rCrSenvironHOME_DIRrrrrs$    D      8