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

Using more pythonic factories with classmethods

parent 50dc5a87
No related branches found
No related tags found
No related merge requests found
Showing
with 129 additions and 154 deletions
......@@ -10,7 +10,7 @@ import json
from cutecoin.core import config
from cutecoin.core.exceptions import KeyAlreadyUsed
from cutecoin.models.account import factory
from cutecoin.models.account import Account
class Core(object):
......@@ -57,7 +57,7 @@ class Core(object):
json_data.close()
for accountData in data['localAccounts']:
self.accounts.append(factory.loadAccount(accountData))
self.accounts.append(Account.load(accountData))
def save(self):
with open(config.parameters['data'], 'w') as outfile:
......
......@@ -6,7 +6,7 @@ Created on 2 févr. 2014
from cutecoin.gen_resources.addAccountDialog_uic import Ui_AddAccountDialog
from PyQt5.QtWidgets import QDialog
from cutecoin.gui.addCommunityDialog import AddCommunityDialog
from cutecoin.models.account import factory
from cutecoin.models.account import Account
from cutecoin.models.account.communities import Communities
from cutecoin.models.account.communities.listModel import CommunitiesListModel
......@@ -39,7 +39,7 @@ class AddAccountDialog(QDialog, Ui_AddAccountDialog):
for key in availableKeys:
self.pgpKeysList.addItem(key['uids'][0])
self.account = factory.createAccount(availableKeys[0]['keyid'], "", Communities())
self.account = Account.create(availableKeys[0]['keyid'], "", Communities())
self.pgpKeysList.setEnabled(True)
self.pgpKeysList.currentIndexChanged[int].connect(self.keyChanged)
self.communityDialog = AddCommunityDialog(self)
......
......@@ -10,7 +10,8 @@ import logging
import json
from cutecoin.models.account.wallets import Wallets
from cutecoin.models.account.communities import Communities
from cutecoin.models.transaction import factory as trxFactory
from cutecoin.models.community import Community
from cutecoin.models.transaction import factory
class Account(object):
'''
......@@ -18,14 +19,39 @@ class Account(object):
Each account has only one pgpKey, and a key can
be locally referenced by only one account.
'''
def __init__(self):
def __init__(self, pgpKeyId, name, communities, wallets):
'''
Constructor
'''
self.pgpKeyId = ""
self.name = ""
self.communities = Communities()
self.wallets = Wallets()
self.pgpKeyId = pgpKeyId
self.name = name
self.communities = communities
self.wallets = wallets
@classmethod
def create(cls, pgpKeyId, name, communities):
'''
Constructor
'''
wallets = Wallets()
account = cls(pgpKeyId, name, communities, wallets)
for community in account.communities.communitiesList:
wallet = account.wallets.addWallet(community.currency)
wallet.refreshCoins(community, account.keyFingerprint())
return account
@classmethod
def load(cls, jsonData):
pgpKeyId = jsonData['pgpKeyId']
name = jsonData['name']
communities = Communities()
wallets = Wallets()
account = cls(pgpKeyId, name, communities, wallets)
for communityData in jsonData['communities']:
account.communities.communitiesList.append(Community.load(communityData, account))
return account
def addWallet(name, currency):
self.wallets.addWallet(name, currency)
......
......@@ -4,7 +4,7 @@ Created on 5 févr. 2014
@author: inso
'''
import ucoinpy as ucoin
from cutecoin.models.community import factory
from cutecoin.models.community import Community
from cutecoin.core.exceptions import NotMemberOfCommunityError
import logging
......@@ -24,7 +24,7 @@ class Communities(object):
Check if the pgpFingerprint is present in the community members list
If its not, the account isnt added and an error is raised.
'''
community = factory.createCommunity(mainNode)
community = Community.create(mainNode)
members = community.ucoinRequest(ucoin.hdc.amendments.view.Members(community.amendmentId()))
logging.debug("Account fingerprint : " + keyFingerprint)
......
'''
Created on 11 févr. 2014
@author: inso
'''
import logging
from cutecoin.models.account import Account
from cutecoin.models.account.wallets import Wallets
from cutecoin.models.account.communities import Communities
from cutecoin.models.wallet import factory as walletFactory
from cutecoin.models.community import factory as communityFactory
def createAccount(pgpKeyId, name, communities):
'''
Constructor
'''
account = Account()
account.pgpKeyId = pgpKeyId
account.name = name
account.communities = communities
account.wallets = Wallets()
for community in account.communities.communitiesList:
wallet = account.wallets.addWallet(community.currency)
wallet.refreshCoins(community, account.keyFingerprint())
return account
def loadAccount(jsonData):
account = Account()
account.pgpKeyId = jsonData['pgpKeyId']
account.name = jsonData['name']
account.communities = Communities()
account.wallets = Wallets()
for communityData in jsonData['communities']:
account.communities.communitiesList.append(communityFactory.loadCommunity(communityData, account))
return account
\ No newline at end of file
......@@ -4,7 +4,7 @@ Created on 7 févr. 2014
@author: inso
'''
from cutecoin.models.wallet import factory
from cutecoin.models.wallet import Wallet
class Wallets(object):
'''
......@@ -22,7 +22,7 @@ class Wallets(object):
This wallet must not already be present in the account,
it means the wallet must have a different name or a different currency.
'''
wallet = factory.createWallet(currency)
wallet = Wallet.create(currency)
if wallet not in self.walletsList:
self.walletsList.append(wallet)
else:
......
......@@ -9,18 +9,43 @@ import hashlib
import json
import logging
from cutecoin.models.node import MainNode
from cutecoin.models.wallet import Wallet
class Community(object):
'''
classdocs
'''
def __init__(self):
def __init__(self, knownNodes):
'''
A community is a group of nodes using the same currency.
They are all using the same amendment and are syncing their datas.
An account is a member of a community if he is a member of the current amendment.
'''
self.knownNodes = []
self.currency = ""
self.knownNodes = knownNodes
currentAmendment = self.ucoinRequest(ucoin.hdc.amendments.Current())
self.currency = currentAmendment['currency']
@classmethod
def create(cls, mainNode):
knownNodes = []
knownNodes.append(mainNode)
return cls(knownNodes)
@classmethod
def load(cls, jsonData, account):
knownNodes = []
for nodeData in jsonData['nodes']:
knownNodes.append(MainNode(nodeData['server'], nodeData['port']))
community = cls(knownNodes)
for walletsData in jsonData['wallets']:
account.wallets.walletsList.append(Wallet.load(walletsData, community))
return community
def membersFingerprints(self):
'''
......@@ -35,8 +60,7 @@ class Community(object):
def ucoinRequest(self, request, get_args={}):
for node in self.knownNodes:
logging.debug("Trying to connect to : " + node.getText())
request.server = node.server
request.port = node.port
request = node.use(request)
return request.get(**get_args)
raise RuntimeError("Cannot connect to any node")
......
'''
Created on 11 févr. 2014
@author: inso
'''
from cutecoin.models.community import Community
from cutecoin.models.node import MainNode
from cutecoin.models.wallet import factory
import ucoinpy as ucoin
def createCommunity(mainNode):
community = Community()
community.knownNodes.append(mainNode)
currentAmendment = community.ucoinRequest(ucoin.hdc.amendments.Current())
community.currency = currentAmendment['currency']
return community
def loadCommunity(jsonData, account):
community = Community()
for nodeData in jsonData['nodes']:
community.knownNodes.append(MainNode(nodeData['server'], nodeData['port']))
community.currency = jsonData['currency']
for walletsData in jsonData['wallets']:
account.wallets.walletsList.append(factory.loadWallet(walletsData, community))
return community
......@@ -4,7 +4,7 @@ Created on 5 févr. 2014
@author: inso
'''
from cutecoin.models.person import factory
from cutecoin.models.person import Person
from PyQt5.QtCore import QAbstractListModel, Qt
class MembersListModel(QAbstractListModel):
......@@ -19,7 +19,7 @@ class MembersListModel(QAbstractListModel):
fingerprints = community.membersFingerprints()
self.members = []
for f in fingerprints:
self.members.append(factory.createPerson(f, community))
self.members.append(Person.create(f, community))
def rowCount(self ,parent):
return len(self.members)
......
......@@ -39,6 +39,11 @@ class MainNode(Node):
peers.append(node)
return peers
def use(self, request):
request.server = self.server
request.port = self.port
return request
def jsonify(self):
return {'server' : self.server,
'port' : self.port}
......
......@@ -5,6 +5,7 @@ Created on 11 févr. 2014
'''
import ucoinpy as ucoin
from cutecoin.core.exceptions import PersonNotFoundError
class Person(object):
'''
......@@ -13,10 +14,38 @@ class Person(object):
'''
def __init__(self):
def __init__(self, name, fingerprint, email):
'''
Constructor
'''
self.name = ""
self.fingerprint = ""
self.email = ""
\ No newline at end of file
self.name = name
self.fingerprint = fingerprint
self.email = email
@classmethod
def create(cls, pgpFingerprint, community):
'''
Create a person from the pgpFingerprint found in a community
'''
keys = community.ucoinRequest(ucoin.pks.Lookup(),
get_args={'search':"0x"+pgpFingerprint, 'op':'index'})['keys']
if len(keys) > 0:
json = keys[0]['key']
name = json['name']
fingerprint = json['fingerprint']
email = json['email']
return cls(name, fingerprint, email)
else:
raise PersonNotFoundError(pgpFingerprint, "pgpFingerprint", community)
return None
@classmethod
def fromJson(cls, jsonPerson):
'''
Create a person from json data
'''
name = jsonPerson['name']
pgpFingerprint = jsonPerson['fingerprint']
email = jsonPerson['email']
return cls(name, pgpFingerprint, email)
'''
Created on 11 févr. 2014
@author: inso
'''
from cutecoin.core.exceptions import PersonNotFoundError
from cutecoin.models.person import Person
import ucoinpy as ucoin
def createPerson(pgpFingerprint, community):
'''
Create a person from the pgpFingerprint found in a community
'''
keys = community.ucoinRequest(ucoin.pks.Lookup(),
get_args={'search':"0x"+pgpFingerprint, 'op':'index'})['keys']
if len(keys) > 0:
person = Person()
json = keys[0]['key']
person.name = json['name']
person.fingerprint = json['fingerprint']
person.email = json['email']
return person
else:
raise PersonNotFoundError(pgpFingerprint, "pgpFingerprint", community)
return None
def createPersonFromJson(jsonPerson):
'''
Create a person from json data
'''
person = Person()
person.name = jsonPerson['name']
person.pgpFingerprint = jsonPerson['fingerprint']
person.email = jsonPerson['email']
pass
\ No newline at end of file
......@@ -6,7 +6,6 @@ Created on 1 févr. 2014
import ucoinpy as ucoin
from cutecoin.models.coin import Coin
from cutecoin.models.person import factory
class Transaction(object):
'''
......
......@@ -5,9 +5,10 @@ Created on 12 févr. 2014
'''
import ucoinpy as ucoin
from cutecoin.models.person import factory
from cutecoin.models.person import Person
from cutecoin.models.transaction import Transfer, Issuance
#TODO: Passer par des factory + pythonic
def createTransaction(senderFingerprint, increment, community):
transactionId = senderFingerprint + "-" + str(increment)
ucoinTransactionView = ucoin.hdc.transactions.View(transactionId)
......@@ -21,7 +22,7 @@ def createTransaction(senderFingerprint, increment, community):
if transaction != None:
transaction.increment = increment
transaction.community = community
transaction.sender = factory.createPerson(senderFingerprint, community)
transaction.recipient = factory.createPerson(ucoinTransaction['recipient'], community)
transaction.sender = Person.create(senderFingerprint, community)
transaction.recipient = Person.create(ucoinTransaction['recipient'], community)
return transaction
......@@ -15,15 +15,28 @@ class Wallet(object):
'''
def __init__(self):
def __init__(self, coins, community):
'''
Constructor
'''
self.coins = []
self.community = None
self.coins = coins
self.community = community
self.name = "Main Wallet"
@classmethod
def create(cls, community):
return cls([], community)
@classmethod
def load(cls, jsonData, community):
coins = []
for coinData in jsonData['coins']:
coins.append(Coin.fromId(coinData['coin']))
return cls(coins, community)
def __eq__(self, other):
return ( self.community == other.community )
......
'''
Created on 11 févr. 2014
@author: inso
'''
from cutecoin.models.wallet import Wallet
from cutecoin.models.coin import Coin
def createWallet(community):
wallet = Wallet()
wallet.community = community
return wallet
def loadWallet(jsonData, community):
wallet = Wallet()
for coinData in jsonData['coins']:
wallet.coins.append(Coin.fromId(coinData['coin']))
wallet.community = community
return wallet
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment