Cleanup: Python code-style (addons, wm)

This commit is contained in:
Campbell Barton
2018-02-01 13:58:44 +11:00
parent 078e012cd9
commit 10fec1f153
3 changed files with 77 additions and 60 deletions

View File

@@ -77,8 +77,8 @@ def modules_refresh(module_cache=addons_fake_modules):
ModuleType = type(ast) ModuleType = type(ast)
try: try:
file_mod = open(mod_path, "r", encoding='UTF-8') file_mod = open(mod_path, "r", encoding='UTF-8')
except OSError as e: except OSError as ex:
print("Error opening file %r: %s" % (mod_path, e)) print("Error opening file %r: %s" % (mod_path, ex))
return None return None
with file_mod: with file_mod:
@@ -89,10 +89,10 @@ def modules_refresh(module_cache=addons_fake_modules):
while not l.startswith("bl_info"): while not l.startswith("bl_info"):
try: try:
l = line_iter.readline() l = line_iter.readline()
except UnicodeDecodeError as e: except UnicodeDecodeError as ex:
if not error_encoding: if not error_encoding:
error_encoding = True error_encoding = True
print("Error reading file as UTF-8:", mod_path, e) print("Error reading file as UTF-8:", mod_path, ex)
return None return None
if len(l) == 0: if len(l) == 0:
@@ -101,10 +101,10 @@ def modules_refresh(module_cache=addons_fake_modules):
lines.append(l) lines.append(l)
try: try:
l = line_iter.readline() l = line_iter.readline()
except UnicodeDecodeError as e: except UnicodeDecodeError as ex:
if not error_encoding: if not error_encoding:
error_encoding = True error_encoding = True
print("Error reading file as UTF-8:", mod_path, e) print("Error reading file as UTF-8:", mod_path, ex)
return None return None
data = "".join(lines) data = "".join(lines)
@@ -182,9 +182,11 @@ def modules_refresh(module_cache=addons_fake_modules):
mod = None mod = None
if mod is None: if mod is None:
mod = fake_module(mod_name, mod = fake_module(
mod_name,
mod_path, mod_path,
force_support=force_support) force_support=force_support,
)
if mod: if mod:
module_cache[mod_name] = mod module_cache[mod_name] = mod
@@ -200,9 +202,12 @@ def modules(module_cache=addons_fake_modules, *, refresh=True):
modules._is_first = False modules._is_first = False
mod_list = list(module_cache.values()) mod_list = list(module_cache.values())
mod_list.sort(key=lambda mod: (mod.bl_info["category"], mod_list.sort(
key=lambda mod: (
mod.bl_info["category"],
mod.bl_info["name"], mod.bl_info["name"],
)) )
)
return mod_list return mod_list
modules._is_first = True modules._is_first = True
@@ -220,8 +225,10 @@ def check(module_name):
loaded_default = module_name in _user_preferences.addons loaded_default = module_name in _user_preferences.addons
mod = sys.modules.get(module_name) mod = sys.modules.get(module_name)
loaded_state = ((mod is not None) and loaded_state = (
getattr(mod, "__addon_enabled__", Ellipsis)) (mod is not None) and
getattr(mod, "__addon_enabled__", Ellipsis)
)
if loaded_state is Ellipsis: if loaded_state is Ellipsis:
print("Warning: addon-module %r found module " print("Warning: addon-module %r found module "

View File

@@ -59,8 +59,8 @@ rna_relative_prop = BoolProperty(
def context_path_validate(context, data_path): def context_path_validate(context, data_path):
try: try:
value = eval("context.%s" % data_path) if data_path else Ellipsis value = eval("context.%s" % data_path) if data_path else Ellipsis
except AttributeError as e: except AttributeError as ex:
if str(e).startswith("'NoneType'"): if str(ex).startswith("'NoneType'"):
# One of the items in the rna path is None, just ignore this # One of the items in the rna path is None, just ignore this
value = Ellipsis value = Ellipsis
else: else:
@@ -159,7 +159,8 @@ class BRUSH_OT_active_index_set(Operator):
description="Brush number", description="Brush number",
) )
_attr_dict = {"sculpt": "use_paint_sculpt", _attr_dict = {
"sculpt": "use_paint_sculpt",
"vertex_paint": "use_paint_vertex", "vertex_paint": "use_paint_vertex",
"weight_paint": "use_paint_weight", "weight_paint": "use_paint_weight",
"image_paint": "use_paint_image", "image_paint": "use_paint_image",
@@ -1022,7 +1023,8 @@ class WM_OT_doc_view_manual(Operator):
"No reference available %r, " "No reference available %r, "
"Update info in 'rna_manual_reference.py' " "Update info in 'rna_manual_reference.py' "
"or callback to bpy.utils.manual_map()" % "or callback to bpy.utils.manual_map()" %
self.doc_id) self.doc_id
)
return {'CANCELLED'} return {'CANCELLED'}
else: else:
import webbrowser import webbrowser
@@ -1234,7 +1236,8 @@ class WM_OT_properties_edit(Operator):
self.soft_max = prop_ui.get("soft_max", self.max) self.soft_max = prop_ui.get("soft_max", self.max)
self.use_soft_limits = ( self.use_soft_limits = (
self.min != self.soft_min or self.min != self.soft_min or
self.max != self.soft_max) self.max != self.soft_max
)
# store for comparison # store for comparison
self._cmp_props = self._cmp_props_get() self._cmp_props = self._cmp_props_get()
@@ -1603,8 +1606,8 @@ class WM_OT_keyconfig_import(Operator):
shutil.copy(self.filepath, path) shutil.copy(self.filepath, path)
else: else:
shutil.move(self.filepath, path) shutil.move(self.filepath, path)
except Exception as e: except Exception as ex:
self.report({'ERROR'}, "Installing keymap failed: %s" % e) self.report({'ERROR'}, "Installing keymap failed: %s" % ex)
return {'CANCELLED'} return {'CANCELLED'}
# sneaky way to check we're actually running the code. # sneaky way to check we're actually running the code.
@@ -1838,12 +1841,14 @@ class WM_OT_addon_enable(Operator):
info_ver = info.get("blender", (0, 0, 0)) info_ver = info.get("blender", (0, 0, 0))
if info_ver > bpy.app.version: if info_ver > bpy.app.version:
self.report({'WARNING'}, self.report(
("This script was written Blender " {'WARNING'},
"This script was written Blender "
"version %d.%d.%d and might not " "version %d.%d.%d and might not "
"function (correctly), " "function (correctly), "
"though it is enabled" % "though it is enabled" %
info_ver)) info_ver
)
return {'FINISHED'} return {'FINISHED'}
else: else:

View File

@@ -99,13 +99,17 @@ def register():
register_class(cls) register_class(cls)
# space_userprefs.py # space_userprefs.py
from bpy.props import StringProperty, EnumProperty from bpy.props import (
EnumProperty,
StringProperty,
)
from bpy.types import WindowManager from bpy.types import WindowManager
def addon_filter_items(self, context): def addon_filter_items(self, context):
import addon_utils import addon_utils
items = [('All', "All", "All Add-ons"), items = [
('All', "All", "All Add-ons"),
('User', "User", "All Add-ons Installed by User"), ('User', "User", "All Add-ons Installed by User"),
('Enabled', "Enabled", "All Enabled Add-ons"), ('Enabled', "Enabled", "All Enabled Add-ons"),
('Disabled', "Disabled", "All Disabled Add-ons"), ('Disabled', "Disabled", "All Disabled Add-ons"),
@@ -132,7 +136,8 @@ def register():
) )
WindowManager.addon_support = EnumProperty( WindowManager.addon_support = EnumProperty(
items=[('OFFICIAL', "Official", "Officially supported"), items=[
('OFFICIAL', "Official", "Officially supported"),
('COMMUNITY', "Community", "Maintained by community developers"), ('COMMUNITY', "Community", "Maintained by community developers"),
('TESTING', "Testing", "Newly contributed scripts (excluded from release builds)") ('TESTING', "Testing", "Newly contributed scripts (excluded from release builds)")
], ],