Package: dodoo
dodoois a beautiful, robust and extensible Odoo server API extension suite
for DevOps. It is based on the excellentClicklibrary.ContentsQuick startCustom scriptsdodoo PluginsSupported Odoo versionsDatabase transactionsLoggingCommand line interface (dodoo)APIenv_optionsdodoo.odoo namespaceOdooEnvironment context manager (experimental)DevelopementUseful linksCreditsMaintainerChanges2.0.0 (2019-01-22)1.1.1 (2018-11-01)1.1.0 (2018-10-31)1.0.4 (2018-10-07)1.0.3 (2018-06-05)1.0.2 (2018-06-01)1.0.1 (2018-05-27)1.0.0 (2018-05-20)1.0.0b4 (2018-05-17)1.0.0b3 (2018-03-22)1.0.0b2 (2018-03-21)1.0.0b1 (2018-03-20)1.0.0a2 (2018-03-19)1.0.0a1 (2018-03-19)Quick startInstall it in an environment where Odoo is installed,pip install dodooCustom scriptsAssuming the following script namedlist-users.py.from__future__importprint_functionforuinenv['res.users'].search([]):print(u.login,u.name)It can be run with:dodoo run -d dbname --log-level=error list-users.pyThe second technique to create scripts looks like this. Assuming
the following script namedlist-users2.py.#!/usr/bin/env pythonfrom__future__importprint_functionimportclickimportdodooCTX_SETTINGS=dict(default_map={'log_level':'error'})@click.command(cls=dodoo.CommandWithOdooEnv,context_settings=CTX_SETTINGS)@click.option('--say-hello',is_flag=True)defmain(env,say_hello):ifsay_hello:click.echo("Hello!")foruinenv['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
...dodoo PluginsFor extending tha comfort of the dodoo API itself, you can write a plugin.
It’s recommended to clone the pluginscaffoldingrepository to get started.The plugin registration is done insetup.pylike this:fromsetuptoolsimportsetupsetup(name='yourplugin',version='0.1',py_modules=['yourplugin'],install_requires=['dodoo',],entry_points='''
        [core_package.cli_plugins]
        cool_subcommand=yourscript.cli:cool_subcommand
        another_subcommand=yourscript.cli:another_subcommand
    ''',)Aside from accessing dodoo options throughctx.objimplicitly, you can be
explicite by reusing dodoo options in the following way:importclickfromdodooimportoptions,main@main.command()# Set the addons path options and make it mandatory, see options.py@options.addons_path_opt(True)defsubcommand(addons_path):"""I do something domain specific."""Supported Odoo versionsOdoo version 8, 9, 10, 11 and 12 are supported.An important design goal is to provide a consistent behaviour
across Odoo versions.Notedodoodoes not mandate any particular method of installing odoo.
The only prerequisiste is thatimport odoo(>= 10) orimport openerp(< 10) must work.Database transactionsBy defaultdodoocommits 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--rollbackoption 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, useenv.cr.commit().LoggingIn version 8, Odoo logs to stdout by default. On other versions
it is stderr.dodooattempts 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 (dodoo)Usage: dodoo [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 asodoooptions 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 ofdodoocompared to, say,odoo shellis
the capability to pass arguments to scripts.In order to avoid confusion betweendodoooptions and your script
options and arguments, it is recommended to separate them with--:dodoo -d dbname -- list-users.py -d a b
./list-users.py -d dbname -- -d a bIn both examples above,sys.argv[1:]will contain['-d','a', 'b']in the script.APIenv_optionsCustomize the behaviour ofdodoo.CommandWithOdooEnvthroughclick.Command(env_options={}).dodoo.CommandWithOdooEnvprepares an odooEnvironmentand passes
it as aenvparameter.It is configurable with the following keyword arguments inenv_options:database_must_existIf this flag is False and the selected database does not exist
do not fail and pass env=None instead (default: True).environment_managerexperimental featureA context manager that yields an intializedodoo.api.Environment.
It is invoked after Odoo configuration parsing and initialization.
It must have the following signature (identical toOdooEnvironmentbelow, plus**kwargs)environment_manager(database,rollback,**kwargs)dodoo.odoo namespaceAs a conveniencedodooexports theodoonamespace, sofrom dodoo import odoois an alias forimport odoo(>9)
orimport openerp as odoo(<=9).OdooEnvironment context manager (experimental)This package also provides an experimentalOdooEnvironmentcontext manager.
It is meant to be used in after properly intializing Odoo (ie parsing the
configuration file etc).WarningThis API is considered experimental, contrarily to the scripting mechanism
(ie passingenvto scripts) andenv_optionsdecorator 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:fromdodooimportOdooEnvironmentwithOdooEnvironment(database='dbname')asenv:env['res.users'].search([])DevelopementTo run tests, typetox. Tests are made using pytest. To run tests matching
a specific keyword for, say, Odoo 12 and python 3.6, usetox-epy36-12.0---kkeyword.This project usesblackas code formatting convention, as well as isort and flake8.
To make sure local coding convention are respected before
you commit, installpre-commitand
runpre-commitinstallafter cloning the repository.Useful linkspypi page:https://pypi.org/project/dodoocode repository:https://github.com/xoe-labs/dodooreport issues at:https://github.com/xoe-labs/dodoo/issuesCreditsOriginal Author:Stéphane Bidoul (ACSONE)Contributor:David Arnold (XOE)Maintainer:David Arnold (XOE)Inspiration has been drawn from:click-odoo by Acsoneanybox.recipe.odooanthem by Camptocampodoo’s own shell commandMaintainerThis project is maintained by XOE Corp. SAS.Changes2.0.0 (2019-01-22)refactor to click native facilities, where possiblereplace@env_options()named parameters withcontext_settingsonclick.Command()replace@env_optionswrapper with custom Command classadddefault_overridescommand key to manage script-scoped parameter
defaults (eg. adjust default forlog_levelorrollback)Rename to dodooAdd plugin facilities1.1.1 (2018-11-01)addwith_addons_pathoption to@dodoo.env_optionsto control the presence of the--addons-pathoption. Defaults to False.
Enabled for the CLI.1.1.0 (2018-10-31)addenvironment_managerto@dodoo.env_options, providing
a hook onodoo.api.Environmentcreation.add--addons-pathoption to the CLI.adddatabase_must_existenv option to@dodoo.env_optionsso scripts can behave how they please in case the database is absent.1.0.4 (2018-10-07)silence deprecation warningadapt tests for Odoo 121.0.3 (2018-06-05)clarify the behaviour of@env_optionwith_databaseanddatabase_requiredparameters; in particular, whenwith_databaseanddatabase_requiredare both set (the default), the--databaseoption can be omitted
as long as a database is declared in the Odoo configuration file.1.0.2 (2018-06-01)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.1.0.1 (2018-05-27)better error logging and handling: all exceptions occuring
in scripts under dodoo.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.1.0.0 (2018-05-20)close db connections when releasing OdooEnvironmentexpose dodoo.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 dodoo.odoo, and I’m not sure how best to detect it yet.1.0.0b4 (2018-05-17)minor documentation improvementsadd 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 user1.0.0b3 (2018-03-22)dodoo now exports the odoo namespace:from dodoo import odoois an alias forimport odoo(>9) orimport openerp as odoo(<=9)add awith_rollbackoption to theenv_optionsdecorator, to control
the presence of the rollback optiondocument theenv_optionsdecorator1.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 transactionsadd a –rollback optioninteractive mode forces –rollback1.0.0b1 (2018-03-20)clear cache when starting environment (mostly useful for tests)simplify and test transaction and exception handlingwhen leaving the env, log the exception to be sure it is visible
when using--logfile1.0.0a2 (2018-03-19)improve transaction management: avoid some rare deadlockavoid masking original exception in case of error during rollbackmake sure launched scripts have__name__ == '__main__'add--logfileoption1.0.0a1 (2018-03-19)first alpha