Skip to content

Config

dcmspec.config.Config

Manages application configuration.

Reserved configuration keys: - cache_dir: Cache Directory path used by the library. If not set by the user, OS-specific default is used.

Users may add their own keys, but should not overwrite reserved keys unless they intend to change library behavior.

Source code in src/dcmspec/config.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class Config:
    """Manages application configuration.

    Reserved configuration keys:
    - cache_dir: Cache Directory path used by the library. If not set by the user, OS-specific default is used.

    Users may add their own keys, but should not overwrite reserved keys unless they intend to change library behavior.
    """

    def __init__(self, app_name: str = "dcmspec", config_file: Optional[str] = None):
        """Initialize the Config object.

        Args:
            app_name: The application name used for determining default config/cache directories.
            config_file: Optional path to a specific config file. If not provided, a default location is used.

        """
        self.app_name: str = app_name
        self.config_file: str = config_file or os.path.join(user_config_dir(app_name), "config.json")

        # Check if config_file is a directory; if so, warn and fall back to default config
        if os.path.isdir(self.config_file):
            print(f"Warning: The config_file path '{self.config_file}' is a directory, not a file. Using default.")
            self._data: Dict[str, Any] = {"cache_dir": user_cache_dir(app_name)}
            return

        # Initialize config with OS-specific default value for cache directory
        self._data: Dict[str, Any] = {"cache_dir": user_cache_dir(app_name)}

        self.load_config()

    def load_config(self) -> None:
        """Load configuration from the config file if it exists.

        Creates the cache directory if it does not exist.
        """
        try:
            if os.path.exists(self.config_file):
                with open(self.config_file, "r", encoding="utf-8") as f:
                    config: Dict[str, Any] = json.load(f)
                    self._data.update(config)
        except (OSError, json.JSONDecodeError) as e:
            print(f"Failed to load configuration file {self.config_file}: {e}")

        cache_dir = self.get_param("cache_dir")
        try:
            os.makedirs(cache_dir, exist_ok=True)
        except FileExistsError:
            print(f"Error: The cache_dir path '{cache_dir}' exists and is not a directory.")
            return

        # Handle rare case where the path may not be a directory and, for any reason, os.makedirs did not fail.
        if not os.path.isdir(cache_dir):
            print(f"Error: The cache_dir path '{cache_dir}' is not a directory.")
            return

    def save_config(self) -> None:
        """Save the current configuration to the config file."""
        try:
            os.makedirs(os.path.dirname(self.config_file), exist_ok=True)
            with open(self.config_file, "w", encoding="utf-8") as f:
                json.dump(self._data, f, indent=4)
        except OSError as e:
            print(f"Failed to save configuration file {self.config_file}: {e}")

    def set_param(self, key: str, value: Any) -> None:
        """Set a configuration parameter."""
        self._data[key] = value

    def get_param(self, key: str) -> Optional[Any]:
        """Get a configuration parameter by key."""
        return self._data.get(key)

    @property
    def cache_dir(self) -> str:
        """Access the cache directory used by the library."""
        return self.get_param("cache_dir")

cache_dir property

Access the cache directory used by the library.

__init__(app_name='dcmspec', config_file=None)

Initialize the Config object.

PARAMETER DESCRIPTION
app_name

The application name used for determining default config/cache directories.

TYPE: str DEFAULT: 'dcmspec'

config_file

Optional path to a specific config file. If not provided, a default location is used.

TYPE: Optional[str] DEFAULT: None

Source code in src/dcmspec/config.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def __init__(self, app_name: str = "dcmspec", config_file: Optional[str] = None):
    """Initialize the Config object.

    Args:
        app_name: The application name used for determining default config/cache directories.
        config_file: Optional path to a specific config file. If not provided, a default location is used.

    """
    self.app_name: str = app_name
    self.config_file: str = config_file or os.path.join(user_config_dir(app_name), "config.json")

    # Check if config_file is a directory; if so, warn and fall back to default config
    if os.path.isdir(self.config_file):
        print(f"Warning: The config_file path '{self.config_file}' is a directory, not a file. Using default.")
        self._data: Dict[str, Any] = {"cache_dir": user_cache_dir(app_name)}
        return

    # Initialize config with OS-specific default value for cache directory
    self._data: Dict[str, Any] = {"cache_dir": user_cache_dir(app_name)}

    self.load_config()

get_param(key)

Get a configuration parameter by key.

Source code in src/dcmspec/config.py
82
83
84
def get_param(self, key: str) -> Optional[Any]:
    """Get a configuration parameter by key."""
    return self._data.get(key)

load_config()

Load configuration from the config file if it exists.

Creates the cache directory if it does not exist.

Source code in src/dcmspec/config.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def load_config(self) -> None:
    """Load configuration from the config file if it exists.

    Creates the cache directory if it does not exist.
    """
    try:
        if os.path.exists(self.config_file):
            with open(self.config_file, "r", encoding="utf-8") as f:
                config: Dict[str, Any] = json.load(f)
                self._data.update(config)
    except (OSError, json.JSONDecodeError) as e:
        print(f"Failed to load configuration file {self.config_file}: {e}")

    cache_dir = self.get_param("cache_dir")
    try:
        os.makedirs(cache_dir, exist_ok=True)
    except FileExistsError:
        print(f"Error: The cache_dir path '{cache_dir}' exists and is not a directory.")
        return

    # Handle rare case where the path may not be a directory and, for any reason, os.makedirs did not fail.
    if not os.path.isdir(cache_dir):
        print(f"Error: The cache_dir path '{cache_dir}' is not a directory.")
        return

save_config()

Save the current configuration to the config file.

Source code in src/dcmspec/config.py
69
70
71
72
73
74
75
76
def save_config(self) -> None:
    """Save the current configuration to the config file."""
    try:
        os.makedirs(os.path.dirname(self.config_file), exist_ok=True)
        with open(self.config_file, "w", encoding="utf-8") as f:
            json.dump(self._data, f, indent=4)
    except OSError as e:
        print(f"Failed to save configuration file {self.config_file}: {e}")

set_param(key, value)

Set a configuration parameter.

Source code in src/dcmspec/config.py
78
79
80
def set_param(self, key: str, value: Any) -> None:
    """Set a configuration parameter."""
    self._data[key] = value