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.strbool(s)[source]

A function for parsing boolean strings

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 RuntimeError when no home directory can be determined – for example a container running with HOME unset and a uid that has no passwd entry, where expanduser returns 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_CONFIG environment variable;

  • config.ini in the current directory, if it exists;

  • config.ini in ~/.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 wherever PSYCODICT_CONFIG points). When no home directory can be determined either, this raises RuntimeError (see psycodict_home()).

class psycodict.config.Configuration(parser=None, defaults={}, writeargstofile=False, readargs=False)[source]

Bases: object

This 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: the PSYCODICT_CONFIG environment variable, then config.ini in the current directory if it exists, then ~/.psycodict/config.ini (created on first use); see find_config_file().

    • secrets_file – the filename for the secrets file, whose values override the configuration file. If not given, secrets.ini next to the configuration file.

    • logging_slowcutoff – a float, giving the threshold above which a slow-query warning will be logged

    • logging_slowlogfile – a filename where slow-query warnings are printed. The default value slow_queries.log is placed in a logs directory next to the configuration file, falling back to ~/.psycodict/logs when that location is not writable; any other value is used verbatim.

    • postgresql_host – the hostname for the database

    • postgresql_port – an integer, the port to use when connecting to the database

    • postgresql_user – the username when connecting to the database

    • postgresql_password – the password for connecting to the database

    • postgresql_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 arguments

  • readargs – 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.

get_postgresql_default()[source]

The built-in default connection options (host, port, user, …), as a dictionary – the values used before the configuration file and command line are consulted.