File: set_working_directory.py

package info (click to toggle)
python-cogent 2024.5.7a1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 74,600 kB
  • sloc: python: 92,479; makefile: 117; sh: 16
file content (44 lines) | stat: -rw-r--r-- 1,108 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
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
41
42
43
44
"""sets as working directory the parent of data/

Assumes this file is distributed inside COGENT3_ROOT/doc directory"""

import os
import pathlib


current = pathlib.Path(__file__).absolute().parent


def _data_path(path: os.PathLike) -> os.PathLike:
    if path.name != "data":
        path = _data_path(path.parent)
    return path


def get_data_dir():
    """returns path to cogent3 doc data directory"""
    for path in current.glob("*/*"):
        if "doc" not in path.parts:
            continue

        if "data" in path.parts:
            if path.parts.index("doc") > path.parts.index("doc"):
                continue

            path = _data_path(path)

        if path.is_dir() and str(path.name) == "data":
            return path.parent

    raise RuntimeError(f"could not find data dir from {current}")


def get_thumbnail_dir():
    """returns path to directory for writing html thumbnail images"""
    thumbdir = pathlib.Path(__file__).parent / "_build" / "html" / "_images"
    thumbdir.mkdir(exist_ok=True, parents=True)
    return thumbdir


data_dir = get_data_dir()
os.chdir(data_dir)