Skip to content
Snippets Groups Projects
Commit 2c0329ce authored by Moul's avatar Moul
Browse files

[enh] #223: Decouple EndPoint class from Click context

- Retrieve context from get_current_context()
- Use RuntimeError to detect that there is no Click context in case Click is installed and loaded
- Use ModuleNotFoundError in case Click is not installed
- General exception for now, to keep Python 3.5 support.
- ModuleNotFoundError requires Python v3.6
- should be added when dropping Python 3.5 support
parent 65a5729e
No related branches found
No related tags found
1 merge request!146Merge dev into master branch to complete v0.8.0 development cycle
...@@ -20,7 +20,6 @@ from ipaddress import ip_address ...@@ -20,7 +20,6 @@ from ipaddress import ip_address
import socket import socket
import logging import logging
from sys import exit, stderr from sys import exit, stderr
from click import pass_context
from asyncio import sleep from asyncio import sleep
from duniterpy.api.client import Client from duniterpy.api.client import Client
from duniterpy.api.bma import blockchain, network from duniterpy.api.bma import blockchain, network
...@@ -126,10 +125,18 @@ def singleton(class_): ...@@ -126,10 +125,18 @@ def singleton(class_):
@singleton @singleton
class EndPoint(object): class EndPoint(object):
@pass_context def __init__(self):
def __init__(ctx, self):
ep = dict() ep = dict()
peer = ctx.obj["PEER"] try:
from click.globals import get_current_context
ctx = get_current_context()
peer = ctx.obj["PEER"]
gtest = ctx.obj["GTEST"]
# To be activated when dropping Python 3.5
# except (ModuleNotFoundError, RuntimeError):
except:
peer, gtest = None, None
if peer: if peer:
if ":" in peer: if ":" in peer:
ep["domain"], ep["port"] = peer.rsplit(":", 1) ep["domain"], ep["port"] = peer.rsplit(":", 1)
...@@ -137,7 +144,7 @@ class EndPoint(object): ...@@ -137,7 +144,7 @@ class EndPoint(object):
ep["domain"], ep["port"] = peer, "443" ep["domain"], ep["port"] = peer, "443"
else: else:
ep["domain"], ep["port"] = ( ep["domain"], ep["port"] = (
G1_TEST_DEFAULT_ENDPOINT if ctx.obj["GTEST"] else G1_DEFAULT_ENDPOINT G1_TEST_DEFAULT_ENDPOINT if gtest else G1_DEFAULT_ENDPOINT
) )
if ep["domain"].startswith("[") and ep["domain"].endswith("]"): if ep["domain"].startswith("[") and ep["domain"].endswith("]"):
ep["domain"] = ep["domain"][1:-1] ep["domain"] = ep["domain"][1:-1]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment