psycodict.config¶
Configuration handling for psycodict.
Configuration merges a config.ini file with command-line
arguments (off by default; see readargs) and hands the result to
PostgresDatabase. When no file is named
explicitly, find_config_file() discovers one: the
PSYCODICT_CONFIG environment variable, then config.ini in the
current directory if it already exists, then ~/.psycodict/config.ini,
which is created on first use. A secrets.ini next to the configuration
file overrides its values, so credentials can be kept out of it.
- psycodict.config.psycodict_home()[source]¶
The directory used for psycodict’s own files (the configuration file and the slow-query log) when no explicit location is given:
~/.psycodict.Raises
RuntimeErrorwhen no home directory can be determined – for example a container running withHOMEunset and a uid that has no passwd entry, whereexpanduserreturns the literal~. Refusing loudly beats quietly creating a directory named~in the working directory.
- psycodict.config.find_config_file()[source]¶
The path of the configuration file when none is specified explicitly.
This is the first of:
the
PSYCODICT_CONFIGenvironment variable;config.iniin the current directory, if it exists;config.iniin~/.psycodict.
A missing configuration file is created at the resolved location. The current-directory candidate requires the file to already exist, so nothing is ever created in the working directory: a fresh setup gets its configuration under
~/.psycodict(or whereverPSYCODICT_CONFIGpoints). When no home directory can be determined either, this raisesRuntimeError(seepsycodict_home()).
- class psycodict.config.Configuration(parser=None, defaults={}, writeargstofile=False, readargs=False)[source]¶
Bases:
objectThis configuration object merges input from the command line and a configuration file.
If the configuration file does not exist, it can create it with values specified by the default command line arguments. This allows a user to edit the configuration file to change the defaults.
Because of this dual approach, the types of all configuration values must be recoverable from their string values. Namely, each object x of type T must satisfy x == T(str(x)). Strings, integers and floats all have this property.
INPUT:
parser– an argparse.ArgumentParser instance. If not provided, a default will be created.defaults– a dictionary with default values for the created argument parser. Only used if a parser is not specified. The keys used are:config_file– the filename for the configuration file. If not given, it is discovered: thePSYCODICT_CONFIGenvironment variable, thenconfig.iniin the current directory if it exists, then~/.psycodict/config.ini(created on first use); seefind_config_file().secrets_file– the filename for the secrets file, whose values override the configuration file. If not given,secrets.ininext to the configuration file.logging_slowcutoff– a float, giving the threshold above which a slow-query warning will be loggedlogging_slowlogfile– a filename where slow-query warnings are printed. The default valueslow_queries.logis placed in alogsdirectory next to the configuration file, falling back to~/.psycodict/logswhen that location is not writable; any other value is used verbatim.postgresql_host– the hostname for the databasepostgresql_port– an integer, the port to use when connecting to the databasepostgresql_user– the username when connecting to the databasepostgresql_password– the password for connecting to the databasepostgresql_dbname– the name of the database to connect to
writeargstofile– a boolean, if config file doesn’t exist, it determines if command line arguments are written to the config file instead of the default argumentsreadargs– a boolean (default False), determining whether command line arguments are read. Leave this off in libraries and applications with their own command line; pass True in a script whose command line psycodict should parse.