chiark / gitweb /
Implement a fallback if the Foundation import fails. Stupid MacOS.
[cura.git] / Cura / util / resources.py
1 from __future__ import absolute_import
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
3
4 import os
5 import sys
6
7 __all__ = ['getPathForResource', 'getPathForImage', 'getPathForMesh']
8
9
10 if sys.platform.startswith('darwin'):
11         if hasattr(sys, 'frozen'):
12                 try:
13                         #Foundation import can crash on some MacOS installs
14                         from Foundation import *
15                         resourceBasePath = NSBundle.mainBundle().resourcePath()
16                 except:
17                         resourceBasePath = os.path.join(os.path.dirname(__file__), "../../../../../")
18         else:
19                 resourceBasePath = os.path.join(os.path.dirname(__file__), "../resources")
20 else:
21         if hasattr(sys, 'frozen'):
22                 resourceBasePath = os.path.join(os.path.dirname(__file__), "../../resources")
23         else:
24                 resourceBasePath = os.path.join(os.path.dirname(__file__), "../resources")
25
26 def getPathForResource(dir, subdir, resource_name):
27         assert os.path.isdir(dir), "{p} is not a directory".format(p=dir)
28         path = os.path.normpath(os.path.join(dir, subdir, resource_name))
29         assert os.path.isfile(path), "{p} is not a file.".format(p=path)
30         return path
31
32 def getPathForImage(name):
33         return getPathForResource(resourceBasePath, 'images', name)
34
35 def getPathForMesh(name):
36         return getPathForResource(resourceBasePath, 'meshes', name)
37
38 def getPathForFirmware(name):
39         return getPathForResource(resourceBasePath, 'firmware', name)