forked from Fraunhofer-FIT-COOP/LabChain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
57 lines (43 loc) · 1.89 KB
/
client.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
import argparse
import logging
import os
# append project dir to python path
from labchain.util.cryptoHelper import CryptoHelper
from labchain.network.networking import ClientNetworkInterface, JsonRpcClient
from labchain.blockchainClient import Wallet, BlockchainClient
# set TERM environment variable if not set
if 'TERM' not in os.environ:
os.environ['TERM'] = 'xterm-color'
CONFIG_DIRECTORY = os.path.join(os.path.expanduser("~"), '.labchain')
WALLET_FILE_PATH = os.path.join(CONFIG_DIRECTORY, 'wallet.csv')
def create_config_directory():
os.makedirs(CONFIG_DIRECTORY, exist_ok=True)
def create_client(wallet_file, node_ip, node_port):
crypto_helper = CryptoHelper.instance()
network_interface = ClientNetworkInterface(JsonRpcClient(), {node_ip: {node_port: {}}})
return BlockchainClient(Wallet(wallet_file), network_interface, crypto_helper)
def setup_logging(verbose, very_verbose):
if very_verbose:
logging.basicConfig(level=logging.DEBUG)
elif verbose:
logging.basicConfig(level=logging.INFO)
else:
logging.basicConfig(level=logging.WARNING)
def parse_args():
parser = argparse.ArgumentParser(description='CLI client for Labchain.')
parser.add_argument('node_ip', help='The IP address of the Labchain node')
parser.add_argument('node_port', help='The port address of the Labchain node')
parser.add_argument('--verbose', '-v', action='store_true')
parser.add_argument('--very-verbose', '-vv', action='store_true')
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
setup_logging(args.verbose, args.very_verbose)
create_config_directory()
if os.path.exists(WALLET_FILE_PATH):
file_mode = 'r+'
else:
file_mode = 'w+'
with open(WALLET_FILE_PATH, file_mode) as open_wallet_file:
client = create_client(open_wallet_file, args.node_ip, args.node_port)
client.main()