Skip to content

Commit cc0973e

Browse files
refactor load config
1 parent bc733f1 commit cc0973e

File tree

7 files changed

+425
-24
lines changed

7 files changed

+425
-24
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
__pycache__/
33
*.py[cod]
44
*$py.class
5+
test.ipynb
56

67
# C extensions
78
*.so

Pipfile

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ name = "pypi"
77

88
[dev-packages]
99
autopep8 = "*"
10+
ipykernel = "*"
1011

1112
[requires]
1213
python_version = "3.10"

Pipfile.lock

+369-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fabric_sdk/context/context.py

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
2-
3-
from ast import List
4-
from decimal import InvalidContext
5-
from msilib.schema import Error
6-
from token import EXACT_TOKEN_TYPES
7-
from typing import Dict
1+
from typing import List, Dict
82

93

104
def dict_get(_dict):
@@ -80,7 +74,7 @@ def __init__(self,
8074
self.credential_store = credential_store
8175

8276
@staticmethod
83-
def load(config, default_name):
77+
def load(config):
8478
config_get = dict_get(config)
8579
return ClientConfig(
8680
organization=config_get('organization', lambda: ''),
@@ -103,6 +97,8 @@ def __init__(self, client, orgs, ca_list) -> None:
10397
self.orgs: OrgConfig = orgs
10498
self.ca_list: List[MSPConfig] = ca_list
10599

100+
# TODO: Doc Exception
101+
106102

107103
class ConfigManager:
108104
def __init__(self) -> None:
@@ -116,35 +112,36 @@ def _select_network(self, name=None) -> Network:
116112
except KeyError:
117113

118114
if len(self._networks) == 1:
119-
return next(self._networks.values())
115+
return list(self._networks.values())[0]
120116
else:
121-
raise Error()
117+
raise Exception()
122118
else:
123119
try:
124120
return self._networks[name]
125121
except KeyError:
126-
raise Error()
122+
raise Exception()
127123

128124
def _select_client(self, network: Network, name=None) -> ClientConfig:
129125
if name is None:
126+
print(network.client)
130127
if len(network.client) == 1:
131128
return network.client[0]
132129
else:
133-
raise Error()
130+
raise Exception()
134131
else:
135132
try:
136133
return [client for client in network.client if client.organization == name][0]
137134
except KeyError:
138-
raise Error()
135+
raise Exception()
139136

140137
def client_compile(self, client_name=None, network_name=None) -> ContextClient:
141138
network = self._select_network(network_name)
142-
client = self._select_client(client_name, network)
139+
client = self._select_client(network, name=client_name)
143140

144141
try:
145142
org: OrgConfig = network._dict_org[client.organization]
146143
except KeyError:
147-
raise Error()
144+
raise Exception()
148145

149146
ca_list = []
150147

fabric_sdk/context/load_yaml.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@
77
import os
88
from .context import ContextClient, ConfigManager
99

10+
import os
11+
1012

1113
def Context(client_name=None, network_name=None) -> ContextClient:
1214
config_path = os.getenv(FABRIC_PYTHON_SDK_NETWORK_CONFIG)
1315
manager = ConfigManager()
1416

15-
with open(config_path, 'r') as config_file:
16-
config = config_file.read()
17-
config = load(config, Loader=Loader)
17+
dir_list = os.listdir(config_path)
18+
dir_list = [file for file in dir_list if file.endswith('.yaml')]
19+
for file in dir_list:
20+
path = f'{config_path}/{file}'
21+
with open(path, 'r') as config_file:
22+
config = config_file.read()
23+
config = load(config, Loader=Loader)
24+
manager.add_new_config(path, config)
25+
config_file.close()
1826

1927
return manager.client_compile(client_name, network_name)

tests/context/msp.yaml

+28
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,31 @@ certificateAuthorities:
6969
enrollSecret: adminpw
7070
# [Optional] The optional name of the CA.
7171
caName: ca.org2.example.com
72+
73+
client:
74+
BCCSP:
75+
security:
76+
default:
77+
provider: SW
78+
enabled: true
79+
hashAlgorithm: SHA2
80+
level: 256
81+
softVerify: true
82+
credentialStore:
83+
cryptoStore:
84+
path: ../crypto-config/peerOrganizations/org1.example.com/users
85+
path: ../crypto-config/peerOrganizations/org1.example.com/users
86+
cryptoconfig:
87+
path: ../crypto-config/peerOrganizations/org1.example.com/users
88+
logging:
89+
level: info
90+
organization: org1
91+
92+
organizations:
93+
org1:
94+
certificateAuthorities:
95+
- ca.org1.example.com
96+
cryptoPath: ../crypto-config/peerOrganizations/org1.example.com/msp
97+
mspid: Org1MSP
98+
peers:
99+
- peer1.org1.com

tests/context/test_context_load.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66

77
def test_load_msp():
88
path = str(Path(__file__).resolve()).split('/')
9-
path[-1] = 'msp.yaml'
10-
os.environ[FABRIC_PYTHON_SDK_NETWORK_CONFIG] = '/'.join(path)
9+
os.environ[FABRIC_PYTHON_SDK_NETWORK_CONFIG] = '/'.join(path[:-1])
1110

12-
context = sdk.context.init()
11+
context = sdk.Context()
1312

14-
assert len(context.certificateAuthorities) == 3
13+
assert len(context.ca_list) == 3

0 commit comments

Comments
 (0)