Skip to content

Commit

Permalink
Use _MEIPASS to find files in pyinstaller bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
emersonrp committed Jun 28, 2024
1 parent 6d42675 commit ab5bb5a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
8 changes: 6 additions & 2 deletions mcp21/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import wx
import re, random, os, importlib
import re, random, os, importlib, sys
from pathlib import Path
import utility

# This module was developed by squinting directly at both the MCP spec
Expand Down Expand Up @@ -41,7 +42,10 @@ def __init__(self, conn):
# walk the packages directory, and instantiate everything we find there.
# this relies on each package having a class called "MCPPackage" that's a
# sane and correct subclass of MCPPackageBase.
path = wx.GetApp().path
if hasattr(sys, '_MEIPASS'):
path = Path(sys._MEIPASS) # pyright: ignore
else:
path = Path(wx.GetApp().path)
for package_file in os.listdir(os.path.join(path, 'mcp21', 'package')):
if package_file == "__pycache__": continue
package, ext = package_file.split('.')
Expand Down
9 changes: 8 additions & 1 deletion window/statusbar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import wx
import os
import time
import sys
from pathlib import Path
import EnhancedStatusBar as ESB

class StatusBar(ESB.EnhancedStatusBar):
Expand All @@ -10,7 +12,12 @@ def __init__(self, parent, connection):
self.connection = connection

icons = {}
iconpath = os.path.join(wx.GetApp().path, "icons", "features")
if hasattr(sys, '_MEIPASS'):
path = Path(sys._MEIPASS) # pyright: ignore
else:
path = Path(wx.GetApp().path)

iconpath = os.path.join(path, "icons", "features")
if os.path.exists(iconpath):
for icon_file in os.listdir(iconpath):
feature, _ = icon_file.split('.')
Expand Down
28 changes: 17 additions & 11 deletions worlds.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import os
import collections
import prefs
import json
import sys
from pathlib import Path

class World(dict):

Expand Down Expand Up @@ -39,40 +42,43 @@ def save(self):
_defaults = {}
def Initialize():
global worlds, _defaults
_config = wx.FileConfig(localFilename = str(prefs.prefs_dir() / 'worlds'))
worldsconfig = wx.FileConfig(localFilename = str(prefs.prefs_dir() / 'worlds'))

# loop worlds...
g_more, worldname, g_index = _config.GetFirstGroup()
g_more, worldname, g_index = worldsconfig.GetFirstGroup()
if g_more: # do we have anything at all from the config file?
while g_more: # yes, loop and fill stuff out.

# TODO - enumerate what the various keys might be and use Read() ReadBool() etc as appropriate
_config.SetPath(worldname)
worldsconfig.SetPath(worldname)

worlddata = {}

# loop data lines inside each world....
e_more, dataname, e_index = _config.GetFirstEntry()
e_more, dataname, e_index = worldsconfig.GetFirstEntry()
while e_more:
worlddata[dataname] = _config.Read(dataname)
worlddata[dataname] = worldsconfig.Read(dataname)
# ew boolean handling. Maybe go thru prefs to do this in one place
if worlddata[dataname] == "True" : worlddata[dataname] = True
if worlddata[dataname] == "False": worlddata[dataname] = False
e_more, dataname, e_index = _config.GetNextEntry(e_index)
e_more, dataname, e_index = worldsconfig.GetNextEntry(e_index)

# build the World object
worlds[worlddata['name']] = World(worlddata)

# carry on, back to the top for the next world
_config.SetPath('/')
g_more, worldname, g_index = _config.GetNextGroup(g_index)
worldsconfig.SetPath('/')
g_more, worldname, g_index = worldsconfig.GetNextGroup(g_index)

else: # nothing from config file, grab the initial_worlds data
import json
path = wx.GetApp().path
if hasattr(sys, '_MEIPASS'):
path = Path(sys._MEIPASS) # pyright: ignore
else:
path = Path(wx.GetApp().path)

initial_worlds = []
try:
initial_worlds = json.load(open(os.path.join(path, 'initial_worlds.json'),'r'))
initial_worlds = json.load(open(path / 'initial_worlds.json','r'))
except Exception as e:
wx.LogError(f"initial_worlds.json file could not be loaded: {e}")

Expand Down

0 comments on commit ab5bb5a

Please sign in to comment.