forked from decred/tinydecred
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.py
138 lines (117 loc) · 3.72 KB
/
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
Copyright (c) 2019, Brian Stafford
See LICENSE for details
Configuration settings for TinyDecred.
"""
import os
import argparse
from appdirs import AppDirs
from tinydecred.util import tinyjson, helpers
from tinydecred.pydecred import nets
# Set the data directory in a OS-appropriate location.
_ad = AppDirs("TinyDecred", False)
DATA_DIR = _ad.user_data_dir
helpers.mkdir(DATA_DIR)
# The master configuration file name.
CONFIG_NAME = "tinywallet.conf"
CONFIG_PATH = os.path.join(DATA_DIR, CONFIG_NAME)
# Some decred constants.
MAINNET = nets.mainnet.Name
TESTNET = nets.testnet.Name
SIMNET = nets.simnet.Name
# Network specific configuration settings.
MainnetConfig = {
"dcrdata": "https://explorer.dcrdata.org/"
}
TestnetConfig = {
"dcrdata": "https://testnet.dcrdata.org/"
}
SimnetConfig = {
"dcrdata": "http://localhost:7777" # Run dcrdata locally
}
def tinyNetConfig(netName):
"""
The default network parameters for the provided network name.
Args:
netName (str): Network name. `mainnet`, `simnet`, etc.
Returns:
obj: The network parameters.
"""
if netName == MAINNET:
return MainnetConfig
if netName == TESTNET:
return TestnetConfig
if netName == SIMNET:
return SimnetConfig
raise Exception("unknown network")
class TinyConfig:
"""
TinyConfig is configuration settings. The configuration file JSON formatted.
"""
def __init__(self):
fileCfg = helpers.fetchSettingsFile(CONFIG_PATH)
self.file = fileCfg
parser = argparse.ArgumentParser()
netGroup = parser.add_mutually_exclusive_group()
netGroup.add_argument("--simnet", action='store_true', help="use simnet")
netGroup.add_argument("--testnet", action='store_true', help="use testnet")
args = parser.parse_args()
self.net = None
if args.simnet:
self.net = nets.simnet
elif args.testnet:
self.net = nets.testnet
else:
print("**********************************************************")
print(" WARNING. WALLET FOR TESTING ONLY. NOT FOR USE ON MAINNET ")
print("**********************************************************")
self.net = nets.mainnet
self.normalize()
def set(self, k, v):
"""
Set the configuration option. The configuration is not saved, so `save`
should be called separately.
Args:
k (str): The setting key.
v (JSON-encodable): The value.
"""
self.file[k] = v
def get(self, *keys):
"""
Retrieve the setting at the provided key path. Multiple keys can be
provided, with each successive key being retreived from the previous
key's value.
Args:
*keys (str): Recursive key list.
Returns:
mixed: The configuration value.
"""
d = self.file
rVal = None
for k in keys:
if k not in d:
return None
rVal = d[k]
d = rVal
return rVal
def normalize(self):
"""
Perform attribute checks and initialization.
"""
file = self.file
netKey = "networks"
if netKey not in file:
file[netKey] = {}
if self.net.Name not in file[netKey]:
d = file[netKey][self.net.Name] = tinyNetConfig(self.net.Name)
d["name"] = self.net.Name
def save(self):
"""
Save the file.
"""
tinyjson.save(CONFIG_PATH, self.file)
# The configuration is only loaded once. Successive calls to the modular `load`
# function will return the same instance.
tinyConfig = TinyConfig()
def load():
return tinyConfig