Merge branch 'blender-v2.92-release'
[blender-addons.git] / ui_translate / settings.py
blob97bddbc33e004a5ce48086f51cc4678b182c8b0e
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 # <pep8 compliant>
21 import os
23 if "bpy" in locals():
24 import importlib
25 importlib.reload(settings_i18n)
26 else:
27 import bpy
28 from bpy.types import (
29 Operator,
30 AddonPreferences,
32 from bpy.props import (
33 BoolProperty,
34 StringProperty,
36 from bl_i18n_utils import settings as settings_i18n
39 settings = settings_i18n.I18nSettings()
42 # Operators ###################################################################
44 class UI_OT_i18n_settings_load(Operator):
45 """Load translations' settings from a persistent JSon file"""
46 bl_idname = "ui.i18n_settings_load"
47 bl_label = "I18n Load Settings"
48 bl_option = {'REGISTER'}
50 # Operator Arguments
51 filepath: StringProperty(
52 subtype='FILE_PATH',
53 description="Path to the saved settings file",
56 filter_glob: StringProperty(
57 default="*.json",
58 options={'HIDDEN'}
60 # /End Operator Arguments
62 def invoke(self, context, event):
63 if not self.properties.is_property_set("filepath"):
64 context.window_manager.fileselect_add(self)
65 return {'RUNNING_MODAL'}
66 else:
67 return self.execute(context)
69 def execute(self, context):
70 if not (self.filepath and settings):
71 return {'CANCELLED'}
72 settings.load(self.filepath, reset=True)
73 return {'FINISHED'}
76 class UI_OT_i18n_settings_save(Operator):
77 """Save translations' settings in a persistent JSon file"""
78 bl_idname = "ui.i18n_settings_save"
79 bl_label = "I18n Save Settings"
80 bl_option = {'REGISTER'}
82 # Operator Arguments
83 filepath: StringProperty(
84 description="Path to the saved settings file",
85 subtype='FILE_PATH',
88 filter_glob: StringProperty(
89 default="*.json",
90 options={'HIDDEN'},
92 # /End Operator Arguments
94 def invoke(self, context, event):
95 if not self.properties.is_property_set("filepath"):
96 context.window_manager.fileselect_add(self)
97 return {'RUNNING_MODAL'}
98 else:
99 return self.execute(context)
101 def execute(self, context):
102 if not (self.filepath and settings):
103 return {'CANCELLED'}
104 settings.save(self.filepath)
105 return {'FINISHED'}
108 # Addon Preferences ###########################################################
110 def _setattr(self, name, val):
111 print(self, name, val)
112 setattr(self, name, val)
115 class UI_AP_i18n_settings(AddonPreferences):
116 bl_idname = __name__.split(".")[0] # We want "top" module name!
117 bl_option = {'REGISTER'}
119 _settings = settings
121 WARN_MSGID_NOT_CAPITALIZED: BoolProperty(
122 name="Warn Msgid Not Capitalized",
123 description="Warn about messages not starting by a capitalized letter (with a few allowed exceptions!)",
124 default=True,
125 get=lambda self: self._settings.WARN_MSGID_NOT_CAPITALIZED,
126 set=lambda self, val: _setattr(self._settings, "WARN_MSGID_NOT_CAPITALIZED", val),
129 GETTEXT_MSGFMT_EXECUTABLE: StringProperty(
130 name="Gettext 'msgfmt' executable",
131 description="The gettext msgfmt 'compiler'. You’ll likely have to edit it if you’re under Windows",
132 subtype='FILE_PATH',
133 default="msgfmt",
134 get=lambda self: self._settings.GETTEXT_MSGFMT_EXECUTABLE,
135 set=lambda self, val: setattr(self._settings, "GETTEXT_MSGFMT_EXECUTABLE", val),
138 FRIBIDI_LIB: StringProperty(
139 name="Fribidi Library",
140 description="The FriBidi C compiled library (.so under Linux, .dll under windows...), you’ll likely have "
141 "to edit it if you’re under Windows, e.g. using the one included in svn's libraries repository",
142 subtype='FILE_PATH',
143 default="libfribidi.so.0",
144 get=lambda self: self._settings.FRIBIDI_LIB,
145 set=lambda self, val: setattr(self._settings, "FRIBIDI_LIB", val),
148 SOURCE_DIR: StringProperty(
149 name="Source Root",
150 description="The Blender source root path",
151 subtype='FILE_PATH',
152 default="blender",
153 get=lambda self: self._settings.SOURCE_DIR,
154 set=lambda self, val: setattr(self._settings, "SOURCE_DIR", val),
157 I18N_DIR: StringProperty(
158 name="Translation Root",
159 description="The bf-translation repository",
160 subtype='FILE_PATH',
161 default="i18n",
162 get=lambda self: self._settings.I18N_DIR,
163 set=lambda self, val: setattr(self._settings, "I18N_DIR", val),
166 SPELL_CACHE: StringProperty(
167 name="Spell Cache",
168 description="A cache storing validated msgids, to avoid re-spellchecking them",
169 subtype='FILE_PATH',
170 default=os.path.join("/tmp", ".spell_cache"),
171 get=lambda self: self._settings.SPELL_CACHE,
172 set=lambda self, val: setattr(self._settings, "SPELL_CACHE", val),
175 PY_SYS_PATHS: StringProperty(
176 name="Import Paths",
177 description="Additional paths to add to sys.path (';' separated)",
178 default="",
179 get=lambda self: self._settings.PY_SYS_PATHS,
180 set=lambda self, val: setattr(self._settings, "PY_SYS_PATHS", val),
183 persistent_data_path: StringProperty(
184 name="Persistent Data Path",
185 description="The name of a json file storing those settings (unfortunately, Blender's system "
186 "does not work here)",
187 subtype='FILE_PATH',
188 default=os.path.join("ui_translate_settings.json"),
190 _is_init = False
192 def draw(self, context):
193 layout = self.layout
194 layout.label(text="WARNING: preferences are lost when add-on is disabled, be sure to use \"Save Persistent\" "
195 "if you want to keep your settings!")
196 layout.prop(self, "WARN_MSGID_NOT_CAPITALIZED")
197 layout.prop(self, "GETTEXT_MSGFMT_EXECUTABLE")
198 layout.prop(self, "FRIBIDI_LIB")
199 layout.prop(self, "SOURCE_DIR")
200 layout.prop(self, "I18N_DIR")
201 layout.prop(self, "SPELL_CACHE")
202 layout.prop(self, "PY_SYS_PATHS")
204 layout.separator()
205 split = layout.split(factor=0.75)
206 col = split.column()
207 col.prop(self, "persistent_data_path")
208 row = col.row()
209 row.operator("ui.i18n_settings_save", text="Save").filepath = self.persistent_data_path
210 row.operator("ui.i18n_settings_load", text="Load").filepath = self.persistent_data_path
211 col = split.column()
212 col.operator("ui.i18n_settings_save", text="Save Persistent To...")
213 col.operator("ui.i18n_settings_load", text="Load Persistent From...")
216 classes = (
217 UI_OT_i18n_settings_load,
218 UI_OT_i18n_settings_save,
219 UI_AP_i18n_settings,