chiark / gitweb /
Merge tag '15.01-RC1' into upstream
[cura.git] / Cura / util / resources.py
1 #coding:utf8
2 """
3 Helper module to get easy access to the path where resources are stored.
4 This is because the resource location is depended on the packaging method and OS
5 """
6 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
7
8 import os
9 import sys
10 import glob
11 import platform
12 import locale
13
14 import gettext
15
16 if sys.platform.startswith('darwin'):
17         try:
18                 #Foundation import can crash on some MacOS installs
19                 from Foundation import *
20         except:
21                 pass
22
23 if sys.platform.startswith('darwin'):
24         if hasattr(sys, 'frozen'):
25                 try:
26                         resourceBasePath = NSBundle.mainBundle().resourcePath()
27                 except:
28                         resourceBasePath = os.path.join(os.path.dirname(__file__), "../../../../../")
29         else:
30                 resourceBasePath = os.path.join(os.path.dirname(__file__), "../../resources")
31 else:
32         resourceBasePath = os.path.join(os.path.dirname(__file__), "../../resources")
33
34 def getPathForResource(dir, subdir, resource_name):
35         assert os.path.isdir(dir), "{p} is not a directory".format(p=dir)
36         path = os.path.normpath(os.path.join(dir, subdir, resource_name))
37         if not os.path.isfile(path):
38                 return None
39         return path
40
41 def getPathForImage(name):
42         return getPathForResource(resourceBasePath, 'images', name)
43
44 def getPathForMesh(name):
45         return getPathForResource(resourceBasePath, 'meshes', name)
46
47 def getPathForFirmware(name):
48         return getPathForResource(resourceBasePath, 'firmware', name)
49
50 def getDefaultMachineProfiles():
51         path = os.path.normpath(os.path.join(resourceBasePath, 'machine_profiles', '*.ini'))
52         return glob.glob(path)
53
54 def setupLocalization(selectedLanguage = None):
55         #Default to english
56         languages = ['en']
57
58         if selectedLanguage is not None:
59                 for item in getLanguageOptions():
60                         if item[1] == selectedLanguage and item[0] is not None:
61                                 languages = [item[0]]
62                                 break
63         if languages[0] == 'AUTO':
64                 languages = ['en']
65                 defaultLocale = getDefaultLocale()
66                 if defaultLocale is not None:
67                         for item in getLanguageOptions():
68                                 if item[0] == 'AUTO':
69                                         continue
70                                 if item[0] is not None and defaultLocale.startswith(item[0]):
71                                         languages = [item[0]]
72
73         locale_path = os.path.normpath(os.path.join(resourceBasePath, 'locale'))
74         translation = gettext.translation('Cura', locale_path, languages, fallback=True)
75         #translation.ugettext = lambda message: u'#' + message
76         translation.install(unicode=True)
77
78 def getLanguageOptions():
79         return [
80                 ['AUTO', 'Autodetect'],
81                 ['en', 'English'],
82                 ['de', 'Deutsch'],
83                 ['fr', 'French'],
84                 ['tr', 'Turkish'],
85                 # ['ko', 'Korean'],
86                 # ['zh', 'Chinese'],
87                 # ['nl', 'Nederlands'],
88                 # ['es', 'Spanish'],
89                 # ['po', 'Polish']
90         ]
91
92 def getDefaultLocale():
93         defaultLocale = None
94
95         # On Windows, we look for the actual UI language, as someone could have
96         # an english windows but use a non-english locale.
97         if platform.system() == "Windows":
98                 try:
99                         import ctypes
100
101                         windll = ctypes.windll.kernel32
102                         defaultLocale = locale.windows_locale[windll.GetUserDefaultUILanguage()]
103                 except:
104                         pass
105
106         if defaultLocale is None:
107                 try:
108                         defaultLocale = locale.getdefaultlocale()[0]
109                 except:
110                         pass
111
112         return defaultLocale