]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/commitdiff
Add quiet flag for running
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Thu, 22 Mar 2018 20:35:00 +0000 (21:35 +0100)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Fri, 23 Mar 2018 22:49:32 +0000 (23:49 +0100)
main.py
market.py
test.py

diff --git a/main.py b/main.py
index 856d449fc16cf94263c81ad45b11bcb406a12ce0..55981bf94d82c966aea93e3f89ea25a01cdb7c79 100644 (file)
--- a/main.py
+++ b/main.py
@@ -63,7 +63,8 @@ def make_order(market, value, currency, action="acquire",
 def get_user_market(config_path, user_id, debug=False):
     pg_config, report_path = parse_config(config_path)
     market_config = list(fetch_markets(pg_config, str(user_id)))[0][0]
-    return market.Market.from_config(market_config, debug=debug)
+    args = type('Args', (object,), { "debug": debug, "quiet": False })()
+    return market.Market.from_config(market_config, args, user_id=user_id, report_path=report_path)
 
 def fetch_markets(pg_config, user):
     connection = psycopg2.connect(**pg_config)
@@ -109,6 +110,9 @@ def parse_args(argv):
     parser.add_argument("--after",
             default=False, action='store_const', const=True,
             help="Run the steps after the cryptoportfolio update")
+    parser.add_argument("--quiet",
+            default=False, action='store_const', const=True,
+            help="Don't print messages")
     parser.add_argument("--debug",
             default=False, action='store_const', const=True,
             help="Run in debug mode")
@@ -131,7 +135,7 @@ def parse_args(argv):
 def process(market_config, user_id, report_path, args):
     try:
         market.Market\
-                .from_config(market_config, debug=args.debug, user_id=user_id, report_path=report_path)\
+                .from_config(market_config, args, user_id=user_id, report_path=report_path)\
                 .process(args.action, before=args.before, after=args.after)
     except Exception as e:
         print("{}: {}".format(e.__class__.__name__, e))
index 2ddebfac3795ebc51167676255e16e4113a834a5..fc5832c089fc6eb63a97ec1621dab65eca406a2e 100644 (file)
--- a/market.py
+++ b/market.py
@@ -13,11 +13,12 @@ class Market:
     trades = None
     balances = None
 
-    def __init__(self, ccxt_instance, debug=False, user_id=None, report_path=None):
-        self.debug = debug
+    def __init__(self, ccxt_instance, args, user_id=None, report_path=None):
+        self.args = args
+        self.debug = args.debug
         self.ccxt = ccxt_instance
         self.ccxt._market = self
-        self.report = ReportStore(self)
+        self.report = ReportStore(self, verbose_print=(not args.quiet))
         self.trades = TradeStore(self)
         self.balances = BalanceStore(self)
         self.processor = Processor(self)
@@ -26,7 +27,7 @@ class Market:
         self.report_path = report_path
 
     @classmethod
-    def from_config(cls, config, debug=False, user_id=None, report_path=None):
+    def from_config(cls, config, args, user_id=None, report_path=None):
         config["apiKey"] = config.pop("key", None)
 
         ccxt_instance = ccxt.poloniexE(config)
@@ -43,7 +44,7 @@ class Market:
         ccxt_instance.session.request = request_wrap.__get__(ccxt_instance.session,
                 ccxt_instance.session.__class__)
 
-        return cls(ccxt_instance, debug=debug, user_id=user_id, report_path=report_path)
+        return cls(ccxt_instance, args, user_id=user_id, report_path=report_path)
 
     def store_report(self):
         self.report.merge(Portfolio.report)
diff --git a/test.py b/test.py
index 13bd332405526ae0e1af0a9b568bf38446506410..3ee34c69a4ff974e428854107527c4070b858be7 100644 (file)
--- a/test.py
+++ b/test.py
@@ -23,6 +23,9 @@ for test_type in limits:
 class WebMockTestCase(unittest.TestCase):
     import time
 
+    def market_args(self, debug=False, quiet=False):
+        return type('Args', (object,), { "debug": debug, "quiet": quiet })()
+
     def setUp(self):
         super(WebMockTestCase, self).setUp()
         self.wm = requests_mock.Mocker()
@@ -1092,7 +1095,7 @@ class MarketTest(WebMockTestCase):
         self.ccxt = mock.Mock(spec=market.ccxt.poloniexE)
 
     def test_values(self):
-        m = market.Market(self.ccxt)
+        m = market.Market(self.ccxt, self.market_args())
 
         self.assertEqual(self.ccxt, m.ccxt)
         self.assertFalse(m.debug)
@@ -1104,19 +1107,27 @@ class MarketTest(WebMockTestCase):
         self.assertEqual(m, m.balances.market)
         self.assertEqual(m, m.ccxt._market)
 
-        m = market.Market(self.ccxt, debug=True)
+        m = market.Market(self.ccxt, self.market_args(debug=True))
         self.assertTrue(m.debug)
 
-        m = market.Market(self.ccxt, debug=False)
+        m = market.Market(self.ccxt, self.market_args(debug=False))
         self.assertFalse(m.debug)
 
+        with mock.patch("market.ReportStore") as report_store:
+            with self.subTest(quiet=False):
+                m = market.Market(self.ccxt, self.market_args(quiet=False))
+                report_store.assert_called_with(m, verbose_print=True)
+            with self.subTest(quiet=True):
+                m = market.Market(self.ccxt, self.market_args(quiet=True))
+                report_store.assert_called_with(m, verbose_print=False)
+
     @mock.patch("market.ccxt")
     def test_from_config(self, ccxt):
         with mock.patch("market.ReportStore"):
             ccxt.poloniexE.return_value = self.ccxt
             self.ccxt.session.request.return_value = "response"
 
-            m = market.Market.from_config({"key": "key", "secred": "secret"})
+            m = market.Market.from_config({"key": "key", "secred": "secret"}, self.market_args())
 
             self.assertEqual(self.ccxt, m.ccxt)
 
@@ -1125,7 +1136,7 @@ class MarketTest(WebMockTestCase):
             m.report.log_http_request.assert_called_with('GET', 'URL', 'data',
                     'headers', 'response')
 
-        m = market.Market.from_config({"key": "key", "secred": "secret"}, debug=True)
+        m = market.Market.from_config({"key": "key", "secred": "secret"}, self.market_args(debug=True))
         self.assertEqual(True, m.debug)
 
     def test_get_tickers(self):
@@ -1134,7 +1145,7 @@ class MarketTest(WebMockTestCase):
                 market.NotSupported
                 ]
 
-        m = market.Market(self.ccxt)
+        m = market.Market(self.ccxt, self.market_args())
         self.assertEqual("tickers", m.get_tickers())
         self.assertEqual("tickers", m.get_tickers())
         self.ccxt.fetch_tickers.assert_called_once()
@@ -1147,7 +1158,7 @@ class MarketTest(WebMockTestCase):
                     "ETH/ETC": { "bid": 1, "ask": 3 },
                     "XVG/ETH": { "bid": 10, "ask": 40 },
                     }
-            m = market.Market(self.ccxt)
+            m = market.Market(self.ccxt, self.market_args())
 
             ticker = m.get_ticker("ETH", "ETC")
             self.assertEqual(1, ticker["bid"])
@@ -1175,7 +1186,7 @@ class MarketTest(WebMockTestCase):
                     market.ExchangeError("foo"),
                     ]
 
-            m = market.Market(self.ccxt)
+            m = market.Market(self.ccxt, self.market_args())
 
             ticker = m.get_ticker("ETH", "ETC")
             self.ccxt.fetch_ticker.assert_called_with("ETH/ETC")
@@ -1195,7 +1206,7 @@ class MarketTest(WebMockTestCase):
             self.assertIsNone(ticker)
 
     def test_fetch_fees(self):
-        m = market.Market(self.ccxt)
+        m = market.Market(self.ccxt, self.market_args())
         self.ccxt.fetch_fees.return_value = "Foo"
         self.assertEqual("Foo", m.fetch_fees())
         self.ccxt.fetch_fees.assert_called_once()
@@ -1222,7 +1233,7 @@ class MarketTest(WebMockTestCase):
         get_ticker.side_effect = _get_ticker
 
         with mock.patch("market.ReportStore"):
-            m = market.Market(self.ccxt)
+            m = market.Market(self.ccxt, self.market_args())
             self.ccxt.fetch_all_balances.return_value = {
                     "USDT": {
                         "exchange_free": D("10000.0"),
@@ -1262,7 +1273,7 @@ class MarketTest(WebMockTestCase):
                 (False, 12), (True, 12)]:
             with self.subTest(sleep=sleep, debug=debug), \
                     mock.patch("market.ReportStore"):
-                m = market.Market(self.ccxt, debug=debug)
+                m = market.Market(self.ccxt, self.market_args(debug=debug))
 
                 order_mock1 = mock.Mock()
                 order_mock2 = mock.Mock()
@@ -1339,7 +1350,7 @@ class MarketTest(WebMockTestCase):
         for debug in [True, False]:
             with self.subTest(debug=debug),\
                     mock.patch("market.ReportStore"):
-                m = market.Market(self.ccxt, debug=debug)
+                m = market.Market(self.ccxt, self.market_args(debug=debug))
 
                 value_from = portfolio.Amount("BTC", "1.0")
                 value_from.linked_to = portfolio.Amount("ETH", "10.0")
@@ -1378,7 +1389,7 @@ class MarketTest(WebMockTestCase):
     def test_store_report(self):
 
         file_open = mock.mock_open()
-        m = market.Market(self.ccxt, user_id=1)
+        m = market.Market(self.ccxt, self.market_args(), user_id=1)
         with self.subTest(file=None),\
                 mock.patch.object(m, "report") as report,\
                 mock.patch("market.open", file_open):
@@ -1388,7 +1399,7 @@ class MarketTest(WebMockTestCase):
 
         report.reset_mock()
         file_open = mock.mock_open()
-        m = market.Market(self.ccxt, report_path="present", user_id=1)
+        m = market.Market(self.ccxt, self.market_args(), report_path="present", user_id=1)
         with self.subTest(file="present"),\
                 mock.patch("market.open", file_open),\
                 mock.patch.object(m, "report") as report,\
@@ -1409,7 +1420,7 @@ class MarketTest(WebMockTestCase):
 
         report.reset_mock()
 
-        m = market.Market(self.ccxt, report_path="error", user_id=1)
+        m = market.Market(self.ccxt, self.market_args(), report_path="error", user_id=1)
         with self.subTest(file="error"),\
                 mock.patch("market.open") as file_open,\
                 mock.patch.object(m, "report") as report,\
@@ -1422,7 +1433,7 @@ class MarketTest(WebMockTestCase):
             self.assertRegex(stdout_mock.getvalue(), "impossible to store report file: FileNotFoundError;")
 
     def test_print_orders(self):
-        m = market.Market(self.ccxt)
+        m = market.Market(self.ccxt, self.market_args())
         with mock.patch.object(m.report, "log_stage") as log_stage,\
                 mock.patch.object(m.balances, "fetch_balances") as fetch_balances,\
                 mock.patch.object(m, "prepare_trades") as prepare_trades,\
@@ -1436,7 +1447,7 @@ class MarketTest(WebMockTestCase):
             prepare_orders.assert_called_with(compute_value="average")
 
     def test_print_balances(self):
-        m = market.Market(self.ccxt)
+        m = market.Market(self.ccxt, self.market_args())
 
         with mock.patch.object(m.balances, "in_currency") as in_currency,\
                 mock.patch.object(m.report, "log_stage") as log_stage,\
@@ -1461,7 +1472,7 @@ class MarketTest(WebMockTestCase):
     @mock.patch("market.ReportStore.log_error")
     @mock.patch("market.Market.store_report")
     def test_process(self, store_report, log_error, process):
-        m = market.Market(self.ccxt)
+        m = market.Market(self.ccxt, self.market_args())
         with self.subTest(before=False, after=False):
             m.process(None)
 
@@ -3571,7 +3582,7 @@ class MainTest(WebMockTestCase):
             main.process("config", 1, "report_path", args_mock)
 
             market_mock.from_config.assert_has_calls([
-                mock.call("config", debug="debug", user_id=1, report_path="report_path"),
+                mock.call("config", args_mock, user_id=1, report_path="report_path"),
                 mock.call().process("action", before="before", after="after"),
                 ])
 
@@ -3797,7 +3808,7 @@ class ProcessorTest(WebMockTestCase):
 
     def test_method_arguments(self):
         ccxt = mock.Mock(spec=market.ccxt.poloniexE)
-        m = market.Market(ccxt)
+        m = market.Market(ccxt, self.market_args())
 
         processor = market.Processor(m)