function load_onedrive_config
Loads OneDrive configuration from a JSON file and returns it as a dictionary, with error handling for missing or invalid files.
/tf/active/vicechatdev/e-ink-llm/main.py
136 - 144
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
jsonpathlib
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_onedrive_auth 77.1% similar
-
function load_remarkable_config 61.2% similar
-
class OneDriveClient 56.3% similar
-
function load_config_v1 56.2% similar
-
function load_connection_config 55.2% similar