Skip to content

extension

extend_project

extend_project(project, cas_lib_dir='cas_lib')

Extend project to add cas_lib attribute to Tiers.

Source code in cassini/ext/cassini_lib/extension.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def extend_project(project: Project, cas_lib_dir: Union[str, Path] = "cas_lib"):
    """
    Extend project to add `cas_lib` attribute to Tiers.
    """
    cas_lib_dir = project.project_folder / cas_lib_dir

    def make_cas_lib_folder(project, cas_lib_dir=cas_lib_dir):
        if not cas_lib_dir.exists():
            cas_lib_dir.mkdir()
            (cas_lib_dir / "0.1.0").mkdir()

    project.__before_setup_files__.append(
        make_cas_lib_folder
    )  # ensures will run even if project already setup.

    for Tier in project.hierarchy:
        if issubclass(Tier, NotebookTierBase):
            # patch in the requried attributes to classes with notebooks!
            Tier.cas_lib_version = MetaAttr(  # type: ignore[attr-defined]
                json_type=str,
                attr_type=Version,
                post_get=lambda v: Version(v) if v else v,
                pre_set=str,
                name="cas_lib_version",
                cas_field="private",
            )

            Tier.cas_lib = create_cas_lib(cas_lib_dir)  # type: ignore[attr-defined]
    return project

create_cas_lib

create_cas_lib(cas_lib_dir)

Create cas_lib attribute for a given cas_lib_dir.

Source code in cassini/ext/cassini_lib/extension.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def create_cas_lib(cas_lib_dir: Path):
    """
    Create `cas_lib` attribute for a given `cas_lib_dir`.
    """

    def cas_lib(self, version=None):
        """
        Allow importing from the appropraite `cas_lib` subdirectory for this tier.

        If the version not provided or `tier.cas_lib_version` not set, is set to the
        latest version found in `cas_lib_dir`, which is then stored in the tier's meta.

        As `cas_lib_version` is persistent after being set the first time, this will pin this version,
        unless either an explicit `version` parameter is provided, or `tier.cas_lib_version` is updated.
        """
        if version is None:
            if self.cas_lib_version:
                version = self.cas_lib_version
            else:
                version = self.cas_lib_version = latest_version(cas_lib_dir)
                print(f"Set {self}.cas_lib_version = {version}")

        if version == "lastest":
            version = latest_version(cas_lib_dir)

        if isinstance(version, str):
            version = Version(version)

        return PatchImporter(version, cas_lib_dir)

    return cas_lib