diff --git a/src/sakia/app.py b/src/sakia/app.py index 44894873b6703f4a54bc64c32e2bcc8aa8fdc646..a49bb3556d404621d55f6e90a5e1807428398df3 100644 --- a/src/sakia/app.py +++ b/src/sakia/app.py @@ -5,10 +5,7 @@ Created on 1 févr. 2014 """ import datetime -import json import logging -import os -import tarfile import aiohttp from PyQt5.QtCore import QObject, pyqtSignal, QTranslator, QCoreApplication, QLocale @@ -19,11 +16,11 @@ from . import __version__ from .options import SakiaOptions from sakia.data.connectors import BmaConnector from sakia.services import NetworkService, BlockchainService, IdentitiesService -from sakia.data.repositories import SakiaDatabase, BlockchainsRepo, CertificationsRepo, \ - IdentitiesRepo, NodesRepo, TransactionsRepo, ConnectionsRepo +from sakia.data.repositories import SakiaDatabase from sakia.data.processors import BlockchainProcessor, NodesProcessor, IdentitiesProcessor, CertificationsProcessor from sakia.data.files import AppDataFile, UserParametersFile from sakia.decorators import asyncify +from sakia.money import Relative class Application(QObject): @@ -177,3 +174,7 @@ class Application(QObject): logging.debug("Could not connect to github : {0}".format(str(e))) except Exception as e: pass + + @property + def current_ref(self): + return Relative \ No newline at end of file diff --git a/src/sakia/money/base_referential.py b/src/sakia/money/base_referential.py index 57adf6715c7316015c565d037af0a602c661a79a..3682c23554735a5dbe00e20a88aec8713eb85692 100644 --- a/src/sakia/money/base_referential.py +++ b/src/sakia/money/base_referential.py @@ -1,20 +1,25 @@ -from PyQt5.QtCore import QCoreApplication, QT_TRANSLATE_NOOP, QObject, QLocale -import asyncio class BaseReferential: """ Interface to all referentials """ - def __init__(self, amount, community, app, block_number=None): + def __init__(self, amount, currency, app, block_number=None): + """ + + :param int amount: + :param str currency: + :param sakia.app.Application app: + :param int block_number: + """ self.amount = amount - self.community = community self.app = app + self.currency = currency self._block_number = block_number @classmethod - def instance(cls, amount, community, app, block_number=None): - return cls(amount, community, app, block_number) + def instance(cls, amount, currency, app, block_number=None): + return cls(amount, currency, app, block_number) @classmethod def translated_name(self): diff --git a/src/sakia/money/dividend_per_day.py b/src/sakia/money/dividend_per_day.py index 7528c867120fd6c249b4e3ba98466c966f2987e2..1b2749bb76d5ff27d81e906686ee3faf76f2db61 100644 --- a/src/sakia/money/dividend_per_day.py +++ b/src/sakia/money/dividend_per_day.py @@ -1,6 +1,7 @@ -from PyQt5.QtCore import QObject, QCoreApplication, QT_TRANSLATE_NOOP, QLocale from .base_referential import BaseReferential from .udd_to_past import UDDToPast +from .currency import shortened +from ..data.processors import BlockchainProcessor from PyQt5.QtCore import QCoreApplication, QT_TRANSLATE_NOOP, QLocale @@ -26,15 +27,16 @@ class DividendPerDay(BaseReferential): 100 UDD equal the Universal Dividend created per day. """.replace('\n', '<br >')) - def __init__(self, amount, community, app, block_number=None): - super().__init__(amount, community, app, block_number) + def __init__(self, amount, currency, app, block_number=None): + super().__init__(amount, currency, app, block_number) + self._blockchain_processor = BlockchainProcessor.instanciate(self.app) @classmethod - def instance(cls, amount, community, app, block_number=None): + def instance(cls, amount, currency, app, block_number=None): if app.preferences['forgetfulness']: - return cls(amount, community, app, block_number) + return cls(amount, currency, app, block_number) else: - return UDDToPast(amount, community, app, block_number) + return UDDToPast(amount, currency, app, block_number) @classmethod def translated_name(cls): @@ -42,7 +44,7 @@ class DividendPerDay(BaseReferential): @property def units(self): - return QCoreApplication.translate("DividendPerDay", DividendPerDay._UNITS_STR_).format(self.community.short_currency) + return QCoreApplication.translate("DividendPerDay", DividendPerDay._UNITS_STR_).format(shortened(self.currency)) @property def formula(self): @@ -65,14 +67,13 @@ class DividendPerDay(BaseReferential): R = UD(t) of one day t = last UD block time - :param int amount: Value :param sakia.core.community.Community community: Community instance :return: float """ - dividend = await self.community.dividend() - params = await self.community.parameters() + dividend, base = self._blockchain_processor.last_ud(self.currency) + params = await self._blockchain_processor.parameters(self.currency) if dividend > 0: - return (self.amount * 100) / (float(dividend) / (params['dt'] / 86400)) + return (self.amount * 100) / (float(dividend * (10**base)) / (params.dt / 86400)) else: return self.amount @@ -82,25 +83,25 @@ class DividendPerDay(BaseReferential): async def localized(self, units=False, international_system=False): value = await self.value() prefix = "" - localized_value = QLocale().toString(float(value), 'f', self.app.preferences['digits_after_comma']) + localized_value = QLocale().toString(float(value), 'f', self.app.parameters.digits_after_comma) if units or international_system: return QCoreApplication.translate("Relative", DividendPerDay._REF_STR_) \ .format(localized_value, prefix, - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value async def diff_localized(self, units=False, international_system=False): value = await self.differential() prefix = "" - localized_value = QLocale().toString(float(value), 'f', self.app.preferences['digits_after_comma']) + localized_value = QLocale().toString(float(value), 'f', self.app.parameters.digits_after_comma) if units or international_system: return QCoreApplication.translate("Relative", DividendPerDay._REF_STR_) \ .format(localized_value, prefix, - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value diff --git a/src/sakia/money/quant_zerosum.py b/src/sakia/money/quant_zerosum.py index 767257c85d1142fb3d510e881d06b8de897150ba..61b486686687fb0ad3176d3a3743bbeca95b3452 100644 --- a/src/sakia/money/quant_zerosum.py +++ b/src/sakia/money/quant_zerosum.py @@ -1,6 +1,8 @@ from PyQt5.QtCore import QCoreApplication, QT_TRANSLATE_NOOP, QLocale from . import Quantitative from .base_referential import BaseReferential +from .currency import shortened +from ..data.processors import BlockchainProcessor class QuantitativeZSum(BaseReferential): @@ -26,8 +28,9 @@ class QuantitativeZSum(BaseReferential): the value is under the average value. """.replace('\n', '<br >')) - def __init__(self, amount, community, app, block_number=None): - super().__init__(amount, community, app, block_number) + def __init__(self, amount, currency, app, block_number=None): + super().__init__(amount, currency, app, block_number) + self._blockchain_processor = BlockchainProcessor.instanciate(self.app) @classmethod def translated_name(cls): @@ -35,7 +38,7 @@ class QuantitativeZSum(BaseReferential): @property def units(self): - return QCoreApplication.translate("QuantitativeZSum", QuantitativeZSum._UNITS_STR_).format(self.community.short_currency) + return QCoreApplication.translate("QuantitativeZSum", QuantitativeZSum._UNITS_STR_).format(shortened(self.currency)) @property def formula(self): @@ -47,7 +50,7 @@ class QuantitativeZSum(BaseReferential): @property def diff_units(self): - return QCoreApplication.translate("Quantitative", Quantitative._UNITS_STR_).format(self.community.short_currency) + return QCoreApplication.translate("Quantitative", Quantitative._UNITS_STR_).format(shortened(self.currency)) async def value(self): """ @@ -66,16 +69,16 @@ class QuantitativeZSum(BaseReferential): :param sakia.core.community.Community community: Community instance :return: int """ - ud_block = await self.community.get_ud_block() - if ud_block and ud_block['membersCount'] > 0: - monetary_mass = await self.community.monetary_mass() - average = int(monetary_mass / ud_block['membersCount']) + last_members_count = self._blockchain_processor.last_members_count(self.currency) + monetary_mass = self._blockchain_processor.monetary_mass(self.currency) + if last_members_count != 0: + average = int(monetary_mass / last_members_count) else: average = 0 return self.amount - average async def differential(self): - return await Quantitative(self.amount, self.community, self.app).value() + return await Quantitative(self.amount, self.currency, self.app).value() async def localized(self, units=False, international_system=False): value = await self.value() @@ -91,10 +94,11 @@ class QuantitativeZSum(BaseReferential): QuantitativeZSum._REF_STR_) \ .format(localized_value, prefix, - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value async def diff_localized(self, units=False, international_system=False): - localized = await Quantitative(self.amount, self.community, self.app).localized(units, international_system) + localized = await Quantitative(self.amount, shortened(self.currency), self.app).localized(units, + international_system) return localized diff --git a/src/sakia/money/quantitative.py b/src/sakia/money/quantitative.py index aa4808e39ce6108a770681ba55d1984246800645..7ce9ecf731610f04f8d9e0e06f8be4125ad5653b 100644 --- a/src/sakia/money/quantitative.py +++ b/src/sakia/money/quantitative.py @@ -1,5 +1,7 @@ from PyQt5.QtCore import QCoreApplication, QT_TRANSLATE_NOOP, QLocale from .base_referential import BaseReferential +from .currency import shortened +from ..data.processors import BlockchainProcessor class Quantitative(BaseReferential): @@ -16,8 +18,9 @@ class Quantitative(BaseReferential): ) _DESCRIPTION_STR_ = QT_TRANSLATE_NOOP('Quantitative', "Base referential of the money. Units values are used here.") - def __init__(self, amount, community, app, block_number=None): - super().__init__(amount, community, app, block_number) + def __init__(self, amount, currency, app, block_number=None): + super().__init__(amount, currency, app, block_number) + self._blockchain_processor = BlockchainProcessor.instanciate(self.app) @classmethod def translated_name(cls): @@ -25,7 +28,7 @@ class Quantitative(BaseReferential): @property def units(self): - return QCoreApplication.translate("Quantitative", Quantitative._UNITS_STR_).format(self.community.short_currency) + return QCoreApplication.translate("Quantitative", Quantitative._UNITS_STR_).format(shortened(self.currency)) @property def formula(self): @@ -81,7 +84,7 @@ class Quantitative(BaseReferential): value = await self.value() prefix = "" if international_system: - localized_value, prefix = Quantitative.to_si(value, self.app.preferences['digits_after_comma']) + localized_value, prefix = Quantitative.to_si(value, self.app.parameters.digits_after_comma) else: localized_value = QLocale().toString(float(value), 'f', 0) @@ -90,7 +93,7 @@ class Quantitative(BaseReferential): Quantitative._REF_STR_) \ .format(localized_value, prefix, - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value @@ -98,7 +101,7 @@ class Quantitative(BaseReferential): value = await self.differential() prefix = "" if international_system: - localized_value, prefix = Quantitative.to_si(value, self.app.preferences['digits_after_comma']) + localized_value, prefix = Quantitative.to_si(value, self.app.parameters.digits_after_comma) else: localized_value = QLocale().toString(float(value), 'f', 0) @@ -107,6 +110,6 @@ class Quantitative(BaseReferential): Quantitative._REF_STR_) \ .format(localized_value, prefix, - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value diff --git a/src/sakia/money/relative.py b/src/sakia/money/relative.py index 004deb6c3aed2d7b2a597740d41e523a518c349d..0c1c5ee089deb38d90c5118da419f7ac3f77e537 100644 --- a/src/sakia/money/relative.py +++ b/src/sakia/money/relative.py @@ -1,6 +1,7 @@ -from PyQt5.QtCore import QObject, QCoreApplication, QT_TRANSLATE_NOOP, QLocale from .base_referential import BaseReferential from .relative_to_past import RelativeToPast +from .currency import shortened +from ..data.processors import BlockchainProcessor from PyQt5.QtCore import QCoreApplication, QT_TRANSLATE_NOOP, QLocale @@ -28,15 +29,24 @@ class Relative(BaseReferential): the average. """.replace('\n', '<br >')) - def __init__(self, amount, community, app, block_number=None): - super().__init__(amount, community, app, block_number) + def __init__(self, amount, currency, app, block_number=None): + super().__init__(amount, currency, app, block_number) + self._blockchain_processor = BlockchainProcessor.instanciate(self.app) @classmethod - def instance(cls, amount, community, app, block_number=None): - if app.preferences['forgetfulness']: - return cls(amount, community, app, block_number) + def instance(cls, amount, currency, app, block_number=None): + """ + + :param int amount: + :param str currency: + :param sakia.app.Application app: + :param int block_number: + :return: + """ + if app.parameters.forgetfulness: + return cls(amount, currency, app, block_number) else: - return RelativeToPast(amount, community, app, block_number) + return RelativeToPast(amount, currency, app, block_number) @classmethod def translated_name(cls): @@ -44,7 +54,7 @@ class Relative(BaseReferential): @property def units(self): - return QCoreApplication.translate("Relative", Relative._UNITS_STR_).format(self.community.short_currency) + return QCoreApplication.translate("Relative", Relative._UNITS_STR_).format(shortened(self.currency)) @property def formula(self): @@ -68,9 +78,9 @@ class Relative(BaseReferential): :param sakia.core.community.Community community: Community instance :return: float """ - dividend = await self.community.dividend() + dividend, base = self._blockchain_processor.last_ud(self.currency) if dividend > 0: - return self.amount / float(dividend) + return self.amount / float(dividend * (10**base)) else: return self.amount @@ -105,15 +115,15 @@ class Relative(BaseReferential): value = await self.value() prefix = "" if international_system: - localized_value, prefix = Relative.to_si(value, self.app.preferences['digits_after_comma']) + localized_value, prefix = Relative.to_si(value, self.app.parameters.digits_after_comma) else: - localized_value = QLocale().toString(float(value), 'f', self.app.preferences['digits_after_comma']) + localized_value = QLocale().toString(float(value), 'f', self.app.parameters.digits_after_comma) if units or international_system: return QCoreApplication.translate("Relative", Relative._REF_STR_) \ .format(localized_value, prefix, - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value @@ -121,14 +131,14 @@ class Relative(BaseReferential): value = await self.differential() prefix = "" if international_system and value != 0: - localized_value, prefix = Relative.to_si(value, self.app.preferences['digits_after_comma']) + localized_value, prefix = Relative.to_si(value, self.app.parameters.digits_after_comma) else: - localized_value = QLocale().toString(float(value), 'f', self.app.preferences['digits_after_comma']) + localized_value = QLocale().toString(float(value), 'f', self.app.parameters.digits_after_comma) if units or international_system: return QCoreApplication.translate("Relative", Relative._REF_STR_) \ .format(localized_value, prefix, - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value diff --git a/src/sakia/money/relative_to_past.py b/src/sakia/money/relative_to_past.py index b92e5dc10a398f586827a8cec8007c1ad2da050c..5da0720007a19391f7ea1f96656a56ce37e31576 100644 --- a/src/sakia/money/relative_to_past.py +++ b/src/sakia/money/relative_to_past.py @@ -1,5 +1,7 @@ from PyQt5.QtCore import QObject, QCoreApplication, QT_TRANSLATE_NOOP, QLocale, QDateTime from .base_referential import BaseReferential +from .currency import shortened +from ..data.processors import BlockchainProcessor class RelativeToPast(BaseReferential): @@ -24,8 +26,9 @@ class RelativeToPast(BaseReferential): This referential is practical to remember what was the value at the Time. """.replace('\n', '<br >')) - def __init__(self, amount, community, app, block_number=None): - super().__init__(amount, community, app, block_number) + def __init__(self, amount, currency, app, block_number=None): + super().__init__(amount, currency, app, block_number) + self._blockchain_processor = BlockchainProcessor.instanciate(self.app) @classmethod def translated_name(cls): @@ -34,7 +37,7 @@ class RelativeToPast(BaseReferential): @property def units(self): return QCoreApplication.translate("RelativeToPast", RelativeToPast._UNITS_STR_).format('t', - self.community.short_currency) + shortened(self.currency)) @property def formula(self): return QCoreApplication.translate('RelativeToPast', RelativeToPast._FORMULA_STR_) @@ -72,7 +75,7 @@ class RelativeToPast(BaseReferential): async def localized(self, units=False, international_system=False): from . import Relative value = await self.value() - block = await self.community.get_ud_block() + last_ud_time = self._blockchain_processor.last_ud_time(self.currency) prefix = "" if international_system: localized_value, prefix = Relative.to_si(value, self.app.preferences['digits_after_comma']) @@ -85,10 +88,10 @@ class RelativeToPast(BaseReferential): prefix, QLocale.toString( QLocale(), - QDateTime.fromTime_t(block['medianTime']).date(), + QDateTime.fromTime_t(last_ud_time).date(), QLocale.dateFormat(QLocale(), QLocale.ShortFormat) ), - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value @@ -111,6 +114,6 @@ class RelativeToPast(BaseReferential): QDateTime.fromTime_t(block['medianTime']).date(), QLocale.dateFormat(QLocale(), QLocale.ShortFormat) ), - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value diff --git a/src/sakia/money/relative_zerosum.py b/src/sakia/money/relative_zerosum.py index e6c8ea5a5fca7babf8966177d3ead7b4c5e286b7..c01c57de1419bc7064b90573926874dd56ee5cb2 100644 --- a/src/sakia/money/relative_zerosum.py +++ b/src/sakia/money/relative_zerosum.py @@ -1,6 +1,8 @@ from PyQt5.QtCore import QCoreApplication, QT_TRANSLATE_NOOP, QLocale from .relative import Relative from .base_referential import BaseReferential +from .currency import shortened +from ..data.processors import BlockchainProcessor class RelativeZSum(BaseReferential): @@ -25,8 +27,9 @@ class RelativeZSum(BaseReferential): the value is under the average value. """.replace('\n', '<br >')) - def __init__(self, amount, community, app, block_number=None): - super().__init__(amount, community, app, block_number) + def __init__(self, amount, currency, app, block_number=None): + super().__init__(amount, currency, app, block_number) + self._blockchain_processor = BlockchainProcessor.instanciate(self.app) @classmethod def translated_name(cls): @@ -34,7 +37,7 @@ class RelativeZSum(BaseReferential): @property def units(self): - return QCoreApplication.translate("RelativeZSum", RelativeZSum._UNITS_STR_).format(self.community.short_currency) + return QCoreApplication.translate("RelativeZSum", RelativeZSum._UNITS_STR_).format(shortened(self.currency)) @property def formula(self): @@ -46,7 +49,7 @@ class RelativeZSum(BaseReferential): @property def diff_units(self): - return QCoreApplication.translate("Relative", Relative._UNITS_STR_).format(self.community.short_currency) + return QCoreApplication.translate("Relative", Relative._UNITS_STR_).format(shortened(self.currency)) async def value(self): """ @@ -75,22 +78,22 @@ class RelativeZSum(BaseReferential): return relative_value - relative_median async def differential(self): - return await Relative(self.amount, self.community, self.app).value() + return await Relative(self.amount, self.currency, self.app).value() async def localized(self, units=False, international_system=False): value = await self.value() prefix = "" if international_system: - localized_value, prefix = Relative.to_si(value, self.app.preferences['digits_after_comma']) + localized_value, prefix = Relative.to_si(value, self.app.parameters.digits_after_comma) else: - localized_value = QLocale().toString(float(value), 'f', self.app.preferences['digits_after_comma']) + localized_value = QLocale().toString(float(value), 'f', self.app.parameters.digits_after_comma) if units or international_system: return QCoreApplication.translate("RelativeZSum", RelativeZSum._REF_STR_)\ .format(localized_value, prefix, - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value @@ -99,12 +102,12 @@ class RelativeZSum(BaseReferential): prefix = "" if international_system and value != 0: - localized_value, prefix = Relative.to_si(value, self.app.preferences['digits_after_comma']) + localized_value, prefix = Relative.to_si(value, self.app.parameters.digits_after_comma) else: - localized_value = QLocale().toString(float(value), 'f', self.app.preferences['digits_after_comma']) + localized_value = QLocale().toString(float(value), 'f', self.app.parameters.digits_after_comma) if units or international_system: return QCoreApplication.translate("Relative", Relative._REF_STR_)\ - .format(localized_value, prefix, self.community.short_currency if units else "") + .format(localized_value, prefix, shortened(self.currency) if units else "") else: return localized_value diff --git a/src/sakia/money/udd_to_past.py b/src/sakia/money/udd_to_past.py index fc85821f9b243d41c2e77dda3e731942e440253a..38667963fb1714e09bfc7614f54ff59ae35dfe9e 100644 --- a/src/sakia/money/udd_to_past.py +++ b/src/sakia/money/udd_to_past.py @@ -1,5 +1,7 @@ from PyQt5.QtCore import QObject, QCoreApplication, QT_TRANSLATE_NOOP, QLocale, QDateTime from .base_referential import BaseReferential +from .currency import shortened +from ..data.processors import BlockchainProcessor class UDDToPast(BaseReferential): @@ -27,8 +29,9 @@ class UDDToPast(BaseReferential): Relative value R is calculated by dividing the quantitative value Q by the """.replace('\n', '<br >')) - def __init__(self, amount, community, app, block_number=None): - super().__init__(amount, community, app, block_number) + def __init__(self, amount, currency, app, block_number=None): + super().__init__(amount, currency, app, block_number) + self._blockchain_processor = BlockchainProcessor.instanciate(self.app) @classmethod def translated_name(cls):