Skip to content

Commit

Permalink
style: linting via black
Browse files Browse the repository at this point in the history
  • Loading branch information
xeroc committed May 13, 2022
1 parent 7be5f41 commit c3a9b15
Show file tree
Hide file tree
Showing 68 changed files with 1,577 additions and 1,754 deletions.
12 changes: 4 additions & 8 deletions grapheneapi/aio/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ def get_network(self):
return self._chain_props

async def cache_chain_properties(self):
""" Cache chain properties to prevent turning lots of methods into async
"""
"""Cache chain properties to prevent turning lots of methods into async"""
self._chain_props = await self.get_chain_properties()

def get_cached_chain_properties(self):
Expand All @@ -58,8 +57,7 @@ async def next(self):
await self.connect()

async def find_next(self):
""" Find the next url in the list
"""
"""Find the next url in the list"""
if int(self.num_retries) < 0: # pragma: no cover
self._cnt_retries += 1
sleeptime = (self._cnt_retries - 1) * 2 if self._cnt_retries < 10 else 10
Expand Down Expand Up @@ -103,8 +101,7 @@ async def func(*args, **kwargs):
except KeyboardInterrupt: # pragma: no cover
raise
except RPCError as e: # pragma: no cover
""" When the backend actual returns an error
"""
"""When the backend actual returns an error"""
self.post_process_exception(e)
# the above line should raise. Let's be sure to at least
# break
Expand All @@ -118,8 +115,7 @@ async def func(*args, **kwargs):
self.error_url()
self.next()
except Exception as e: # pragma: no cover
""" When something fails talking to the backend
"""
"""When something fails talking to the backend"""
import traceback

log.debug(traceback.format_exc())
Expand Down
9 changes: 4 additions & 5 deletions grapheneapi/aio/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@


class Http(Rpc):
""" RPC Calls
"""
"""RPC Calls"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -24,10 +23,10 @@ async def disconnect(self):
await self.session.close()

async def rpcexec(self, payload):
""" Execute a RPC call
"""Execute a RPC call
:param dict payload: json-rpc request in format:
{"jsonrpc": "2.0", "method": "call", "params": "[x, y, z]", "id": 1}
:param dict payload: json-rpc request in format:
{"jsonrpc": "2.0", "method": "call", "params": "[x, y, z]", "id": 1}
"""
async with self.session.post(self.url, json=payload) as response:
response_text = await response.text()
Expand Down
6 changes: 3 additions & 3 deletions grapheneapi/aio/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ def __init__(self, url, *args, loop=None, **kwargs):
self.loop = kwargs.get(loop, asyncio.get_event_loop())

def __getattr__(self, name):
""" Map all methods to RPC calls and pass through the arguments
"""Map all methods to RPC calls and pass through the arguments
This method is actually defined in RPC class, but we need to
overwrite this here so that we can use async/await.
This method is actually defined in RPC class, but we need to
overwrite this here so that we can use async/await.
"""

async def method(*args, **kwargs):
Expand Down
22 changes: 11 additions & 11 deletions grapheneapi/aio/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def disconnect(self):
await self.ws.close()

async def _parsing_wrapper(self):
""" Wraps parse_messages() coroutine to retrieve and handle exceptions
"""Wraps parse_messages() coroutine to retrieve and handle exceptions
When parse_messages() stopped for any reason, websocket transport should be
stopped, and get_response_by_id() should be notified about broken parser
Expand All @@ -52,12 +52,12 @@ async def _parsing_wrapper(self):
self._event.set()

async def parse_messages(self):
""" Listen websocket for incoming messages in infinity manner
"""Listen websocket for incoming messages in infinity manner
Messages which are responses (has id) are stored in dict, while
messages which are notifies are stored in asyncio queue and are
supposed to be processed later by whom who sets the subscribtion
callback
Messages which are responses (has id) are stored in dict, while
messages which are notifies are stored in asyncio queue and are
supposed to be processed later by whom who sets the subscribtion
callback
"""

async for message in self.ws:
Expand Down Expand Up @@ -97,9 +97,9 @@ async def parse_messages(self):
await self.notifications.put(m)

async def get_response_by_id(self, request_id):
""" Pop response from dict containing all query results
"""Pop response from dict containing all query results
:param int request_id: request id to get response to
:param int request_id: request id to get response to
"""
response = None
while not response:
Expand All @@ -114,10 +114,10 @@ async def get_response_by_id(self, request_id):
return response

async def rpcexec(self, payload):
""" Execute a RPC call
"""Execute a RPC call
:param dict payload: json-rpc request in format:
{"jsonrpc": "2.0", "method": "call", "params": "[x, y, z]", "id": 1}
:param dict payload: json-rpc request in format:
{"jsonrpc": "2.0", "method": "call", "params": "[x, y, z]", "id": 1}
"""
if not self.ws:
await self.connect()
Expand Down
42 changes: 19 additions & 23 deletions grapheneapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ def connect(self):
self.register_apis()

def find_next(self):
""" Find the next url in the list
"""
"""Find the next url in the list"""
if int(self.num_retries) < 0: # pragma: no cover
self._cnt_retries += 1
sleeptime = (self._cnt_retries - 1) * 2 if self._cnt_retries < 10 else 10
Expand Down Expand Up @@ -119,8 +118,7 @@ def find_next(self):
return url

def reset_counter(self):
""" reset the failed connection counters
"""
"""reset the failed connection counters"""
self._cnt_retries = 0
for i in self._url_counter:
self._url_counter[i] = 0
Expand All @@ -141,34 +139,34 @@ def post_process_exception(self, exception):

@property
def api_id(self):
""" This allows to list api_ids, if they have been registered through
api_register() -- LEGACY
"""This allows to list api_ids, if they have been registered through
api_register() -- LEGACY
In previous API version, one would connect and register to APIs
like this
In previous API version, one would connect and register to APIs
like this
.. code-block:: python
.. code-block:: python
self.api_id["database"] = self.database(api_id=1)
self.api_id["history"] = self.history(api_id=1)
self.api_id["network_broadcast"] = self.network_broadcast(
api_id=1)
self.api_id["database"] = self.database(api_id=1)
self.api_id["history"] = self.history(api_id=1)
self.api_id["network_broadcast"] = self.network_broadcast(
api_id=1)
"""
return self.connection.api_id

def register_apis(self): # pragma: no cover
""" This method is called right after connection and has previously
been used to register to different APIs within the backend that are
considered default. The requirement to register to APIs has been
removed in some systems.
"""This method is called right after connection and has previously
been used to register to different APIs within the backend that are
considered default. The requirement to register to APIs has been
removed in some systems.
"""
pass

def __getattr__(self, name):
""" Proxies RPC calls to actual Websocket or Http instance.
"""Proxies RPC calls to actual Websocket or Http instance.
Connection-related errors catched here and handled.
Connection-related errors catched here and handled.
"""

def func(*args, **kwargs):
Expand All @@ -182,8 +180,7 @@ def func(*args, **kwargs):
except KeyboardInterrupt: # pragma: no cover
raise
except RPCError as e: # pragma: no cover
""" When the backend actual returns an error
"""
"""When the backend actual returns an error"""
self.post_process_exception(e)
# the above line should raise. Let's be sure to at least
# break
Expand All @@ -197,8 +194,7 @@ def func(*args, **kwargs):
self.error_url()
self.next()
except Exception as e: # pragma: no cover
""" When something fails talking to the backend
"""
"""When something fails talking to the backend"""
import traceback

log.debug(traceback.format_exc())
Expand Down
85 changes: 42 additions & 43 deletions grapheneapi/grapheneapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,49 @@


class GrapheneAPI(object):
""" Graphene JSON-HTTP-RPC API
"""Graphene JSON-HTTP-RPC API
This class serves as an abstraction layer for easy use of the
Grapehene API.
This class serves as an abstraction layer for easy use of the
Grapehene API.
:param str host: Host of the API server
:param int port: Port to connect to
:param str username: Username for Authentication (if required,
defaults to "")
:param str password: Password for Authentication (if required,
defaults to "")
:param str host: Host of the API server
:param int port: Port to connect to
:param str username: Username for Authentication (if required,
defaults to "")
:param str password: Password for Authentication (if required,
defaults to "")
All RPC commands of the Graphene client are exposed as methods
in the class ``grapheneapi``. Once an instance of GrapheneAPI is
created with host, port, username, and password, e.g.,
All RPC commands of the Graphene client are exposed as methods
in the class ``grapheneapi``. Once an instance of GrapheneAPI is
created with host, port, username, and password, e.g.,
.. code-block:: python
.. code-block:: python
from grapheneapi import GrapheneAPI
rpc = GrapheneAPI("localhost", 8092, "", "")
from grapheneapi import GrapheneAPI
rpc = GrapheneAPI("localhost", 8092, "", "")
any call available to that port can be issued using the instance
via the syntax rpc.*command*(*parameters*). Example:
any call available to that port can be issued using the instance
via the syntax rpc.*command*(*parameters*). Example:
.. code-block:: python
.. code-block:: python
rpc.info()
rpc.info()
.. note:: A distinction has to be made whether the connection is
made to a **witness/full node** which handles the
blockchain and P2P network, or a **cli-wallet** that
handles wallet related actions! The available commands
differ drastically!
.. note:: A distinction has to be made whether the connection is
made to a **witness/full node** which handles the
blockchain and P2P network, or a **cli-wallet** that
handles wallet related actions! The available commands
differ drastically!
If you are connected to a wallet, you can simply initiate a transfer with:
If you are connected to a wallet, you can simply initiate a transfer with:
.. code-block:: python
.. code-block:: python
res = client.transfer("sender","receiver","5", "USD", "memo", True);
res = client.transfer("sender","receiver","5", "USD", "memo", True);
Again, the witness node does not offer access to construct any transactions,
and hence the calls available to the witness-rpc can be seen as read-only for
the blockchain.
Again, the witness node does not offer access to construct any transactions,
and hence the calls available to the witness-rpc can be seen as read-only for
the blockchain.
"""

def __init__(self, host, port, username="", password=""):
Expand All @@ -66,20 +66,20 @@ def __init__(self, host, port, username="", password=""):
self.headers = {"content-type": "application/json"}

def rpcexec(self, payload):
""" Manual execute a command on API (internally used)
"""Manual execute a command on API (internally used)
param str payload: The payload containing the request
return: Servers answer to the query
rtype: json
raises RPCConnection: if no connction can be made
raises UnauthorizedError: if the user is not authorized
raise ValueError: if the API returns a non-JSON formated answer
param str payload: The payload containing the request
return: Servers answer to the query
rtype: json
raises RPCConnection: if no connction can be made
raises UnauthorizedError: if the user is not authorized
raise ValueError: if the API returns a non-JSON formated answer
It is not recommended to use this method directly, unless
you know what you are doing. All calls available to the API
will be wrapped to methods directly::
It is not recommended to use this method directly, unless
you know what you are doing. All calls available to the API
will be wrapped to methods directly::
info -> grapheneapi.info()
info -> grapheneapi.info()
"""
try:
response = requests.post(
Expand Down Expand Up @@ -108,8 +108,7 @@ def rpcexec(self, payload):
return ret["result"]

def __getattr__(self, name):
""" Map all methods to RPC calls and pass through the arguments
"""
"""Map all methods to RPC calls and pass through the arguments"""

def method(*args):
query = {"method": name, "params": args, "jsonrpc": "2.0", "id": 0}
Expand Down
15 changes: 7 additions & 8 deletions grapheneapi/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@


class Http(Rpc):
""" RPC Calls
"""
"""RPC Calls"""

def proxies(self): # pragma: no cover
proxy_url = self.get_proxy_url()
Expand All @@ -20,13 +19,13 @@ def proxies(self): # pragma: no cover
return {"http": proxy_url, "https": proxy_url}

def rpcexec(self, payload):
""" Execute a call by sending the payload
"""Execute a call by sending the payload
:param json payload: Payload data
:raises ValueError: if the server does not respond in proper JSON
format
:raises HttpInvalidStatusCode: if the server returns a status code
that is not 200
:param json payload: Payload data
:raises ValueError: if the server does not respond in proper JSON
format
:raises HttpInvalidStatusCode: if the server returns a status code
that is not 200
"""
log.debug(json.dumps(payload))
query = requests.post(self.url, json=payload, proxies=self.proxies())
Expand Down
Loading

0 comments on commit c3a9b15

Please sign in to comment.