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 7 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()
40 changes: 35 additions & 5 deletions web3/providers/ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,52 @@ 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"
if os.path.exists("\\\\.\\pipe\\geth.ipc"):
Copy link
Member

Choose a reason for hiding this comment

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

I think these should still use os.path.join to construct these paths as it will use the proper path separators for the os.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Heh, I've been out of windows for so long that I don't remember what \\.\pipe\geth.ipc even means.

@Sebohe because of the '\' start, it wasn't intuitive to me how to use join here. This was the first way I could find to do the join: os.path.join('\\\\', '.', 'pipe', 'geth')

It looks like this is the last change needed to get merged.

return "\\\\.\\pipe\\geth.ipc"
elif os.path.exists("\\\\.\\pipe\\jsonrpc.ipc"):
return "\\\\.\\pipe\\jsonrpc.ipc"
Copy link
Collaborator

Choose a reason for hiding this comment

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

A utility function like first_available_path(paths) could maybe remove some of the repetition here.


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