Beautiful, robust CLI for Odoo
click-odoo helps you create and run beautiful and robust command line scripts for Odoo. It is based on the excellent Click library.
Useful community-managed scripts can be found in click-odoo-contrib.
- Quick start
- Supported Odoo versions
- Database transactions
- Command line interface (click-odoo)
- Useful links
Install it in an environment where Odoo is installed,
pip install click-odoo
Assuming the following script named list-users.py.
#!/usr/bin/env click-odoo from __future__ import print_function for u in env['res.users'].search(): print(u.login, u.name)
It can be run with:
click-odoo -d dbname --log-level=error list-users.py
./list-users.py -d dbname --log-level=error
The third technique to create scripts looks like this. Assuming the following script named list-users2.py.
#!/usr/bin/env python from __future__ import print_function import click import click_odoo @click.command() @click_odoo.env_options(default_log_level='error') @click.option('--say-hello', is_flag=True) def main(env, say_hello): if say_hello: click.echo("Hello!") for u in env['res.users'].search(): print(u.login, u.name) if __name__ == '__main__': main()
It can be run like this:
$ ./list-users2.py --help Usage: list-users2.py [OPTIONS] Options: -c, --config PATH Specify the Odoo configuration file. Other ways to provide it are with the ODOO_RC or OPENERP_SERVER environment variables, or ~/.odoorc (Odoo >= 10) or ~/.openerp_serverrc. -d, --database TEXT Specify the database name. If present, this parameter takes precedence over the database provided in the Odoo configuration file. --log-level TEXT Specify the logging level. Accepted values depend on the Odoo version, and include debug, info, warn, error. [default: error] --logfile PATH Specify the log file. --rollback Rollback the transaction even if the script does not raise an exception. Note that if the script itself commits, this option has no effect, this is why it is not named dry run. This option is implied when an interactive console is started. --say-hello --help Show this message and exit. $ ./list-users2.py --say-hello -d dbname Hello! admin Administrator ...
Odoo version 8, 9, 10 and 11 are supported.
An important design goal is to provide a consistent behaviour across Odoo versions.
click-odoo does not mandate any particular method of installing odoo. The only prerequisiste is that import odoo (>= 10) or import openerp (< 10) must work.
By default click-odoo commits the transaction for you, unless your script raises an exception. This is so that you don’t need to put explicit commits in your scripts, which are therefore easier to compose in larger transactions (provided they pass around the same env).
There is a --rollback option to force a rollback.
A rollback is always performed after an interactive session. If you need to commit changes made before or during an interactive session, use env.cr.commit().
In version 8, Odoo logs to stdout by default. On other versions it is stderr. click-odoo attempts to use stderr for Odoo 8 too.
Logging is controlled by the usual Odoo logging options (--log-level, --logfile) or the Odoo configuration file.
Usage: click-odoo [OPTIONS] [SCRIPT] [SCRIPT_ARGS]... Execute a python script in an initialized Odoo environment. The script has access to a 'env' global variable which is an odoo.api.Environment initialized for the given database. If no script is provided, the script is read from stdin or an interactive console is started if stdin appears to be a terminal. Options: -c, --config PATH Specify the Odoo configuration file. Other ways to provide it are with the ODOO_RC or OPENERP_SERVER environment variables, or ~/.odoorc (Odoo >= 10) or ~/.openerp_serverrc. -d, --database TEXT Specify the database name. If present, this parameter takes precedence over the database provided in the Odoo configuration file. --log-level TEXT Specify the logging level. Accepted values depend on the Odoo version, and include debug, info, warn, error. [default: info] --logfile PATH Specify the log file. --rollback Rollback the transaction even if the script does not raise an exception. Note that if the script itself commits, this option has no effect. This is why it is not named dry run. This option is implied when an interactive console is started. -i, --interactive / --no-interactive Inspect interactively after running the script. --shell-interface TEXT Preferred shell interface for interactive mode. Accepted values are ipython, ptpython, bpython, python. If not provided they are tried in this order. --help Show this message and exit.
Most options above are the same as odoo options and behave identically. Additional Odoo options can be set in the the configuration file. Note however that most server-related options (workers, http interface etc) are ignored because no server is actually started when running a script.
An important feature of click-odoo compared to, say, odoo shell is the capability to pass arguments to scripts.
In order to avoid confusion between click-odoo options and your script options and arguments, it is recommended to separate them with --:
click-odoo -d dbname -- list-users.py -d a b ./list-users.py -d dbname -- -d a b
In both examples above, sys.argv[1:] will contain ['-d', 'a', 'b'] in the script.
@click_odoo.env_options() is a decorator that is used very much like @click.option() and inserts the list of predefined click-odoo options. Instead of passing down these options to the command, it prepares an odoo Environment and passes it as a env parameter.
It is configurable with the following parameters:
- The default value for the -log-level option (default: ‘info’).
- Controls the presence of the --rollback option (default: True). This is useful for creating commands that commit and leave no possibility for rollback.
- Controls the presence of the --database option (default: True). This is useful to create scripts that have access to a pre-loaded Odoo configuration, without any database. In such case, the environment is not set (env is None). If with_database is False, database_required is implied to be False too.
- Controls if a database must be provided through the --database option or the Odoo configuration file (default: True).
As a convenience click_odoo exports the odoo namespace, so from click_odoo import odoo is an alias for import odoo (>9) or import openerp as odoo (<=9).
This package also provides an experimental OdooEnvironment context manager. It is meant to be used in after properly intializing Odoo (ie parsing the configuration file etc).
This API is considered experimental, contrarily to the scripting mechanism (ie passing env to scripts) and env_options decorator which are stable features. Should you have a specific usage for this API and would like it to become stable, get it touch to discuss your requirements.
from click_odoo import OdooEnvironment with OdooEnvironment(database='dbname') as env: env['res.users'].search()
- Stéphane Bidoul (ACSONE)
Inspiration has been drawn from:
This project is maintained by ACSONE SA/NV.
- clarify the behaviour of @env_option with_database and database_required parameters; in particular, when with_database and database_required are both set (the default), the --database option can be omitted as long as a database is declared in the Odoo configuration file.
- refactor the OdooEnvironment class: it is much cleaner when it leaves the global Odoo config alone, so we completely move responsibility to initialize the Odoo config to the CLI part.
- better error logging and handling: all exceptions occuring in scripts under click_odoo.env_options are logged and converted to ClickException so we are sure they are both in the log file and on the console (handled by click) for the user to see. The OdooEnvironment context manager does not do additional logging, leaving that responsibility to the caller.
- close db connections when releasing OdooEnvironment
- expose click_odoo.odoo_bin (odoo or openerp-server depending on Odoo series). not documented yet, because it should ideally be a full path corresponding to the installed click_odoo.odoo, and I’m not sure how best to detect it yet.
- minor documentation improvements
- add the possibility to run script without --database (ie without env, but with a properly initialized Odoo library such as addons path)
- be more resilient in case we can’t obtain a context for the user
- click_odoo now exports the odoo namespace: from click_odoo import odoo is an alias for import odoo (>9) or import openerp as odoo (<=9)
- add a with_rollback option to the env_options decorator, to control the presence of the rollback option
- document the env_options decorator
- commit in case of success, so users do not need to commit in their scripts, therefore making scripts easier to compose in larger transactions
- add a –rollback option
- interactive mode forces –rollback
- clear cache when starting environment (mostly useful for tests)
- simplify and test transaction and exception handling
- when leaving the env, log the exception to be sure it is visible when using --logfile
- improve transaction management: avoid some rare deadlock
- avoid masking original exception in case of error during rollback
- make sure scripts launched by click-odoo have __name__ == '__main__'
- add --logfile option
- first alpha
Release history Release notifications
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size & hash SHA256 hash help||File type||Python version||Upload date|
|click_odoo-1.0.3-py2.py3-none-any.whl (10.2 kB) Copy SHA256 hash SHA256||Wheel||py2.py3||Jun 5, 2018|
|click-odoo-1.0.3.tar.gz (19.4 kB) Copy SHA256 hash SHA256||Source||None||Jun 5, 2018|