CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-jupyter-core

Core foundational package providing base application classes, path utilities, and configuration management for the Jupyter ecosystem.

Pending
Overview
Eval results
Files

path-management.mddocs/

Path Management

Cross-platform directory discovery and path utilities for managing Jupyter configuration, data, and runtime directories. Provides standardized access to user-level, environment-level, and system-wide Jupyter installations with full support for virtual environments and platform-specific conventions.

Capabilities

Core Directory Functions

Primary functions for discovering standard Jupyter directories across different platforms and installation contexts.

def jupyter_config_dir() -> str:
    """Get the Jupyter config directory for this platform and user.
    
    Returns JUPYTER_CONFIG_DIR if defined, otherwise the appropriate
    directory for the platform.
    
    Returns:
        str: Path to Jupyter config directory
    """

def jupyter_data_dir() -> str:
    """Get the config directory for Jupyter data files for this platform and user.
    
    These are non-transient, non-configuration files.
    
    Returns JUPYTER_DATA_DIR if defined, else a platform-appropriate path.
    
    Returns:
        str: Path to Jupyter data directory
    """

def jupyter_runtime_dir() -> str:
    """Return the runtime dir for transient jupyter files.
    
    Returns JUPYTER_RUNTIME_DIR if defined.
    The default is now (data_dir)/runtime on all platforms.
    
    Returns:
        str: Path to Jupyter runtime directory
    """

Usage Example:

from jupyter_core.paths import (
    jupyter_config_dir, jupyter_data_dir, jupyter_runtime_dir
)

# Get standard Jupyter directories
config_dir = jupyter_config_dir()    # ~/.jupyter
data_dir = jupyter_data_dir()        # ~/.local/share/jupyter  
runtime_dir = jupyter_runtime_dir()  # ~/.local/share/jupyter/runtime

print(f"Config: {config_dir}")
print(f"Data: {data_dir}")
print(f"Runtime: {runtime_dir}")

Search Path Functions

Functions for discovering search paths that include user, environment, and system-wide locations for Jupyter files and configuration.

def jupyter_path(*subdirs: str) -> list[str]:
    """Return a list of directories to search for data files.
    
    There are four sources of paths to search:
    - $JUPYTER_PATH environment variable (always highest priority)
    - user directories (e.g. ~/.local/share/jupyter)
    - environment directories (e.g. {sys.prefix}/share/jupyter)
    - system-wide paths (e.g. /usr/local/share/jupyter)
    
    Args:
        *subdirs: Optional subdirectory path components to append
        
    Returns:
        list[str]: Ordered list of directories to search
    """

def jupyter_config_path() -> list[str]:
    """Return the search path for Jupyter config files as a list.
    
    Searches user, environment, and system config directories in priority order.
    
    Returns:
        list[str]: Ordered list of config directories to search
    """

Usage Example:

from jupyter_core.paths import jupyter_path, jupyter_config_path

# Get search paths for data files
data_paths = jupyter_path()
print("Data search paths:", data_paths)

# Get search paths for kernel specifications
kernel_paths = jupyter_path("kernels")
print("Kernel search paths:", kernel_paths)

# Get search paths for config files
config_paths = jupyter_config_path()
print("Config search paths:", config_paths)

Path Configuration Functions

Functions for determining path behavior based on environment variables and system configuration.

def use_platform_dirs() -> bool:
    """Determine if platformdirs should be used for system-specific paths.
    
    Returns the value of JUPYTER_PLATFORM_DIRS environment variable.
    
    Returns:
        bool: True if platformdirs should be used
    """

def prefer_environment_over_user() -> bool:
    """Determine if environment-level paths should take precedence over user-level paths.
    
    Returns True if:
    - JUPYTER_PREFER_ENV_PATH is set to a truthy value, OR
    - We detect a Python virtual environment and the user owns sys.prefix, OR
    - We detect a conda environment (not base) and the user owns sys.prefix
    
    Returns:
        bool: True if environment paths should be preferred
    """

def envset(name: str, default: bool | None = False) -> bool | None:
    """Return the boolean value of a given environment variable.
    
    An environment variable is considered set if it is assigned to a value
    other than 'no', 'n', 'false', 'off', '0', or '0.0' (case insensitive).
    
    Args:
        name: Environment variable name
        default: Default value if not defined
        
    Returns:
        bool | None: Boolean value of environment variable
    """

File Utility Functions

Utilities for file and directory operations with cross-platform compatibility and security considerations.

def get_home_dir() -> str:
    """Get the real path of the home directory
    
    Returns:
        str: Resolved home directory path
    """

def exists(path: str) -> bool:
    """Replacement for `os.path.exists` which works for host mapped volumes
    on Windows containers
    
    Args:
        path: Path to check
        
    Returns:
        bool: True if path exists
    """

def is_hidden(abs_path: str, abs_root: str = "") -> bool:
    """Is a file hidden or contained in a hidden directory?
    
    Checks if a path is hidden based on name (starts with '.') or 
    platform-specific hidden flags.
    
    Args:
        abs_path: Absolute path to check
        abs_root: Root directory for relative hidden checks
        
    Returns:
        bool: True if file/directory is hidden
    """

File Security Functions

Functions for creating files with appropriate security permissions, especially important for configuration and credential files.

def secure_write(fname: str, binary: bool = False):
    """Context manager that opens a file with restricted permissions (0o0600).
    
    Creates files that are readable/writable only by the owner.
    On Windows, uses platform-specific security restrictions.
    
    Args:
        fname: Path to file to create
        binary: Whether to open in binary mode
        
    Yields:
        file: Opened file handle with secure permissions
    """

def win32_restrict_file_to_user(fname: str) -> None:
    """Secure a windows file to read-only access for the user.
    
    Uses Windows security APIs to restrict file access to current user
    and administrators only.
    
    Args:
        fname: Path to file to secure
    """

def get_file_mode(fname: str) -> int:
    """Retrieves the file mode in a filesystem-tolerant manner.
    
    Some filesystems auto-enable execute bits, so this function
    masks out problematic permission bits for validation.
    
    Args:
        fname: Path to file
        
    Returns:
        int: Masked file mode suitable for permission checking
    """

def issue_insecure_write_warning() -> None:
    """Issue an insecure write warning when JUPYTER_ALLOW_INSECURE_WRITES is set."""

Usage Example:

from jupyter_core.paths import secure_write

# Create a config file with secure permissions
config_data = {"key": "secret_value"}

with secure_write("/path/to/config.json") as f:
    import json
    json.dump(config_data, f)
    
# File is created with 0o0600 permissions (user read/write only)

Platform Constants

Platform-specific constants and paths used throughout the Jupyter ecosystem.

# Application name for directory paths ("Jupyter" on Windows/macOS, "jupyter" on Linux) 
APPNAME: str

# System-wide Jupyter data paths
SYSTEM_JUPYTER_PATH: list[str]

# Environment-level Jupyter data paths  
ENV_JUPYTER_PATH: list[str]

# System-wide Jupyter config paths
SYSTEM_CONFIG_PATH: list[str]

# Environment-level Jupyter config paths
ENV_CONFIG_PATH: list[str]

# Global flag for allowing insecure writes
allow_insecure_writes: bool

# BSD hidden file flag constant  
UF_HIDDEN: int  # Value: 32768, used for BSD hidden file detection

Environment Variables

The path management system respects numerous environment variables for customization:

Directory Override Variables

  • JUPYTER_CONFIG_DIR: Override the default config directory
  • JUPYTER_DATA_DIR: Override the default data directory
  • JUPYTER_RUNTIME_DIR: Override the default runtime directory

Search Path Variables

  • JUPYTER_PATH: Prepend additional directories to data search path
  • JUPYTER_CONFIG_PATH: Prepend additional directories to config search path

Behavior Control Variables

  • JUPYTER_NO_CONFIG: Disable config file loading entirely
  • JUPYTER_PLATFORM_DIRS: Use platformdirs for directory discovery
  • JUPYTER_PREFER_ENV_PATH: Prefer environment over user directories
  • JUPYTER_USE_PROGRAMDATA: Use PROGRAMDATA directory on Windows
  • JUPYTER_ALLOW_INSECURE_WRITES: Allow insecure file permissions

System Integration Variables

  • IPYTHONDIR: IPython directory location (for migration)
  • CONDA_PREFIX: Conda environment detection
  • CONDA_DEFAULT_ENV: Conda environment name detection

Platform-Specific Behavior

Windows

  • Uses %APPDATA%\jupyter for config by default
  • Uses %APPDATA%\jupyter for data by default
  • Supports PROGRAMDATA for system-wide installations (opt-in)
  • Implements Windows-specific file security via ACLs
  • Handles executable resolution differences

macOS

  • Uses ~/Library/Jupyter for data directory
  • Uses ~/.jupyter for config directory
  • Detects Homebrew installations for appropriate naming
  • Uses BSD hidden file flags

Linux/Unix

  • Follows XDG Base Directory specification when JUPYTER_PLATFORM_DIRS=1
  • Uses ~/.local/share/jupyter for data
  • Uses ~/.jupyter for config
  • Uses ~/.local/share/jupyter/runtime for runtime files
  • Supports standard Unix file permissions and hidden files

Virtual Environment Support

Automatic detection and appropriate handling of:

  • Python venv: Detects via sys.prefix != sys.base_prefix
  • Conda environments: Detects via CONDA_PREFIX environment variable
  • Custom environments: Respects JUPYTER_PREFER_ENV_PATH override

When in a virtual environment, environment-level paths take precedence over user-level paths by default, ensuring isolated package installations work correctly.

Install with Tessl CLI

npx tessl i tessl/pypi-jupyter-core

docs

application-framework.md

command-line-tools.md

index.md

path-management.md

utility-functions.md

tile.json