-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathsound_the_alarm.py
executable file
·82 lines (68 loc) · 2.34 KB
/
sound_the_alarm.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from collections import OrderedDict
import subprocess
import alarmenv
# Read the system configuration
AlmEnv=alarmenv.alarmEnv()
# Holds the class instances of each individual section by type
sections = {
"content": OrderedDict(),
"effect": OrderedDict(),
"tts": OrderedDict()
}
wadparts=[]
for section in AlmEnv.sections():
# We are going to pass only the main and section specific
# parts of the configuration to the section modules
mainitems = AlmEnv.items('main')
# We'll add the net state to the main section
mainitems.extend((('netup',AlmEnv.netup),))
if (section != 'main' and
AlmEnv.hasAndIs(section, 'enabled', 1)):
try:
handler = AlmEnv.handler(section)
getsec = 'get_' + handler
# Section type -- one of 'effect' 'content' 'tts'
stype = AlmEnv.stype(section)
# AlmEnv options specific to this section
items = AlmEnv.items(section)
# Get the constructor
construct = getattr(__import__(getsec, fromlist=[handler]), handler)
# Construct an instance and put it in out holder
sections[stype][section]=construct(stype,items,AlmEnv.debug,mainitems)
if stype == 'content':
wadparts.extend(sections[stype][section].get(AlmEnv.netup) + " ")
except ImportError:
raise ImportError('Failed to load '+section)
count = 1
# Do the Begin part of all effects
for ename in sections['effect']:
if AlmEnv.debug:
print ename
sections['effect'][ename].begin()
# Turn all of the parts into a single string
wad = (''.join(str(x) for x in wadparts) + AlmEnv.get('main','end'))
if AlmEnv.debug:
print wad
if AlmEnv.get('main','readaloud') == str(1):
# strip any quotation marks
wad = wad.replace('"', ' ').replace("'",' ').strip()
# Try to speak the text
played = False
for tname in sections['tts']:
if AlmEnv.debug:
print tname + ':' + str(played)
if not played: # don't try unless we haven't played
played = sections['tts'][tname].play(wad)
if not played: # Nothing worked, so try festival
print subprocess.call("echo " + wad + " | festival --tts ", shell=True)
else:
print wad
# Do the End part of all effects (in the reverse order they were started)
effects = sections['effect'].items()
effects.reverse()
for effect in effects:
if AlmEnv.debug:
print effect
effect[1].end()