This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathlanguage_class.py
190 lines (162 loc) · 4.49 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
#!/usr/bin/env python
#
# Raspberry Pi Internet Radio Class
# $Id: language_class.py,v 1.20 2017/03/22 13:53:51 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
from translate_class import Translate
import ConfigParser
log = Log()
translate = Translate()
# 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_radio': 'Loading radio stations',
'loading_media': 'Loading media library',
'search': 'Search',
'source_radio': 'Internet Radio',
'source_media': 'Music library',
'source_airplay': 'Airplay receiver',
'stopping_radio': 'Stopping radio',
'sleep': 'sleep',
'on': 'on',
'off': 'off',
'on': 'on',
'yes': 'yes',
'no': 'no',
'random': 'Random',
'consume': 'Consume',
'repeat': 'Repeat',
'reload': 'Reload',
'timer': 'Timer',
'alarm': 'Alarm',
'alarmhours': 'Alarm hours',
'alarmminutes': 'Alarm minutes',
'streaming': 'Streaming',
'colour': 'Colour',
'voice': 'voice',
}
# 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()
for line in lines:
if line.startswith('#'):
continue
if len(line) < 1:
continue
line = line.rstrip()
param,value = line.split(':')
self.LanguageText[param] = str(value)
except:
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)
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)
return
# Remove problem charachters from speech text
def purgeChars(self,message):
chars = ['!',':','|','*','[',']',
'_','"','.']
# If the first character is ! then supress the message
message = message.lstrip()
if message[0] is '!':
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 self.LanguageText:
text = self.LanguageText[label]
print label + ': ' + text
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