This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
textspeak.py
581 lines (516 loc) · 25.3 KB
/
textspeak.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
#-*- coding: utf-8 -*-
"""
Simple text-to-speech program, uses the Google Translate text-to-speech online
service. Since the public Google TTS only supports text up to 100 characters,
the text is divided into smaller chunks and the resulting MP3 files are merged.
Idea and parsing from http://glowingpython.blogspot.com/2012/11/
text-to-speech-with-correct-intonation.html
------------------------------------------------------------------------------
This file is part of TextSpeak - a simple text-to-speech program.
Released under the MIT License.
@author Erki Suurjaak
@created 07.11.2012
@modified 02.03.2015
"""
import base64
import datetime
import os
import Queue
import shutil
import sys
import threading
import traceback
import urllib2
import wx
import wx.lib.newevent
import wx.lib.scrolledpanel
import wx.lib.sized_controls
import wx.media
import wx.py
import conf
"""Event class and event binder for new results."""
ResultEvent, EVT_RESULT = wx.lib.newevent.NewEvent()
class TextSpeakWindow(wx.Frame):
"""TextSpeak GUI window."""
def __init__(self):
wx.Frame.__init__(self, parent=None,
title=conf.Title, size=conf.WindowSize)
self.data = {}
self.text = None
self.text_id = None # ID of currently selected text
self.panels_history = []
# In Windows 7 and Vista the wx.media.MediaCtrl fires state change
# events unreliably, so cannot use sequential play.
self.mc_hack = hasattr(sys, "getwindowsversion") \
and sys.getwindowsversion()[0] > 5
icons = wx.IconBundle()
for s in [-1, 32]:
icons.AddIcon(wx.ArtProvider_GetIcon(
wx.ART_TICK_MARK, wx.ART_FRAME_ICON, (s, s)))
self.SetIcons(icons)
panel_frame = wx.lib.sized_controls.SizedPanel(self)
panel_frame.SetSizerType("vertical")
splitter = self.splitter_main = \
wx.SplitterWindow(parent=panel_frame, style=wx.BORDER_NONE)
splitter.SetSizerProps(expand=True, proportion=1)
splitter.SetMinimumPaneSize(1)
# Create main panel with text box, buttons, and media control.
panel_main = wx.lib.sized_controls.SizedPanel(splitter)
panel_main.SetSizerType("vertical")
panel_labels = wx.lib.sized_controls.SizedPanel(panel_main)
panel_labels.SetSizerProps(expand=True, border=(["top"], 5))
panel_labels.SetSizerType("horizontal")
label_text = wx.StaticText(panel_labels, label="&Enter text to speak:")
panel_labels.Sizer.AddStretchSpacer()
label_help = wx.StaticText(
panel_labels, label="Use commas and line breaks to create pauses ")
label_help.ForegroundColour = "GRAY"
label_help.SetSizerProps(halign="right")
text = self.edit_text = wx.TextCtrl(
panel_main, size=(-1, 50), style=wx.TE_MULTILINE | wx.TE_RICH2)
text.SetSizerProps(expand=True, proportion=1,
border=(["left", "right", "bottom"], 3))
gauge = self.gauge = wx.Gauge(panel_main)
gauge.SetSizerProps(expand=True)
panel_buttons = wx.lib.sized_controls.SizedPanel(panel_main)
panel_buttons.SetSizerType("horizontal")
panel_buttons.SetSizerProps(expand=True)
self.button_go = wx.Button(panel_buttons, label="&Text to speech")
panel_buttons.Sizer.AddSpacer(10)
label_lang = wx.StaticText(panel_buttons, label="&Language:")
label_lang.SetSizerProps(border=(["all"], 0), valign="center")
self.list_lang = wx.ComboBox(parent=panel_buttons,
choices=[i[1] for i in conf.Languages], style=wx.CB_READONLY)
self.list_lang.SetSizerProps(border=(["all"], 0), valign="center")
panel_buttons.Sizer.AddStretchSpacer()
self.cb_allatonce = wx.CheckBox(parent=panel_buttons,
label="Complete audio before playing")
self.cb_allatonce.SetSizerProps(halign="right", valign="center")
self.cb_allatonce.Show(not self.mc_hack)
self.button_save = wx.Button(panel_buttons, label="&Save MP3")
gauge.ToolTipString = "Audio data chunks"
self.button_save.Enabled = False
self.list_lang.Selection = conf.Languages.index(("en", "English"))
self.list_lang.ToolTipString = "Choose the speech language"
self.cb_allatonce.Value = True
self.cb_allatonce.ToolTipString = \
"Download all audio chunks and merge them before playing anything"
mc = self.mediactrl = wx.media.MediaCtrl(panel_main, size=(-1, 70))
mc.ShowPlayerControls(wx.media.MEDIACTRLPLAYERCONTROLS_DEFAULT)
mc.SetSizerProps(expand=True)
self.text_info = wx.StaticText(panel_main, label=conf.InfoText,
style=wx.ALIGN_CENTER)
# Create side panel with list of texts, and program information.
panel_side = wx.lib.sized_controls.SizedPanel(splitter)
panel_side.SetSizerType("vertical")
panel_list = self.panel_list = \
wx.lib.scrolledpanel.ScrolledPanel(panel_side)
panel_list.BackgroundColour = "WHITE"
panel_list.SetSizerProps(expand=True, proportion=100)
panel_list.Sizer = wx.BoxSizer(wx.VERTICAL)
panel_side.Sizer.AddStretchSpacer()
panel_btm = wx.lib.sized_controls.SizedPanel(panel_side)
panel_btm.SetSizerProps(halign="right")
panel_btm.SetSizerType("horizontal")
panel_btm.Sizer.AddStretchSpacer()
self.text_version = wx.StaticText(panel_btm,
label=conf.VersionText, style=wx.ALIGN_RIGHT)
self.text_version.ForegroundColour = "GRAY"
self.text_version.ToolTipString = "Ctrl-shift-doubleclick " \
"opens Python console."
panel_btm.Sizer.Add((10, 5))
self.text_version.SetSizerProps(halign="right")
self.link_www = wx.HyperlinkCtrl(panel_btm, id=-1,
label="github", url=conf.URLHomepage)
self.link_www.ToolTipString = "Go to source code repository " \
"at %s" % conf.URLHomepage
self.link_www.SetSizerProps(halign="right")
self.out_queue = Queue.Queue()
self.mp3_loader = TextToMP3Loader(self, self.out_queue)
self.dialog_save = wx.FileDialog(
parent=self,
defaultDir=os.getcwd(),
style=wx.FD_OVERWRITE_PROMPT | wx.FD_SAVE | wx.RESIZE_BORDER)
self.frame_console = wx.py.shell.ShellFrame(
parent=self, title="%s Console" % conf.Title)
self.frame_console.SetIcons(icons)
self.frame_console.Enabled = False # Flag for first toggle
if not self.mc_hack:
mc.Bind(wx.media.EVT_MEDIA_LOADED, self.on_media_loaded)
mc.Bind(wx.media.EVT_MEDIA_FINISHED, self.on_media_finished)
self.Bind(EVT_RESULT, self.on_result_event)
self.Bind(wx.EVT_BUTTON, self.on_text_to_speech, self.button_go)
self.Bind(wx.EVT_BUTTON, self.on_save_mp3, self.button_save)
self.Bind(wx.EVT_CLOSE, lambda evt: self.cleanup())
idfocus = wx.NewId()
self.Bind(wx.EVT_MENU, lambda e: self.edit_text.SetFocus(), id=idfocus)
ac = wx.AcceleratorTable([(wx.ACCEL_ALT, ord("T"), self.button_go.Id),
(wx.ACCEL_ALT, ord("S"), self.button_save.Id),
(wx.ACCEL_ALT, ord("E"), idfocus), ])
self.SetAcceleratorTable(ac)
self.Bind(wx.EVT_CLOSE, self.on_exit)
self.Bind(wx.EVT_SIZE, self.on_size)
self.Bind(wx.EVT_SPLITTER_SASH_POS_CHANGED, self.on_size, splitter)
self.frame_console.Bind(wx.EVT_CLOSE, self.on_toggle_console)
self.text_version.Bind(wx.EVT_LEFT_DCLICK, self.on_toggle_console)
conf.load()
if conf.LastText and isinstance(conf.LastText, basestring):
self.edit_text.Value = conf.LastText
if conf.LastLanguage:
index = next((i for i, x in enumerate(conf.Languages)
if x[0] == conf.LastLanguage), None)
if index is not None:
self.list_lang.Selection = index
if not 0 <= conf.LastVolume <= 1:
conf.LastVolume = 0.5
if conf.WindowPosition and conf.WindowSize:
if [-1, -1] != conf.WindowSize:
self.Position, self.Size = conf.WindowPosition, conf.WindowSize
else:
self.Maximize()
else:
self.Center(wx.HORIZONTAL)
self.Position.top = 50
sashPos = 3 * self.Size.width / 4
splitter.SplitVertically(panel_main, panel_side, sashPosition=sashPos)
self.Show(True)
self.edit_text.SetFocus()
self.edit_text.SetInsertionPoint(-1)
def on_size(self, event):
"""Handler for window size event, tweaks control layout."""
event.Skip()
self.text_info.Label = conf.InfoText
wx.CallAfter(lambda: self and
(self.text_info.Wrap(self.mediactrl.Size.width),
self.text_info.Parent.Layout()))
def on_exit(self, event):
"""Handler on application exit, saves configuration."""
conf.LastText = self.edit_text.Value
conf.LastLanguage = conf.Languages[self.list_lang.Selection][0]
if not self.mediactrl.Tell() < 0: # Nothing loaded and 0 volume if -1
conf.LastVolume = round(self.mediactrl.GetVolume(), 2)
conf.WindowPosition = self.Position[:]
conf.WindowSize = [-1, -1] if self.IsMaximized() else self.Size[:]
conf.save()
event.Skip()
def on_toggle_console(self, event):
"""Handler for events to toggle Python console shown/invisible."""
if isinstance(event, wx.MouseEvent) \
and not (event.CmdDown() and event.ShiftDown()):
return # Must be Ctrl-Shift-doubleclick
if not self.frame_console.Enabled: # First view: set position and size
self.frame_console.Enabled = True
self.frame_console.Size = (self.Size.width, self.Size.height / 3)
self.frame_console.Position = (self.Position.x,
self.Position.y + self.Size.height)
self.frame_console.Show(not self.frame_console.Shown)
def on_result_event(self, event):
"""Handler for a result chunk from TextToMP3Loader."""
text_id = event.TextId
data = self.data[text_id]
if hasattr(event, "Error"):
wx.MessageBox(event.Error, conf.Title, wx.ICON_WARNING | wx.OK)
return
index, count = event.Index, event.Count
chunks, filename = event.Chunks, event.Filename
data["count"] = count
data["chunks"] = chunks
data["filenames"].append(filename)
is_first = (self.text_id == text_id) \
and (len(data["filenames"]) == 1)
is_last = (index == data["count"] - 1)
is_volumeset = not self.mediactrl.Tell() < 0
if is_last and (self.mc_hack
or wx.media.MEDIASTATE_PLAYING != self.mediactrl.State):
# All chunks finished, merge them into one
self.merge_chunks(data)
data["completed"] = True
self.text_id = text_id
if not is_first or self.mc_hack:
self.mediactrl.DONTPLAY = True
self.mediactrl.Load(data["filenames"][0])
if not is_volumeset:
self.mediactrl.SetVolume(conf.LastVolume)
wx.CallLater(500, self.mediactrl.Play)
self.button_save.Enabled = True
if is_first and (is_last or not (self.mc_hack or data["allatonce"])):
# First result: set playing at once
data["current"] = data["filenames"][-1]
self.mediactrl.Load(data["current"])
if not is_volumeset:
self.mediactrl.SetVolume(conf.LastVolume)
if data["allatonce"]:
self.gauge.SetValue(100.0 * (index + 1) / data["count"])
def on_save_mp3(self, event):
"""Handler for clicking to save the merged MP3 file."""
data = self.data[self.text_id]
self.dialog_save.Filename = data["filenames"][0]
if wx.ID_OK == self.dialog_save.ShowModal():
try:
shutil.copyfile(data["filenames"][0], self.dialog_save.GetPath())
except Exception as e:
wx.MessageBox(str(e), conf.Title, wx.ICON_WARNING | wx.OK)
def on_text_to_speech(self, event):
"""Handler for the speak button, sends entered text for processing."""
text = self.edit_text.Value.strip()
lang = conf.Languages[self.list_lang.Selection][0]
text_present = [i for i in self.data.values()
if i["text"] == text and i["lang"] == lang]
if not text:
pass
elif not text_present:
self.text_id = wx.NewId()
data = self.data[self.text_id] = {"filenames": [], "lang": lang,
"lang_text": conf.Languages[self.list_lang.Selection][1],
"text": text, "current": None, "count": 0, "id": self.text_id,
"datetime": datetime.datetime.now(), "stopped": False,
"completed": False, "allatonce": self.cb_allatonce.Value
}
self.out_queue.put(data)
self.button_save.Enabled = False
# Create panel in history list
p = wx.lib.sized_controls.SizedPanel(self.panel_list,
size=(150, 100))
p.text_id = data["id"]
self.panel_list.SetupScrolling(scroll_x=False)
self.panel_list.BackgroundColour = "LIGHT GRAY"
p.SetSizerType("vertical")
p.BackgroundColour = "WHITE"
p_top = wx.lib.sized_controls.SizedPanel(p)
p_top.SetSizerType("horizontal")
t_date = wx.StaticText(
p_top, label="%s\n%s" % (
data["datetime"].strftime("%H:%M %d.%m.%Y"),
data["lang_text"])
)
t_date.text_id = data["id"]
t_date.ForegroundColour = "GRAY"
h = wx.HyperlinkCtrl(p_top, id=wx.NewId(), label="Open", url="")
h.text_id = data["id"]
t_text = wx.StaticText(p, label=text[:200].replace("\n", " "))
t_text.text_id = data["id"]
t_text.ForegroundColour = "LIGHT GRAY"
self.panel_list.Sizer.Insert(0, p, border=2, proportion=1,
flag=wx.ALL | wx.EXPAND)
self.panels_history.append(p)
self.Bind(wx.EVT_HYPERLINK, self.on_open_text, h)
t_date.Bind(wx.EVT_LEFT_DCLICK, self.on_open_text)
t_text.Bind(wx.EVT_LEFT_DCLICK, self.on_open_text)
p.Bind(wx.EVT_LEFT_DCLICK, self.on_open_text)
self.panel_list.Layout()
t_text.Wrap(p.Size.width - 5)
self.panel_list.Refresh()
elif text_present[0]["id"] != self.text_id \
and text_present[0]["filenames"]:
self.mediactrl.Load(text_present[0]["filenames"][0])
if self.mc_hack:
wx.CallLater(500, self.mediactrl.Play)
else:
self.mediactrl.Play()
def on_open_text(self, event):
"""Handler for opening a text from history, loads and plays it."""
self.text_id = event.EventObject.text_id
data = self.data[self.text_id]
self.edit_text.Value = data["text"]
self.list_lang.Value = data["lang_text"]
if data["filenames"]:
self.mediactrl.Load(data["filenames"][0])
if self.mc_hack:
wx.CallLater(500, self.mediactrl.Play)
def on_media_loaded(self, event):
"""Handler for MediaCtrl finishing loading media, sets it to play."""
if hasattr(self.mediactrl, "DONTPLAY"):
delattr(self.mediactrl, "DONTPLAY")
else:
self.mediactrl.Play()
data = self.data[self.text_id]
index = data["filenames"].index(data["current"])
self.gauge.SetValue(100.0 * (index + 1) / data["count"])
def on_media_finished(self, event):
"""
Handler for when MediaCtrl finishes playing something, loads the next
chunk if available.
"""
data = self.data[self.text_id]
index = data["filenames"].index(data["current"]) \
if data["current"] in data["filenames"] else -1
if (index < data["count"] - 1
and len(data["filenames"]) > index + 1):
# Next chunk available, set it playing
filename = data["filenames"][index + 1]
data["current"] = filename
self.mediactrl.Load(filename)
if index == data["count"] - 1 and not data["completed"]:
# All chunks finished, merge them into one
self.merge_chunks(data)
data["completed"] = True
# Load large file in MediaCtrl in place of last chunk, skip replay
# as we already played it all in chunks
self.mediactrl.DONTPLAY = True
self.mediactrl.Load(data["filenames"][0])
self.button_save.Enabled = True
def merge_chunks(self, data):
"""Merges all the audio chunks in data into one file."""
fn = "speech_%s_%s.mp3" % (
data["lang"], data["datetime"].strftime("%Y%m%d-%H%M%S"))
filename_main = unique_path(fn)
with open(filename_main, "wb") as f:
# MP3s can be simply concatenated together, result is legible.
for i, filename in enumerate(data["filenames"]):
f.write(open(filename, "rb").read())
# Add more silence for separators like commas and periods.
silence_count = 0
if data["chunks"][i][-1] in [".","?","!"]:
silence_count = conf.SilenceCountLong
elif data["chunks"][i][-1] in [",",":",";","(",")"]:
silence_count = conf.SilenceCountShort
f.write(base64.decodestring(conf.Silence) * silence_count)
for filename in data["filenames"]:
try:
os.unlink(filename)
except Exception: pass
data.update(filenames=[filename_main], current=filename_main, count=1)
def cleanup(self):
"""Deletes the MP3 files created during this run."""
for f in [i for d in self.data.values() for i in d["filenames"]]:
try:
os.unlink(f)
except Exception: pass
self.Destroy()
class TextToMP3Loader(threading.Thread):
"""Background thread for loading smaller MP3 files from Google TTS."""
GOOGLE_TRANSLATE_URL = "http://translate.google.com/" \
"translate_tts?tl=%s&q=%s"
def __init__(self, event_handler, in_queue):
threading.Thread.__init__(self)
self.daemon = True # Daemon threads do not keep application running
self.event_handler = event_handler
self.in_queue = in_queue
self.is_running = False
self.opener = urllib2.build_opener()
self.opener.addheaders = [("User-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) "
"%s" % conf.Title)]
self.start()
def run(self):
self.is_running = True
SILENCE_RAW = base64.decodestring(conf.Silence)
cached_results = {} # {(language_code, sentence): content, }
while self.is_running:
data = self.in_queue.get()
text_chunks = self.parse_text(data["text"].encode("utf-8"))
for i, sentence in enumerate(text_chunks):
cachekey = (data["lang"], sentence)
if cachekey in cached_results:
content = cached_results[cachekey]
elif conf.SilenceMarker in sentence.lower():
silence_count = sentence.lower().count(conf.SilenceMarker)
content = 2 * silence_count * SILENCE_RAW
else:
content, tries, MAX_TRIES = None, 0, 3
url = self.GOOGLE_TRANSLATE_URL % (
data["lang"], urllib2.quote(sentence))
while tries < MAX_TRIES:
try:
tries += 1
content = self.opener.open(url).read()
except Exception: pass
if content is None and tries >= MAX_TRIES:
error = "Error accessing the Google Translate " \
"online service.\n\nURL: %s\n\n%s" % (
url.replace("?", "?\n"), traceback.format_exc())
event = ResultEvent(TextId=data["id"], Error=error)
wx.PostEvent(self.event_handler, event)
break # break for i, sentence in enumerate(text_chunks)
filename = "speech_temp_%s_%d_%02d.mp3" % \
(data["lang"], data["id"], i)
filename = unique_path(filename)
with open(filename, "wb") as f:
f.write(content)
event = ResultEvent(TextId=data["id"], Chunks=text_chunks,
Count=len(text_chunks), Index=i, Filename=filename)
wx.PostEvent(self.event_handler, event)
cached_results[cachekey] = content
def parse_text(self, text):
"""
Returns a list of sentences with less than 100 characters.
Modified @from http://glowingpython.blogspot.com/2012/11/
text-to-speech-with-correct-intonation.html
"""
MAXLEN = 100
sentences = []
punct = [",",":",";",".","–","?","!","(",")"] # Interpunctuation marks
text = text.replace("\r", " ").replace("\t", " ") # Remove CR and tabs
words = text.split(" ") if len(text) > MAXLEN else []
sentence = "" if len(text) > MAXLEN else text
# Preprocess list for silence markers
if conf.SilenceMarker in text:
words_new = []
if not words and sentence: # Was too short to be cut initially
words = text.split(" ")
sentence = ""
for w in filter(None, words):
if conf.SilenceMarker not in w.lower():
words_new.append(w)
else:
text_chunks = w.lower().split(conf.SilenceMarker)
for i, part in enumerate(text_chunks):
if part:
words_new.append(part)
if i < len(text_chunks) - 1:
words_new.append(conf.SilenceMarker)
else:
if words_new and conf.SilenceMarker in words_new[-1]:
words_new[-1] += conf.SilenceMarker
else:
words_new.append(conf.SilenceMarker)
words = words_new
for w in words:
if conf.SilenceMarker in w:
if sentence:
sentences.append(sentence.strip())
sentences.append(w)
sentence = ""
elif w[-1] in punct or w[0] in punct: # Encountered punctuation
if w[-1] in punct and (len(sentence) + len(w) + 1 < MAXLEN):
# Word ends with punct and sentence can still be added to
sentences.append(sentence.strip() + " " + w.strip())
sentence = "" # Save sentence and word, start new sentence
elif w[0] in punct and w[-1] not in punct:
# Word starts with punctuation, like '('
sentences.append(sentence.strip()) # Save current sentence
sentence = w # Start a new sentence with punct and word
else: # word ends with punct and sentence already long enough
sentences.extend([sentence.strip(), w.strip()])
sentence = ""
else:
if (len(sentence) + len(w) + 1 < MAXLEN): # Sentence still
sentence += " " + w # short enough
else: # Sentence too long
sentences.append(sentence.strip())
sentence = w # Start a new sentence with the word
if sentence:
sentences.append(sentence.strip())
return sentences
def unique_path(pathname):
"""
Returns a unique version of the path. If a file or directory with the
same name already exists, returns a unique version
(e.g. "C:\config (2).sys" if ""C:\config.sys" already exists).
"""
result = pathname
base, ext = os.path.splitext(result)
counter = 2
while os.path.exists(result):
result = "%s (%s)%s" % (base, counter, ext)
counter += 1
return result
if "__main__" == __name__:
app = wx.App(0)
window = TextSpeakWindow()
try:
app.MainLoop()
except Exception, e:
traceback.print_exc()
if window:
window.cleanup() # Remove any possible temporary files left