-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleSource.py
More file actions
66 lines (50 loc) · 2.15 KB
/
Copy pathExampleSource.py
File metadata and controls
66 lines (50 loc) · 2.15 KB
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
"""A provider that supplies words and draws nothing.
The other half of this template. A provider has no region, no painting
and no theme reaching it: it answers a role's calls and says so in its
schema. This one is a `TextSource`, the simplest role there is - it says
what the words are, and tells whoever asked when they change.
Keep this file or `Example.py`, not both: a plugin folder holds one class,
and `__init__.py` says which. See the README.
"""
import logging
from PyQt5.QtCore import QTimer
from PiClock3.TextSource import TextSource
logger = logging.getLogger(__name__)
class ExampleSource(TextSource):
"""Called whatever suits: the loader finds the class by inspection."""
# what the service must be credited as, where a widget shows credit.
# A source that needs none says nothing.
attribution = ''
def __init__(self, piclock, name, config):
super().__init__(piclock, name, config)
self.listeners = []
self.at = 0
self.timer = None
def start(self):
"""one instance, however many widgets name it.
A provider starts once and fetches once; a widget that names it
gets what is already here.
"""
self.timer = QTimer()
self.timer.timeout.connect(self.tick)
self.timer.start(int(self.config['refresh']) * 1000)
def subscribe(self, fn):
"""called by every widget that names this provider.
Keep them all: two widgets may draw the same words, and a
provider that remembered only the last one would leave the first
showing what it had at startup.
"""
self.listeners.append(fn)
def text(self):
"""the words as they stand, drawn as they are given"""
lines = self.config['lines']
return self.expand(lines[self.at % len(lines)]) if lines else ''
def tick(self):
"""move to the next line and tell whoever is listening.
A real source fetches here instead, and tells nobody when the
answer has not changed - a widget redrawing the same words costs
a repaint for nothing.
"""
self.at += 1
for fn in self.listeners:
fn()