Skip to content

Commit

Permalink
linting with black
Browse files Browse the repository at this point in the history
  • Loading branch information
xeroc committed Dec 17, 2018
1 parent ac36e90 commit a265822
Show file tree
Hide file tree
Showing 70 changed files with 2,416 additions and 1,530 deletions.
10 changes: 2 additions & 8 deletions grapheneapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
__all__ = [
'grapheneapi',
"rpc",
"api",
"exceptions",
"http",
"websocket"
]
# -*- coding: utf-8 -*-
__all__ = ["grapheneapi", "rpc", "api", "exceptions", "http", "websocket"]
31 changes: 15 additions & 16 deletions grapheneapi/api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# -*- coding: utf-8 -*-
import logging
from collections import Counter
from itertools import cycle
from time import sleep
from .exceptions import (
RPCError,
NumRetriesReached
)
from .exceptions import RPCError, NumRetriesReached

from .websocket import Websocket
from .http import Http
Expand Down Expand Up @@ -64,9 +62,9 @@ def updated_connection(self):
@property
def connection(self):
if self._active_url != self.url:
log.debug("Updating connection from {} to {}".format(
self._active_url, self.url
))
log.debug(
"Updating connection from {} to {}".format(self._active_url, self.url)
)
self._active_connection = self.updated_connection()
self._active_url = self.url
return self._active_connection
Expand All @@ -85,14 +83,12 @@ def find_next(self):
"""
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
sleeptime = (self._cnt_retries - 1) * 2 if self._cnt_retries < 10 else 10
if sleeptime:
log.warning(
"Lost connection to node during rpcexec(): %s (%d/%d) "
% (self.url, self._cnt_retries, self.num_retries) +
"Retrying in %d seconds" % sleeptime
% (self.url, self._cnt_retries, self.num_retries)
+ "Retrying in %d seconds" % sleeptime
)
sleep(sleeptime)
return next(self.urls)
Expand All @@ -103,13 +99,13 @@ def find_next(self):
if (
# Only provide URLS if num_retries is bigger equal 0,
# i.e. we want to do reconnects at all
int(self.num_retries) >= 0 and
int(self.num_retries) >= 0
# the counter for this host/endpoint should be smaller than
# num_retries
v <= self.num_retries and
and v <= self.num_retries
# let's not retry with the same URL *if* we have others
# available
(k != self.url or len(self._url_counter) == 1)
and (k != self.url or len(self._url_counter) == 1)
)
]
if not len(urls):
Expand Down Expand Up @@ -181,8 +177,9 @@ def func(*args, **kwargs):
# the above line should raise. Let's be sure to at least
# break
break # pragma: no cover
except IOError as e: # pragma: no cover
except IOError: # pragma: no cover
import traceback

log.debug(traceback.format_exc())
log.warning("Connection was closed remotely.")
log.warning("Reconnecting ...")
Expand All @@ -192,11 +189,13 @@ def func(*args, **kwargs):
""" When something fails talking to the backend
"""
import traceback

log.debug(traceback.format_exc())
log.warning(str(e))
log.warning("Reconnecting ...")
self.error_url()
self.next()

return r

return func
37 changes: 18 additions & 19 deletions grapheneapi/grapheneapi.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# -*- coding: utf-8 -*-
import sys
import json
import logging

try:
import requests
except ImportError:
raise ImportError("Missing dependency: python-requests")
from .exceptions import (
RPCError,
UnauthorizedError,
RPCConnection,
)
from .exceptions import RPCError, UnauthorizedError, RPCConnection

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -59,12 +57,13 @@ class GrapheneAPI(object):
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=""):
self.host = host
self.port = port
self.username = username
self.password = password
self.headers = {'content-type': 'application/json'}
self.headers = {"content-type": "application/json"}

def rpcexec(self, payload):
""" Manual execute a command on API (internally used)
Expand All @@ -83,19 +82,20 @@ def rpcexec(self, payload):
info -> grapheneapi.info()
"""
try:
response = requests.post("http://{}:{}/rpc".format(self.host,
self.port),
data=json.dumps(payload, ensure_ascii=False).encode('utf8'),
headers=self.headers,
auth=(self.username, self.password))
response = requests.post(
"http://{}:{}/rpc".format(self.host, self.port),
data=json.dumps(payload, ensure_ascii=False).encode("utf8"),
headers=self.headers,
auth=(self.username, self.password),
)
if response.status_code == 401:
raise UnauthorizedError
ret = json.loads(response.text)
if 'error' in ret:
if 'detail' in ret['error']:
raise RPCError(ret['error']['detail'])
if "error" in ret:
if "detail" in ret["error"]:
raise RPCError(ret["error"]["detail"])
else:
raise RPCError(ret['error']['message'])
raise RPCError(ret["error"]["message"])
except requests.exceptions.RequestException:
raise RPCConnection("Error connecting to Client!")
except UnauthorizedError:
Expand All @@ -110,11 +110,10 @@ def rpcexec(self, payload):
def __getattr__(self, name):
""" Map all methods to RPC calls and pass through the arguments
"""

def method(*args):
query = {"method": name,
"params": args,
"jsonrpc": "2.0",
"id": 0}
query = {"method": name, "params": args, "jsonrpc": "2.0", "id": 0}
r = self.rpcexec(query)
return r

return method
23 changes: 8 additions & 15 deletions grapheneapi/http.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# -*- coding: utf-8 -*-
import json
import time
import logging
import requests
from .exceptions import (
RPCError,
HttpInvalidStatusCode
)
from .exceptions import RPCError, HttpInvalidStatusCode
from .rpc import Rpc

log = logging.getLogger(__name__)
Expand All @@ -14,14 +12,12 @@
class Http(Rpc):
""" RPC Calls
"""

def proxies(self):
proxy_url = self.get_proxy_url()
if proxy_url is None:
return None
return {
"http": proxy_url,
"https": proxy_url
}
return {"http": proxy_url, "https": proxy_url}

def rpcexec(self, payload):
""" Execute a call by sending the payload
Expand All @@ -33,13 +29,10 @@ def rpcexec(self, payload):
that is not 200
"""
log.debug(json.dumps(payload))
query = requests.post(
self.url,
json=payload,
proxies=self.proxies()
)
query = requests.post(self.url, json=payload, proxies=self.proxies())
if query.status_code != 200: # pragma: no cover
raise HttpInvalidStatusCode("Status code returned: {}".format(
query.status_code))
raise HttpInvalidStatusCode(
"Status code returned: {}".format(query.status_code)
)

return query.text
50 changes: 30 additions & 20 deletions grapheneapi/rpc.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# -*- coding: utf-8 -*-
import json
import time
import logging
import requests
import urllib
from .exceptions import (
RPCError,
NumRetriesReached
)
from .exceptions import RPCError, NumRetriesReached

log = logging.getLogger(__name__)


Expand All @@ -28,6 +27,7 @@ class Rpc:
print(ws.get_account_count())
"""

def __init__(self, url, **kwargs):
self.api_id = {}
self._request_id = 0
Expand All @@ -48,27 +48,34 @@ def setup_proxy(self, options):
self.proxy_user = url.username
self.proxy_pass = url.password
self.proxy_rdns = True
if not(url.scheme.endswith('h')):
if not (url.scheme.endswith("h")):
self.proxy_rdns = False
else:
self.proxy_type = self.proxy_type[0:len(self.proxy_type)-1]
self.proxy_type = self.proxy_type[0 : len(self.proxy_type) - 1]
else:
# Defaults (tweakable)
self.proxy_host = options.pop("proxy_host", None)
self.proxy_port = options.pop("proxy_port", 80)
self.proxy_type = options.pop("proxy_type", 'http')
self.proxy_type = options.pop("proxy_type", "http")
self.proxy_user = options.pop("proxy_user", None)
self.proxy_pass = options.pop("proxy_pass", None)
self.proxy_rdns = False
log.info("Using proxy %s:%d %s" % (self.proxy_host, self.proxy_port, self.proxy_type))
log.info(
"Using proxy %s:%d %s" % (self.proxy_host, self.proxy_port, self.proxy_type)
)

def get_proxy_url(self):
if not self.proxy_host:
return None
auth = ""
if self.proxy_user:
auth = "%s:%s@" % (self.proxy_user, self.proxy_pass)
url = self.proxy_type + "://" + auth + ("%s:%d" % (self.proxy_host, self.proxy_port))
url = (
self.proxy_type
+ "://"
+ auth
+ ("%s:%d" % (self.proxy_host, self.proxy_port))
)
return url

def get_request_id(self):
Expand All @@ -90,24 +97,24 @@ def parse_response(self, query):

log.debug(json.dumps(query))

if 'error' in ret: # pragma: no cover
if 'detail' in ret['error']:
raise RPCError(ret['error']['detail'])
if "error" in ret: # pragma: no cover
if "detail" in ret["error"]:
raise RPCError(ret["error"]["detail"])
else:
raise RPCError(ret['error']['message'])
raise RPCError(ret["error"]["message"])
else:
return ret["result"]

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

def method(*args, **kwargs):

# Sepcify the api to talk to
if "api_id" not in kwargs: # pragma: no cover
if ("api" in kwargs):
if (kwargs["api"] in self.api_id and
self.api_id[kwargs["api"]]):
if "api" in kwargs:
if kwargs["api"] in self.api_id and self.api_id[kwargs["api"]]:
api_id = self.api_id[kwargs["api"]]
else:
api_id = kwargs["api"]
Expand All @@ -119,11 +126,14 @@ def method(*args, **kwargs):
# let's be able to define the num_retries per query
self.num_retries = kwargs.get("num_retries", self.num_retries)

query = {"method": "call",
"params": [api_id, name, list(args)],
"jsonrpc": "2.0",
"id": self.get_request_id()}
query = {
"method": "call",
"params": [api_id, name, list(args)],
"jsonrpc": "2.0",
"id": self.get_request_id(),
}
r = self.rpcexec(query)
message = self.parse_response(r)
return message

return method
22 changes: 12 additions & 10 deletions grapheneapi/websocket.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import ssl
import json
import logging
Expand All @@ -9,7 +10,6 @@


class Websocket(Rpc):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# We need a lock to ensure thread-safty
Expand All @@ -20,16 +20,19 @@ def connect(self):
self._request_id = 0
if self.url[:3] == "wss":
ssl_defaults = ssl.get_default_verify_paths()
sslopt_ca_certs = {'ca_certs': ssl_defaults.cafile}
sslopt_ca_certs = {"ca_certs": ssl_defaults.cafile}
self.ws = websocket.WebSocket(sslopt=sslopt_ca_certs)
else: # pragma: no cover
self.ws = websocket.WebSocket()

self.ws.connect(self.url,
http_proxy_host = self.proxy_host,
http_proxy_port = self.proxy_port,
http_proxy_auth = (self.proxy_user,self.proxy_pass) if self.proxy_user else None,
proxy_type = self.proxy_type
self.ws.connect(
self.url,
http_proxy_host=self.proxy_host,
http_proxy_port=self.proxy_port,
http_proxy_auth=(self.proxy_user, self.proxy_pass)
if self.proxy_user
else None,
proxy_type=self.proxy_type,
)

if self.user and self.password:
Expand All @@ -45,6 +48,7 @@ def disconnect(self):

""" RPC Calls
"""

def rpcexec(self, payload):
""" Execute a call by sending the payload
Expand All @@ -65,9 +69,7 @@ def rpcexec(self, payload):

# Send over websocket
try:
self.ws.send(
json.dumps(payload, ensure_ascii=False).encode('utf8')
)
self.ws.send(json.dumps(payload, ensure_ascii=False).encode("utf8"))
# Receive from websocket
ret = self.ws.recv()

Expand Down
Loading

0 comments on commit a265822

Please sign in to comment.