Doc: add bpy.utils.previews

Updated sphinx_doc_gen.py to better handle pure py-classes.
This commit is contained in:
Campbell Barton
2015-05-12 18:06:31 +10:00
parent f727df6076
commit 34c78a659b
2 changed files with 49 additions and 15 deletions

View File

@@ -257,6 +257,7 @@ else:
"bpy.props", "bpy.props",
"bpy.types", # supports filtering "bpy.types", # supports filtering
"bpy.utils", "bpy.utils",
"bpy.utils.previews",
"bpy_extras", "bpy_extras",
"gpu", "gpu",
"mathutils", "mathutils",
@@ -642,10 +643,17 @@ def pyfunc2sphinx(ident, fw, module_name, type_name, identifier, py_func, is_cla
else: else:
func_type = "staticmethod" func_type = "staticmethod"
fw(ident + ".. %s:: %s%s\n\n" % (func_type, identifier, arg_str)) doc = py_func.__doc__
if py_func.__doc__: if (not doc) or (not doc.startswith(".. %s:: " % func_type)):
write_indented_lines(ident + " ", fw, py_func.__doc__) fw(ident + ".. %s:: %s%s\n\n" % (func_type, identifier, arg_str))
ident_temp = ident + " "
else:
ident_temp = ident
if doc:
write_indented_lines(ident_temp, fw, doc)
fw("\n") fw("\n")
del doc, ident_temp
if is_class: if is_class:
write_example_ref(ident + " ", fw, module_name + "." + type_name + "." + identifier) write_example_ref(ident + " ", fw, module_name + "." + type_name + "." + identifier)
@@ -916,7 +924,7 @@ def pymodule2sphinx(basepath, module_name, module, title):
fw(value.__doc__) fw(value.__doc__)
else: else:
fw(".. class:: %s\n\n" % type_name) fw(".. class:: %s\n\n" % type_name)
write_indented_lines(" ", fw, value.__doc__, False) write_indented_lines(" ", fw, value.__doc__, True)
else: else:
fw(".. class:: %s\n\n" % type_name) fw(".. class:: %s\n\n" % type_name)
fw("\n") fw("\n")
@@ -929,6 +937,11 @@ def pymodule2sphinx(basepath, module_name, module, title):
if type(descr) == ClassMethodDescriptorType: if type(descr) == ClassMethodDescriptorType:
py_descr2sphinx(" ", fw, descr, module_name, type_name, key) py_descr2sphinx(" ", fw, descr, module_name, type_name, key)
# needed for pure python classes
for key, descr in descr_items:
if type(descr) == types.FunctionType:
pyfunc2sphinx(" ", fw, module_name, type_name, key, descr, is_class=True)
for key, descr in descr_items: for key, descr in descr_items:
if type(descr) == MethodDescriptorType: if type(descr) == MethodDescriptorType:
py_descr2sphinx(" ", fw, descr, module_name, type_name, key) py_descr2sphinx(" ", fw, descr, module_name, type_name, key)
@@ -1608,6 +1621,7 @@ def write_rst_contents(basepath):
# py modules # py modules
"bpy.utils", "bpy.utils",
"bpy.utils.previews",
"bpy.path", "bpy.path",
"bpy.app", "bpy.app",
"bpy.app.handlers", "bpy.app.handlers",

View File

@@ -23,13 +23,20 @@ This module contains utility functions to handle custom previews.
It behaves as a high-level 'cached' previews manager. It behaves as a high-level 'cached' previews manager.
This allows addons to generate their own previews, and use them as icons in UI widgets This allows scripts to generate their own previews, and use them as icons in UI widgets
('icon_value' of UILayout functions). ('icon_value' for UILayout functions).
Custom Icon Example
-------------------
.. literalinclude:: ../../../release/scripts/templates_py/ui_previews_custom_icon.py
""" """
__all__ = ( __all__ = (
"new", "new",
"remove", "remove",
"ImagePreviewCollection",
) )
import _bpy import _bpy
@@ -42,14 +49,21 @@ _uuid_open = set()
# High-level previews manager. # High-level previews manager.
# not accessed directly # not accessed directly
class _BPyImagePreviewCollection(dict): class ImagePreviewCollection(dict):
""" """
Dict-like class of previews. Dictionary-like class of previews.
This is a subclass of Python's built-in dict type,
used to store multiple image previews.
.. note::
- instance with :mod:`bpy.utils.previews.new`
- keys must be ``str`` type.
- values will be :class:`bpy.types.ImagePreview`
""" """
# Internal notes: # Internal notes:
# - keys in the dict are stored by name
# - values are instances of bpy.types.ImagePreview
# - Blender's internal 'PreviewImage' struct uses 'self._uuid' prefix. # - Blender's internal 'PreviewImage' struct uses 'self._uuid' prefix.
def __init__(self): def __init__(self):
@@ -93,11 +107,13 @@ class _BPyImagePreviewCollection(dict):
release.__doc__ = _utils_previews.release.__doc__ release.__doc__ = _utils_previews.release.__doc__
def clear(self): def clear(self):
"""Clear all previews."""
for name in self.keys(): for name in self.keys():
_utils_previews.release(self._gen_key(name)) _utils_previews.release(self._gen_key(name))
super().clear() super().clear()
def close(self): def close(self):
"""Close the collection and clear all previews."""
self.clear() self.clear()
_uuid_open.remove(self._uuid) _uuid_open.remove(self._uuid)
@@ -114,24 +130,28 @@ class _BPyImagePreviewCollection(dict):
def new(): def new():
""" """
Return a new preview collection. :return: a new preview collection.
:rtype: :class:`ImagePreviewCollection`
""" """
return _BPyImagePreviewCollection() return ImagePreviewCollection()
def remove(p): def remove(pcoll):
""" """
Remove the specified previews collection. Remove the specified previews collection.
:arg pcoll: Preview collection to close.
:type pcoll: :class:`ImagePreviewCollection`
""" """
p.close() pcoll.close()
# don't complain about resources on exit (only unregister) # don't complain about resources on exit (only unregister)
import atexit import atexit
def exit_clear_warning(): def exit_clear_warning():
del _BPyImagePreviewCollection.__del__ del ImagePreviewCollection.__del__
atexit.register(exit_clear_warning) atexit.register(exit_clear_warning)
del atexit, exit_clear_warning del atexit, exit_clear_warning