chiark / gitweb /
Updated translations. Included russian translation from russian reseller.
[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 getSimpleModeProfiles():
53         path = os.path.normpath(os.path.join(resourceBasePath, 'quickprint', 'profiles', '*.ini'))
54         user_path = os.path.normpath(os.path.expanduser(os.path.join('~', '.Cura', 'quickprint', 'profiles')))
55         if os.path.isdir(user_path):
56                 files = sorted(glob.glob(os.path.join(user_path, '*.ini')))
57                 if len(files) > 0:
58                         return files
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                 files = sorted(glob.glob(os.path.join(user_path, '*.ini')))
66                 if len(files) > 0:
67                         return files
68         return sorted(glob.glob(path))
69
70 def setupLocalization(selectedLanguage = None):
71         #Default to english
72         languages = ['en']
73
74         if selectedLanguage is not None:
75                 for item in getLanguageOptions():
76                         if item[1] == selectedLanguage and item[0] is not None:
77                                 languages = [item[0]]
78
79         locale_path = os.path.normpath(os.path.join(resourceBasePath, 'locale'))
80         translation = gettext.translation('Cura', locale_path, languages, fallback=True)
81         #translation.ugettext = lambda message: u'#' + message
82         translation.install(unicode=True)
83
84 def getLanguageOptions():
85         return [
86                 ['en', 'English'],
87                 ['de', 'Deutsch'],
88                 ['fr', 'French'],
89                 ['tr', 'Turkish'],
90                 ['ru', 'Russian'],
91                 # ['ko', 'Korean'],
92                 # ['zh', 'Chinese'],
93                 # ['nl', 'Nederlands'],
94                 # ['es', 'Spanish'],
95                 # ['po', 'Polish']
96         ]