Skip to content

pandas_openscm.db.path_handling#

Functionality for handling paths

In order to make our databases portable, we need to be a bit smarter than just using raw paths.

Classes:

Name Description
DBPath

Database-related path

DBPath #

Database-related path

Carries the information required to write paths with certainty and keep the database portable.

Methods:

Name Description
from_abs_path_and_db_dir

Initialise from an absolute path and a database directory

rel_db_validator

Validate the value of rel_db

Attributes:

Name Type Description
abs Path

The absolute path for the file

rel_db Path

The path relative to the database's directory

Source code in src/pandas_openscm/db/path_handling.py
@define
class DBPath:
    """
    Database-related path

    Carries the information required to write paths with certainty
    and keep the database portable.
    """

    abs: Path
    """The absolute path for the file"""

    rel_db: Path = field()
    """The path relative to the database's directory"""

    @rel_db.validator  # ty: ignore[unresolved-attribute]
    def rel_db_validator(self, attribute: attr.Attribute[Any], value: Path) -> None:
        """
        Validate the value of `rel_db`

        Parameters
        ----------
        attribute
            Attribute being set

        value
            Value to use

        Raises
        ------
        AssertionError
            `value` is not within `self.abs`
        """
        if not str(self.abs).endswith(str(value)):
            msg = (
                f"{attribute.name} value, {value!r}, is not a sub-path of {self.abs=!r}"
            )
            raise AssertionError(msg)

    @classmethod
    def from_abs_path_and_db_dir(cls, abs: Path, db_dir: Path) -> DBPath:
        """
        Initialise from an absolute path and a database directory

        Parameters
        ----------
        abs
            Absolute path

        db_dir
            Database directory

        Returns
        -------
        :
            Initialised `DBPath`
        """
        return cls(abs=abs, rel_db=abs.relative_to(db_dir))

abs instance-attribute #

abs: Path

The absolute path for the file

rel_db class-attribute instance-attribute #

rel_db: Path = field()

The path relative to the database's directory

from_abs_path_and_db_dir classmethod #

from_abs_path_and_db_dir(abs: Path, db_dir: Path) -> DBPath

Initialise from an absolute path and a database directory

Parameters:

Name Type Description Default
abs Path

Absolute path

required
db_dir Path

Database directory

required

Returns:

Type Description
DBPath

Initialised DBPath

Source code in src/pandas_openscm/db/path_handling.py
@classmethod
def from_abs_path_and_db_dir(cls, abs: Path, db_dir: Path) -> DBPath:
    """
    Initialise from an absolute path and a database directory

    Parameters
    ----------
    abs
        Absolute path

    db_dir
        Database directory

    Returns
    -------
    :
        Initialised `DBPath`
    """
    return cls(abs=abs, rel_db=abs.relative_to(db_dir))

rel_db_validator #

rel_db_validator(
    attribute: Attribute[Any], value: Path
) -> None

Validate the value of rel_db

Parameters:

Name Type Description Default
attribute Attribute[Any]

Attribute being set

required
value Path

Value to use

required

Raises:

Type Description
AssertionError

value is not within self.abs

Source code in src/pandas_openscm/db/path_handling.py
@rel_db.validator  # ty: ignore[unresolved-attribute]
def rel_db_validator(self, attribute: attr.Attribute[Any], value: Path) -> None:
    """
    Validate the value of `rel_db`

    Parameters
    ----------
    attribute
        Attribute being set

    value
        Value to use

    Raises
    ------
    AssertionError
        `value` is not within `self.abs`
    """
    if not str(self.abs).endswith(str(value)):
        msg = (
            f"{attribute.name} value, {value!r}, is not a sub-path of {self.abs=!r}"
        )
        raise AssertionError(msg)