chiark / gitweb /
9dbed7670b0eed3eb9d3affd482bc61c511e7781
[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
12 import gettext
13
14 if sys.platform.startswith('darwin'):
15         try:
16                 #Foundation import can crash on some MacOS installs
17                 from Foundation import *
18         except:
19                 pass
20
21 if sys.platform.startswith('darwin'):
22         if hasattr(sys, 'frozen'):
23                 try:
24                         resourceBasePath = NSBundle.mainBundle().resourcePath()
25                 except:
26                         resourceBasePath = os.path.join(os.path.dirname(__file__), "../../../../../")
27         else:
28                 resourceBasePath = os.path.join(os.path.dirname(__file__), "../../resources")
29 else:
30         resourceBasePath = os.path.join(os.path.dirname(__file__), "../../resources")
31
32 def getPathForResource(dir, subdir, resource_name):
33         assert os.path.isdir(dir), "{p} is not a directory".format(p=dir)
34         path = os.path.normpath(os.path.join(dir, subdir, resource_name))
35         if not os.path.isfile(path):
36                 return None
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         #Default to english
54         languages = ['en']
55
56         if selectedLanguage is not None:
57                 for item in getLanguageOptions():
58                         if item[1] == selectedLanguage and item[0] is not None:
59                                 languages = [item[0]]
60
61         locale_path = os.path.normpath(os.path.join(resourceBasePath, 'locale'))
62         translation = gettext.translation('Cura', locale_path, languages, fallback=True)
63         #translation.ugettext = lambda message: u'#' + message
64         translation.install(unicode=True)
65
66 def getLanguageOptions():
67         return [
68                 ['en', 'English'],
69                 ['de', 'Deutsch'],
70                 ['fr', 'French'],
71                 # ['ko', 'Korean'],
72                 # ['zh', 'Chinese'],
73                 # ['nl', 'Nederlands'],
74                 # ['es', 'Spanish'],
75                 # ['po', 'Polish']
76         ]