-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgSpeech.py
executable file
·727 lines (608 loc) · 25.2 KB
/
gSpeech.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#!/usr/bin/env python
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
import os, sys, shutil, tempfile, ConfigParser, subprocess, multiprocessing
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Notify', '0.7')
from gi.repository import Gtk, Notify, Gdk, GdkPixbuf
gi.require_version('Gst', '1.0')
from gi.repository import Gst
Gst.init("")
APPNAME = "gSpeech"
SCRIPT_DIR = os.path.abspath(os.path.dirname(sys.argv[0]))
import gettext
localdir = os.path.abspath(SCRIPT_DIR) + "/locale"
gettext.install(APPNAME, localdir)
#########################
# Application info
ICON = os.path.join(SCRIPT_DIR, 'icons', APPNAME + '.svg')
VERSION = "0.8.0.0"
AUTHORNAME = "Lahire Biette,Sardi Carlo,Intika"
AUTHOR = AUTHORNAME + ' ' + AUTHOREMAIL
COMMENT = _("A little script to read SVOX Pico texts selected with the mouse.")
COPYRIGHT_YEAR = '2011,2014,2018,2019'
COPYRIGHTS = u"Copyright © %s %s" % (COPYRIGHT_YEAR, AUTHORNAME)
AUTHORS = [
_(u"Developers :"),
u"%s" % (AUTHOR),
#~ "",
#~ _(u"Contributors:"),
]
TRANSLATORS = u"pt-PT, pt-BR, es-ES & it-IT :\n\
Dupouy Paul"
#~ ARTISTS = []
WEBSITE = 'https://github.com/Intika-Linux-Apps/gSpeech'
LICENSE = """Copyright © %s - %s.
%s is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
%s is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with %s. If not, see <http://www.gnu.org/licenses/>.
""" % (COPYRIGHT_YEAR, AUTHOR, APPNAME, APPNAME, APPNAME)
# Supported SVOX Pico's languages
LISTLANG = ["de-DE", "en-GB", "en-US", "es-ES", "fr-FR", "it-IT"]
# Supported SVOX Pico's languages
SPEEDS = ["0.6", "0.8", "1.0", "1.2", "1.4", "1.6", "1.8", "2.0", "2.2"]
# Temporaries files
CACHEFOLDER = os.getenv('HOME') + '/.cache/' + APPNAME + '/'
if not os.path.exists(CACHEFOLDER):
os.makedirs(CACHEFOLDER)
# Temporary PID file
PID = CACHEFOLDER + 'gspeech.pid'
# Temporary wav speech file
SPEECH = CACHEFOLDER + 'speech.wav'
###############################
###############################
# Main Class
class MainApp:
""" the main class of the software """
def __init__(self, config):
# init app name in notification
Notify.init('gSpeech')
# define speech language
self.lang = DefaultLang
self.speed = DefaultSpeed
# select related icon
#~ self.icon = APPNAME + '-' + self.lang
self.icon = ICON
self.config = config
if IsAppIndicator == True :
self.ind =appindicator.Indicator.new(APPNAME, self.icon, appindicator.IndicatorCategory.APPLICATION_STATUS)
self.ind.set_status (appindicator.IndicatorStatus.ACTIVE)
self.onRightClick(self)
elif IsAppIndicator == False :
# create GTK status icon
self.tray = Gtk.StatusIcon()
self.tray.set_from_file(self.icon) # select icon
self.tray.connect('popup-menu', self.onRightClick) # right click
self.tray.connect('activate', self.onLeftClick) # left click
self.tray.set_tooltip_text((_(u"SVOX Pico simple GUI")))
self.window = Gtk.Dialog(APPNAME,
None,
Gtk.DialogFlags.MODAL| Gtk.DialogFlags.DESTROY_WITH_PARENT)
self.window.set_border_width(10)
self.window.set_keep_above(True)
self.window.set_icon_from_file(ICON)
self.window.connect('delete-event', lambda w, e: w.hide() or True)
hbox = Gtk.HBox()
# Create an accelerator group
self.accelgroup = Gtk.AccelGroup()
# Add the accelerator group to the toplevel window
self.window.add_accel_group(self.accelgroup)
button = Gtk.Button()
button.set_image(Gtk.Image.new_from_stock(Gtk.STOCK_EXECUTE, Gtk.IconSize.MENU))
button.set_label(_("Read Selected Text"))
button.set_property("width-request", 300)
button.connect("clicked", self.onExecute)
button.add_accelerator("clicked",self.accelgroup , ord('x'),Gdk.ModifierType.SHIFT_MASK, Gtk.AccelFlags.VISIBLE)
hbox.pack_start(button, False, False,0)
self.window.vbox.pack_start(hbox, False, False,0)
hbox = Gtk.HBox()
button = Gtk.Button()
button.set_image(Gtk.Image.new_from_stock(Gtk.STOCK_EXECUTE,Gtk.IconSize.MENU))
button.set_label(_("Read Clipboard Content"))
button.set_property("width-request", 300)
button.connect("clicked", self.onExecute)
button.add_accelerator("clicked",self.accelgroup , ord('c'), Gdk.ModifierType.SHIFT_MASK, Gtk.AccelFlags.VISIBLE)
hbox.pack_start(button, False, False,0)
self.window.vbox.pack_start(hbox, False, False,0)
hbox = Gtk.HBox()
self.WinPlayPause = Gtk.Button(stock = Gtk.STOCK_MEDIA_PAUSE)
self.WinPlayPause.connect("clicked", self.onPlayPause)
self.WinPlayPause.set_property("width-request", 75)
button.add_accelerator("clicked",self.accelgroup , ord('p'),Gdk.ModifierType.SHIFT_MASK, Gtk.AccelFlags.VISIBLE)
hbox.pack_start(self.WinPlayPause, False, False,0)
button = Gtk.Button(stock = Gtk.STOCK_MEDIA_STOP)
button.connect("clicked", self.onStop)
button.set_property("width-request", 75)
button.add_accelerator("clicked",self.accelgroup , ord('q'), Gdk.ModifierType.SHIFT_MASK, Gtk.AccelFlags.VISIBLE)
hbox.pack_start(button, False, False,0)
combobox = Gtk.ComboBoxText.new()
combobox.set_property("width-request", 75)
hbox.pack_start(combobox, False, False,0)
count = 0
for i in LISTLANG:
combobox.append_text(i)
if i == self.lang:
combobox.set_active(count)
count += 1
combobox.connect('changed', self.changed_cb)
button = Gtk.Button(stock = Gtk.STOCK_SAVE)
button.connect("clicked", self.onSave)
button.set_property("width-request", 75)
button.add_accelerator("clicked",self.accelgroup , ord('s'), Gdk.ModifierType.SHIFT_MASK, Gtk.AccelFlags.VISIBLE)
hbox.pack_end(button, False, False,0)
self.window.vbox.pack_start(hbox, False, False,0)
def changed_cb(self, combobox):
model = combobox.get_model()
index = combobox.get_active()
if index:
self.onLang(self, model[index][0])
return
# action on right click
def onRightClick(self, icon=None, event_button=None, event_time=None):
# create menu
menu = Gtk.Menu()
mediawin = Gtk.MenuItem.new_with_label(_("Control Center Window"))
mediawin.connect('activate', self.onMediaDialog)
mediawin.show()
menu.append(mediawin)
# Separator
rmItem = Gtk.SeparatorMenuItem()
rmItem.show()
menu.append(rmItem)
# Execute menu item : execute speeching from Desktop clipboard
rmItem = Gtk.MenuItem.new_with_label(_("Read Clipboard Content"))
rmItem.connect('activate', self.onExecute)
rmItem.show()
menu.append(rmItem)
# Execute menu item : execute speeching from X.org clipboard
rmItem = Gtk.MenuItem.new_with_label(_("Read Selected Text"))
rmItem.connect('activate', self.onExecute)
rmItem.show()
menu.append(rmItem)
# Separator
rmItem = Gtk.SeparatorMenuItem()
rmItem.show()
menu.append(rmItem)
# Play item menu
self.MenuPlayPause = Gtk.CheckMenuItem.new_with_label(_("Pause"))
self.MenuPlayPause.set_active(False)
self.MenuPlayPause.connect('toggled', self.onPlayPause)
self.MenuPlayPause.show()
menu.append(self.MenuPlayPause)
# Stop item menu
rmItem = Gtk.MenuItem.new_with_label(_("Stop"))
rmItem.connect('activate', self.onStop)
rmItem.show()
menu.append(rmItem)
# Save item menu
rmItem = Gtk.MenuItem.new_with_label(_("Save"))
rmItem.connect('activate', self.onSave)
rmItem.show()
menu.append(rmItem)
# Separator
rmItem = Gtk.SeparatorMenuItem()
rmItem.show()
menu.append(rmItem)
# Preference item menu
rmItem = Gtk.MenuItem.new_with_label(_(u"Voice Speed"))
rmItem.show()
# Creating and linking langues submenu
menulngs = Gtk.Menu()
rmItem.set_submenu(menulngs)
# Creating languages items in submenu
# one empty item to initiate radioitem group
smItem = Gtk.RadioMenuItem(None, None)
for i in SPEEDS:
# Creating new item
#smItem = Gtk.RadioMenuItem(smItem, i, True)
smItem = Gtk.RadioMenuItem.new_with_label_from_widget(smItem, i)
# ... adding item in submenu
menulngs.append(smItem)
# linking it with onLang fonction
smItem.connect("toggled", self.onSpeed, i)
# i is defaut language activating radio button
if i == self.speed :
smItem.set_active(True)
# show item
smItem.show()
menu.append(rmItem)
# Preference item menu
rmItem = Gtk.MenuItem.new_with_label(_(u"Languages"))
rmItem.show()
# Creating and linking langues submenu
menulngs = Gtk.Menu()
rmItem.set_submenu(menulngs)
# Creating languages items in submenu
# one empty item to initiate radioitem group
smItem = Gtk.RadioMenuItem(None, None)
for i in LISTLANG:
# Creating new item
#smItem = Gtk.RadioMenuItem(smItem, i, True)
smItem = Gtk.RadioMenuItem.new_with_label_from_widget(smItem, i)
# ... adding item in submenu
menulngs.append(smItem)
# linking it with onLang fonction
smItem.connect("toggled", self.onLang, i)
# i is defaut language activating radio button
if i == self.lang :
smItem.set_active(True)
# show item
smItem.show()
menu.append(rmItem)
# Preferences
options = Gtk.MenuItem.new_with_label(_("Options"))
options.connect('activate', self.onOptions)
options.show()
menu.append(options)
# Separator
rmItem = Gtk.SeparatorMenuItem()
rmItem.show()
menu.append(rmItem)
## Reload item menu
item = Gtk.MenuItem.new_with_label(_("Reload"))
item.connect('activate', self.onReload)
item.show()
menu.append(item)
# About item menu : show About dialog
about = Gtk.MenuItem.new_with_label(_("About"))
about.connect('activate', self.onAbout)
about.show()
menu.append(about)
# Quit item menu
item = Gtk.MenuItem.new_with_label(_("Quit"))
item.connect('activate', self.destroy)
item.show()
menu.append(item)
if IsAppIndicator == True :
menu.show()
self.ind.set_menu(menu)
elif IsAppIndicator == False :
menu.popup(None, None, None,self.tray, event_button, event_time)
## onReload item function: reload script
def onReload(self, widget):
myscript = os.path.abspath(sys.argv[0])
subprocess.Popen(myscript)
sys.exit()
## action on language submenu items
def onLang(self, widget, lng):
self.lang = lng
#~ self.icon = APPNAME + '-' + self.lang
self.icon = os.path.join(SCRIPT_DIR, 'icons', APPNAME + '-' + self.lang + '.svg')
config.default_language = lng
config.update()
if IsAppIndicator == True :
self.ind.set_icon(self.icon)
elif IsAppIndicator == False :
self.tray.set_from_file(self.icon)
## action on speed submenu items
def onSpeed(self, widget, speed):
self.speed = speed
config.default_speed = speed
config.update()
# show about dialog
def onAbout(self, widget):
self.aboutdiag = AboutDialog()
# show options dialog
def onOptions(self, widget):
self.options_dialog = OptionDialog(self.config)
# show multimedia control dialog
def onMediaDialog(self, widget):
if self.window.get_property('visible'):
self.window.hide()
else :
self.window.show_all()
# error message on playing function
def onMessage(self, bus, message):
t = message.type
if t == Gst.MessageType.EOS:
#file ended, stop
self.player.set_state(Gst.State.NULL)
elif t == Gst.MessageType.ERROR:
#Error ocurred, print and stop
self.player.set_state(Gst.State.NULL)
err, debug = message.parse_error()
print "Error: %s" % err, debug
# left click on status icon function
# function like this and not merge with onExecute
# to have possibility to use for something different
def onLeftClick(self, widget, data=None):
self.onExecute(self, widget)
# on Execute item function : execute speech
def onExecute(self, widget, data=None):
if widget.get_label() == _("Read selected text") :
text = Gtk.Clipboard.get(Gdk.SELECTION_PRIMARY).wait_for_text()
else :
text = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD).wait_for_text()
if text == None:
if self.config.show_notification:
try:
Notify.Notification.new(APPNAME, _("No text selected."), self.icon).show()
except:
pass
else :
if self.config.show_notification:
try:
Notify.Notification.new(APPNAME, _(u"I'm reading the text. One moment please."), self.icon).show()
except:
pass
#~ text = text.lower()
text = text.replace('\"', '')
text = text.replace('`', '')
text = text.replace('´', '')
text = text.replace('-','')
lngDict = CONFIGDIR + '/' + self.lang + '.dic'
if os.path.exists(lngDict) :
for line in open(lngDict,'r').readlines():
bad = line.split('=')[0]
#~ bad = bad.lower()
try :
good = line.split('=')[1]
except :
good = ' '
text = text.replace(bad, good)
if len(text) <= 32768:
os.system('nanotts --speed %s -m -v %s -o %s \"%s\" ' % ( self.speed, self.lang, SPEECH, text ))
#os.system('pico2wave -l %s -w %s \"%s\" ' % ( self.lang, SPEECH, text ))
elif os.path.isfile('/usr/bin/sox'):
discours = text.split('\n\n')
cmds = []
names = []
text = ''
for idx,paragraph in enumerate(discours):
text += paragraph
if idx == len(discours)-1 or len(text) + len(discours[idx+1]) >= 32767:
filename = CACHEFOLDER + 'speech' + str(idx) + '.wav'
cmds.append('nanotts --speed %s -m -v %s -o %s \"%s\" ' % ( self.speed, self.lang, filename, text ))
#cmds.append('pico2wave -l %s -w %s \"%s\" ' % ( self.lang, filename, text ))
names.append(filename)
text = ''
nproc = int(.5 * multiprocessing.cpu_count())
if nproc == 0:
nproc = 1
multiprocessing.Pool(nproc).map(os.system, cmds)
os.system('sox %s %s' % ( ' '.join(names), SPEECH ))
for fichier in names:
os.remove(fichier)
else:
"""A corriger"""
print("Le text est trop long pour être lue sans utiliser sox")
exit()
if hasattr(self, 'player'):
self.player.set_state(Gst.State.NULL)
player = self.onPlayer(SPEECH)
self.player.set_state(Gst.State.PLAYING)
self.buttonState()
# player fonction
def onPlayer(self,file):
#Element playbin automatic plays any file
self.player = Gst.ElementFactory.make("playbin", "player")
#self.player = Gst.ElementFactory.make("playbin", None)
#Set the uri to the file
self.player.set_property("uri", "file://" + file)
#Enable message bus to check for errors in the pipeline
bus = self.player.get_bus()
bus.add_signal_watch()
bus.connect("message", self.onMessage)
# play, pause and stop function for respectivs items
def onPlayPause(self, widget, data=None):
if widget.get_label() == Gtk.STOCK_MEDIA_PLAY or (hasattr(widget,'get_active') and widget.get_active() == False):
self.player.set_state(Gst.State.PLAYING)
else:
self.player.set_state(Gst.State.PAUSED)
self.buttonState()
def onStop(self, widget, data=None):
self.player.set_state(Gst.State.NULL)
self.buttonState()
def buttonState(self):
#block handler cause set_active activate the function onPlayPause
self.MenuPlayPause.handler_block_by_func(self.onPlayPause)
if Gst.State.PLAYING == self.player.get_state(Gst.CLOCK_TIME_NONE)[1] :
self.WinPlayPause.set_label(Gtk.STOCK_MEDIA_PAUSE)
self.MenuPlayPause.set_active(False)
elif Gst.State.PAUSED == self.player.get_state(Gst.CLOCK_TIME_NONE)[1] or Gst.State.NULL == self.player.get_state(Gst.CLOCK_TIME_NONE)[1] :
self.WinPlayPause.set_label(Gtk.STOCK_MEDIA_PLAY)
self.MenuPlayPause.set_active(True)
self.MenuPlayPause.handler_unblock_by_func(self.onPlayPause)
# saving file speech on clicking Save item
def onSave(self, widget, data=None):
SaveFile()
# destroy app on clicking Quit item
def destroy(self, widget, data=None):
# remove temporary sound file
if os.path.isfile(SPEECH):
os.remove(SPEECH)
# remove file with current process pid
if os.path.isfile(PID):
os.remove(PID)
# quit program
Gtk.main_quit()
def main(self):
Gtk.main()
# About dialog class
class AboutDialog:
""" the about dialog class """
def __init__(self):
# Create AboutDialog object
dialog = Gtk.AboutDialog()
#~ dialog.set_logo_icon_name(APPNAME)
dialog.set_logo(GdkPixbuf.Pixbuf.new_from_file(ICON))
dialog.set_name(APPNAME)
dialog.set_version(VERSION)
dialog.set_copyright(COPYRIGHTS)
dialog.set_license(LICENSE)
dialog.set_authors(AUTHORS)
dialog.set_comments(COMMENT)
dialog.set_translator_credits(TRANSLATORS)
dialog.set_website(WEBSITE)
dialog.set_website_label(_("%s's Website") % APPNAME)
#~ dialog.set_artists(ARTISTS)
dialog.connect("response", lambda self, *f: self.destroy())
dialog.show_all()
class OptionDialog:
""" the options dialog class """
def __init__(self, config):
dialog = Gtk.Dialog(
APPNAME,
None,
Gtk.DialogFlags.MODAL| Gtk.DialogFlags.DESTROY_WITH_PARENT
)
dialog.set_name(APPNAME)
dialog.set_border_width(10)
hbox = Gtk.HBox()
notification_check = Gtk.CheckButton(_("Active notification"))
notification_check.set_active(config.show_notification)
notification_check.connect("toggled", self.on_checked, config)
hbox.add(notification_check)
dialog.vbox.pack_start(hbox, False, False, 0)
dialog.show_all()
def on_checked(self, checkBox, config):
config.show_notification = checkBox.get_active()
config.update()
# Audio speech wav file class saving
class SaveFile:
""" the class to save the speech .wav file """
def __init__(self):
dialog = Gtk.FileChooserDialog(_("Save the speech"),
None,
Gtk.FileChooserAction.SAVE,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK))
dialog.set_default_response(Gtk.ResponseType.OK)
dialog.set_current_folder(os.path.expanduser('~'))
#~ dialog.set_modal(False)
#~ dialog.set_transient_for(window)
#~ dialog.set_decorated(False)
filter = Gtk.FileFilter()
filter.set_name(_(u"Wave file (*.wav)"))
filter.add_mime_type("audio/x-wav")
filter.add_pattern("*.wav")
dialog.add_filter(filter)
response = dialog.run()
if response == Gtk.ResponseType.OK:
filename2 = dialog.get_filename()
filename2 = filename2 + '.wav'
shutil.copy(SPEECH, filename2)
dialog.destroy()
def IniRead(configfile, section, key, default):
if os.path.isfile(configfile):
parser = ConfigParser.SafeConfigParser()
parser.read(configfile)
try:
var = parser.get( section , key )
except:
var = default
else:
var = default
if var.lower() in ['1', 'yes', 'true', 'on'] :
return True
elif var.lower() in ['0', 'no', 'false', 'off'] :
return False
else :
return var
class Config:
_path = ''
use_appindicator = True
default_language = ''
default_speed = ''
section = 'CONFIGURATION'
show_notification = True
def __init__(self, path):
self._path = path
raw = ConfigParser.ConfigParser()
raw.read(self._path)
if raw.has_option(self.section, 'shownotification'):
self.show_notification = raw.getboolean(
self.section,
'shownotification'
)
def update(self):
raw = ConfigParser.RawConfigParser()
raw.add_section(self.section)
raw.set(
self.section,
'USEAPPINDICATOR',
self.use_appindicator
)
raw.set(
self.section,
'DEFAULTLANGUAGE',
self.default_language
)
raw.set(
self.section,
'DEFAULTSPEED',
self.default_speed
)
raw.set(
self.section,
'SHOWNOTIFICATION',
self.show_notification
)
with open(self._path, 'wb') as configfile:
raw.write(configfile)
if __name__ == "__main__":
# is PID exists?
if os.path.isfile(PID):
# yes. read it
file = open(PID, 'r')
# convert content into integer, to retreive PID
pid = int(file.read())
# try to kill PID
try:
os.kill(pid, 0)
# error. process not exists, pass
except OSError:
pass
# process exists. print error message and quit
else:
# process existing => show error message
print "** %s is already running\nOtherwise, delete %s" % (APPNAME,PID)
quit()
# finding pid of this process
pid = "%s" % os.getpid()
# write pid in PID
file = open(PID, 'w')
file.write(pid)
file.close()
CONFIGDIR = os.path.join(os.path.expanduser('~'), '.config/gSpeech')
if not os.path.isdir(CONFIGDIR) :
os.mkdir(CONFIGDIR, 0775)
CONFIGFILE = os.path.join(CONFIGDIR, 'gSpeech.conf')
if not os.path.isfile(CONFIGFILE) :
raw = ConfigParser.RawConfigParser()
raw.add_section('CONFIGURATION')
raw.set('CONFIGURATION', 'USEAPPINDICATOR', 'True')
raw.set('CONFIGURATION', 'DEFAULTSPEED', '1.0')
raw.set('CONFIGURATION', 'DEFAULTLANGUAGE', '')
#~ config.set('CONFIGURATION', 'SHOWMEDIADIALOG', 'False')
with open(CONFIGFILE, 'wb') as configfile:
raw.write(configfile)
IsAppIndicator = bool(IniRead(CONFIGFILE, 'CONFIGURATION', 'USEAPPINDICATOR', 'True' ))
DefaultSpeed = str(IniRead(CONFIGFILE, 'CONFIGURATION', 'DEFAULTSPEED', '' ))
DefaultLang = str(IniRead(CONFIGFILE, 'CONFIGURATION', 'DEFAULTLANGUAGE', '' ))
DefaultLang = DefaultLang[ : 2 ] + '-' + DefaultLang[ 3 : ][ : 2 ]
# if SVOX Pico not support this language, find os environment language
if not DefaultLang in LISTLANG :
DefaultLang = os.environ.get('LANG', 'en_US')[ : 2 ] + '-' + os.environ.get('LANG', 'en_US')[ 3 : ][ : 2 ]
# if SVOX Pico not support this language, use US english
if not DefaultLang in LISTLANG :
DefaultLang = "en-US"
try :
gi.require_version('AppIndicator3', '0.1')
from gi.repository import AppIndicator3 as appindicator
except :
IsAppIndicator = False
config = Config(CONFIGFILE)
config.use_appindicator = IsAppIndicator
config.default_language = DefaultLang
config.default_speed = DefaultSpeed
gSpeech = MainApp(config)
gSpeech.main()