Skip to content

Commit

Permalink
Merge pull request #52 from ashwinvis/notify
Browse files Browse the repository at this point in the history
CLI tool with notification support
  • Loading branch information
Ravi kumar authored Jan 27, 2019
2 parents 9768bc8 + a72221e commit b2b58bd
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 7 deletions.
10 changes: 8 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
-----------------
Expand Down Expand Up @@ -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`_

Expand Down
25 changes: 25 additions & 0 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
@@ -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"::
Expand Down
77 changes: 77 additions & 0 deletions forex_python/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""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(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
)
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(args, 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), file=output)
21 changes: 16 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import io
import os
from setuptools import setup, find_packages

Expand All @@ -19,6 +18,17 @@
"""

install_requires=[
'requests',
'simplejson',
]

if os.name == 'posix':
install_requires += [
'notify2',
'dbus-python'
]

setup(
name='forex-python',
version=VERSION,
Expand All @@ -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',
Expand All @@ -45,4 +52,8 @@
'Programming Language :: Python :: 3.5',
'Topic :: Software Development :: Internationalization',
],
entry_points={
'console_scripts':
['forex-python=forex_python.__main__:run']
},
)
29 changes: 29 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit b2b58bd

Please sign in to comment.