Skip to content
Snippets Groups Projects
Commit 27f38d78 authored by inso's avatar inso
Browse files

Loading and saving now adapted to new model

parent bdfa8827
No related branches found
No related tags found
No related merge requests found
Showing with 115 additions and 63 deletions
......@@ -159,7 +159,7 @@ class Test_Community():
def test_community_jsonify(self, monkeypatch):
monkeypatch.setattr(ucoin.hdc.amendments.Promoted,
'__get__', patch_amendment_Promoted_get)
main_node = Node(trust=True, hoster=True,
main_node = Node.create(trust=True, hoster=True,
server="192.168.100.10", port=3800)
community = Community.create(main_node)
wallets = Wallets()
......
......@@ -38,7 +38,7 @@ def patch_peer_get(*args, **kwargs):
"port": "3800",
"signature": "-----BEGIN PGP SIGNATURE----- ... -----END PGP SIGNATURE-----"
}
def patch_downstream_get(*args, **kwargs):
return {
"peers": [
......@@ -48,37 +48,36 @@ def patch_downstream_get(*args, **kwargs):
}
class Test_Node():
class Test_Node.create():
def test_peers(self, monkeypatch):
monkeypatch.setattr(ucoin.ucg.peering.Peers,
'__get__', patch_peers_get)
node = Node("192.168.100.12", 3800)
node = Node.create("192.168.100.12", 3800)
assert (peer for peer in node.peers() if peer["ipv4"] == "192.168.100.10")
assert (peer for peer in node.peers() if peer["ipv4"] == "192.168.100.11")
def test_peering(self, monkeypatch):
monkeypatch.setattr(ucoin.ucg.peering.Peer, '__get__', patch_peer_get)
node = Node("192.168.100.12", 3800)
node = Node.create("192.168.100.12", 3800)
peering = node.peering()
assert peering["ipv4"] == "192.168.100.10"
assert peering["port"] == str(3800)
def test_eq(self, monkeypatch):
node1 = Node("192.168.100.12", 3800)
node2 = Node("192.168.100.13", 3800)
node3 = Node("192.168.100.12", 3801)
node4 = Node("192.168.100.12", 3800)
node1 = Node.create("192.168.100.12", 3800)
node2 = Node.create("192.168.100.13", 3800)
node3 = Node.create("192.168.100.12", 3801)
node4 = Node.create("192.168.100.12", 3800)
assert node1 != node2
assert node1 != node3
assert node1 == node4
def test_downstream(self, monkeypatch):
monkeypatch.setattr(ucoin.ucg.peering.peers.DownStream, '__get__', patch_downstream_get)
node = Node("192.168.100.12", 3800)
node = Node.create("192.168.100.12", 3800)
downstream = node.downstream_peers()
assert downstream[0].server == "11.11.11.11" and downstream[0].port == 8881
assert downstream[1].server == "11.11.11.12" and downstream[1].port == 8882
\ No newline at end of file
......@@ -87,7 +87,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.core.current_account))
self.tabs_communities.clear()
for community in self.core.current_account.communities.communities_list:
for community in self.core.current_account.communities:
tab_community = CommunityTabWidget(
self.core.current_account,
community)
......
......@@ -57,7 +57,7 @@ class StepPageCommunities(Step):
'''
server = self.config_dialog.lineedit_server.text()
port = self.config_dialog.spinbox_port.value()
default_node = Node(server, port, trust=True, hoster=True)
default_node = Node.create(server, port, trust=True, hoster=True)
account = self.config_dialog.account
self.config_dialog.community = account.communities.add_community(
default_node)
......@@ -105,8 +105,8 @@ class ProcessConfigureAccount(QDialog, Ui_AccountConfigurationDialog):
self.account = Account.create(
available_keys[0]['keyid'],
"",
Communities(),
Wallets())
Communities.create(),
Wallets.create())
self.combo_keys_list.currentIndexChanged[
int].connect(self.key_changed)
......@@ -140,7 +140,7 @@ class ProcessConfigureAccount(QDialog, Ui_AccountConfigurationDialog):
self.list_communities.setModel(CommunitiesListModel(self.account))
def open_process_edit_community(self, index):
community = self.account.communities.communities_list[index.row()]
community = self.account.communities[index.row()]
dialog = ProcessConfigureCommunity(self.account, community)
dialog.button_box.accepted.connect(self.action_edit_community)
dialog.exec_()
......
......@@ -43,7 +43,7 @@ class StepPageInit(Step):
'''
server = self.config_dialog.lineedit_server.text()
port = self.config_dialog.spinbox_port.value()
default_node = Node(server, port, trust=True, hoster=True)
default_node = Node.create(server, port, trust=True, hoster=True)
account = self.config_dialog.account
self.config_dialog.community = account.add_community(default_node)
self.config_dialog.nodes.append(default_node)
......@@ -161,7 +161,7 @@ class ProcessConfigureCommunity(QDialog, Ui_CommunityConfigurationDialog):
server = self.lineedit_server.text()
port = self.spinbox_port.value()
if self.community is not None:
self.nodes.append(Node(server, port, trust=True))
self.nodes.append(Node.create(server, port, trust=True))
self.tree_nodes.setModel(NodesTreeModel(self.community,
self.nodes))
......
......@@ -31,7 +31,7 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog):
self.setupUi(self)
self.sender = sender
for wallet in sender.wallets:
self.combo_wallets.addItem(wallet.getText())
self.combo_wallets.addItem(wallet.get_text())
for contact in sender.contacts:
self.combo_contact.addItem(contact.name)
......@@ -76,12 +76,12 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog):
self.combo_contact.currentIndex()]
if self.radio_node_address.isChecked():
node = Node(
node = Node.create(
self.edit_node_address.text(), int(
self.edit_port.text()))
else:
# TODO: Manage trusted nodes
node = Node(
node = Node.create(
self.edit_node_address.text(), int(
self.edit_port.text()))
......
......@@ -46,21 +46,15 @@ class Account(object):
def load(cls, json_data):
keyid = json_data['keyid']
name = json_data['name']
communities = Communities()
wallets = Wallets()
contacts = []
for contact_data in json_data['contacts']:
contacts.append(Person.from_json(contact_data))
account = cls(keyid, name, communities, wallets, contacts)
for community_data in json_data['communities']:
account.communities.communities_list.append(
Community.load(
community_data,
account))
wallets = Wallets.load(json_data['wallets'])
communities = Communities.load(json_data['communities'])
account = cls(keyid, name, communities, wallets, contacts)
return account
def __eq__(self, other):
......@@ -119,7 +113,7 @@ class Account(object):
return sent
def quality(self, community):
if community in self.communities.communities_list:
if community in self.communities:
wallets = self.wallets.community_wallets(community.currency)
return community.person_quality(wallets, self.fingerprint())
else:
......@@ -134,6 +128,7 @@ class Account(object):
def jsonify(self):
data = {'name': self.name,
'keyid': self.keyid,
'communities': self.communities.jsonify(self.wallets),
'communities': self.communities.jsonify(),
'wallets': self.wallets.jsonify(),
'contacts': self.jsonify_contacts()}
return data
......@@ -14,27 +14,53 @@ class Communities(object):
The list of the communities an account is member of.
'''
def __init__(self):
def __init__(self, _communities_list):
'''
Constructor
'''
self.communities_list = []
self._communities_list = []
@classmethod
def create(cls):
return cls([])
@classmethod
def load(cls, json_data):
_communities_list = []
for community_data in json_data:
_communities_list.append(Community.load(community_data))
return cls(_communities_list)
def __iter__(self):
return self._communities_list.__iter__()
def __contains__(self, wallet):
return wallet in self._communities_list
def __reverse__(self):
return self._communities_list.__reverse__()
def __len__(self):
return len(self._communities_list)
def __getitem__(self, key):
return self._communities_list[key]
def add_community(self, wallets):
'''
Add a community with a mainNode
'''
community = Community.create(wallets)
if community not in self.communities_list:
self.communities_list.append(community)
if community not in self._communities_list:
self._communities_list.append(community)
return community
return None
def jsonify(self, wallets):
def jsonify(self):
'''
Return the list of communities in a key:value form.
'''
data = []
for community in self.communities_list:
data.append(community.jsonify(wallets))
for community in self._communities_list:
data.append(community.jsonify())
return data
......@@ -21,13 +21,13 @@ class CommunitiesListModel(QAbstractListModel):
self.communities = account.communities
def rowCount(self, parent):
return len(self.communities.communities_list)
return len(self.communities)
def data(self, index, role):
if role == Qt.DisplayRole:
row = index.row()
value = self.communities.communities_list[row].name()
value = self.communities[row].name()
return value
def flags(self, index):
......
......@@ -14,11 +14,22 @@ class Wallets(object):
The list of the wallets owned by an account.
'''
def __init__(self, _wallets_list=[]):
def __init__(self, wallets_list):
'''
Constructor
'''
self._wallets_list = _wallets_list
self._wallets_list = wallets_list
@classmethod
def create(cls):
return cls([])
@classmethod
def load(cls, json_data):
wallets_list = []
for wallet_data in json_data:
wallets_list.append(Wallet.load(wallet_data))
return cls(wallets_list)
def __iter__(self):
return self._wallets_list.__iter__()
......@@ -51,17 +62,6 @@ class Wallets(object):
def community_wallets(self, currency):
return Wallets([w for w in self._wallets_list if w.currency == currency])
def jsonify(self, community):
'''
Return the list of wallets in a key:value form.
'''
community_wallets = [
w for w in self._wallets_list if w.community == community]
data = []
for wallet in community_wallets:
data.append(wallet.jsonify())
return data
def request(self, request, get_args={}):
for wallet in self._wallets_list:
try:
......@@ -77,3 +77,12 @@ class Wallets(object):
except:
pass
return response
def jsonify(self):
'''
Return the list of wallets in a key:value form.
'''
data = []
for wallet in self._wallets_list:
data.append(wallet.jsonify())
return data
......@@ -13,7 +13,7 @@ class Node(object):
A ucoin node using BMA protocol
'''
def __init__(self, server, port, trust=False, hoster=False):
def __init__(self, server, port, trust, hoster):
'''
Constructor
'''
......@@ -22,6 +22,18 @@ class Node(object):
self.trust = trust
self.hoster = hoster
@classmethod
def create(cls, server, port, trust=False, hoster=False):
return cls(server, port, trust, hoster)
@classmethod
def load(cls, json_data):
server = json_data['server']
port = json_data['port']
trust = json_data['trust']
hoster = json_data['hoster']
return cls(server, port, trust, hoster)
def __eq__(self, other):
return (self.server == other.server and self.port == other.port)
......@@ -39,7 +51,7 @@ class Node(object):
peers = []
for peer in ucoin.network.peering.peers.DownStream().get()['peers']:
node = Node(peer['ipv4'], peer['port'])
node = Node.create(peer['ipv4'], peer['port'])
peers.append(node)
return peers
......
......@@ -40,12 +40,17 @@ class Wallet(object):
@classmethod
def load(cls, json_data):
coins = []
for coinData in json_data['coins']:
coins.append(Coin.from_id(coinData['coin']))
nodes = []
for coin_data in json_data['coins']:
coins.append(Coin.from_id(coin_data['coin']))
for node_data in json_data['nodes']:
nodes.append(Node.load(node_data))
fingerprint = json_data['fingerprint']
name = json_data['name']
currency = json_data['currency']
return cls(fingerprint, coins, currency, name)
return cls(fingerprint, coins, currency, nodes, name)
def __eq__(self, other):
return (self.community == other.community)
......@@ -134,7 +139,7 @@ class Wallet(object):
for peer in next_node.peers():
# Look for next node informations
found = self._search_node_by_fingerprint(
node_fg, Node(
node_fg, Node.create(
peer['ipv4'], int(
peer['port'])), traversed_nodes)
if found is not None:
......@@ -180,9 +185,15 @@ class Wallet(object):
data.append({'coin': coin.get_id()})
return data
def jsonify_nodes_list(self):
data = []
for node in self.nodes:
data.append(node.jsonify())
return data
def jsonify(self):
#TODO: Jsonify nodes
return {'coins': self.jsonify_coins_list(),
'fingerprint': self.fingerprint,
'name': self.name,
'currency': self.currency}
'currency': self.currency,
'nodes': self.jsonify_nodes_list()}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment