Skip to content

Commit dd3f73e

Browse files
committed
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
1 parent 9768bc8 commit dd3f73e

File tree

4 files changed

+137
-7
lines changed

4 files changed

+137
-7
lines changed

.travis.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ python:
66
- "3.4"
77
- "3.5"
88

9+
addons:
10+
apt:
11+
packages:
12+
- python3-notify2
13+
- python-notify2
14+
- dbus
15+
916
install:
1017
- python setup.py install
11-
- pip install nose
1218
- pip install coveralls
1319
script:
14-
- nosetests --with-coverage --cover-package=forex_python
20+
- coverage run --source=forex_python -m unittest discover
1521

1622
after_success:
1723
coveralls

forex_python/__main__.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"""Command line utilty for forex-python
2+
=======================================
3+
4+
Inspired by another package: anshulc95/exch
5+
"""
6+
from __future__ import print_function
7+
8+
import argparse
9+
import os
10+
import sys
11+
12+
from . import converter
13+
14+
15+
parser = argparse.ArgumentParser(
16+
formatter_class=argparse.ArgumentDefaultsHelpFormatter
17+
)
18+
parser.add_argument(
19+
"-b", "--base", default="USD", help="Currency you are converting from."
20+
)
21+
parser.add_argument(
22+
"-d", "--dest", default="INR", help="Currency you are converting to."
23+
)
24+
parser.add_argument(
25+
"-a", "--amount", default=1.0, type=float, help="Amount to convert."
26+
)
27+
parser.add_argument(
28+
"-n", "--notify", action="store_true", help="Display desktop alerts."
29+
)
30+
31+
32+
def symbol(currency_code):
33+
sym = converter.get_symbol(currency_code)
34+
if sym is not None:
35+
return sym
36+
else:
37+
return currency_code
38+
39+
40+
def conversion_result(amount, base, converted_amount, dest, use_symbols=False):
41+
if use_symbols:
42+
return "{} {} = {} {}".format(
43+
symbol(base), amount, symbol(dest), converted_amount
44+
)
45+
else:
46+
return "{} {} = {} {}".format(amount, base, converted_amount, dest)
47+
48+
49+
50+
def notify_posix(args):
51+
try:
52+
import notify2
53+
except ImportError:
54+
print("Requires Linux or macOS with notify2 and dbus package.")
55+
raise
56+
notify2.init("forex-python")
57+
notification = conversion_result(
58+
1.0, args.base, converter.get_rate(args.base, args.dest), args.dest,
59+
True
60+
)
61+
n = notify2.Notification(
62+
"forex-python", notification, "notification-message-im" # Icon name
63+
)
64+
n.show()
65+
66+
67+
def run(args=None, output=sys.stdout):
68+
args = parser.parse_args(args)
69+
if args.notify:
70+
if os.name == "posix":
71+
notify_posix(args)
72+
else:
73+
raise ValueError(
74+
"The option [-n] is available only for POSIX operating systems")
75+
else:
76+
print(
77+
conversion_result(
78+
args.amount,
79+
args.base,
80+
converter.convert(args.base, args.dest, args.amount),
81+
args.dest,
82+
),
83+
file=output
84+
)

setup.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import io
21
import os
32
from setuptools import setup, find_packages
43

@@ -19,6 +18,17 @@
1918
2019
"""
2120

21+
install_requires=[
22+
'requests',
23+
'simplejson',
24+
]
25+
26+
if os.name == 'posix':
27+
install_requires += [
28+
'notify2',
29+
'dbus-python'
30+
]
31+
2232
setup(
2333
name='forex-python',
2434
version=VERSION,
@@ -29,10 +39,7 @@
2939
long_description=long_description_text,
3040
packages=find_packages(exclude=['tests', 'tests.*']),
3141
include_package_data=True,
32-
install_requires=[
33-
'requests',
34-
'simplejson',
35-
],
42+
install_requires=install_requires,
3643
classifiers=[
3744
'Intended Audience :: Developers',
3845
'Operating System :: OS Independent',
@@ -45,4 +52,8 @@
4552
'Programming Language :: Python :: 3.5',
4653
'Topic :: Software Development :: Internationalization',
4754
],
55+
entry_points={
56+
'console_scripts':
57+
['forex-python=forex_python.__main__:run']
58+
},
4859
)

tests/test_main.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import unittest
2+
import os
3+
from forex_python.__main__ import run, symbol
4+
try:
5+
import dbus
6+
except ImportError:
7+
dbus = False
8+
9+
10+
class TestCLI(unittest.TestCase):
11+
"""Test forex-python command-line interface."""
12+
13+
def test_defaults(self):
14+
with open(os.devnull, "w") as null:
15+
run([], output=null)
16+
17+
@unittest.skipIf(not dbus, "dbus is not available")
18+
def test_notify(self):
19+
run(["--notify"])
20+
21+
def test_options(self):
22+
with open(os.devnull, "w") as null:
23+
run(["-b", "GBP", "-d", "EUR", "-a", "121"], null)
24+
25+
def test_symbol(self):
26+
assert symbol("EUR") == u"\u20ac"
27+
28+
if __name__ == "__main__":
29+
unittest.main()

0 commit comments

Comments
 (0)