chiark / gitweb /
a38525de7be08f64cbc982a14fc87bb9ba0cc2f6
[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 import profile
16
17 if sys.platform.startswith('darwin'):
18         try:
19                 #Foundation import can crash on some MacOS installs
20                 from Foundation import *
21         except:
22                 pass
23
24 if sys.platform.startswith('darwin'):
25         if hasattr(sys, 'frozen'):
26                 try:
27                         resourceBasePath = NSBundle.mainBundle().resourcePath()
28                 except:
29                         resourceBasePath = os.path.join(os.path.dirname(__file__), "../../../../../")
30         else:
31                 resourceBasePath = os.path.join(os.path.dirname(__file__), "../../resources")
32 else:
33         resourceBasePath = os.path.join(os.path.dirname(__file__), "../../resources")
34
35 def getPathForResource(dir, subdir, resource_name):
36         assert os.path.isdir(dir), "{p} is not a directory".format(p=dir)
37         path = os.path.normpath(os.path.join(dir, subdir, resource_name))
38         if not os.path.isfile(path):
39                 return None
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 getSimpleModeIniFiles(subdir, pattern = '*.ini'):
56         machine_type = profile.getMachineSetting('machine_type')
57         paths = []
58         paths.append(os.path.normpath(os.path.expanduser(os.path.join('~', '.Cura', 'quickprint', machine_type, subdir))))
59         paths.append(os.path.normpath(os.path.expanduser(os.path.join('~', '.Cura', 'quickprint', subdir))))
60         paths.append(os.path.normpath(os.path.join(resourceBasePath, 'quickprint', machine_type, subdir)))
61         paths.append(os.path.normpath(os.path.join(resourceBasePath, 'quickprint', subdir)))
62         for path in paths:
63                 if os.path.isdir(path):
64                         files = sorted(glob.glob(os.path.join(path, pattern)))
65                         if len(files) > 0:
66                                 return files
67         return []
68
69
70 def getSimpleModeProfiles():
71         return getSimpleModeIniFiles('profiles')
72
73 def getSimpleModeMaterials():
74         return getSimpleModeIniFiles('materials')
75
76 def getSimpleModeOptions():
77         return getSimpleModeIniFiles('options')
78
79 def setupLocalization(selectedLanguage = None):
80         #Default to english
81         languages = ['en']
82
83         if selectedLanguage is not None:
84                 for item in getLanguageOptions():
85                         if item[1] == selectedLanguage and item[0] is not None:
86                                 languages = [item[0]]
87                                 break
88         if languages[0] == 'AUTO':
89                 languages = ['en']
90                 defaultLocale = getDefaultLocale()
91                 if defaultLocale is not None:
92                         for item in getLanguageOptions():
93                                 if item[0] == 'AUTO':
94                                         continue
95                                 if item[0] is not None and defaultLocale.startswith(item[0]):
96                                         languages = [item[0]]
97
98         locale_path = os.path.normpath(os.path.join(resourceBasePath, 'locale'))
99         translation = gettext.translation('Cura', locale_path, languages, fallback=True)
100         #translation.ugettext = lambda message: u'#' + message
101         translation.install(unicode=True)
102
103 def getLanguageOptions():
104         return [
105                 ['AUTO', 'Autodetect'],
106                 ['en', 'English'],
107                 ['de', 'Deutsch'],
108                 ['fr', 'French'],
109                 ['tr', 'Turkish'],
110                 ['ru', 'Russian'],
111                 # ['ko', 'Korean'],
112                 # ['zh', 'Chinese'],
113                 # ['nl', 'Nederlands'],
114                 # ['es', 'Spanish'],
115                 # ['po', 'Polish']
116         ]
117
118 def getDefaultLocale():
119         defaultLocale = None
120
121         # On Windows, we look for the actual UI language, as someone could have
122         # an english windows but use a non-english locale.
123         if platform.system() == "Windows":
124                 try:
125                         import ctypes
126
127                         windll = ctypes.windll.kernel32
128                         defaultLocale = locale.windows_locale[windll.GetUserDefaultUILanguage()]
129                 except:
130                         pass
131
132         if defaultLocale is None:
133                 try:
134                         defaultLocale = locale.getdefaultlocale()[0]
135                 except:
136                         pass
137
138         return defaultLocale