🔍 Code Extractor

function load_onedrive_config

Maturity: 44

Loads OneDrive configuration from a JSON file and returns it as a dictionary, with error handling for missing or invalid files.

File:
/tf/active/vicechatdev/e-ink-llm/main.py
Lines:
136 - 144
Complexity:
simple

Purpose

This function provides a safe way to load OneDrive configuration settings from a JSON file. It handles cases where the file doesn't exist or cannot be parsed, returning an empty dictionary as a fallback. This is typically used to load authentication credentials, API endpoints, or other OneDrive-related settings needed for cloud integration.

Source Code

def load_onedrive_config(config_file: str = None) -> dict:
    """Load OneDrive configuration from file"""
    if config_file and Path(config_file).exists():
        try:
            with open(config_file, 'r') as f:
                return json.load(f)
        except Exception as e:
            print(f"⚠️  Warning: Could not load OneDrive config from {config_file}: {e}")
    return {}

Parameters

Name Type Default Kind
config_file str None positional_or_keyword

Parameter Details

config_file: Optional string path to the JSON configuration file containing OneDrive settings. If None or if the file doesn't exist, the function returns an empty dictionary. Expected to be a valid file path string pointing to a JSON file with OneDrive configuration parameters.

Return Value

Type: dict

Returns a dictionary containing the OneDrive configuration loaded from the JSON file. If the file doesn't exist, cannot be read, or contains invalid JSON, returns an empty dictionary {}. The structure of the returned dictionary depends on the contents of the configuration file.

Dependencies

  • json
  • pathlib

Required Imports

import json
from pathlib import Path

Usage Example

import json
from pathlib import Path

def load_onedrive_config(config_file: str = None) -> dict:
    """Load OneDrive configuration from file"""
    if config_file and Path(config_file).exists():
        try:
            with open(config_file, 'r') as f:
                return json.load(f)
        except Exception as e:
            print(f"⚠️  Warning: Could not load OneDrive config from {config_file}: {e}")
    return {}

# Example usage:
# Load config from a specific file
config = load_onedrive_config('onedrive_config.json')
print(config)

# Load with no file specified (returns empty dict)
config = load_onedrive_config()
print(config)  # Output: {}

# Load from non-existent file (returns empty dict with no error)
config = load_onedrive_config('nonexistent.json')
print(config)  # Output: {}

Best Practices

  • Always check if the returned dictionary is empty before using configuration values
  • The function silently handles errors and returns an empty dict, so implement additional validation if specific config keys are required
  • Consider logging or handling the warning message appropriately in production environments
  • Ensure the JSON file has proper read permissions before calling this function
  • The function uses Path.exists() which only checks file existence, not whether it's readable or valid JSON
  • This function is designed to be fault-tolerant, making it safe to call even with invalid or missing file paths

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function test_onedrive_auth 77.1% similar

    Asynchronous function that tests OneDrive authentication by loading configuration from a JSON file and attempting to obtain an access token.

    From: /tf/active/vicechatdev/e-ink-llm/test_mixed_mode.py
  • function load_remarkable_config 61.2% similar

    Loads reMarkable tablet configuration from a JSON file, returning an empty dictionary if the file doesn't exist or cannot be loaded.

    From: /tf/active/vicechatdev/e-ink-llm/main.py
  • class OneDriveClient 56.3% similar

    A comprehensive Microsoft OneDrive client that uses the Microsoft Graph API to authenticate and perform file operations (upload, download, list, delete) on OneDrive storage.

    From: /tf/active/vicechatdev/e-ink-llm/onedrive_client.py
  • function load_config_v1 56.2% similar

    Parses a .env file and loads key-value pairs into a dictionary, ignoring comments and handling errors gracefully.

    From: /tf/active/vicechatdev/SPFCsync/grant_sharepoint_access.py
  • function load_connection_config 55.2% similar

    Loads database connection configuration from a specified configuration file and returns a ConnectionConfig object.

    From: /tf/active/vicechatdev/full_smartstat/sql_query_generator.py
← Back to Browse