-
Notifications
You must be signed in to change notification settings - Fork 0
/
dash_config.py
63 lines (53 loc) · 1.97 KB
/
dash_config.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
import sys
import os
import io
import re
class DashConfig:
def __init__(self, filename):
try:
# read dash.conf config but skip commented lines
f = io.open(filename)
lines = []
for line in f:
if re.match('^\s*#', line):
continue
lines.append(line)
f.close()
# data is dash.conf without commented lines
self.data = ''.join(lines)
match = re.findall(r'(.*?)=(.*?)$', self.data, re.MULTILINE)
self.tokens = {key: value for (key, value) in match}
except IOError as e:
print("[warning] error reading config file: %s" % e)
# strip keys in tokens
for key in self.tokens.keys():
if ' ' in key:
self.tokens[key.replace(' ', '')] = self.tokens[key]
del self.tokens[key]
def is_mainnet(self):
if not ('testnet' in self.tokens):
return True
return int(self.tokens['testnet']) == 0
def get_rpc_creds(self):
creds = {}
if not ('rpchost' in self.tokens):
creds[u'host'] = '127.0.0.1'
else:
creds[u'host'] = self.tokens['rpchost']
# standard Dash defaults...
default_port = 9998 if (self.is_mainnet()) else 19998
if not ('rpcport' in self.tokens):
creds[u'port'] = default_port
else:
creds[u'port'] = int(self.tokens['rpcport'])
creds[u'user'] = self.tokens['rpcuser']
creds[u'password'] = self.tokens['rpcpassword']
# return a dictionary with RPC credential key, value pairs
return creds
@classmethod
def get_default_dash_conf(cls):
home = os.environ.get('HOME')
dash_conf = os.path.join(home, ".dashcore/dash.conf")
if sys.platform == 'darwin':
dash_conf = os.path.join(home, "Library/Application Support/DashCore/dash.conf")
return dash_conf