Browse Source

Move TTS formatting functions to new module

The new `pluginutils` module contains the TTS formatting functions
previously only defined in the Dark Sky plugin.  This change comes
because these functions are of general utility, and may eventually
be configurable in one centralized place for all of Kaylee.
Clara Hobbs 6 years ago
parent
commit
54ee668490
2 changed files with 59 additions and 43 deletions
  1. 17
    43
      kayleevc/plugins/darksky.py
  2. 42
    0
      kayleevc/pluginutils.py

+ 17
- 43
kayleevc/plugins/darksky.py View File

30
 
30
 
31
 import requests
31
 import requests
32
 
32
 
33
+from kayleevc.pluginutils import *
33
 from . import PluginBase, Handler
34
 from . import PluginBase, Handler
34
 
35
 
35
 
36
 
112
         with open(self._cache_filename, 'r') as f:
113
         with open(self._cache_filename, 'r') as f:
113
             return json.load(f)
114
             return json.load(f)
114
 
115
 
115
-    def _format_temperature(self, temperature, unit='Fahrenheit'):
116
-        """Format a temperature for the TTS system"""
117
-        return f'{temperature:.{self._temp_precision}f} degrees {unit}'
118
-
119
-    def _format_percent(self, value):
120
-        """Format a percentage value for the TTS system"""
121
-        return f'{100*value:.0f} percent'
122
-
123
-    def _format_time(self, epoch_time):
124
-        """Format a time in seconds since the epoch for the TTS system"""
125
-        t = time.localtime(epoch_time)
126
-
127
-        # Format each part of the time to be pronounced nicely
128
-        hour = time.strftime('%I', t)
129
-        if hour[0] == '0':
130
-            hour = hour[1]
131
-
132
-        minute = time.strftime('%M', t)
133
-        if minute[0] == '0':
134
-            if minute[1] == '0':
135
-                minute = "o'clock"
136
-            else:
137
-                minute = 'O' + minute[1]
138
-
139
-        ante_post = time.strftime('%p', t)
140
-        if ante_post[0] == 'A':
141
-            ante_post = 'AE ' + ante_post[1]
142
-
143
-        # Put the parts together
144
-        return f'{hour} {minute} {ante_post}'
145
-
146
     def __call__(self, tts):
116
     def __call__(self, tts):
147
         """Update the cache if necessary and load it"""
117
         """Update the cache if necessary and load it"""
148
         self._update_cache_if_stale()
118
         self._update_cache_if_stale()
155
     def __call__(self, tts):
125
     def __call__(self, tts):
156
         """Speak the current temperature"""
126
         """Speak the current temperature"""
157
         super().__call__(tts)
127
         super().__call__(tts)
158
-        tts(self._format_temperature(self.cache['currently']['temperature']))
128
+        tts(format_temperature(self.cache['currently']['temperature'],
129
+            self._temp_precision))
159
 
130
 
160
 
131
 
161
 class DarkSkyTodaysHighHandler(DarkSkyHandler):
132
 class DarkSkyTodaysHighHandler(DarkSkyHandler):
164
     def __call__(self, tts):
135
     def __call__(self, tts):
165
         """Speak today's high"""
136
         """Speak today's high"""
166
         super().__call__(tts)
137
         super().__call__(tts)
167
-        tts(self._format_temperature(
168
-            self.cache['daily']['data'][0]['temperatureMax']))
138
+        tts(format_temperature(
139
+            self.cache['daily']['data'][0]['temperatureMax'],
140
+            self._temp_precision))
169
 
141
 
170
 
142
 
171
 class DarkSkyTodaysLowHandler(DarkSkyHandler):
143
 class DarkSkyTodaysLowHandler(DarkSkyHandler):
174
     def __call__(self, tts):
146
     def __call__(self, tts):
175
         """Speak today's low"""
147
         """Speak today's low"""
176
         super().__call__(tts)
148
         super().__call__(tts)
177
-        tts(self._format_temperature(
178
-            self.cache['daily']['data'][0]['temperatureMin']))
149
+        tts(format_temperature(
150
+            self.cache['daily']['data'][0]['temperatureMin'],
151
+            self._temp_precision))
179
 
152
 
180
 
153
 
181
 class DarkSkyTomorrowsHighHandler(DarkSkyHandler):
154
 class DarkSkyTomorrowsHighHandler(DarkSkyHandler):
184
     def __call__(self, tts):
157
     def __call__(self, tts):
185
         """Speak tomorrow's high"""
158
         """Speak tomorrow's high"""
186
         super().__call__(tts)
159
         super().__call__(tts)
187
-        tts(self._format_temperature(
188
-            self.cache['daily']['data'][1]['temperatureMax']))
160
+        tts(format_temperature(
161
+            self.cache['daily']['data'][1]['temperatureMax'],
162
+            self._temp_precision))
189
 
163
 
190
 
164
 
191
 class DarkSkyHumidityHandler(DarkSkyHandler):
165
 class DarkSkyHumidityHandler(DarkSkyHandler):
194
     def __call__(self, tts):
168
     def __call__(self, tts):
195
         """Speak the current relative humidity"""
169
         """Speak the current relative humidity"""
196
         super().__call__(tts)
170
         super().__call__(tts)
197
-        tts(self._format_percent(self.cache['currently']['humidity']))
171
+        tts(format_percent(self.cache['currently']['humidity']))
198
 
172
 
199
 
173
 
200
 class DarkSkyCurrentConditionsHandler(DarkSkyHandler):
174
 class DarkSkyCurrentConditionsHandler(DarkSkyHandler):
204
         """Speak the current weather conditions"""
178
         """Speak the current weather conditions"""
205
         super().__call__(tts)
179
         super().__call__(tts)
206
         summary = self.cache['currently']['summary']
180
         summary = self.cache['currently']['summary']
207
-        temperature = self._format_temperature(
208
-                self.cache['currently']['temperature'])
209
-        humidity = self._format_percent(self.cache['currently']['humidity'])
181
+        temperature = format_temperature(
182
+                self.cache['currently']['temperature'], self._temp_precision)
183
+        humidity = format_percent(self.cache['currently']['humidity'])
210
         tts(f'{summary}, {temperature}, relative humidity {humidity}')
184
         tts(f'{summary}, {temperature}, relative humidity {humidity}')
211
 
185
 
212
 
186
 
216
     def __call__(self, tts):
190
     def __call__(self, tts):
217
         """Speak the time of sunset"""
191
         """Speak the time of sunset"""
218
         super().__call__(tts)
192
         super().__call__(tts)
219
-        tts(self._format_time(self.cache['daily']['data'][0]['sunsetTime']))
193
+        tts(format_time(self.cache['daily']['data'][0]['sunsetTime']))
220
 
194
 
221
 
195
 
222
 class DarkSkyProviderHandler(DarkSkyHandler):
196
 class DarkSkyProviderHandler(DarkSkyHandler):

+ 42
- 0
kayleevc/pluginutils.py View File

1
+# This is part of Kaylee
2
+# -- this code is licensed GPLv3
3
+# Copyright 2015-2017 Clayton G. Hobbs
4
+# Portions Copyright 2013 Jezra
5
+
6
+"""Helper utilities for Kaylee plugins"""
7
+
8
+import time
9
+
10
+
11
+def format_temperature(temperature, precision, unit='degrees Fahrenheit'):
12
+    """Format a temperature for the TTS system"""
13
+    return f'{temperature:.{precision}f} {unit}'
14
+
15
+def format_percent(value):
16
+    """Format a floating-point value as a percentage for the TTS system"""
17
+    return f'{100*value:.0f} percent'
18
+
19
+def format_time(seconds=None):
20
+    """Format a time in seconds since the Epoch for the TTS system"""
21
+    t = time.localtime(seconds)
22
+
23
+    # Format each part of the time to be pronounced nicely
24
+    hour = time.strftime('%I', t)
25
+    if hour[0] == '0':
26
+        hour = hour[1]
27
+
28
+    minute = time.strftime('%M', t)
29
+    if minute[0] == '0':
30
+        if minute[1] == '0':
31
+            minute = "o'clock"
32
+        else:
33
+            minute = 'O' + minute[1]
34
+
35
+    ante_post = time.strftime('%p', t)
36
+    if ante_post[0] == 'A':
37
+        ante_post = 'AE ' + ante_post[1]
38
+    elif ante_post[0] == 'P':
39
+        ante_post = 'P ' + ante_post[1]
40
+
41
+    # Put the parts together
42
+    return f'{hour} {minute} {ante_post}'

Loading…
Cancel
Save