Debian Codegraph @

src/fenrirscreenreader/commands/onHeartBeat/76000-time.py

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # Fenrir TTY screen reader
5 # By Chrys, Storm Dragon, and contributors.
6
7 import datetime
8 import fcntl
9 import os
10 import tempfile
11 import time
12
13 from fenrirscreenreader.core.i18n import _
14
15
16 ANNOUNCEMENT_LOCK_TIMEOUT_SEC = 5.0
17
18
19 class command:
20 def __init__(self):
21 pass
22
23 def initialize(self, environment):
24 self.env = environment
25 self.last_time = datetime.datetime.now()
26 self.lastDateString = ""
27 self.lastTimeString = ""
28
29 def shutdown(self):
30 pass
31
32 def get_description(self):
33 return "No Description found"
34
35 def _get_announcement_lock_path(self):
36 return os.path.join(
37 tempfile.gettempdir(),
38 "fenrirscreenreader-time-announcement.lock",
39 )
40
41 def _read_announcement_lock_slot(self, lock_file):
42 lock_file.seek(0)
43 lock_content = lock_file.readline().strip().split()
44 if len(lock_content) < 2:
45 return ""
46 return lock_content[1]
47
48 def _claim_announcement_lock(self, announcement_slot):
49 now = time.time()
50 lock_path = self._get_announcement_lock_path()
51 try:
52 lock_fd = os.open(lock_path, os.O_CREAT | os.O_RDWR, 0o666)
53 except OSError:
54 return False
55 try:
56 os.chmod(lock_path, 0o666)
57 except OSError:
58 pass
59
60 with os.fdopen(lock_fd, "r+", encoding="utf-8") as lock_file:
61 try:
62 fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX)
63 lock_slot = self._read_announcement_lock_slot(lock_file)
64 lock_stat = os.fstat(lock_file.fileno())
65
66 if lock_slot == announcement_slot:
67 return False
68
69 lock_file.seek(0)
70 lock_file.truncate()
71 lock_file.write(f"{os.getpid()} {announcement_slot} {now}\n")
72 lock_file.flush()
73 os.fsync(lock_file.fileno())
74 return True
75 finally:
76 fcntl.flock(lock_file.fileno(), fcntl.LOCK_UN)
77
78 def run(self):
79 if not self.env["runtime"]["SettingsManager"].get_setting_as_bool(
80 "time", "enabled"
81 ):
82 return
83
84 on_minutes = self.env["runtime"]["SettingsManager"].get_setting(
85 "time", "on_minutes"
86 )
87 delay_sec = self.env["runtime"]["SettingsManager"].get_setting_as_int(
88 "time", "delay_sec"
89 )
90
91 # no need
92 if on_minutes == "" and delay_sec <= 0:
93 return
94
95 on_minutes = on_minutes.split(",")
96 now = datetime.datetime.now()
97
98 # ignore on_minutes if there is a delay_sec
99 if delay_sec > 0:
100 if int((now - self.last_time).total_seconds()) < delay_sec:
101 return
102 announcement_slot = f"delay:{int(now.timestamp()) // delay_sec}"
103 else:
104 # should announce?
105 if not str(now.minute).zfill(2) in on_minutes:
106 return
107 # already announced?
108 if now.hour == self.last_time.hour:
109 if now.minute == self.last_time.minute:
110 return
111 announcement_slot = f"minute:{datetime.datetime.strftime(now, '%Y%m%d%H%M')}"
112
113 date_format = self.env["runtime"]["SettingsManager"].get_setting(
114 "general", "date_format"
115 )
116 date_string = datetime.datetime.strftime(now, date_format)
117
118 present_date = (
119 self.env["runtime"]["SettingsManager"].get_setting_as_bool(
120 "time", "present_date"
121 )
122 and self.lastDateString != date_string
123 )
124 present_time = self.env["runtime"][
125 "SettingsManager"
126 ].get_setting_as_bool("time", "present_time")
127
128 # no changed value to announce
129 if not (present_date or present_time):
130 return
131
132 if not self._claim_announcement_lock(announcement_slot):
133 self.last_time = now
134 return
135
136 time_format = self.env["runtime"]["SettingsManager"].get_setting(
137 "general", "time_format"
138 )
139 time_string = datetime.datetime.strftime(now, time_format)
140
141 if self.env["runtime"]["SettingsManager"].get_setting_as_bool(
142 "time", "interrupt"
143 ):
144 self.env["runtime"]["OutputManager"].interrupt_output()
145 if self.env["runtime"]["SettingsManager"].get_setting_as_bool(
146 "time", "announce"
147 ):
148 self.env["runtime"]["OutputManager"].play_sound_icon("announce")
149
150 if present_time:
151 # present the time
152 self.env["runtime"]["OutputManager"].present_text(
153 _("It's {0}").format(
154 time_string.replace(":00", " O'clock ").lstrip("0")
155 ),
156 sound_icon="",
157 interrupt=False,
158 )
159 # Check if it's 12:00 AM
160 if now.hour == 0 and now.minute == 0:
161 # present the date
162 self.env["runtime"]["OutputManager"].present_text(
163 date_string, sound_icon="", interrupt=False
164 )
165
166 self.last_time = datetime.datetime.now()
167 self.lastTimeString = time_string
168
169 def set_callback(self, callback):
170 pass