-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlanguage_class.py
246 lines (211 loc) · 7.3 KB
/
language_class.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env python3
#
# Raspberry Pi Internet Radio Class
# $Id: language_class.py,v 1.4 2022/11/26 12:27:07 bob Exp $
#
# Author : Bob Rathbone
# Site : http://www.bobrathbone.com
#
# This class reads the /var/lib/radio/language file for both espeech and LCD display
# The format of this file is:
# <label>:<text>
#
# License: GNU V3, See https://www.gnu.org/copyleft/gpl.html
#
# Disclaimer: Software is provided as is and absolutly no warranties are implied or given.
# The authors shall not be liable for any loss or damage however caused.
import os
import sys
import threading
from log_class import Log
import configparser
log = Log()
# System files
RadioLibDir = "/var/lib/radiod"
LanguageFile = RadioLibDir + "/language"
VoiceFile = RadioLibDir + "/voice"
class Language:
speech = False
# Speech text (Loaded from /var/lib/radio/language)
LanguageText = {
'main_display': 'Main display',
'search_menu': 'Search menu',
'select_source': 'Select source',
'options_menu': 'Options menu',
'rss_display': 'RSS display',
'information': 'Information display',
'the_time': 'The time is',
'loading': 'Loading',
'airplay': 'Airplay',
'spotify': 'Spotify',
'loading_radio': 'Loading radio stations',
'updating_media': 'Updating media',
'update_complete': 'Update complete',
'loading_media': 'Loading media library',
'loading_playlists': 'Loading playlists',
'loading_playlist': 'Loading playlist',
'starting_airplay': 'Starting Airplay',
'starting_spotify': 'Starting Spotify',
'waiting_for_spotify_client': 'Waiting for Spotify client',
'radio_stations' : 'Radio stations',
'media_library': 'Media library',
'updatng_media': 'Updating media',
'search': 'Search',
'source_radio': 'Internet Radio',
'source_media': 'Media library',
'source_airplay': 'Airplay receiver',
'source_soptify': 'Spotify receiver',
'stopping_radio': 'Stopping radio',
'current_station': 'Current station',
'connecting': 'Connecting',
'connected': 'Connected',
'connection_error': 'Connection error',
'station': 'Station',
'track': 'Track',
'sleep': 'Sleep',
'on': 'on',
'off': 'off',
'on': 'on',
'yes': 'yes',
'no': 'no',
'radio_version': 'Radio version',
'mpd_version': 'MPD version',
'wait': 'Please wait',
'title_unknown': 'Title unknown',
'track_unknown': 'Track unknown',
'no_information': 'No information',
# Options
'random': 'Random',
'consume': 'Consume',
'repeat': 'Repeat',
'single': 'Single',
'reload': 'Reload',
'timer': 'Timer',
'alarm': 'Alarm',
'alarmhours': 'Alarm hours',
'alarmminutes': 'Alarm minutes',
'streaming': 'Streaming',
'colour': 'Colour',
'dot': 'dot',
# Volume and speech
'voice': 'voice',
'volume': 'Volume',
'vol': 'Vol',
'muted': 'Sound muted',
# Menu labels
'menu_search': 'Search',
'menu_find': 'Find',
'menu_source': 'Input Source',
'menu_option': 'Menu option:',
# Shutdown
'stop': 'Stopping radio',
'stopped': 'Radio stopped',
'shutdown': 'Shutting down system',
}
# Initialisation routine - Load language
def __init__(self,speech = False):
log.init('radio')
self.speech = speech
self.load()
return
# Load language text file
def load(self):
if os.path.isfile(LanguageFile):
try:
with open(LanguageFile) as f:
lines = f.readlines()
try:
for line in lines:
line = line.rstrip() # Remove LF
line = line.rstrip(':') # Remove any :
if line.startswith('#'):
continue
if len(line) < 1:
continue
line = line.rstrip()
param,value = line.split(':')
self.LanguageText[param] = str(value)
except Exception as e:
log.message(LanguageFile + ":" + str(e), log.ERROR)
except Exception as e:
log.message("Error reading " + LanguageFile, log.ERROR)
return
# Get the text by label
def getText(self,label):
text = ''
try:
text = self.LanguageText[label]
except:
log.message("language.getText Invalid label " + label, log.ERROR)
text = text.lstrip()
return text
# Get the menu text
def getMenuText(self):
menuText = []
sLabels = ['main_display','search_menu','select_source',
'options_menu','rss_display','information',
'sleep',
]
for label in sLabels:
menuText.append(self.getText(label))
return menuText
# Get the menu text
def getOptionText(self):
OptionText = []
sLabels = [ 'random','consume','repeat','reload','timer', 'alarm',
'alarmhours','alarmminutes','streaming','colour',
]
for label in sLabels:
OptionText.append(self.getText(label))
return OptionText
# Speak message
def speak(self,message,volume):
if os.path.isfile(VoiceFile) and self.speech:
try:
message = self.purgeChars(message)
cmd = self.execCommand("cat " + VoiceFile)
cmd = cmd + str(volume) + " --stdout | aplay"
cmd = "echo " + '"' + message + '"' + " | " + cmd + " >/dev/null 2>&1"
log.message(cmd, log.DEBUG)
# If the first character is ! then supress the message
if len(message) > 0 and message[0] != '!':
self.execCommand(cmd)
except:
log.message("Error reading " + VoiceFile, log.ERROR)
else:
log.message("No " + LanguageFile + " found!", log.ERROR)
return
# Remove problem characters from speech text
def purgeChars(self,message):
chars = ['!',':','|','*','[',']',
'_','"','.']
# If the first character is ! then supress the message
message = message.lstrip()
if message[0] == '!':
supress = True
else:
supress = False
for char in chars:
message = message.replace(char,'')
message = message.replace('/',' ')
message = message.replace('-',',')
if supress:
message = '!' + message
return message
# Display text
def displayList(self):
for label in sorted(self.LanguageText):
print("%s: %s" % (label, self.LanguageText[label]))
return
# Execute system command
def execCommand(self,cmd):
p = os.popen(cmd)
return p.readline().rstrip('\n')
# Test Language class
if __name__ == '__main__':
language = Language()
language.load()
language.displayList()
# End of class
# set tabstop=4 shiftwidth=4 expandtab
# retab