aiida.control package

User facing APIs to control AiiDA from the verdi cli, scripts or plugins

Submodules

Manage code objects with lazy loading of the db env

class aiida.control.code.CodeBuilder(**kwargs)[source]

Bases: object

Build a code with validation of attribute combinations

class CodeType[source]

Bases: enum.Enum

ON_COMPUTER = 'on computer'
STORE_AND_UPLOAD = 'store in the db and upload'
__module__ = 'aiida.control.code'
exception CodeValidationError(msg)[source]

Bases: exceptions.Exception

A CodeBuilder instance may raise this

  • when asked to instanciate a code with missing or invalid code attributes
  • when asked for a code attibute that has not been set yet
__init__(msg)[source]

x.__init__(…) initializes x; see help(type(x)) for signature

__module__ = 'aiida.control.code'
__repr__() <==> repr(x)[source]
__str__() <==> str(x)[source]
__weakref__

list of weak references to the object (if defined)

__dict__ = dict_proxy({'__module__': 'aiida.control.code', '_set_code_attr': <function _set_code_attr>, 'CodeValidationError': <class 'aiida.control.code.CodeValidationError'>, 'validate_installed': <function validate_installed>, '_get_and_count': <function _get_and_count>, '_get': <function _get>, 'CodeType': <enum 'CodeType'>, '__dict__': <attribute '__dict__' of 'CodeBuilder' objects>, 'validate': <function validate>, '__weakref__': <attribute '__weakref__' of 'CodeBuilder' objects>, 'from_code': <staticmethod object>, '__init__': <function __init__>, 'validate_upload': <function validate_upload>, 'is_local': <function is_local>, '__setattr__': <function __setattr__>, '__getattr__': <function __getattr__>, 'validate_code_type': <function validate_code_type>, 'new': <function new>, '__doc__': 'Build a code with validation of attribute combinations', 'get_code_spec': <staticmethod object>})
__getattr__(key)[source]

Access code attributes used to build the code

__init__(**kwargs)[source]

x.__init__(…) initializes x; see help(type(x)) for signature

__module__ = 'aiida.control.code'
__setattr__(key, value)[source]

x.__setattr__(‘name’, value) <==> x.name = value

__weakref__

list of weak references to the object (if defined)

_get(key)[source]

Return a spec, or None if not defined

Parameters:key – name of a code spec
_get_and_count(key, used)[source]

Return a spec, or raise if not defined. Moreover, add the key to the ‘used’ dict.

Parameters:
  • key – name of a code spec
  • used – should be a set of keys that you want to track. key will be added to this set if the value exists in the spec and can be retrieved.
_set_code_attr(key, value)[source]

Set a code attribute, if it passes validation.

Checks compatibility with other code attributes.

static from_code(code)[source]

Create CodeBuilder from existing code instance.

See also get_code_spec()

static get_code_spec(code)[source]

Get code attributes from existing code instance.

These attributes can be used to create a new CodeBuilder:

spec = CodeBuilder.get_code_spec(old_code)
builder = CodeBuilder(**spec)
new_code = builder.new()
is_local()[source]

Analogous to Code.is_local()

new(**kwargs)[source]

Build and return a new code instance (not stored)

validate(raise_error=True)[source]
validate_code_type()[source]

Make sure the code type is set correctly

validate_installed()[source]

If the code is on-computer, catch invalid store-and-upload attributes

validate_upload()[source]

If the code is stored and uploaded, catch invalid on-computer attributes

Manage computer objects with lazy loading of the db env

class aiida.control.computer.ComputerBuilder(**kwargs)[source]

Bases: object

Build a computer with validation of attribute combinations

exception ComputerValidationError(msg)[source]

Bases: exceptions.Exception

A ComputerBuilder instance may raise this

  • when asked to instanciate a code with missing or invalid computer attributes
  • when asked for a computer attibute that has not been set yet
__init__(msg)[source]

x.__init__(…) initializes x; see help(type(x)) for signature

__module__ = 'aiida.control.computer'
__repr__() <==> repr(x)[source]
__str__() <==> str(x)[source]
__weakref__

list of weak references to the object (if defined)

__dict__ = dict_proxy({'__module__': 'aiida.control.computer', 'get_computer_spec': <staticmethod object>, '_get_and_count': <function _get_and_count>, '_set_computer_attr': <function _set_computer_attr>, '__getattr__': <function __getattr__>, 'from_computer': <staticmethod object>, '_get': <function _get>, 'ComputerValidationError': <class 'aiida.control.computer.ComputerValidationError'>, '__dict__': <attribute '__dict__' of 'ComputerBuilder' objects>, 'new': <function new>, 'validate': <function validate>, '__setattr__': <function __setattr__>, '__weakref__': <attribute '__weakref__' of 'ComputerBuilder' objects>, '__doc__': 'Build a computer with validation of attribute combinations', '__init__': <function __init__>})
__getattr__(key)[source]

Access computer attributes used to build the computer

__init__(**kwargs)[source]

x.__init__(…) initializes x; see help(type(x)) for signature

__module__ = 'aiida.control.computer'
__setattr__(key, value)[source]

x.__setattr__(‘name’, value) <==> x.name = value

__weakref__

list of weak references to the object (if defined)

_get(key)[source]

Return a spec, or None if not defined

Parameters:key – name of a computer spec
_get_and_count(key, used)[source]

Return a spec, or raise if not defined. Moreover, add the key to the ‘used’ dict.

Parameters:
  • key – name of a computer spec
  • used – should be a set of keys that you want to track. key will be added to this set if the value exists in the spec and can be retrieved.
_set_computer_attr(key, value)[source]

Set a computer attribute if it passes validation.

static from_computer(computer)[source]

Create ComputerBuilder from existing computer instance.

See also get_computer_spec()

static get_computer_spec(computer)[source]

Get computer attributes from existing computer instance.

These attributes can be used to create a new ComputerBuilder:

spec = ComputerBuilder.get_computer_spec(old_computer)
builder = ComputerBuilder(**spec)
new_computer = builder.new()
new(**kwargs)[source]

Build and return a new computer instance (not stored)

validate(raise_error=True)[source]

Validate the computer options

aiida.control.computer.configure_computer(computer, user=None, **kwargs)[source]

Configure a computer for a user with valid auth params passed via kwargs.

aiida.control.computer.get_computer_configuration(computer, user=None)[source]

Get the configuratio of computer for user as a dictionary.

Provides an API for postgres database maintenance tasks.

This API creates and drops postgres users and databases used by the verdi quicksetup commandline tool. It allows convenient access to this functionality from within python without knowing details about how postgres is installed by default on various systems. If the postgres setup is not the default installation, additional information needs to be provided.

class aiida.control.postgres.Postgres(host='localhost', port=None, interactive=False, quiet=True)[source]

Bases: object

Provides postgres database manipulation assuming no prior setup

  • Can be used to create the initial aiida db user and database.
  • Works in every reasonable environment, provided the user can sudo

Tries to use psychopg2 with a fallback to psql subcommands (using sudo su to run as postgres user).

Parameters:
  • port – (str) Assume the database server runs on this port
  • interactive – (bool) Allow prompting the user for information Will also be passed to sudo (if using psycopg2 fails) and to the callback that can be set to be called when automatic setup detection fails
  • quiet – (bool) Suppress messages

Simple Example:

postgres = Postgres()
postgres.determine_setup()
postgres.create_dbuser('username', 'password')
if not postgres.db_exists('dbname'):
    postgres.create_db('username', 'dbname')

Complex Example:

postgres = Postgres(port=5433, interactive=True)
postgres.setup_fail_callback = prompt_db_info
postgres.determine_setup()
if postgres.pg_execute:
    print('setup sucessful!')
__dict__ = dict_proxy({'set_port': <function set_port>, 'copy_db': <function copy_db>, 'set_host': <function set_host>, '__weakref__': <attribute '__weakref__' of 'Postgres' objects>, 'set_setup_fail_callback': <function set_setup_fail_callback>, '__module__': 'aiida.control.postgres', 'determine_setup': <function determine_setup>, 'create_dbuser': <function create_dbuser>, 'drop_db': <function drop_db>, 'create_db': <function create_db>, 'drop_dbuser': <function drop_dbuser>, 'dbuser_exists': <function dbuser_exists>, '__dict__': <attribute '__dict__' of 'Postgres' objects>, 'db_exists': <function db_exists>, '__doc__': "\n Provides postgres database manipulation assuming no prior setup\n\n * Can be used to create the initial aiida db user and database.\n * Works in every reasonable environment, provided the user can sudo\n\n Tries to use psychopg2 with a fallback to psql subcommands (using ``sudo su`` to run as postgres user).\n\n :param port: (str) Assume the database server runs on this port\n :param interactive: (bool) Allow prompting the user for information\n Will also be passed to ``sudo`` (if using ``psycopg2`` fails) and to\n the callback that can be set to be called when automatic setup detection fails\n :param quiet: (bool) Suppress messages\n\n Simple Example::\n\n postgres = Postgres()\n postgres.determine_setup()\n postgres.create_dbuser('username', 'password')\n if not postgres.db_exists('dbname'):\n postgres.create_db('username', 'dbname')\n\n Complex Example::\n\n postgres = Postgres(port=5433, interactive=True)\n postgres.setup_fail_callback = prompt_db_info\n postgres.determine_setup()\n if postgres.pg_execute:\n print('setup sucessful!')\n ", '__init__': <function __init__>, '_no_setup_detected': <function _no_setup_detected>})
__init__(host='localhost', port=None, interactive=False, quiet=True)[source]

x.__init__(…) initializes x; see help(type(x)) for signature

__module__ = 'aiida.control.postgres'
__weakref__

list of weak references to the object (if defined)

_no_setup_detected()[source]

Print a warning message and calls the failed setup callback

copy_db(src_db, dest_db, dbuser)[source]
create_db(dbuser, dbname)[source]

Create a database in postgres

Parameters:
  • dbuser – (str), Name of the user which should own the db.
  • dbname – (str), Name of the database.
create_dbuser(dbuser, dbpass)[source]

Create a database user in postgres

Parameters:
  • dbuser – (str), Name of the user to be created.
  • dbpass – (str), Password the user should be given.
db_exists(dbname)[source]

Check wether a postgres database with dbname exists

Parameters:dbname – Name of the database to check for
Returns:(bool), True if database exists, False otherwise
dbuser_exists(dbuser)[source]

Find out if postgres user with name dbuser exists

Parameters:dbuser – (str) database user to check for
Returns:(bool) True if user exists, False otherwise
determine_setup()[source]

Find out how postgres can be accessed.

Depending on how postgres is set up, psycopg2 can be used to create dbs and db users, otherwise a subprocess has to be used that executes psql as an os user with the right permissions.

drop_db(dbname)[source]

Drop a database in postgres

Parameters:dbname – (str), Name of the database.
drop_dbuser(dbuser)[source]

Drop a database user in postgres

Parameters:dbuser – (str), Name of the user to be dropped.
set_host(host)[source]

Set the host manually

set_port(port)[source]

Set the port manually

set_setup_fail_callback(callback)[source]

Set a callback to be called when setup cannot be determined automatically

Parameters:callback – a callable with signature callback(interactive, dbinfo)
aiida.control.postgres._pg_execute_not_connected(command, **kwargs)[source]

A dummy implementation of a postgres command execution function.

Represents inability to execute postgres commands.

aiida.control.postgres._pg_execute_psyco(command, **kwargs)[source]

executes a postgres commandline through psycopg2

Parameters:
  • command – A psql command line as a str
  • kwargs – will be forwarded to psycopg2.connect
aiida.control.postgres._pg_execute_sh(command, user='postgres', **kwargs)[source]

executes a postgres command line as another system user in a subprocess.

Parameters:
  • command – A psql command line as a str
  • user – Name of a system user with postgres permissions
  • kwargs – connection details to forward to psql, signature as in psycopg2.connect

To stop sudo from asking for a password and fail if one is required, pass noninteractive=True as a kwarg.

aiida.control.postgres._try_connect(**kwargs)[source]

try to start a psycopg2 connection.

Returns:True if successful, False otherwise
aiida.control.postgres._try_subcmd(**kwargs)[source]

try to run psql in a subprocess.

Returns:True if successful, False otherwise
aiida.control.postgres.manual_setup_instructions(dbuser, dbname)[source]

Create a message with instructions for manually creating a database

aiida.control.postgres.prompt_db_info(*args)[source]

Prompt interactively for postgres database connecting details

Can be used as a setup fail callback for aiida.control.postgres.Postgres

Returns:dictionary with the following keys: host, port, database, user

Functions to create, inspect and manipulate profiles in the configuration file.

aiida.control.profile.setup_profile(profile, only_config, set_default=False, non_interactive=False, **kwargs)[source]

Setup an AiiDA profile and AiiDA user (and the AiiDA default user).

Parameters:
  • profile – Profile name
  • only_config – do not create a new user
  • set_default – set the new profile as the default
  • non_interactive – do not prompt for configuration values, fail if not all values are given as kwargs.
  • backend – one of ‘django’, ‘sqlalchemy’
  • email – valid email address for the user
  • db_host – hostname for the database
  • db_port – port to connect to the database
  • db_user – name of the db user
  • db_pass – password of the db user