Add non-interactive mode#60
Open
jerem wants to merge 1 commit into
Open
Conversation
Run a Python snippet against a connected client and exit, instead of dropping into the REPL. Two ways to trigger: * ``--command "<code>"`` passes the code as a string; * a non-tty stdin is read as a script (e.g. ``odooly --env demo < script.py``). In both cases the ``client`` and ``env`` globals are pre-populated and ``_set_interactive`` is not called, so the snippet runs as a plain Python script (``client.connect()`` raises, login errors propagate).
Member
|
Thank you for the idea, and the implementation. Maybe I will add this feature, or part of it in a next release. I've tested alternatives, and there's ways to achieve this without much boilerplate: ~$ python3 <<'PY'
> import odooly
> client = odooly.Client.from_config('demo')
> env = client.env
> users = env['res.users'].search([])
> print(len(users), 'users')
> PY
28 users~$ python3 <<<'from odooly import Client;print(Client.from_config("demo").env["res.users"].search_count([]))'
28 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add a non-interactive mode so Odooly can be used in scripts and one-off automations without dropping into the REPL.
--command "<code>"runs an inline Python snippet against a connected client, then exits.In both cases
clientandenvare pre-populated as globals.Client._set_interactive()is not called, so the snippet behaves like a regular Python script: login errors propagate as exceptions, and REPL-only helpers likeclient.connect()raise.Note on the flag name
The de-facto standard short flag for "run inline code" is
-c(python -c,bash -c,sh -c,perl -e/-c, etc.). In Odooly-cis already taken by--config, so this PR uses--command(long form only).If you would like to free up
-cfor the convention, we could rename the short flag for--configin a follow-up. Options:-C, --config FILE(uppercase-C, used bygit -Candmake -Cfor related concepts). Closest to the existing UX.--configas long form only. Cleanest, very small breaking change for users who relied on-c.-f, --config FILE. Generic, less idiomatic.This would be a breaking CLI change, so happy to keep
--commandas-is if you prefer to leave-calone. Let me know which direction you want and I will fold it into this PR or open a separate one.Examples
Notes
execsemantics, not REPL semantics: bare expressions do not auto-print, useprint(...). This matchespython -c "...".sys.stdin.isattytoTruesince pytest stdin is not a real TTY; otherwise auto-detection would route them to the script path.Test plan
python -m unittest tests.test_interact tests.test_client tests.test_model tests.test_util: 286 tests pass (284 existing plus 2 new).test_commandcovers--command(no REPL invoked, snippet runs,env/clientavailable).test_stdin_pipecovers non-tty stdin (no REPL invoked, stdin read and executed).