cmdkit.platform#
Sensible default behavior for applications with platform-specific file paths.
Applications have a system, user, and local path. With respect to configuration files
this ties in with the ideas implemented by Configuration. We want to
allow for a cascade of files and environment variables. In addition to the site of configuration,
applications have standard locations for any data they may need or create as well as a place
for log files, telemetry, or tracebacks.
The AppContext data class holds these conventional paths and other
context. The default() constructor builds these paths following
platform-specific conventions.
Platform-Specific Paths#
Assuming the following basic initialization, we initialize these file paths.
context = AppContext.default('MyApp', create_dirs=True, config_format='toml')
Windows#
System
context.path.system.lib:%PROGRAMDATA%\MyApp\Librarycontext.path.system.log:%PROGRAMDATA%\MyApp\Logscontext.path.system.config:%PROGRAMDATA%\MyApp\Config.toml
User
context.path.user.lib:%APPDATA%\MyApp\Librarycontext.path.user.log:%APPDATA%\MyApp\Logscontext.path.user.config:%APPDATA%\MyApp\Config.toml
Local
context.path.local.lib:%MYAPP_SITE%\Librarycontext.path.local.log:%MYAPP_SITE%\Logscontext.path.local.config:%MYAPP_SITE%\Config.toml
macOS#
System
context.path.system.lib:/Library/MyAppcontext.path.system.log:/Library/MyApp/Logscontext.path.system.config:/Library/Preferences/MyApp/config.toml
User
context.path.user.lib:$HOME/Library/MyAppcontext.path.user.log:$HOME/Library/MyApp/Logscontext.path.user.config:$HOME/Library/Preferences/MyApp/config.toml
Local
context.path.local.lib:$MYAPP_SITE/Librarycontext.path.local.log:$MYAPP_SITE/Logscontext.path.local.config:$MYAPP_SITE/config.toml
Linux / POSIX#
System
context.path.system.lib:/var/lib/myappcontext.path.system.log:/var/log/myappcontext.path.system.config:/etc/myapp.toml
User
context.path.user.lib:$HOME/.myapp/libcontext.path.user.log:$HOME/.myapp/logcontext.path.user.config:$HOME/.myapp/config.toml
Local
context.path.local.lib:$MYAPP_SITE\libcontext.path.local.log:$MYAPP_SITE\logcontext.path.local.config:$MYAPP_SITE\config.toml
If the environment variable MYAPP_SITE is not defined, the local site defaults to
the .myapp in the current working directory.
Reference#
- class AppContext(cwd: str, home: str, name: str, is_admin: bool, default_site: str, default_path: Namespace, path: Namespace)[source]#
Runtime application context.
Defines filesystem paths for system, user, and local site. Depends on platform and whether runtime environment is admin or user space. The ‘default’ path mapping references the one identified by ‘default_site’.
- classmethod default(name: str, create_dirs: bool = True, config_format: str = 'toml') AppContext[source]#
Define default context with platform-specific paths.
If
create_dirs=True, we create thedefault_sitedirecties if needed. Thedefault_sitecorresponds to thedefault_pathextracted frompath, and islocalif the application site environment variable exists, otherwiseuser.Available configuration formats are
toml,yaml, andjson.
- cwd: str#
The current working directory.
- home: str#
The user’s home directory (platform specific).
- name: str#
The application name (
MyAppin the above example).
- path: Namespace#
A hierarchical
Namespacemapping out file paths. Each of the memberssystem,user, andlocalhave keyslib(directory),log(directory) , andconfig(file).
- default_site: str#
Either
localoruserdepending on whether the application site environment variable was defined.