chiark / gitweb /
Add "Reload platform" to refresh all the objects on the platform from their files
[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 import glob
7
8 #Cura/util classes should not depend on wx...
9 import wx
10 import gettext
11
12 if sys.platform.startswith('darwin'):
13         try:
14                 #Foundation import can crash on some MacOS installs
15                 from Foundation import *
16         except:
17                 pass
18
19 if sys.platform.startswith('darwin'):
20         if hasattr(sys, 'frozen'):
21                 try:
22                         resourceBasePath = NSBundle.mainBundle().resourcePath()
23                 except:
24                         resourceBasePath = os.path.join(os.path.dirname(__file__), "../../../../../")
25         else:
26                 resourceBasePath = os.path.join(os.path.dirname(__file__), "../resources")
27 else:
28         if hasattr(sys, 'frozen'):
29                 resourceBasePath = os.path.join(os.path.dirname(__file__), "../../resources")
30         else:
31                 resourceBasePath = os.path.join(os.path.dirname(__file__), "../resources")
32
33 def getPathForResource(dir, subdir, resource_name):
34         assert os.path.isdir(dir), "{p} is not a directory".format(p=dir)
35         path = os.path.normpath(os.path.join(dir, subdir, resource_name))
36         assert os.path.isfile(path), "{p} is not a file.".format(p=path)
37         return path
38
39 def getPathForImage(name):
40         return getPathForResource(resourceBasePath, 'images', name)
41
42 def getPathForMesh(name):
43         return getPathForResource(resourceBasePath, 'meshes', name)
44
45 def getPathForFirmware(name):
46         return getPathForResource(resourceBasePath, 'firmware', name)
47
48 def getDefaultMachineProfiles():
49         path = os.path.normpath(os.path.join(resourceBasePath, 'machine_profiles', '*.ini'))
50         return glob.glob(path)
51
52 def setupLocalization(selectedLanguage = None):
53         try:
54                 if sys.platform.startswith('darwin'):
55                         languages = NSLocale.preferredLanguages()
56                 else:
57                         #Using wx.Locale before you created wx.App seems to cause an nasty exception. So default to 'en' at the moment.
58                         languages = [wx.Locale(wx.LANGUAGE_DEFAULT).GetCanonicalName()]
59         except Exception as e:
60                 languages = ['en']
61
62         if selectedLanguage is not None:
63                 for item in getLanguageOptions():
64                         if item[1] == selectedLanguage and item[0] is not None:
65                                 languages = [item[0]]
66
67         locale_path = os.path.normpath(os.path.join(resourceBasePath, 'locale'))
68         translation = gettext.translation('Cura', locale_path, languages, fallback=True)
69         translation.install(unicode=True)
70
71 def getLanguageOptions():
72         return [
73                 # [None, 'System default'],
74                 ['en', 'English'],
75                 # ['de', 'Deutsch'],
76                 # ['fr', 'French'],
77                 # ['nl', 'Nederlands'],
78                 # ['sp', 'Spanish'],
79                 # ['po', 'Polish']
80         ]