changeset 086f841d105c in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset&node=086f841d105c
description:
        Add command line completion with argcomplete

        issue11592
        review419481003
diffstat:

 CHANGELOG              |   1 +
 bin/trytond            |   8 ++++++++
 bin/trytond-admin      |   8 ++++++++
 bin/trytond-console    |   8 ++++++++
 bin/trytond-cron       |   8 ++++++++
 bin/trytond-stat       |   8 ++++++++
 bin/trytond-worker     |   8 ++++++++
 setup.py               |   1 +
 trytond/commandline.py |  38 ++++++++++++++++++++++++++++++++------
 9 files changed, 82 insertions(+), 6 deletions(-)

diffs (267 lines):

diff -r ce683c18b5f1 -r 086f841d105c CHANGELOG
--- a/CHANGELOG Fri Aug 26 09:53:30 2022 +0200
+++ b/CHANGELOG Fri Aug 26 18:12:02 2022 +0200
@@ -1,3 +1,4 @@
+* Add command line completion with argcomplete
 * Include record name and value in validation error message
 * Add header parameter on export data
 * Enforce certificate validation for SMTP connection (issue11564)
diff -r ce683c18b5f1 -r 086f841d105c bin/trytond
--- a/bin/trytond       Fri Aug 26 09:53:30 2022 +0200
+++ b/bin/trytond       Fri Aug 26 18:12:02 2022 +0200
@@ -1,4 +1,5 @@
 #!/usr/bin/env python3
+# PYTHON_ARGCOMPLETE_OK
 # This file is part of Tryton.  The COPYRIGHT file at the top level of
 # this repository contains the full copyright notices and license terms.
 import glob
@@ -7,6 +8,11 @@
 import sys
 import threading
 
+try:
+    import argcomplete
+except ImportError:
+    argcomplete = None
+
 DIR = os.path.abspath(os.path.normpath(os.path.join(__file__,
     '..', '..', 'trytond')))
 if os.path.isdir(DIR):
@@ -16,6 +22,8 @@
 from trytond.config import config, split_netloc
 
 parser = commandline.get_parser_daemon()
+if argcomplete:
+    argcomplete.autocomplete(parser)
 options = parser.parse_args()
 commandline.config_log(options)
 extra_files = config.update_etc(options.configfile)
diff -r ce683c18b5f1 -r 086f841d105c bin/trytond-admin
--- a/bin/trytond-admin Fri Aug 26 09:53:30 2022 +0200
+++ b/bin/trytond-admin Fri Aug 26 18:12:02 2022 +0200
@@ -1,9 +1,15 @@
 #!/usr/bin/env python3
+# PYTHON_ARGCOMPLETE_OK
 # This file is part of Tryton.  The COPYRIGHT file at the top level of
 # this repository contains the full copyright notices and license terms.
 import os
 import sys
 
+try:
+    import argcomplete
+except ImportError:
+    argcomplete = None
+
 DIR = os.path.abspath(os.path.normpath(os.path.join(__file__,
     '..', '..', 'trytond')))
 if os.path.isdir(DIR):
@@ -13,6 +19,8 @@
 from trytond.config import config
 
 parser = commandline.get_parser_admin()
+if argcomplete:
+    argcomplete.autocomplete(parser)
 options = parser.parse_args()
 config.update_etc(options.configfile)
 commandline.config_log(options)
diff -r ce683c18b5f1 -r 086f841d105c bin/trytond-console
--- a/bin/trytond-console       Fri Aug 26 09:53:30 2022 +0200
+++ b/bin/trytond-console       Fri Aug 26 18:12:02 2022 +0200
@@ -1,9 +1,15 @@
 #!/usr/bin/env python3
+# PYTHON_ARGCOMPLETE_OK
 # This file is part of Tryton.  The COPYRIGHT file at the top level of
 # this repository contains the full copyright notices and license terms.
 import os
 import sys
 
+try:
+    import argcomplete
+except ImportError:
+    argcomplete = None
+
 DIR = os.path.abspath(os.path.normpath(os.path.join(__file__,
     '..', '..', 'trytond')))
 if os.path.isdir(DIR):
@@ -13,6 +19,8 @@
 from trytond.config import config
 
 parser = commandline.get_parser_console()
+if argcomplete:
+    argcomplete.autocomplete(parser)
 options = parser.parse_args()
 config.update_etc(options.configfile)
 
diff -r ce683c18b5f1 -r 086f841d105c bin/trytond-cron
--- a/bin/trytond-cron  Fri Aug 26 09:53:30 2022 +0200
+++ b/bin/trytond-cron  Fri Aug 26 18:12:02 2022 +0200
@@ -1,9 +1,15 @@
 #!/usr/bin/env python3
+# PYTHON_ARGCOMPLETE_OK
 # This file is part of Tryton.  The COPYRIGHT file at the top level of
 # this repository contains the full copyright notices and license terms.
 import os
 import sys
 
+try:
+    import argcomplete
+except ImportError:
+    argcomplete = None
+
 DIR = os.path.abspath(os.path.normpath(os.path.join(__file__,
     '..', '..', 'trytond')))
 if os.path.isdir(DIR):
@@ -13,6 +19,8 @@
 from trytond.config import config
 
 parser = commandline.get_parser_cron()
+if argcomplete:
+    argcomplete.autocomplete(parser)
 options = parser.parse_args()
 config.update_etc(options.configfile)
 commandline.config_log(options)
diff -r ce683c18b5f1 -r 086f841d105c bin/trytond-stat
--- a/bin/trytond-stat  Fri Aug 26 09:53:30 2022 +0200
+++ b/bin/trytond-stat  Fri Aug 26 18:12:02 2022 +0200
@@ -1,4 +1,5 @@
 #!/usr/bin/env python3
+# PYTHON_ARGCOMPLETE_OK
 # This file is part of Tryton.  The COPYRIGHT file at the top level of
 # this repository contains the full copyright notices and license terms.
 import curses
@@ -8,6 +9,11 @@
 import sys
 from collections import defaultdict
 
+try:
+    import argcomplete
+except ImportError:
+    argcomplete = None
+
 DIR = os.path.abspath(os.path.normpath(os.path.join(__file__,
     '..', '..', 'trytond')))
 if os.path.isdir(DIR):
@@ -17,6 +23,8 @@
 from trytond.config import config
 
 parser = commandline.get_parser_stat()
+if argcomplete:
+    argcomplete.autocomplete(parser)
 options = parser.parse_args()
 config.update_etc(options.configfile)
 
diff -r ce683c18b5f1 -r 086f841d105c bin/trytond-worker
--- a/bin/trytond-worker        Fri Aug 26 09:53:30 2022 +0200
+++ b/bin/trytond-worker        Fri Aug 26 18:12:02 2022 +0200
@@ -1,9 +1,15 @@
 #!/usr/bin/env python3
+# PYTHON_ARGCOMPLETE_OK
 # This file is part of Tryton.  The COPYRIGHT file at the top level of
 # this repository contains the full copyright notices and license terms.
 import os
 import sys
 
+try:
+    import argcomplete
+except ImportError:
+    argcomplete = None
+
 DIR = os.path.abspath(os.path.normpath(os.path.join(__file__,
     '..', '..', 'trytond')))
 if os.path.isdir(DIR):
@@ -13,6 +19,8 @@
 from trytond.config import config
 
 parser = commandline.get_parser_worker()
+if argcomplete:
+    argcomplete.autocomplete(parser)
 options = parser.parse_args()
 config.update_etc(options.configfile)
 commandline.config_log(options)
diff -r ce683c18b5f1 -r 086f841d105c setup.py
--- a/setup.py  Fri Aug 26 09:53:30 2022 +0200
+++ b/setup.py  Fri Aug 26 18:12:02 2022 +0200
@@ -173,6 +173,7 @@
         'weasyprint': ['weasyprint'],
         'coroutine': ['gevent>=1.1'],
         'image': ['pillow'],
+        'completion': ['argcomplete'],
         },
     dependency_links=dependency_links,
     zip_safe=False,
diff -r ce683c18b5f1 -r 086f841d105c trytond/commandline.py
--- a/trytond/commandline.py    Fri Aug 26 09:53:30 2022 +0200
+++ b/trytond/commandline.py    Fri Aug 26 18:12:02 2022 +0200
@@ -6,6 +6,7 @@
 import logging.config
 import logging.handlers
 import os
+import os.path
 from contextlib import contextmanager
 from io import StringIO
 
@@ -14,6 +15,25 @@
 logger = logging.getLogger(__name__)
 
 
+def database_completer(parsed_args, **kwargs):
+    from trytond.config import config
+    from trytond.transaction import Transaction
+    config.update_etc(parsed_args.configfile)
+    with Transaction().start(
+            None, 0, readonly=True, close=True) as transaction:
+        return transaction.database.list()
+
+
+def module_completer(**kwargs):
+    from trytond.modules import get_module_list
+    return get_module_list()
+
+
+def language_completer(**kwargs):
+    files = os.listdir(os.path.join(os.path.dirname(__file__), 'ir', 'locale'))
+    return [os.path.splitext(f)[0] for f in files]
+
+
 def get_base_parser():
     parser = argparse.ArgumentParser()
     parser.add_argument('--version', action='version',
@@ -37,8 +57,10 @@
         db_names = list(next(csv.reader(StringIO(db_names))))
     else:
         db_names = []
-    parser.add_argument("-d", "--database", dest="database_names", nargs='+',
-        default=db_names, metavar='DATABASE', help="specify the database name")
+    parser.add_argument(
+        "-d", "--database", dest="database_names", nargs='+',
+        default=db_names, metavar='DATABASE',
+        help="specify the database name").completer = database_completer
     parser.add_argument("--logconf", dest="logconf", metavar='FILE',
         help="logging configuration file (ConfigParser format)")
 
@@ -77,8 +99,10 @@
 def get_parser_admin():
     parser = get_parser()
 
-    parser.add_argument("-u", "--update", dest="update", nargs='+', default=[],
-        metavar='MODULE', help="activate or update a module")
+    parser.add_argument(
+        "-u", "--update", dest="update", nargs='+', default=[],
+        metavar='MODULE',
+        help="activate or update a module").completer = module_completer
     parser.add_argument("--all", dest="update", action="append_const",
         const="ir", help="update all activated modules")
     parser.add_argument("--activate-dependencies", dest="activatedeps",
@@ -93,8 +117,10 @@
         help="Send a test email to the specified address.")
     parser.add_argument("-m", "--update-modules-list", action="store_true",
         dest="update_modules_list", help="Update list of tryton modules")
-    parser.add_argument("-l", "--language", dest="languages", nargs='+',
-        default=[], metavar='CODE', help="Load language translations")
+    parser.add_argument(
+        "-l", "--language", dest="languages", nargs='+',
+        default=[], metavar='CODE',
+        help="Load language translations").completer = language_completer
     parser.add_argument("--hostname", dest="hostname", default=None,
         help="Limit database listing to the hostname")
     parser.add_argument("--validate", dest="validate", nargs='*',

Reply via email to