From 84e77134a55acb55d0ff030ebb0e2d476f773244 Mon Sep 17 00:00:00 2001 From: inso <insomniak.fr@gmaiL.com> Date: Tue, 10 Jan 2017 08:50:43 +0100 Subject: [PATCH] Fix proxy and quantitative --- src/sakia/app.py | 4 +-- src/sakia/data/connectors/node.py | 34 ++++++++++++------- src/sakia/data/processors/dividends.py | 3 +- src/sakia/gui/dialogs/connection_cfg/model.py | 4 +-- src/sakia/money/quantitative.py | 2 +- src/sakia/services/network.py | 9 +++-- .../tests/unit/data/test_node_connector.py | 2 +- .../tests/unit/money/test_quantitative.py | 16 ++++----- 8 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/sakia/app.py b/src/sakia/app.py index fa83118f..57daa5bc 100644 --- a/src/sakia/app.py +++ b/src/sakia/app.py @@ -168,7 +168,8 @@ class Application(QObject): try: with aiohttp.ClientSession() as session: with aiohttp.Timeout(15): - response = await session.get("https://api.github.com/repos/duniter/sakia/releases", proxy=proxy) + response = await session.get("https://api.github.com/repos/duniter/sakia/releases", + proxy=self.parameters.proxy()) if response.status == 200: releases = await response.json() latest = None @@ -187,7 +188,6 @@ class Application(QObject): logging.debug("Found version : {0}".format(latest_version)) logging.debug("Current version : {0}".format(__version__)) self.available_version = version - self.version_requested.emit() except (aiohttp.errors.ClientError, aiohttp.errors.TimeoutError) as e: self._logger.debug("Could not connect to github : {0}".format(str(e))) diff --git a/src/sakia/data/connectors/node.py b/src/sakia/data/connectors/node.py index e96a3793..8bcd80c8 100644 --- a/src/sakia/data/connectors/node.py +++ b/src/sakia/data/connectors/node.py @@ -26,7 +26,7 @@ class NodeConnector(QObject): identity_changed = pyqtSignal() neighbour_found = pyqtSignal(Peer) - def __init__(self, node, session): + def __init__(self, node, user_parameters, session=None): """ Constructor """ @@ -36,6 +36,7 @@ class NodeConnector(QObject): 'peer': None} self._connected = {'block': False, 'peer': False} + self._user_parameters = user_parameters self.session = session self._refresh_counter = 1 self._logger = logging.getLogger('sakia') @@ -46,7 +47,7 @@ class NodeConnector(QObject): ws.cancel() @classmethod - async def from_address(cls, currency, secured, address, port, proxy): + async def from_address(cls, currency, secured, address, port, user_parameters): """ Factory method to get a node from a given address :param str currency: The node currency. None if we don't know\ @@ -54,14 +55,14 @@ class NodeConnector(QObject): :param bool secured: True if the node uses https :param str address: The node address :param int port: The node port - :param aiohttp.ClientSession session: The client session :return: A new node :rtype: sakia.core.net.Node """ http_scheme = "https" if secured else "http" ws_scheme = "ws" if secured else "wss" session = aiohttp.ClientSession() - peer_data = await bma.network.peering(ConnectionHandler(http_scheme, ws_scheme, address, port, proxy, session)) + peer_data = await bma.network.peering(ConnectionHandler(http_scheme, ws_scheme, address, port, + proxy=user_parameters.proxy(), session=session)) peer = Peer.from_signed_raw("{0}{1}\n".format(peer_data['raw'], peer_data['signature'])) @@ -72,10 +73,10 @@ class NodeConnector(QObject): node = Node(peer.currency, peer.pubkey, peer.endpoints, peer.blockUID) logging.getLogger('sakia').debug("Node from address : {:}".format(str(node))) - return cls(node, session) + return cls(node, session, user_parameters) @classmethod - def from_peer(cls, currency, peer): + def from_peer(cls, currency, peer, user_parameters): """ Factory method to get a node from a peer document. :param str currency: The node currency. None if we don't know\ @@ -90,7 +91,7 @@ class NodeConnector(QObject): node = Node(peer.currency, peer.pubkey, peer.endpoints, peer.blockUID) logging.getLogger('sakia').debug("Node from peer : {:}".format(str(node))) - return cls(node, None) + return cls(node, None, user_parameters) async def safe_request(self, endpoint, request, proxy, req_args={}): try: @@ -155,7 +156,7 @@ class NodeConnector(QObject): for endpoint in [e for e in self.node.endpoints if isinstance(e, BMAEndpoint)]: if not self._connected['block']: try: - conn_handler = endpoint.conn_handler(self.session) + conn_handler = endpoint.conn_handler(self.session, proxy=self._user_parameters.proxy()) ws_connection = bma.ws.block(conn_handler) async with ws_connection as ws: self._connected['block'] = True @@ -195,7 +196,8 @@ class NodeConnector(QObject): """ for endpoint in [e for e in self.node.endpoints if isinstance(e, BMAEndpoint)]: try: - block_data = await self.safe_request(endpoint, bma.blockchain.current) + block_data = await self.safe_request(endpoint, bma.blockchain.current, + proxy=self._user_parameters.proxy()) if not block_data: continue await self.refresh_block(block_data) @@ -221,10 +223,12 @@ class NodeConnector(QObject): self.node.state = Node.ONLINE if not self.node.current_buid or self.node.current_buid.sha_hash != block_data['hash']: for endpoint in [e for e in self.node.endpoints if isinstance(e, BMAEndpoint)]: - conn_handler = endpoint.conn_handler(self.session) + conn_handler = endpoint.conn_handler(self.session, + proxy=self._user_parameters.proxy()) self._logger.debug("Requesting {0}".format(conn_handler)) try: previous_block = await self.safe_request(endpoint, bma.blockchain.block, + proxy=self._user_parameters.proxy(), req_args={'number': self.node.current_buid.number}) if not previous_block: continue @@ -256,7 +260,8 @@ class NodeConnector(QObject): """ for endpoint in [e for e in self.node.endpoints if isinstance(e, BMAEndpoint)]: try: - summary_data = await self.safe_request(endpoint, bma.node.summary) + summary_data = await self.safe_request(endpoint, bma.node.summary, + proxy=self._user_parameters.proxy()) if not summary_data: continue self.node.software = summary_data["duniter"]["software"] @@ -281,6 +286,7 @@ class NodeConnector(QObject): for endpoint in [e for e in self.node.endpoints if isinstance(e, BMAEndpoint)]: try: data = await self.safe_request(endpoint, bma.wot.lookup, + proxy=self._user_parameters.proxy(), req_args={'search':self.node.pubkey}) if not data: continue @@ -317,7 +323,8 @@ class NodeConnector(QObject): for endpoint in [e for e in self.node.endpoints if isinstance(e, BMAEndpoint)]: if not self._connected['peer']: try: - conn_handler = endpoint.conn_handler(self.session) + conn_handler = endpoint.conn_handler(self.session, + proxy=self._user_parameters.proxy()) ws_connection = bma.ws.peer(conn_handler) async with ws_connection as ws: self._connected['peer'] = True @@ -360,7 +367,8 @@ class NodeConnector(QObject): for endpoint in [e for e in self.node.endpoints if isinstance(e, BMAEndpoint)]: try: peers_data = await self.safe_request(endpoint, bma.network.peers, - req_args={'leaves': 'true'}) + req_args={'leaves': 'true'}, + proxy=self._user_parameters.proxy()) if not peers_data: continue self.node.state = Node.ONLINE diff --git a/src/sakia/data/processors/dividends.py b/src/sakia/data/processors/dividends.py index ed37c7b3..7c5b44cf 100644 --- a/src/sakia/data/processors/dividends.py +++ b/src/sakia/data/processors/dividends.py @@ -1,5 +1,5 @@ import attr -import re +import logging from ..entities import Dividend from .nodes import NodesProcessor from ..connectors import BmaConnector @@ -17,6 +17,7 @@ class DividendsProcessor: """ _repo = attr.ib() _bma_connector = attr.ib() + _logger = attr.ib(default=attr.Factory(lambda: logging.getLogger('sakia'))) @classmethod def instanciate(cls, app): diff --git a/src/sakia/gui/dialogs/connection_cfg/model.py b/src/sakia/gui/dialogs/connection_cfg/model.py index 48420919..145449ed 100644 --- a/src/sakia/gui/dialogs/connection_cfg/model.py +++ b/src/sakia/gui/dialogs/connection_cfg/model.py @@ -31,7 +31,7 @@ class ConnectionConfigModel(QObject): async def create_connection(self, server, port, secured): self.node_connector = await NodeConnector.from_address(None, secured, server, port, - proxy=self.app.parameters.proxy()) + user_parameters=self.app.parameters) self.connection = Connection(self.node_connector.node.currency, "", "") self.node_connector.node.state = Node.ONLINE @@ -169,7 +169,7 @@ class ConnectionConfigModel(QObject): try: data = await self.node_connector.safe_request(endpoint, bma.wot.lookup, req_args={'search': search}, - proxy=self.app.parameters.proxy()) + proxy=self.user_parameters.proxy()) if data: registered = parser(data) tries += 1 diff --git a/src/sakia/money/quantitative.py b/src/sakia/money/quantitative.py index ea7aa469..1059f42a 100644 --- a/src/sakia/money/quantitative.py +++ b/src/sakia/money/quantitative.py @@ -75,7 +75,7 @@ class Quantitative(BaseReferential): scientific_value = value exponent = 0 - while scientific_value > 1000 and int(scientific_value) * 10**exponent == scientific_value: + while scientific_value > 1000 and int(scientific_value) * 10**exponent == value: exponent += 3 scientific_value /= 1000 diff --git a/src/sakia/services/network.py b/src/sakia/services/network.py index b5cc0a3f..df27163c 100644 --- a/src/sakia/services/network.py +++ b/src/sakia/services/network.py @@ -21,7 +21,7 @@ class NetworkService(QObject): nodes_changed = pyqtSignal() root_nodes_changed = pyqtSignal() - def __init__(self, app, currency, node_processor, connectors, session, blockchain_service): + def __init__(self, app, currency, node_processor, connectors, blockchain_service): """ Constructor of a network @@ -29,7 +29,6 @@ class NetworkService(QObject): :param str currency: The currency name of the community :param sakia.data.processors.NodesProcessor node_processor: the nodes processor for given currency :param list connectors: The connectors to nodes of the network - :param aiohttp.ClientSession session: The main aiohttp client session :param sakia.services.BlockchainService blockchain_service: the blockchain service """ super().__init__() @@ -74,8 +73,8 @@ class NetworkService(QObject): """ connectors = [] for node in node_processor.nodes(currency): - connectors.append(NodeConnector(node, None)) - network = cls(app, currency, node_processor, connectors, None, blockchain_service) + connectors.append(NodeConnector(node, app.parameters)) + network = cls(app, currency, node_processor, connectors, blockchain_service) return network def start_coroutines(self): @@ -201,7 +200,7 @@ class NetworkService(QObject): if self._processor.unknown_node(self.currency, peer.pubkey): self._logger.debug("New node found : {0}".format(peer.pubkey[:5])) try: - connector = NodeConnector.from_peer(self.currency, peer) + connector = NodeConnector.from_peer(self.currency, peer, self.app.parameters) self._processor.insert_node(connector.node) await connector.init_session() connector.refresh(manual=True) diff --git a/src/sakia/tests/unit/data/test_node_connector.py b/src/sakia/tests/unit/data/test_node_connector.py index 9b316e01..8f0515aa 100644 --- a/src/sakia/tests/unit/data/test_node_connector.py +++ b/src/sakia/tests/unit/data/test_node_connector.py @@ -12,7 +12,7 @@ Endpoints: BASIC_MERKLED_API duniter.inso.ovh 80 82o1sNCh1bLpUXU6nacbK48HBcA9Eu2sPkL1/3c2GtDPxBUZd2U2sb7DxwJ54n6ce9G0Oy7nd1hCxN3fS0oADw== """) - connector = NodeConnector.from_peer('meta_brouzouf', peer) + connector = NodeConnector.from_peer('meta_brouzouf', peer, None) assert connector.node.pubkey == "8Fi1VSTbjkXguwThF4v2ZxC5whK7pwG2vcGTkPUPjPGU" assert connector.node.endpoints[0].inline() == "BASIC_MERKLED_API duniter.inso.ovh 80" assert connector.node.currency == "meta_brouzouf" diff --git a/src/sakia/tests/unit/money/test_quantitative.py b/src/sakia/tests/unit/money/test_quantitative.py index e8cf769a..74dd67f6 100644 --- a/src/sakia/tests/unit/money/test_quantitative.py +++ b/src/sakia/tests/unit/money/test_quantitative.py @@ -31,9 +31,9 @@ def test_localized_no_si(application_with_one_connection, bob): def test_localized_with_si(application_with_one_connection, bob): application_with_one_connection.parameters.digits_after_comma = 6 - referential = Quantitative(101000000, bob.currency, application_with_one_connection, None) + referential = Quantitative(101010000, bob.currency, application_with_one_connection, None) value = referential.localized(units=True, international_system=True) - assert value == "1,010.00 x10³ TC" + assert value == "1,010.10 x10³ TC" def test_localized_no_units_no_si(application_with_one_connection, bob): @@ -45,9 +45,9 @@ def test_localized_no_units_no_si(application_with_one_connection, bob): def test_localized_no_units_with_si(application_with_one_connection, bob): application_with_one_connection.parameters.digits_after_comma = 6 - referential = Quantitative(101000000, bob.currency, application_with_one_connection, None) + referential = Quantitative(101010000, bob.currency, application_with_one_connection, None) value = referential.localized(units=False, international_system=True) - assert value == "1,010.00 x10³" + assert value == "1,010.10 x10³" def test_diff_localized_no_si(application_with_one_connection, bob): @@ -58,9 +58,9 @@ def test_diff_localized_no_si(application_with_one_connection, bob): def test_diff_localized_with_si(application_with_one_connection, bob): application_with_one_connection.parameters.digits_after_comma = 6 - referential = Quantitative(101000000, bob.currency, application_with_one_connection, None) + referential = Quantitative(101010000, bob.currency, application_with_one_connection, None) value = referential.diff_localized(units=True, international_system=True) - assert value == "1,010.00 x10³ TC" + assert value == "1,010.10 x10³ TC" def test_diff_localized_no_units_no_si(application_with_one_connection, bob): @@ -72,6 +72,6 @@ def test_diff_localized_no_units_no_si(application_with_one_connection, bob): def test_diff_localized_no_units_with_si(application_with_one_connection, bob): application_with_one_connection.parameters.digits_after_comma = 6 - referential = Quantitative(101000000, bob.currency, application_with_one_connection, None) + referential = Quantitative(10100000000, bob.currency, application_with_one_connection, None) value = referential.diff_localized(units=False, international_system=True) - assert value == "1,010.00 x10³" + assert value == "101.00 x10â¶" -- GitLab