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