Skip to content

Commit 45f58f8

Browse files
committed
solving path issues when starting talkshow from compiled exe file.
1 parent 07688f2 commit 45f58f8

File tree

5 files changed

+48
-13
lines changed

5 files changed

+48
-13
lines changed

CommandBar.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
debug = logger.debug
77
info = logger.info
88
warn = logger.warn
9+
10+
import talkshowUtils
11+
def loadImage(path):
12+
newpath = talkshowUtils.getRelativePath(path)
13+
return pyglet.image.load(newpath)
14+
915

1016
class CommandBar(object):
1117

@@ -59,7 +65,7 @@ def __init__(self, screen, orientation=0):
5965
increaseY = int(self.style.warning.height) + self.imagePadding[0] + self.imagePadding[2]
6066

6167
# create the warning button properties
62-
self.warningImage = pyglet.image.load(self.style.warning.background_image[5:-2])
68+
self.warningImage = loadImage(self.style.warning.background_image[5:-2])
6369
self.warningHeight = int(self.style.warning.height)
6470
self.warningWidth = int(self.style.warning.width)
6571
self.warningX = x
@@ -70,7 +76,7 @@ def __init__(self, screen, orientation=0):
7076
y = y + increaseY
7177

7278
# create the home button properties
73-
self.homeImage = pyglet.image.load(self.style.home.background_image[5:-2])
79+
self.homeImage = loadImage(self.style.home.background_image[5:-2])
7480
self.homeHeight = int(self.style.home.height)
7581
self.homeWidth = int(self.style.home.width)
7682
self.homeX = x
@@ -81,7 +87,7 @@ def __init__(self, screen, orientation=0):
8187
y = y + increaseY
8288

8389
# create the back button properties
84-
self.backImage = pyglet.image.load(self.style.back.background_image[5:-2])
90+
self.backImage = loadImage(self.style.back.background_image[5:-2])
8591
self.backHeight = int(self.style.back.height)
8692
self.backWidth = int(self.style.back.width)
8793
self.backX = x
@@ -92,7 +98,7 @@ def __init__(self, screen, orientation=0):
9298
y = y + increaseY
9399

94100
# create the shutDown button properties
95-
self.shutDownImage = pyglet.image.load(self.style.shutDown.background_image[5:-2])
101+
self.shutDownImage = loadImage(self.style.shutDown.background_image[5:-2])
96102
self.shutDownHeight = int(self.style.shutDown.height)
97103
self.shutDownWidth = int(self.style.shutDown.width)
98104
self.shutDownX = x
@@ -103,7 +109,7 @@ def __init__(self, screen, orientation=0):
103109
y = y + increaseY
104110

105111
# create the settings button properties
106-
self.settingsImage = pyglet.image.load(self.style.settings.background_image[5:-2])
112+
self.settingsImage = loadImage(self.style.settings.background_image[5:-2])
107113
self.settingsHeight = int(self.style.settings.height)
108114
self.settingsWidth = int(self.style.settings.width)
109115
self.settingsX = x
@@ -129,7 +135,7 @@ def __init__(self, screen, orientation=0):
129135
increaseY = int(self.style.volumeDown.height) + self.imagePadding[0] + self.imagePadding[2]
130136

131137
# create the volumeDown button properties
132-
self.volumeDownImage = pyglet.image.load(self.style.volumeDown.background_image[5:-2])
138+
self.volumeDownImage = loadImage(self.style.volumeDown.background_image[5:-2])
133139
self.volumeDownHeight = int(self.style.volumeDown.height)
134140
self.volumeDownWidth = int(self.style.volumeDown.width)
135141
self.volumeDownX = x
@@ -140,7 +146,7 @@ def __init__(self, screen, orientation=0):
140146
y = y + increaseY
141147

142148
# create the volumeUp button properties
143-
self.volumeUpImage = pyglet.image.load(self.style.volumeUp.background_image[5:-2])
149+
self.volumeUpImage = loadImage(self.style.volumeUp.background_image[5:-2])
144150
self.volumeUpHeight = int(self.style.volumeUp.height)
145151
self.volumeUpWidth = int(self.style.volumeUp.width)
146152
self.volumeUpX = x
@@ -161,7 +167,7 @@ def __init__(self, screen, orientation=0):
161167

162168

163169
# create the playPrevious button properties
164-
self.playPreviousImage = pyglet.image.load(self.style.playPrevious.background_image[5:-2])
170+
self.playPreviousImage = loadImage(self.style.playPrevious.background_image[5:-2])
165171
self.playPreviousHeight = int(self.style.playPrevious.height)
166172
self.playPreviousWidth = int(self.style.playPrevious.width)
167173
self.playPreviousX = x
@@ -172,7 +178,7 @@ def __init__(self, screen, orientation=0):
172178
y = y + increaseY
173179

174180
# create the play button properties
175-
self.playImage = pyglet.image.load(self.style.play.background_image[5:-2])
181+
self.playImage = loadImage(self.style.play.background_image[5:-2])
176182
self.playHeight = int(self.style.play.height)
177183
self.playWidth = int(self.style.play.width)
178184
self.playX = x
@@ -183,7 +189,7 @@ def __init__(self, screen, orientation=0):
183189
y = y + increaseY
184190

185191
# create the playNext button properties
186-
self.playNextImage = pyglet.image.load(self.style.playNext.background_image[5:-2])
192+
self.playNextImage = loadImage(self.style.playNext.background_image[5:-2])
187193
self.playNextHeight = int(self.style.playNext.height)
188194
self.playNextWidth = int(self.style.playNext.width)
189195
self.playNextX = x

talkshow.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
info = logger.info
1010
warn = logger.warn
1111
error = logger.error
12+
import talkshowUtils
1213
info("Initialising talkshow. Version of Python: %s" % str(sys.version))
1314

1415

@@ -54,6 +55,7 @@
5455

5556

5657

58+
5759
#TODO: make this configurable
5860
ORIENTATION = 1
5961

@@ -346,7 +348,7 @@ def initializeConfig(self):
346348
self.ALARM_DIRECTORY = alarmDir
347349
info("Alarm sound is loaded from {0}".format(self.ALARM_DIRECTORY))
348350
else:
349-
self.ALARM_DIRECTORY = 'alarm'
351+
self.ALARM_DIRECTORY = talkshowUtils.getRelativePath('alarm')
350352
warn("Expected folder {1} under {0} does not exist. \
351353
Please create a folder named 'Alarm' under your content directory and place an alarm sound file into it. \
352354
Defaulting back to inbuilt alarm sound.".format(alarmDir, self.config.CONTENT_DIRECTORY))

talkshowConfig.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
import os
5151
import parseCSS
52+
import talkshowUtils
5253

5354
class config(object):
5455

@@ -62,6 +63,6 @@ class config(object):
6263

6364
def __init__(self):
6465
self.CONTENT_DIRECTORY = CONTENT_DIRECTORY
65-
self.path = os.path.join(STYLE_DIRECTORY, STYLE_FILENAME)
66+
self.path = talkshowUtils.getRelativePath(os.path.join(STYLE_DIRECTORY, STYLE_FILENAME))
6667
self.parser = parseCSS.CSSParser(self.path)
6768
self.style = self.parser.style

talkshowUtils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
import sys
3+
from talkshowLogger import logger
4+
5+
def getRelativePath(path):
6+
'''
7+
Get the right path irrespective whether the file is within the compiled .exe or in source
8+
'''
9+
logger.debug("sys.executable under {0} ABSOLUTE={1}".format(os.path.dirname(sys.executable), os.path.abspath(os.path.dirname(sys.executable))))
10+
logger.debug("__file__ under {0} ABSOLUTE={1}".format(os.path.dirname(__file__), os.path.abspath(os.path.dirname(__file__))))
11+
12+
application_path = os.path.dirname(__file__)
13+
14+
if getattr(sys, 'frozen', False):
15+
logger.debug("starting from frozen .exe file")
16+
elif __file__:
17+
logger.debug("starting from python source")
18+
else:
19+
logger.warn("talkshow.getRelativePath function has a problem...")
20+
21+
returnpath = os.path.join(application_path, path)
22+
23+
logger.debug("returnpath = {0} ABS={1}".format(returnpath, os.path.abspath(returnpath)))
24+
25+
return returnpath

widget.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import talkshowConfig
99
conf = talkshowConfig.config()
10+
import talkshowUtils
1011

1112
style = conf.style
1213
mydict = conf.parser.dict
@@ -323,7 +324,7 @@ def __init__(self, parent, name, x, y, w, h, text="button", handler = None, imag
323324
if imagePath:
324325

325326
#self, p, name, path, x=0, y=0, w=None, h=None, color="#ffffff", opacity=1.0
326-
self.image = wrappers.Image(self, name, imagePath) #self.style.home.background_image[5:-2]
327+
self.image = wrappers.Image(self, name, talkshowUtils.getRelativePath(imagePath)) #self.style.home.background_image[5:-2]
327328

328329

329330
else:

0 commit comments

Comments
 (0)