Keymap: load/save improvements

- avoid passing redundant operator name to keymap property set function.
- avoid double attr lookups when setting each property.
- handle exceptions on value type mismatch.
- avoid resource warning on failed load.
This commit is contained in:
Campbell Barton 2014-02-19 10:26:53 +11:00
parent 7afbdbf812
commit 2a9e33fba5
2 changed files with 12 additions and 9 deletions

View File

@ -494,10 +494,10 @@ def keyconfig_set(filepath, report=None):
keyconfigs_old = keyconfigs[:]
try:
keyfile = open(filepath)
exec(compile(keyfile.read(), filepath, "exec"), {"__file__": filepath})
keyfile.close()
error_msg = ""
with open(filepath, 'r', encoding='utf-8') as keyfile:
exec(compile(keyfile.read(), filepath, "exec"),
{"__file__": filepath})
except:
import traceback
error_msg = traceback.format_exc()

View File

@ -175,7 +175,7 @@ def _export_properties(prefix, properties, kmi_id, lines=None):
elif properties.is_property_set(pname):
value = string_value(value)
if value != "":
lines.append("set_kmi_prop(%s, '%s', %s, '%s')\n" % (prefix, pname, value, kmi_id))
lines.append("kmi_props_setattr(%s, '%s', %s)\n" % (prefix, pname, value))
return lines
@ -221,11 +221,14 @@ def keyconfig_export(wm, kc, filepath):
f.write("import bpy\n")
f.write("import os\n\n")
f.write("def set_kmi_prop(kmiprops, prop, value, kmiid):\n"
" if hasattr(kmiprops, prop):\n"
" setattr(kmiprops, prop, value)\n"
" else:\n"
" print(\"Warning: property '%s' not found in keymap item '%s'\" % (prop, kmiid))\n\n")
f.write("def kmi_props_setattr(kmi_props, attr, value):\n"
" try:\n"
" setattr(kmi_props, attr, value)\n"
" except AttributeError:\n"
" print(\"Warning: property '%s' not found in keymap item '%s'\" %\n"
" (attr, kmi_props.__class__.__name__))\n"
" except Exception as e:\n"
" print(\"Warning: %r\" % e)\n\n")
f.write("wm = bpy.context.window_manager\n")
f.write("kc = wm.keyconfigs.new(os.path.splitext(os.path.basename(__file__))[0])\n\n") # keymap must be created by caller