-
Notifications
You must be signed in to change notification settings - Fork 4
/
service.py
73 lines (60 loc) · 3.39 KB
/
service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import logging
from cryptoadvance.specter.services.service import Service, devstatus_alpha, devstatus_prod, devstatus_beta
# A SpecterError can be raised and will be shown to the user as a red banner
from cryptoadvance.specter.specter_error import SpecterError
from flask import current_app as app
from cryptoadvance.specter.wallet import Wallet
from flask_apscheduler import APScheduler
logger = logging.getLogger(__name__)
class {{ext.id | camelcase }}Service(Service):
id = "{{ext.id}}"
name = "{{ext.id | camelcase}} Service"
icon = "{{ext.id }}/img/ghost.png"
logo = "{{ext.id }}/img/logo.jpeg"
desc = "Where a {{ext.id }} grows bigger."
has_blueprint = True
blueprint_module = "{{ext.org}}.specterext.{{ext.id }}.controller"
{% if ext.devicename %}devices = ["{{ext.org}}.specterext.{{ext.id }}.devices.{{ext.devicename | snakecase }}"]{% endif %}
devstatus = devstatus_alpha
isolated_client = {{ ext.isolated_client }}
# TODO: As more Services are integrated, we'll want more robust categorization and sorting logic
sort_priority = 2
# ServiceEncryptedStorage field names for this service
# Those will end up as keys in a json-file
SPECTER_WALLET_ALIAS = "wallet"
def callback_after_serverpy_init_app(self, scheduler: APScheduler):
def every5seconds(hello, world="world"):
with scheduler.app.app_context():
print(f"Called {hello} {world} every5seconds")
# Here you can schedule regular jobs. triggers can be one of "interval", "date" or "cron"
# Examples:
# interval: https://apscheduler.readthedocs.io/en/3.x/modules/triggers/interval.html
# scheduler.add_job("every5seconds4", every5seconds, trigger='interval', seconds=5, args=["hello"])
# Date: https://apscheduler.readthedocs.io/en/3.x/modules/triggers/date.html
# scheduler.add_job("MyId", my_job, trigger='date', run_date=date(2009, 11, 6), args=['text'])
# cron: https://apscheduler.readthedocs.io/en/3.x/modules/triggers/cron.html
# sched.add_job("anotherID", job_function, trigger='cron', day_of_week='mon-fri', hour=5, minute=30, end_date='2014-05-30')
# Maybe you should store the scheduler for later use:
self.scheduler = scheduler
# There might be other callbacks you're interested in. Check the callbacks.py in the specter-desktop source.
# if you are, create a method here which is "callback_" + callback_id
@classmethod
def get_associated_wallet(cls) -> Wallet:
"""Get the Specter `Wallet` that is currently associated with this service"""
service_data = cls.get_current_user_service_data()
if not service_data or cls.SPECTER_WALLET_ALIAS not in service_data:
# Service is not initialized; nothing to do
return
try:
return app.specter.wallet_manager.get_by_alias(
service_data[cls.SPECTER_WALLET_ALIAS]
)
except SpecterError as e:
logger.debug(e)
# Referenced an unknown wallet
# TODO: keep ignoring or remove the unknown wallet from service_data?
return
@classmethod
def set_associated_wallet(cls, wallet: Wallet):
"""Set the Specter `Wallet` that is currently associated with this Service"""
cls.update_current_user_service_data({cls.SPECTER_WALLET_ALIAS: wallet.alias})