-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from ashwinvis/notify
CLI tool with notification support
- Loading branch information
Showing
6 changed files
with
166 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |