changeset dd33e25eb6d4 in modules/currency:default
details: https://hg.tryton.org/modules/currency?cmd=changeset;node=dd33e25eb6d4
description:
        Use entrypoint console scripts

        and tests scripts with scenario.

        issue8662
        review258231002
diffstat:

 scripts/__init__.py                |    2 +
 scripts/import_currencies.py       |  112 +++++++++++++++++++++++++++++++++++++
 scripts/trytond_import_currencies  |  105 ----------------------------------
 setup.py                           |    7 +-
 tests/scenario_currency_import.rst |   16 +++++
 tests/test_currency.py             |    5 +
 6 files changed, 138 insertions(+), 109 deletions(-)

diffs (295 lines):

diff -r fc40c6134a3a -r dd33e25eb6d4 scripts/__init__.py
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/__init__.py       Mon Oct 14 00:13:00 2019 +0200
@@ -0,0 +1,2 @@
+# This file is part of Tryton.  The COPYRIGHT file at the top level of
+# this repository contains the full copyright notices and license terms.
diff -r fc40c6134a3a -r dd33e25eb6d4 scripts/import_currencies.py
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/import_currencies.py      Mon Oct 14 00:13:00 2019 +0200
@@ -0,0 +1,112 @@
+#!/usr/bin/env python3
+# 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 gettext
+import os
+import sys
+from argparse import ArgumentParser
+
+import pycountry
+from forex_python.converter import CurrencyCodes
+
+try:
+    from progressbar import ProgressBar, Bar, ETA, SimpleProgress
+except ImportError:
+    ProgressBar = None
+
+try:
+    from proteus import Model, config
+except ImportError:
+    prog = os.path.basename(sys.argv[0])
+    sys.exit("proteus must be installed to use %s" % prog)
+
+
+def _progress(iterable):
+    if ProgressBar:
+        pbar = ProgressBar(
+            widgets=[SimpleProgress(), Bar(), ETA()])
+    else:
+        pbar = iter
+    return pbar(iterable)
+
+
+def _get_language_codes():
+    Language = Model.get('ir.lang')
+    languages = Language.find([('translatable', '=', True)])
+    for l in languages:
+        yield l.code
+
+
+def get_currencies():
+    Currency = Model.get('currency.currency')
+
+    with config.get_config().set_context(active_test=False):
+        return {c.code: c for c in Currency.find([])}
+
+
+def update_currencies(currencies):
+    print("Update currencies", file=sys.stderr)
+    Currency = Model.get('currency.currency')
+    codes = CurrencyCodes()
+
+    records = []
+    for currency in _progress(pycountry.currencies):
+        code = currency.alpha_3
+        if code in currencies:
+            record = currencies[code]
+        else:
+            record = Currency(code=code)
+        record.name = currency.name
+        record.numeric_code = currency.numeric
+        record.symbol = codes.get_symbol(currency.alpha_3) or currency.alpha_3
+        records.append(record)
+
+    Currency.save(records)
+    return {c.code: c for c in records}
+
+
+def translate_currencies(currencies):
+    Currency = Model.get('currency.currency')
+
+    current_config = config.get_config()
+    for code in _get_language_codes():
+        try:
+            gnutranslation = gettext.translation(
+                'iso4217', pycountry.LOCALES_DIR, languages=[code])
+        except IOError:
+            continue
+        print("Update currencies %s" % code, file=sys.stderr)
+        with current_config.set_context(language=code):
+            records = []
+            for currency in _progress(pycountry.currencies):
+                record = Currency(currencies[currency.alpha_3].id)
+                record.name = gnutranslation.gettext(currency.name)
+                records.append(record)
+            Currency.save(records)
+
+
+def main(database, config_file=None):
+    config.set_trytond(database, config_file=config_file)
+    do_import()
+
+
+def do_import():
+    currencies = get_currencies()
+    currencies = update_currencies(currencies)
+    translate_currencies(currencies)
+
+
+def run():
+    parser = ArgumentParser()
+    parser.add_argument('-d', '--database', dest='database')
+    parser.add_argument('-c', '--config', dest='config_file',
+        help='the trytond config file')
+
+    args = parser.parse_args()
+    if not args.database:
+        parser.error('Missing database')
+    main(args.database, args.config_file)
+
+
+if __name__ == '__main__':
+    run()
diff -r fc40c6134a3a -r dd33e25eb6d4 scripts/trytond_import_currencies
--- a/scripts/trytond_import_currencies Sat Sep 28 23:59:09 2019 +0200
+++ /dev/null   Thu Jan 01 00:00:00 1970 +0000
@@ -1,105 +0,0 @@
-#!/usr/bin/env python3
-# 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 gettext
-import os
-import sys
-from argparse import ArgumentParser
-
-import pycountry
-from forex_python.converter import CurrencyCodes
-
-try:
-    from progressbar import ProgressBar, Bar, ETA, SimpleProgress
-except ImportError:
-    ProgressBar = None
-
-try:
-    from proteus import Model, config
-except ImportError:
-    prog = os.path.basename(sys.argv[0])
-    sys.exit("proteus must be installed to use %s" % prog)
-
-
-def _progress(iterable):
-    if ProgressBar:
-        pbar = ProgressBar(
-            widgets=[SimpleProgress(), Bar(), ETA()])
-    else:
-        pbar = iter
-    return pbar(iterable)
-
-
-def _get_language_codes():
-    Language = Model.get('ir.lang')
-    languages = Language.find([('translatable', '=', True)])
-    for l in languages:
-        yield l.code
-
-
-def get_currencies():
-    Currency = Model.get('currency.currency')
-
-    with config.get_config().set_context(active_test=False):
-        return {c.code: c for c in Currency.find([])}
-
-
-def update_currencies(currencies):
-    print("Update currencies")
-    Currency = Model.get('currency.currency')
-    codes = CurrencyCodes()
-
-    records = []
-    for currency in _progress(pycountry.currencies):
-        code = currency.alpha_3
-        if code in currencies:
-            record = currencies[code]
-        else:
-            record = Currency(code=code)
-        record.name = currency.name
-        record.numeric_code = currency.numeric
-        record.symbol = codes.get_symbol(currency.alpha_3) or currency.alpha_3
-        records.append(record)
-
-    Currency.save(records)
-    return {c.code: c for c in records}
-
-
-def translate_currencies(currencies):
-    Currency = Model.get('currency.currency')
-
-    current_config = config.get_config()
-    for code in _get_language_codes():
-        try:
-            gnutranslation = gettext.translation(
-                'iso4217', pycountry.LOCALES_DIR, languages=[code])
-        except IOError:
-            continue
-        print("Update currencies %s" % code)
-        with current_config.set_context(language=code):
-            records = []
-            for currency in _progress(pycountry.currencies):
-                record = Currency(currencies[currency.alpha_3].id)
-                record.name = gnutranslation.gettext(currency.name)
-                records.append(record)
-            Currency.save(records)
-
-
-def main(database, config_file=None):
-    config.set_trytond(database, config_file=config_file)
-
-    currencies = get_currencies()
-    currencies = update_currencies(currencies)
-    translate_currencies(currencies)
-
-
-if __name__ == '__main__':
-    parser = ArgumentParser()
-    parser.add_argument('-d', '--database', dest='database')
-    parser.add_argument('-c', '--config', dest='config_file',
-        help='the trytond config file')
-
-    args = parser.parse_args()
-    if not args.database:
-        parser.error('Missing database')
-    main(args.database, args.config_file)
diff -r fc40c6134a3a -r dd33e25eb6d4 setup.py
--- a/setup.py  Sat Sep 28 23:59:09 2019 +0200
+++ b/setup.py  Mon Oct 14 00:13:00 2019 +0200
@@ -51,7 +51,7 @@
         requires.append(get_require_version('trytond_%s' % dep))
 requires.append(get_require_version('trytond'))
 
-tests_require = [get_require_version('proteus')]
+tests_require = [get_require_version('proteus'), 'pycountry', 'forex-python']
 dependency_links = []
 if minor_version % 2:
     dependency_links.append('https://trydevpi.tryton.org/')
@@ -81,9 +81,6 @@
             + ['tryton.cfg', 'view/*.xml', 'locale/*.po', 'icons/*.svg',
             'tests/*.rst']),
         },
-    scripts=[
-        'scripts/trytond_import_currencies',
-        ],
     classifiers=[
         'Development Status :: 5 - Production/Stable',
         'Environment :: Plugins',
@@ -133,6 +130,8 @@
     entry_points="""
     [trytond.modules]
     currency = trytond.modules.currency
+    [console_scripts]
+    trytond_import_currencies = 
trytond.modules.currency.scripts.import_currencies:run [data]
     """,
     test_suite='tests',
     test_loader='trytond.test_loader:Loader',
diff -r fc40c6134a3a -r dd33e25eb6d4 tests/scenario_currency_import.rst
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/scenario_currency_import.rst        Mon Oct 14 00:13:00 2019 +0200
@@ -0,0 +1,16 @@
+===============
+Currency Import
+===============
+
+Imports::
+
+    >>> from trytond.tests.tools import activate_modules
+    >>> from trytond.modules.currency.scripts import import_currencies
+
+Install currency::
+
+    >>> config = activate_modules('currency')
+
+Import currencies::
+
+    >>> import_currencies.do_import()
diff -r fc40c6134a3a -r dd33e25eb6d4 tests/test_currency.py
--- a/tests/test_currency.py    Sat Sep 28 23:59:09 2019 +0200
+++ b/tests/test_currency.py    Mon Oct 14 00:13:00 2019 +0200
@@ -298,4 +298,9 @@
             tearDown=doctest_teardown, encoding='utf-8',
             checker=doctest_checker,
             optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
+    suite.addTests(doctest.DocFileSuite(
+            'scenario_currency_import.rst',
+            tearDown=doctest_teardown, encoding='utf-8',
+            checker=doctest_checker,
+            optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
     return suite

Reply via email to