Skip to content

Functions

create_omoospace(name, under='.', brief=None, contents_dir='Contents', subspaces_dir='Subspaces', language=None, readme=False, chinese_to_pinyin=False, reveal_in_explorer=False)

Create an omoospace.

Parameters:

Name Type Description Default
name str

Omoospace name

required
under str

Add omoospace to which folder. Defaults to '.'.

'.'
brief str

Omoospace brief. Defaults to None.

None
chinese_to_pinyin bool

Whether convert chinese to pinyin. Defaults to False.

False
reveal_in_explorer bool

Whether open folder after or not. Defaults to True.

False

Raises: ExistsError: Target path already exists. CreateFailed: Fail to create directories.

Returns:

Name Type Description
Omoospace Omoospace

New created omoospace.

Source code in src\omoospace\functions.py
def create_omoospace(
    name: str,
    under: str = ".",
    brief: str = None,
    contents_dir: str = "Contents",
    subspaces_dir: str = "Subspaces",
    language: Language = None,
    readme: bool = False,
    chinese_to_pinyin: bool = False,
    reveal_in_explorer: bool = False,
) -> Omoospace:
    """Create an omoospace.

    Args:
        name (str): Omoospace name
        under (str, optional): Add omoospace to which folder. Defaults to '.'.
        brief (str, optional): Omoospace brief. Defaults to None.
        chinese_to_pinyin (bool, optional): Whether convert chinese to pinyin. Defaults to False.
        reveal_in_explorer (bool, optional): Whether open folder after or not. Defaults to True.
    Raises:
        ExistsError: Target path already exists.
        CreateFailed: Fail to create directories.

    Returns:
        Omoospace: New created omoospace.
    """

    if language and language not in ALLOWED_LANGS:
        raise ValueError(f"{language} is not a valid language.")
    language = language or "en"

    dirname = normalize_name(name, chinese_to_pinyin=chinese_to_pinyin)
    root_dir = Opath(under, dirname).resolve()

    # Check if root_dir is in a omoospace
    try:
        Omoospace(root_dir)
        raise FileExistsError(f"{root_dir} already exists.")

    except FileNotFoundError:
        pass

    profile_file = f"Omoospace.{language}.yml" if language != "en" else "Omoospace.yml"
    contents_dir = contents_dir or "Contents"

    paths = [profile_file, f"{contents_dir}/"]

    if subspaces_dir:
        paths.append(f"{subspaces_dir}/")

    if readme:
        readme_content = f"""# {name}
{brief or ""}"""
        paths.append({"README.md": readme_content})

    make_path(
        *paths,
        under=root_dir,
    )

    if reveal_in_explorer:
        root_dir.reveal_in_explorer()

    omoospace = Omoospace(root_dir)
    omoospace.brief = brief or name

    if subspaces_dir:
        omoospace.subspaces_dir = subspaces_dir
    if contents_dir != "Contents":
        omoospace.contents_dir = contents_dir

    return omoospace

extract_objective(path)

Returns the objective.

Parameters:

Name Type Description Default
path AnyPath

The giving path.

required

Returns:

Name Type Description
Objective Objective

Objective.

Source code in src\omoospace\functions.py
def extract_objective(path: AnyPath) -> Objective:
    """Returns the objective.

    Args:
        path (AnyPath): The giving path.

    Returns:
        Objective: Objective.

    """

    try:
        return Subspace(path).objective
    except FileNotFoundError:
        return None
    except ValueError:
        return None

extract_pathname(path)

Returns the pathname of a path.

Parameters:

Name Type Description Default
path AnyPath

The giving path.

required

Returns:

Name Type Description
ObjectivePath str

Objective path.

Source code in src\omoospace\functions.py
def extract_pathname(path: AnyPath) -> str:
    """Returns the pathname of a path.

    Args:
        path (AnyPath): The giving path.

    Returns:
        ObjectivePath: Objective path.

    """

    try:
        return Subspace(path).pathname
    except FileNotFoundError:
        return None
    except ValueError:
        return ""