Giter Club home page Giter Club logo

click-odoo's Introduction

click-odoo

License: LGPL-3

image

image

image

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

Check Odoo is correctly installed: python -c "import odoo" must work when run from another directory than the Odoo root directory.

Install click-odoo:

pip install click-odoo

Assuming the following script named list-users.py.

#!/usr/bin/env python
from __future__ import print_function

for u in env['res.users'].search([]):
    print(u.login, u.name)

It can be run with:

python -m click_odoo -d dbname --log-level=error ./list-users.py

or:

click-odoo -d dbname --log-level=error ./list-users.py

The other 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, critical. [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
...

Finally, you can start an interactive shell by simply typing python -m click_odoo -d dbname or click-odoo -d dbname. This will launch the python REPL with an Odoo env available as a global variable.

Supported Odoo versions

Odoo version 11, 12, 13, 14, 15 and 16 are supported.

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

Note

click-odoo does not mandate any particular method of installing odoo. The only prerequisiste is that import odoo must work when run from another directory than the Odoo root directory.

You may also rely on the fact that python adds the current directory to sys.path, so import odoo works from the Odoo root directory. In such case, the only working invocation method may be python -m click_odoo.

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, 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().

Logging

Logging is controlled by the usual Odoo logging options (--log-level, --logfile) or the Odoo configuration file.

Note the --log-level option applies to the odoo package only.

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 FILE               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.
  --addons-path TEXT              Specify the addons path. If present, this
                                  parameter takes precedence over the addons
                                  path provided in the Odoo configuration
                                  file.
  -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 FILE                  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.

API

click_odoo.env_options decorator

@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 keyword arguments:

default_log_level

The default value for the --log-level option (default: 'info').

with_rollback

Controls the presence of the --rollback option (default: True). This is useful for creating commands that commit and leave no possibility for rollback.

with_database

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.

database_required

Controls if a database must be provided through the --database option or the Odoo configuration file (default: True).

database_must_exist

If this flag is False and the selected database does not exist do not fail and pass env=None instead (default: True).

with_addons_path

Controls the presence of the --addons-path option (default: False).

environment_manager

experimental feature A context manager that yields an intialized odoo.api.Environment. It is invoked after Odoo configuration parsing and initialization. It must have the following signature (identical to OdooEnvironment below, plus the click ctx as well as **kwargs for future proofing):

environment_manager(database, rollback, ctx, **kwargs)

Customizing click_odoo.env_options (experimental)

click_odoo.env_options is a class that can be extended for customization purposes.

It currently has one method that is intended to be overridden, with the following signature:

def get_odoo_args(self, ctx: click.Context) -> List[str]:
    ...

It must return a list of Odoo command line arguments computed from the Click context. It will be called after parsing all parameters of the command, and before initializing Odoo and invoking the command function.

click_odoo.odoo namespace

As a convenience click_odoo exports the odoo namespace, so from click_odoo import odoo is an alias for import odoo.

OdooEnvironment context manager (experimental)

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

Warning

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.

Example:

from click_odoo import OdooEnvironment


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

Developement

To run tests, type tox. Tests are made using pytest. To run tests matching a specific keyword for, say, Odoo 12 and python 3.6, use tox -e py36-12.0 -- -k keyword.

This project uses black as code formatting convention, as well as isort and flake8. To make sure local coding convention are respected before you commit, install pre-commit and run pre-commit install after cloning the repository.

Credits

Author:

Contributors:

Inspiration has been drawn from:

Maintainer

ACSONE SA/NV

This project is maintained by ACSONE SA/NV.

click-odoo's People

Contributors

sbidoul avatar thomasbinsfeld avatar voronind avatar yajo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

click-odoo's Issues

[Question] how to make linting tools ignore the warning that `env` is not defined

In the example in the README file you have this:

#!/usr/bin/env python
from __future__ import print_function

for u in env['res.users'].search([]):
    print(u.login, u.name)

In my vs code it shows warning that env is not defined, is there a way to make that warning disappear? The closes thing I could find was the key word global but I don't thing it is usable here.

My question: how to make this warning disappear?

support registry database signaling to

Currently, when a click-odoo script makes change that require a registry reload or cache invalidation, the information is not propagated to other Odoo instances running concurrently on the same database.

Conversely, if an Odoo instance running on the same database as a long running click-odoo script that does several transactions will not receive information that the registry needs reloading.

Asking for help: database concections not closed in tests

@sbidoul Sorry for the bluntness of asking for help, but I can't get behind this error and I wonder if you have had it too or can help anyway:

https://travis-ci.com/xoe-labs/dodoo-tester/jobs/162852673#L806-L807

Could you have a look? It seems to be a ROLLBACK that's not closed properly, see

  datid  |       datname        |  pid  | usesysid |  usename  | application_name | client_addr | client_hostname | client_port |         backend_start         |          xact_start           |          query_start          |         state_change          | wait_event_type | wait_event | state  | backend_xid | backend_xmin |          query           |  backend_type  
---------+----------------------+-------+----------+-----------+------------------+-------------+-----------------+-------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+-----------------+------------+--------+-------------+--------------+--------------------------+----------------
         |                      |  1255 |       10 | postgres  |                  |             |                 |             |                               |                               |                               |                               |                 |            |        |             |              | <insufficient privilege> | 
         |                      |  1253 |          |           |                  |             |                 |             |                               |                               |                               |                               |                 |            |        |             |              | <insufficient privilege> | 
 1429916 | dodoo-tester-test-11 | 12707 |    16385 | blaggacao |                  |             |                 |          -1 | 2018-12-04 18:41:25.977417-05 |                               | 2018-12-04 18:42:07.447718-05 | 2018-12-04 18:42:07.447762-05 | Client          | ClientRead | idle   |             |              | ROLLBACK                 | client backend
 1429916 | dodoo-tester-test-11 | 12715 |    16385 | blaggacao |                  |             |                 |          -1 | 2018-12-04 18:41:27.80847-05  |                               | 2018-12-04 18:42:07.495542-05 | 2018-12-04 18:42:07.495571-05 | Client          | ClientRead | idle   |             |              | ROLLBACK                 | client backend
 1429916 | dodoo-tester-test-11 | 12716 |    16385 | blaggacao |                  |             |                 |          -1 | 2018-12-04 18:41:27.854904-05 |                               | 2018-12-04 18:42:07.495372-05 | 2018-12-04 18:42:07.495403-05 | Client          | ClientRead | idle   |             |              | ROLLBACK                 | client backend
   16386 | blaggacao            | 12850 |    16385 | blaggacao | psql             |             |                 |          -1 | 2018-12-04 18:43:00.974713-05 | 2018-12-04 18:43:45.833475-05 | 2018-12-04 18:43:45.833475-05 | 2018-12-04 18:43:45.833481-05 |                 |            | active |             |       563647 | select *                +| client backend
         |                      |       |          |           |                  |             |                 |             |                               |                               |                               |                               |                 |            |        |             |              | from pg_stat_activity;   | 
         |                      |  1251 |          |           |                  |             |                 |             |                               |                               |                               |                               |                 |            |        |             |              | <insufficient privilege> | 
         |                      |  1250 |          |           |                  |             |                 |             |                               |                               |                               |                               |                 |            |        |             |              | <insufficient privilege> | 
         |                      |  1252 |          |           |                  |             |                 |             |                               |                               |                               |                               |                 |            |        |             |              | <insufficient privilege> | 
(9 rows)

Reusable odoodb pytest fixture

We have the odoodb fixture in this project, but repeated in a similar way in click-odoo-contrib. There must be a way to make something reusable.

Improve UX with hook for subcommands

  • Second order libraries like, click-odoo-migrator axpose their own entry point
  • It would be nice if click-odoo provided a hook to register second order modules as subcommands so that click-odoo-migrator would become click-odoo migrator.

env is teared down when entering subcommands

The following should work, but fails because OdooEnvironment tears down env before entering the subcommand. We need to find a better place/mechanism to do the teardown.

import click

import click_odoo


@click.group()
@click_odoo.env_options()
@click.option('--say-hello', is_flag=True)
@click.pass_context
def cli(ctx, env, say_hello):
    click.echo("cli")
    if say_hello:
        click.echo("hello")
    ctx.obj = env
    print(env['res.users'].search([]).mapped('login'))


@cli.command()
@click.pass_context
def sync(ctx):
    env = ctx.obj  # bug: env has been teared down at this point
    click.echo("syncing")
    print(env['res.users'].search([]).mapped('login'))


if __name__ == "__main__":
    cli()

Optionally define odoo search path

I want to run click-odoo against several code bases, therefore we need a way to specify the search path for the odoo library as argument (and defer importing the odoo library at the latest possible moment).

There must be a way to achieve this without manipulating the environment during the script's run time (if that's possible at all).

documentation example

Thanks making all those great usefull tools !

I'm getting some trouble following the README example. Not sure if it's related to my config ?

  • I've create a new script addons/scripts/import_partner.py:
#!/usr/bin/env click-odoo
from __future__ import print_function

import click
import click_odoo

@click.command()
@click_odoo.env_options()
def import_partner(env):
    click.echo("Hello!")
    for u in env['res.users'].search([]):
        print(u.login, u.name)

if __name__ == "__main__":
    import_partner()
  • I've installed click-odoo in my virtual env (OCB v14 / python 3.7.10 / click v7.1.2 / click-odoo current master branch 1.4.4.dev9+gf5b8035)

Does click-odoo support odoo v13 and 14 (README stoped to v12

when running as suggested I'm getting this error:

$ chmod +x addons/scripts/import_partner.py 
$ addons/scripts/import_partner.py -c addons/odoo.local.conf -d MyDB
2021-04-27 15:31:58,402 16340 INFO ? odoo: Odoo version 14.0 
2021-04-27 15:31:58,403 16340 INFO ? odoo: Using configuration file at ./addons/odoo.local.conf 
2021-04-27 15:31:58,403 16340 INFO ? odoo: addons paths: ['./ocb/odoo/addons', './.filestore/addons/14.0', './addons', './addons-contrib', './addons-useless'] 
2021-04-27 15:31:58,403 16340 INFO ? odoo: database: default@default:5432 
> ./src/click-odoo/click_odoo/env_options.py(189)_invoke()
-> database = ctx.params.get("database")
(Pdb) pp ctx.params
{'addons_path': None,
 'config': 'addons/odoo.local.conf',
 'database': 'MyDB',
 'interactive': False,
 'log_level': 'info',
 'logfile': None,
 'rollback': False,
 'script': 'addons/scripts/import_partner.py',
 'script_args': (),
 'shell_interface': None}
(Pdb) c
2021-04-27 15:32:08,633 16340 INFO MyDB odoo.modules.loading: loading 1 modules... 
2021-04-27 15:32:08,794 16340 INFO MyDB odoo.addons.base.models.ir_actions_report: Will use the Wkhtmltopdf binary at /usr/local/bin/wkhtmltopdf 
2021-04-27 15:32:08,911 16340 INFO MyDB odoo.modules.loading: 1 modules loaded in 0.28s, 0 queries (+0 extra) 
2021-04-27 15:32:08,934 16340 INFO MyDB odoo.modules.loading: loading 55 modules... 
2021-04-27 15:32:09,309 16340 INFO MyDB odoo.modules.loading: 55 modules loaded in 0.37s, 0 queries (+0 extra) 
2021-04-27 15:32:09,689 16340 INFO MyDB odoo.modules.loading: Modules loaded. 
2021-04-27 15:32:09,724 16340 INFO MyDB odoo: Odoo version 14.0 
2021-04-27 15:32:09,724 16340 INFO MyDB odoo: addons paths: ['./ocb/odoo/addons', './.filestore/addons/14.0', './addons', './addons-contrib', './addons-useless'] 
2021-04-27 15:32:09,724 16340 INFO MyDB odoo: database: default@default:5432 
> ./src/click-odoo/click_odoo/env_options.py(189)_invoke()
-> database = ctx.params.get("database")
(Pdb) pp ctx.params
{'config': None,
 'database': None,
 'log_level': 'info',
 'logfile': None,
 'rollback': False}
(Pdb) c
2021-04-27 15:32:13,736 16340 ERROR MyDB click_odoo.env_options: exception 
Traceback (most recent call last):
  File "./src/click-odoo/click_odoo/env_options.py", line 189, in _invoke
    database = ctx.params.get("database")
click.exceptions.UsageError: No database provided, please provide one with the -d option or the Odoo configuration file.
Error: No database provided, please provide one with the -d option or the Odoo configuration file.
2021-04-27 15:32:13,738 16340 INFO MyDB odoo.sql_db: ConnectionPool(used=0/count=0/max=64): Closed 1 connections to 'port=5432 sslmode=prefer dbname=MyDB' 

I've place a pdb in _invoke method which is called twice with different ctx.params I haven't deep more...

As a workaround I've place a setup.py file with minimalist config:

import setuptools

setuptools.setup(
    name='SCRIPTS',
    version="1.0.0",
    setup_requires=['click-odoo'],
    entry_points='''
        [console_scripts]
        import-partner=addons.scripts.import_partner:import_partner
    ''',
)

Then it works like a charm _invoke is called only once:

chmod -x addons/scripts/import_partner.py 
$ import-partner -c addons/odoo.local.conf -d MyDb
2021-04-27 15:35:58,133 16771 INFO ? odoo: Odoo version 14.0 
2021-04-27 15:35:58,134 16771 INFO ? odoo: Using configuration file at ./addons/odoo.local.conf 
2021-04-27 15:35:58,134 16771 INFO ? odoo: addons paths: ['./ocb/odoo/addons', './.filestore/addons/14.0', './addons', './addons-contrib', './addons-useless'] 
2021-04-27 15:35:58,134 16771 INFO ? odoo: database: default@default:5432 
> ./src/click-odoo/click_odoo/env_options.py(189)_invoke()
-> database = ctx.params.get("database")
(Pdb) c
2021-04-27 15:35:59,653 16771 INFO MyDB odoo.modules.loading: loading 1 modules... 
2021-04-27 15:35:59,814 16771 INFO MyDB odoo.addons.base.models.ir_actions_report: Will use the Wkhtmltopdf binary at /usr/local/bin/wkhtmltopdf 
2021-04-27 15:35:59,938 16771 INFO MyDB odoo.modules.loading: 1 modules loaded in 0.28s, 0 queries (+0 extra) 
2021-04-27 15:35:59,974 16771 INFO MyDB odoo.modules.loading: loading 55 modules... 
2021-04-27 15:36:00,339 16771 INFO MyDB odoo.modules.loading: 55 modules loaded in 0.36s, 0 queries (+0 extra) 
2021-04-27 15:36:00,711 16771 INFO MyDB odoo.modules.loading: Modules loaded. 
Hello!
...

Crash Error use click-odoo

Hi,

I used click-odoo from console in a enviroment python3.6, i have odoo11 installed and it shows:


Traceback (most recent call last):
File "/opt/odoo/odoo11-venv/lib/python3.6/site-packages/click_odoo/env.py", line 8, in
import odoo
ModuleNotFoundError: No module named 'odoo'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/opt/odoo/odoo11-venv/lib/python3.6/site-packages/click_odoo/env.py", line 15, in
import openerp as odoo
ModuleNotFoundError: No module named 'openerp'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/opt/odoo/odoo11-venv/bin/click-odoo", line 7, in
from click_odoo.cli import main
File "/opt/odoo/odoo11-venv/lib/python3.6/site-packages/click_odoo/init.py", line 4, in
from .env_options import env_options # noqa
File "/opt/odoo/odoo11-venv/lib/python3.6/site-packages/click_odoo/env_options.py", line 11, in
from .env import OdooEnvironment, odoo
File "/opt/odoo/odoo11-venv/lib/python3.6/site-packages/click_odoo/env.py", line 20, in
raise ImportError("No module named odoo nor openerp")
ImportError: No module named odoo nor openerp


I have to do some configuration?

click-odoo-initdb question

Hello: I've created a few template dbs in Odoo and I would like to get an example of using click-odoo-initdb to create a new database from one of these templates. If you have a working example syntax of it that would be great. I'm just a little confused on how to create/access the cache of template dbs.

saas releases support

Hello!

I'm using doodba and got the following error on running in saas release:

Traceback (most recent call last):
  File "/usr/local/bin/click-odoo-initdb", line 5, in <module>
    from click_odoo_contrib.initdb import main
  File "/usr/local/lib/python3.8/site-packages/click_odoo_contrib/initdb.py", line 14, in <module>
    import click_odoo
  File "/usr/local/lib/python3.8/site-packages/click_odoo/__init__.py", line 4, in <module>
    from .compat import odoo  # noqa
  File "/usr/local/lib/python3.8/site-packages/click_odoo/compat.py", line 33, in <module>
    if odoo_version_info < (10, 0):
TypeError: '<' not supported between instances of 'str' and 'int'

https://github.com/odoo/odoo/blob/saas-15.2/odoo/release.py#L15

version_info = ('saas~15', 2, 0, FINAL, 0, '')

Related code:

if odoo_version_info < (10, 0):
odoo_bin = "openerp-server"
else:
odoo_bin = "odoo"
if odoo_version_info < (15, 0):
environment_manage = Environment.manage
else:

Improve logging

  • there is a info log line being emitted when the database connections are closed even if log level > info
  • setting --log-level=debug applies to odoo logs only but not logging performed by the script

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.