xref: /petsc/lib/petsc/bin/maint/petsclinter/petsclinter/classes/_path.py (revision 4c7cc9c8e01791debde927bc0816c9a347055c8f)
1#!/usr/bin/env python3
2"""
3# Created:
4# @author: Jacob Faibussowitsch
5"""
6from __future__ import annotations
7
8from .._typing import *
9
10import pathlib
11
12from .. import __version__
13
14# pathlib.Path() actually dynamically selects between pathlib.PosixPath and
15# pathlib.WindowsPath (but this is static on each machine). For this reason, we need to
16# use type(pathlib.Path()) to select the right type at runtime, but the type checkers
17# don't like it because it is -- seemingly -- dynamic. So we arbitrarily pick PosixPath
18# for type checkers.
19#
20# see https://stackoverflow.com/questions/29850801/subclass-pathlib-path-fails
21if TYPE_CHECKING:
22  _PathType = pathlib.PosixPath
23else:
24  _PathType = type(pathlib.Path())
25
26class Path(_PathType):
27  """
28  a basic pathlib.Path wrapper with some additional utility backported
29  """
30  def append_suffix(self, suffix: str) -> Path:
31    r"""Create a path with `suffix` appended, regardless of whether the current path has a suffix
32    or not.
33
34    Parameters
35    ----------
36    suffix:
37      the suffix to append
38
39    Returns
40    -------
41    path:
42      the path with the suffix
43    """
44    suffix     = str(suffix)
45    dotstring  = '' if suffix.startswith('.') else '.'
46    path: Path = self.with_suffix(f'{self.suffix}{dotstring}{suffix}')
47    return path
48
49  def append_name(self, name: str) -> Path:
50    r"""Create a path with `name` appended
51
52    Parameters
53    ----------
54    name:
55      the name to append
56
57    Returns
58    -------
59    path:
60      the path with the name
61    """
62    path: Path = self.with_name(f'{self.stem}{name}')
63    return path
64