Skip to main content

Beautiful, robust CLI for Odoo

Project description

License: LGPL-3 https://badge.fury.io/py/click-odoo.svg https://travis-ci.org/acsone/click-odoo.svg?branch=master https://codecov.io/gh/acsone/click-odoo/branch/master/graph/badge.svg

click-odoo helps you create and run beautiful and robust command line scripts for Odoo. It is based on the excellent Click library.

Quick start

Install it in a (preferably virtual) 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

or:

./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.
  --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
...

Supported Odoo versions

Odoo version 8, 9, 10 and 11 are supported.

An important design goal is to provide a consistent behaviour across Odoo versions.

Database transactions

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 and they are therefore easier to compose in larger transactions.

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 after an interactive session, use env.cr.commit.

Logging

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.

Command line interface (click-odoo)

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.
  --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 the same. Additional options can be set 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.

API

click_odoo.env_options decorator

TODO

OdooEnvironment context manager (experimental)

This package also provides an experimental an OdooEnvironment context manager.

Example:

from click_odoo import OdooEnvironment


with OdooEnvironment(database='dbname') as env:
    env['res.users'].search([])

Credits

Author:

Inspiration has been drawn from:

Maintainer

ACSONE SA/NV

This project is maintained by ACSONE SA/NV.

Changes

1.0.0b2 (2018-03-21)

  • 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

1.0.0b1 (2018-03-20)

  • 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

1.0.0a2 (2018-03-19)

  • 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

1.0.0a1 (2018-03-19)

  • first alpha

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

click-odoo-1.0.0b2.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

click_odoo-1.0.0b2-py2.py3-none-any.whl (14.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file click-odoo-1.0.0b2.tar.gz.

File metadata

File hashes

Hashes for click-odoo-1.0.0b2.tar.gz
Algorithm Hash digest
SHA256 9d087d7f18d79915df3cdcde53233d0813f95fe54af1d92bf8d0a6be9d633e9a
MD5 5b67687e78fcfb49258b6a677f75c7c1
BLAKE2b-256 523f1ab047b01a9943f5099f6e14439a63abfb9d37e8e373adf63695226e905a

See more details on using hashes here.

File details

Details for the file click_odoo-1.0.0b2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for click_odoo-1.0.0b2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4c9da2e3efda3b1573ccb22b49ef709c486868543c9048623bd369355dc0e19a
MD5 7d65fc8c2f80f269dd614fdf11ec4239
BLAKE2b-256 7070883e80194b7ee2d924895b47fd12ea814e7ebe57a4a8646f76b71070ed7b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page