From dd3f73e9141d23b1e5517db1bcc1a40df0ac444a Mon Sep 17 00:00:00 2001 From: Ashwin Vishnu Date: Thu, 17 May 2018 13:13:55 +0200 Subject: [PATCH 1/4] Add basic CLI Use symbols in notifications Add tests Fix test_defaults Add test_notify Try to use apt installed site-packages Say yes to pew! Mention dependencies explicitly Add dbus to travis Skip if no dbus Simply use unittest instead of nose Correct package name --- .travis.yml | 10 ++++- forex_python/__main__.py | 84 ++++++++++++++++++++++++++++++++++++++++ setup.py | 21 +++++++--- tests/test_main.py | 29 ++++++++++++++ 4 files changed, 137 insertions(+), 7 deletions(-) create mode 100644 forex_python/__main__.py create mode 100644 tests/test_main.py diff --git a/.travis.yml b/.travis.yml index bc04fd2..cf2c35a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,12 +6,18 @@ python: - "3.4" - "3.5" +addons: + apt: + packages: + - python3-notify2 + - python-notify2 + - dbus + install: - python setup.py install - - pip install nose - pip install coveralls script: - - nosetests --with-coverage --cover-package=forex_python + - coverage run --source=forex_python -m unittest discover after_success: coveralls diff --git a/forex_python/__main__.py b/forex_python/__main__.py new file mode 100644 index 0000000..21bc274 --- /dev/null +++ b/forex_python/__main__.py @@ -0,0 +1,84 @@ +"""Command line utilty for forex-python +======================================= + +Inspired by another package: anshulc95/exch +""" +from __future__ import print_function + +import argparse +import os +import sys + +from . import converter + + +parser = argparse.ArgumentParser( + formatter_class=argparse.ArgumentDefaultsHelpFormatter +) +parser.add_argument( + "-b", "--base", default="USD", help="Currency you are converting from." +) +parser.add_argument( + "-d", "--dest", default="INR", help="Currency you are converting to." +) +parser.add_argument( + "-a", "--amount", default=1.0, type=float, help="Amount to convert." +) +parser.add_argument( + "-n", "--notify", action="store_true", help="Display desktop alerts." +) + + +def symbol(currency_code): + sym = converter.get_symbol(currency_code) + if sym is not None: + return sym + else: + return currency_code + + +def conversion_result(amount, base, converted_amount, dest, use_symbols=False): + if use_symbols: + return "{} {} = {} {}".format( + symbol(base), amount, symbol(dest), converted_amount + ) + else: + return "{} {} = {} {}".format(amount, base, converted_amount, dest) + + + +def notify_posix(args): + try: + import notify2 + except ImportError: + print("Requires Linux or macOS with notify2 and dbus package.") + raise + notify2.init("forex-python") + notification = conversion_result( + 1.0, args.base, converter.get_rate(args.base, args.dest), args.dest, + True + ) + n = notify2.Notification( + "forex-python", notification, "notification-message-im" # Icon name + ) + n.show() + + +def run(args=None, output=sys.stdout): + args = parser.parse_args(args) + if args.notify: + if os.name == "posix": + notify_posix(args) + else: + raise ValueError( + "The option [-n] is available only for POSIX operating systems") + else: + print( + conversion_result( + args.amount, + args.base, + converter.convert(args.base, args.dest, args.amount), + args.dest, + ), + file=output + ) diff --git a/setup.py b/setup.py index d71ffd9..ae353d2 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,3 @@ -import io import os from setuptools import setup, find_packages @@ -19,6 +18,17 @@ """ +install_requires=[ + 'requests', + 'simplejson', +] + +if os.name == 'posix': + install_requires += [ + 'notify2', + 'dbus-python' + ] + setup( name='forex-python', version=VERSION, @@ -29,10 +39,7 @@ long_description=long_description_text, packages=find_packages(exclude=['tests', 'tests.*']), include_package_data=True, - install_requires=[ - 'requests', - 'simplejson', - ], + install_requires=install_requires, classifiers=[ 'Intended Audience :: Developers', 'Operating System :: OS Independent', @@ -45,4 +52,8 @@ 'Programming Language :: Python :: 3.5', 'Topic :: Software Development :: Internationalization', ], + entry_points={ + 'console_scripts': + ['forex-python=forex_python.__main__:run'] + }, ) diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 0000000..18126c5 --- /dev/null +++ b/tests/test_main.py @@ -0,0 +1,29 @@ +import unittest +import os +from forex_python.__main__ import run, symbol +try: + import dbus +except ImportError: + dbus = False + + +class TestCLI(unittest.TestCase): + """Test forex-python command-line interface.""" + + def test_defaults(self): + with open(os.devnull, "w") as null: + run([], output=null) + + @unittest.skipIf(not dbus, "dbus is not available") + def test_notify(self): + run(["--notify"]) + + def test_options(self): + with open(os.devnull, "w") as null: + run(["-b", "GBP", "-d", "EUR", "-a", "121"], null) + + def test_symbol(self): + assert symbol("EUR") == u"\u20ac" + +if __name__ == "__main__": + unittest.main() From f9daa149a458fe0842f4044d4f6ac44bb5eea735 Mon Sep 17 00:00:00 2001 From: Ashwin Vishnu Date: Thu, 17 Jan 2019 06:06:39 +0100 Subject: [PATCH 2/4] Show amount in notification --- forex_python/__main__.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/forex_python/__main__.py b/forex_python/__main__.py index 21bc274..0b0a4ae 100644 --- a/forex_python/__main__.py +++ b/forex_python/__main__.py @@ -37,7 +37,11 @@ def symbol(currency_code): return currency_code -def conversion_result(amount, base, converted_amount, dest, use_symbols=False): +def conversion_result(args, use_symbols=False): + amount = args.amount + base = args.base + dest = args.dest + converted_amount = converter.convert(args.base, args.dest, args.amount) if use_symbols: return "{} {} = {} {}".format( symbol(base), amount, symbol(dest), converted_amount @@ -54,10 +58,7 @@ def notify_posix(args): print("Requires Linux or macOS with notify2 and dbus package.") raise notify2.init("forex-python") - notification = conversion_result( - 1.0, args.base, converter.get_rate(args.base, args.dest), args.dest, - True - ) + notification = conversion_result(args, True) n = notify2.Notification( "forex-python", notification, "notification-message-im" # Icon name ) @@ -73,12 +74,4 @@ def run(args=None, output=sys.stdout): raise ValueError( "The option [-n] is available only for POSIX operating systems") else: - print( - conversion_result( - args.amount, - args.base, - converter.convert(args.base, args.dest, args.amount), - args.dest, - ), - file=output - ) + print(conversion_result(args), file=output) From a72221e075eab7e8a6aee65e3e8657311aadeaee Mon Sep 17 00:00:00 2001 From: Ashwin Vishnu Date: Thu, 17 Jan 2019 15:42:41 +0100 Subject: [PATCH 3/4] Doc, readme CLI --- README.rst | 11 +++++++++++ docs/source/usage.rst | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/README.rst b/README.rst index 16bf64b..93fc59b 100644 --- a/README.rst +++ b/README.rst @@ -28,6 +28,7 @@ Features: - Convert amount from one currency to other.('USD 10$' to INR). - Currency symbols. - Currency names. +- Command line interface (CLI). Currency Source: ----------------- @@ -141,6 +142,16 @@ Usage Examples: >>> print c.get_symbol('GBP') £ +- Use the CLI to get quick answers from the terminal / command line. + + .. code-block:: bash + + ❯❯❯ forex-python -b EUR + 1.0 EUR = 81.0055 INR + ❯❯❯ forex-python -b EUR -d USD + 1.0 EUR = 1.1389 USD + ❯❯❯ forex-python -b EUR -d USD -a 3.14 + 3.14 EUR = 3.576146 USD You can view the complete `Documentation Here`_ diff --git a/docs/source/usage.rst b/docs/source/usage.rst index 76c6f4d..ab154b7 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -1,6 +1,31 @@ Usage Examples: =============== +Command Line Interface +---------------------- + +- Display help and use the CLI to get quick answers from the terminal / + command line. + +.. code-block:: bash + + ❯❯❯ forex-python -h + usage: forex-python [-h] [-b BASE] [-d DEST] [-a AMOUNT] [-n] + + optional arguments: + -h, --help show this help message and exit + -b BASE, --base BASE Currency you are converting from. (default: USD) + -d DEST, --dest DEST Currency you are converting to. (default: INR) + -a AMOUNT, --amount AMOUNT + Amount to convert. (default: 1.0) + -n, --notify Display desktop alerts. (default: False) + ❯❯❯ forex-python -b EUR + 1.0 EUR = 81.0055 INR + ❯❯❯ forex-python -b EUR -d USD + 1.0 EUR = 1.1389 USD + ❯❯❯ forex-python -b EUR -d USD -a 3.14 + 3.14 EUR = 3.576146 USD + Currency Rates -------------- 1. list all latest currency rates for "USD":: From d5d0a24836d0ee061c05ecd919a358e5f247c96d Mon Sep 17 00:00:00 2001 From: ravigadila Date: Sun, 27 Jan 2019 14:22:33 +0530 Subject: [PATCH 4/4] change rel --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index ae353d2..eb95906 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ import os from setuptools import setup, find_packages -VERSION = '1.1' +VERSION = '1.2' long_description_text = """Forex Python is a Free Foreign exchange rates and currency conversion. Features: List all currency rates.