chiark / gitweb /
Merge tag '15.01-RC4' 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 getSimpleModeProfiles():
55         path = os.path.normpath(os.path.join(resourceBasePath, 'quickprint', 'profiles', '*.ini'))
56         user_path = os.path.normpath(os.path.expanduser(os.path.join('~', '.Cura', 'quickprint', 'profiles')))
57         if os.path.isdir(user_path):
58                 return sorted(glob.glob(user_path))
59         return sorted(glob.glob(path))
60
61 def getSimpleModeMaterials():
62         path = os.path.normpath(os.path.join(resourceBasePath, 'quickprint', 'materials', '*.ini'))
63         user_path = os.path.normpath(os.path.expanduser(os.path.join('~', '.Cura', 'quickprint', 'materials')))
64         if os.path.isdir(user_path):
65                 return sorted(glob.glob(user_path))
66         return sorted(glob.glob(path))
67
68 def setupLocalization(selectedLanguage = None):
69         #Default to english
70         languages = ['en']
71
72         if selectedLanguage is not None:
73                 for item in getLanguageOptions():
74                         if item[1] == selectedLanguage and item[0] is not None:
75                                 languages = [item[0]]
76                                 break
77         if languages[0] == 'AUTO':
78                 languages = ['en']
79                 defaultLocale = getDefaultLocale()
80                 if defaultLocale is not None:
81                         for item in getLanguageOptions():
82                                 if item[0] == 'AUTO':
83                                         continue
84                                 if item[0] is not None and defaultLocale.startswith(item[0]):
85                                         languages = [item[0]]
86
87         locale_path = os.path.normpath(os.path.join(resourceBasePath, 'locale'))
88         translation = gettext.translation('Cura', locale_path, languages, fallback=True)
89         #translation.ugettext = lambda message: u'#' + message
90         translation.install(unicode=True)
91
92 def getLanguageOptions():
93         return [
94                 ['AUTO', 'Autodetect'],
95                 ['en', 'English'],
96                 ['de', 'Deutsch'],
97                 ['fr', 'French'],
98                 ['tr', 'Turkish'],
99                 # ['ko', 'Korean'],
100                 # ['zh', 'Chinese'],
101                 # ['nl', 'Nederlands'],
102                 # ['es', 'Spanish'],
103                 # ['po', 'Polish']
104         ]
105
106 def getDefaultLocale():
107         defaultLocale = None
108
109         # On Windows, we look for the actual UI language, as someone could have
110         # an english windows but use a non-english locale.
111         if platform.system() == "Windows":
112                 try:
113                         import ctypes
114
115                         windll = ctypes.windll.kernel32
116                         defaultLocale = locale.windows_locale[windll.GetUserDefaultUILanguage()]
117                 except:
118                         pass
119
120         if defaultLocale is None:
121                 try:
122                         defaultLocale = locale.getdefaultlocale()[0]
123                 except:
124                         pass
125
126         return defaultLocale