-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
73 lines (53 loc) · 2.12 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from logging import getLogger
import platform
import os
logger = getLogger(__file__)
def parse_bitcoin_conf(fd):
"""Returns dict from bitcoin.conf-like configuration file from open fd"""
conf = {}
for line in fd:
line = line.strip()
if not line.startswith('#') and '=' in line:
key, value = line.split('=', 1)
conf[key] = value
return conf
def coin_config_path(coin):
"""Returns bitcoin.conf-like configuration path for provided coin"""
paths = {
# FIXME use proper AppData path
'Windows': r'~\AppData\Roaming\{0}\{0}.conf',
'Darwin': r'~/Library/Application Support/{0}/{0}.conf',
# Fallback path (Linux, FreeBSD...)
None: '~/.{0}/{0}.conf',
}
path = paths.get(platform.system(), paths[None])
return os.path.expanduser(path.format(coin))
def rpcurl_from_config(coin, default=None, config_path=None):
"""Returns RPC URL loaded from bitcoin.conf-like configuration of desired
currency"""
config_path = config_path or coin_config_path(coin)
cookie_path = os.path.join(os.path.dirname(config_path), '.cookie')
credentials = ''
try:
with open(config_path) as fd:
conf = parse_bitcoin_conf(fd)
if 'rpcpassword' in conf:
# Password authentication
credentials = '{rpcuser}:{rpcpassword}'.format(**conf)
elif os.path.exists(cookie_path):
# Cookie authentication
with open(cookie_path) as cfd:
credentials = cfd.read().decode('utf-8').strip() \
.replace('/', '%2F')
else:
return default
return 'http://{0}@127.0.0.1:{1}/' \
.format(credentials, conf.get('rpcport', 8336))
except Exception as exc: # pylint: disable=broad-except
logger.exception(exc)
return default
def walk(directory):
"""Generator yielding provided directory files recursively"""
for dirpath, dirnames, filenames in os.walk(directory):
for f in filenames:
yield os.path.join(dirpath, f)