Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto web3 #344

Merged
merged 8 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions web3/auto/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import importlib

for connector in ('ipc', 'http'):
connection = importlib.import_module('web3.auto.' + connector)
if connection.w3:
w3 = connection.w3

if w3.isConnected():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check looks redundant: if w3 is set, then isConnected() is True

break
19 changes: 19 additions & 0 deletions web3/auto/http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from web3 import (
HTTPProvider,
Web3
)
import os


def connect():

w3 = Web3(HTTPProvider(
os.environ.get('WEB3_HTTP_PROVIDER_URI', 'http://localhost:8545')
))
if w3.isConnected():
return w3
else:
return False


w3 = connect()
16 changes: 16 additions & 0 deletions web3/auto/ipc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from web3.providers.ipc import get_default_ipc_path
from web3 import (
IPCProvider,
Web3
)


def connect():
w3 = Web3(IPCProvider(get_default_ipc_path()))
if w3.isConnected():
return w3

return False


w3 = connect()
53 changes: 48 additions & 5 deletions web3/providers/ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,65 @@ def get_default_ipc_path(testnet=False):
testnet = ""

if sys.platform == 'darwin':
return os.path.expanduser(os.path.join(
ipc_path = os.path.expanduser(os.path.join(
"~",
"Library",
"Ethereum",
testnet,
"geth.ipc",
"geth.ipc"
))
if os.path.exists(ipc_path):
return ipc_path

ipc_path = os.path.expanduser(os.path.join(
"~",
"Library",
"Application Support",
"io.parity.ethereum",
"jsonrpc.ipc"
))
if os.path.exists(ipc_path):
return ipc_path

elif sys.platform.startswith('linux'):
return os.path.expanduser(os.path.join(
ipc_path = os.path.expanduser(os.path.join(
"~",
".ethereum",
testnet,
"geth.ipc",
"geth.ipc"
))
if os.path.exists(ipc_path):
return ipc_path

ipc_path = os.path.expanduser(os.path.join(
"~",
".local",
"share",
"io.parity.ethereum",
"jsonrpc.ipc"
))
if os.path.exists(ipc_path):
return ipc_path

elif sys.platform == 'win32':
return "\\\\.\\pipe\\geth.ipc"
ipc_path = os.path.join(
"\\\\",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I so wish we had windows CI to know that this works.... 😢

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tagging #166 for motivation.

Copy link
Author

@sbc64 sbc64 Nov 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it on my windows machine if it is of some consolation to you. I couldn' get seperated "\\", "\\" to work. It would merge them to one \. That is why I used 4 backslashes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also had geth and parity running while doing my in house testing, but I agree a more thourough test would be comforting.

".",
"pipe",
"geth.ipc"
)
if os.path.exists(ipc_path):
return ipc_path

ipc_path = os.path.join(
"\\\\",
".",
"pipe",
"jsonrpc.ipc"
)
if os.path.exists(ipc_path):
return ipc_path

else:
raise ValueError(
"Unsupported platform '{0}'. Only darwin/linux2/win32 are "
Expand Down