chiark / gitweb /
Add more documentation, fix commandline slicing.
[cura.git] / Cura / util / resources.py
1 """
2 Helper module to get easy access to the path where resources are stored.
3 This is because the resource location is depended on the packaging method and OS
4 """
5 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
6
7 import os
8 import sys
9 import glob
10
11 #Cura/util classes should not depend on wx...
12 import wx
13 import gettext
14
15 if sys.platform.startswith('darwin'):
16         try:
17                 #Foundation import can crash on some MacOS installs
18                 from Foundation import *
19         except:
20                 pass
21
22 if sys.platform.startswith('darwin'):
23         if hasattr(sys, 'frozen'):
24                 try:
25                         resourceBasePath = NSBundle.mainBundle().resourcePath()
26                 except:
27                         resourceBasePath = os.path.join(os.path.dirname(__file__), "../../../../../")
28         else:
29                 resourceBasePath = os.path.join(os.path.dirname(__file__), "../resources")
30 else:
31         if hasattr(sys, 'frozen'):
32                 resourceBasePath = os.path.join(os.path.dirname(__file__), "../../resources")
33         else:
34                 resourceBasePath = os.path.join(os.path.dirname(__file__), "../resources")
35
36 def getPathForResource(dir, subdir, resource_name):
37         assert os.path.isdir(dir), "{p} is not a directory".format(p=dir)
38         path = os.path.normpath(os.path.join(dir, subdir, resource_name))
39         assert os.path.isfile(path), "{p} is not a file.".format(p=path)
40         return path
41
42 def getPathForImage(name):
43         return getPathForResource(resourceBasePath, 'images', name)
44
45 def getPathForMesh(name):
46         return getPathForResource(resourceBasePath, 'meshes', name)
47
48 def getPathForFirmware(name):
49         return getPathForResource(resourceBasePath, 'firmware', name)
50
51 def getDefaultMachineProfiles():
52         path = os.path.normpath(os.path.join(resourceBasePath, 'machine_profiles', '*.ini'))
53         return glob.glob(path)
54
55 def setupLocalization(selectedLanguage = None):
56         try:
57                 if sys.platform.startswith('darwin'):
58                         languages = NSLocale.preferredLanguages()
59                 else:
60                         #Using wx.Locale before you created wx.App seems to cause an nasty exception. So default to 'en' at the moment.
61                         languages = [wx.Locale(wx.LANGUAGE_DEFAULT).GetCanonicalName()]
62         except Exception as e:
63                 languages = ['en']
64
65         if selectedLanguage is not None:
66                 for item in getLanguageOptions():
67                         if item[1] == selectedLanguage and item[0] is not None:
68                                 languages = [item[0]]
69
70         locale_path = os.path.normpath(os.path.join(resourceBasePath, 'locale'))
71         translation = gettext.translation('Cura', locale_path, languages, fallback=True)
72         translation.install(unicode=True)
73
74 def getLanguageOptions():
75         return [
76                 # [None, 'System default'],
77                 ['en', 'English'],
78                 # ['de', 'Deutsch'],
79                 # ['fr', 'French'],
80                 # ['nl', 'Nederlands'],
81                 # ['sp', 'Spanish'],
82                 # ['po', 'Polish']
83         ]