Skip to content
Snippets Groups Projects
text_editor_api_navigator.py 23.1 KiB
Newer Older
# text_editor_api_navigator.py
#
# ***** BEGIN GPL LICENSE BLOCK *****
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ***** END GPL LICENCE BLOCK *****

bl_info = {
    "name": "API Navigator",
    "author": "Dany Lebel (Axon_D)",
    "version": (1, 0),
    "blender": (2, 5, 5),
    "api": 32760,
    "location": "Text Editor -> Properties (CTRL+F) -> API Navigator Panel",
    "description": "Allows to explore the python api via the user interface",
    "warning": "",
    "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/"
        "Scripts/Text_Editor/API_Navigator",
    "tracker_url": "http://projects.blender.org/tracker/index.php?"\
        "func=detail&aid=24982",
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
    "category": "Text Editor"}

"""
    You can browse through the tree structure of the api. Each child object appears in a list
that tries to be representative of its type. These lists are :
    
    * Items (for an iterable object)
    * Item Values (for an iterable object wich only supports index)
    * Modules
    * Types
    * Properties
    * Structs and Functions
    * Methods and Functions
    * Attributes
    * Inaccessible (some objects may be listed but inaccessible)

    The lists can be filtered to help searching in the tree. Just enter the text in the
filter section. It is also possible to explore other modules. Go the the root and select
it in the list of available modules. It will be imported dynamically.

    In the text section, some informations are displayed. The type of the object,
what it returns, and its docstring. We could hope that these docstrings will be as
descriptive as possible. This text data block named api_doc_ can be toggled on and off
with the Escape key. (but a bug prevent the keymap to register correctly at start)

"""

import bpy, sys
from console.complete_import import get_root_modules


############ Global Variables ############

last_text = None  # last text data block

root_module = None  # root module of the tree

root_m_path = ''  # root_module + path as a string

current_module = None  # the object itself in the tree structure


tree_level = None  # the list of objects from the current_module

def init_tree_level():
    global tree_level
    tree_level = [[],[],[],[],[],[],[], [], []]

init_tree_level()


api_doc_ = ''  # the documentation formated for the API Navigator

module_type = None  # the type of current_module

return_report = ''  # what current_module returns

filter_mem = {}  # remember last filters entered for each path

too_long = False  # is tree_level list too long to display in a panel?


############   Functions   ############

def get_root_module(path):
    #print('get_root_module')
    global root_module
    if '.' in path:
        root = path[:path.find('.')]
    else :
        root = path
    try :
        root_module = __import__(root)
    except :
        root_module = None


def evaluate(module):
    #print('evaluate')
    global root_module, tree_level, root_m_path
    
    path = bpy.context.window_manager.api_nav_props.path
    try :
        len_name = root_module.__name__.__len__()
        root_m_path = 'root_module' + module[len_name:]
        current_module = eval(root_m_path)
        return current_module
    except :
        init_tree_level
        return None


def get_tree_level():
    #print('get_tree_level')
    
    path = bpy.context.window_manager.api_nav_props.path
    
    def object_list():
        #print('object_list')
        global current_module, root_m_path
        
        itm, val, mod, typ, props, struct, met, att, bug = [], [], [], [], [], [], [], [], []
        iterable = isiterable(current_module)
        if iterable:
            iter(current_module)
            current_type = str(module_type)
            if current_type != "<class 'str'>":
                if iterable == 'a':
                    #if iterable == 'a':
                        #current_type.__iter__()
                    itm = list(current_module.keys())
                    if not itm:
                        val = list(current_module)
                else :
                    val = list(current_module)
        
        for i in dir(current_module):
            try :
                t = str(type(eval(root_m_path + '.' + i)))
            except AttributeError :
                bug += [i]
                continue
            
            if t == "<class 'module'>":
                mod += [i]
            elif t[0:16] == "<class 'bpy_prop":
                props += [i]
            elif t[8:11] == 'bpy':
                struct += [i]
            elif t == "<class 'builtin_function_or_method'>":
                met += [i]
            elif t == "<class 'type'>":
                typ += [i]
            else :
                att += [i]
        
        return [itm, val, mod, typ, props, struct, met, att, bug]
    
    if not path:
        return [[], [], [i for i in get_root_modules()], [], [], [], [], [], []]
    return object_list()


def parent(path):
    """Returns the parent path"""
    #print('parent')
    
    parent = path
    if parent[-1] == ']' and '[' in parent:
        while parent[-1] != '[':
            parent = parent[:-1]
    elif '.' in parent:
        while parent[-1] != '.':
            parent = parent[:-1]
    else :
        return ''
    parent = parent[:-1]
    return parent


def update_filter(self):
    """Update the filter according to the current path"""
    global filter_mem
    
    try :
        bpy.context.window_manager.api_nav_props.filter = filter_mem[bpy.context.window_manager.api_nav_props.path]
    except :
        bpy.context.window_manager.api_nav_props.filter = ''
    return {'FINISHED'}


def isiterable(mod):
    
    try :
        iter(mod)
    except :
        return False
    try :
        mod['']
        return 'a'
    except KeyError:
        return 'a'
    except (AttributeError, TypeError):
        return 'b'


def fill_filter_mem():
    global filter_mem
    
    filter = bpy.context.window_manager.api_nav_props.filter
    if filter:
        filter_mem[bpy.context.window_manager.api_nav_props.old_path] = bpy.context.window_manager.api_nav_props.filter
    else :
        filter_mem.pop(bpy.context.window_manager.api_nav_props.old_path, None)
        

############ Api Tree Properties ############

def addProperties(ApiNavProps):
    from bpy.props import StringProperty, PointerProperty, IntProperty, BoolProperty
    
    bpy.types.WindowManager.api_nav_props = PointerProperty(
        type=ApiNavProps, name='API Nav Props', description='')
    
    ApiNavProps.path = StringProperty(name='path',
        description='Enter bpy.ops.api_navigator to see the documentation',
        default='bpy')
    
    ApiNavProps.old_path = StringProperty(name='old_path', default='')
    
    ApiNavProps.filter = StringProperty(name='filter',
        description='Filter the resulting modules', default='')
    
    ApiNavProps.reduce_to = IntProperty(name='Reduce to ',
        description='Display a maximum number of x entries by pages',
        default=10, min=1)
    
    ApiNavProps.pages = IntProperty(name='Pages',
        description='Display a Page', default=0, min=0)

def delProperties():
    del bpy.types.WindowManager.api_nav_props

######  API Navigator parent class  #######

class ApiNavigator():
    """Parent class for API Navigator"""
    
    
    def generate_global_values(self):
        """Populate the level attributes to display the panel buttons and the documentation"""
        global tree_level, current_module, module_type, return_report, last_text
        
        text = bpy.context.space_data.text
        if text:
            if text.name != 'api_doc_':
                last_text = bpy.context.space_data.text.name
            elif bpy.data.texts.__len__() < 2:
                last_text = None
        else :
            last_text = None
        bpy.context.window_manager.api_nav_props.pages = 0
        get_root_module(bpy.context.window_manager.api_nav_props.path)
        current_module = evaluate(bpy.context.window_manager.api_nav_props.path)
        module_type = str(type(current_module))
        return_report = str(current_module)
        tree_level = get_tree_level()
        
        if tree_level.__len__() > 30:
            global too_long
            too_long = True
        else :
            too_long = False
        
        self.generate_api_doc()
        return {'FINISHED'}
    
    
    def generate_api_doc(self):
        """Format the doc string for API Navigator"""
        global current_module, api_doc_, return_report, module_type
    
        path = bpy.context.window_manager.api_nav_props.path
        line = "-" * (path.__len__()+2)
        header = """\n\n\n\t\t%s\n\t   %s\n\
_____________________________________________\n\
\n\
Type : %s\n\
\n\
\n\
Return : %s\n\
_____________________________________________\n\
\n\
Doc:
\n\
""" % (path, line, module_type, return_report)
        footer = "\n\
_____________________________________________\n\
\n\
\n\
\n\
#############################################\n\
#                  api_doc_                 #\n\
#            Escape to toggle text          #\n\
#  (F8 to reload modules if doesn't work)   #\n\
#############################################"
        doc = current_module.__doc__
        api_doc_ = header + str(doc) + footer
        return {'FINISHED'}
    
    
    def doc_text_datablock(self):
        """Create the text databloc or overwrite it if it already exist"""
        global api_doc_
        
        space_data = bpy.context.space_data
        
        try :
            doc_text = bpy.data.texts['api_doc_']
            space_data.text = doc_text
            doc_text.clear()
        except :
            bpy.data.texts.new(name='api_doc_')
            doc_text = bpy.data.texts['api_doc_']
            space_data.text = doc_text
        
        doc_text.write(text=api_doc_)
        return {'FINISHED'}



############   Operators   ############
    
class Update(ApiNavigator, bpy.types.Operator):
    """Update the tree structure"""
    bl_idname = "api_navigator.update"
    bl_label = "API Navigator Update"
    
    
    def execute(self, context):
        if bpy.context.window_manager.api_nav_props.path != bpy.context.window_manager.api_nav_props.old_path:
            fill_filter_mem()
            bpy.context.window_manager.api_nav_props.old_path = bpy.context.window_manager.api_nav_props.path
            update_filter(self)
            self.generate_global_values()
            self.doc_text_datablock()
            return {'FINISHED'}
        return {'FINISHED'}


class BackToBpy(ApiNavigator, bpy.types.Operator):
    """go back to module bpy"""
    bl_idname = "api_navigator.back_to_bpy"
    bl_label = "Back to bpy"
    
    def execute(self, context):
        fill_filter_mem()
        if not bpy.context.window_manager.api_nav_props.path:
            bpy.context.window_manager.api_nav_props.old_path = bpy.context.window_manager.api_nav_props.path = 'bpy'
        else :
            bpy.context.window_manager.api_nav_props.old_path = bpy.context.window_manager.api_nav_props.path = 'bpy'
        update_filter(self)
        self.generate_global_values()
        self.doc_text_datablock()
        return {'FINISHED'}


class Down(ApiNavigator, bpy.types.Operator):
    """go to this Module"""
    bl_idname = "api_navigator.down"
    bl_label = "API Navigator Down"
    pointed_module = bpy.props.StringProperty(name='Current Module', default='')
    
    
    def execute(self, context):
        fill_filter_mem()
        
        if not bpy.context.window_manager.api_nav_props.path:
            bpy.context.window_manager.api_nav_props.old_path = bpy.context.window_manager.api_nav_props.path = bpy.context.window_manager.api_nav_props.path + self.pointed_module
        else :
            bpy.context.window_manager.api_nav_props.old_path = bpy.context.window_manager.api_nav_props.path = bpy.context.window_manager.api_nav_props.path + '.' + self.pointed_module
        
        update_filter(self)
        self.generate_global_values()
        self.doc_text_datablock()
        return {'FINISHED'}


class Parent(ApiNavigator, bpy.types.Operator):
    """go to Parent Module"""
    bl_idname = "api_navigator.parent"
    bl_label = "API Navigator Parent"
    
    
    def execute(self, context):
        path = bpy.context.window_manager.api_nav_props.path
        
        if path:
            fill_filter_mem()
            bpy.context.window_manager.api_nav_props.old_path = bpy.context.window_manager.api_nav_props.path = parent(bpy.context.window_manager.api_nav_props.path)
            update_filter(self)
            self.generate_global_values()
            self.doc_text_datablock()
        
        return {'FINISHED'}


class ClearFilter(ApiNavigator, bpy.types.Operator):
    """Clear the filter"""
    bl_idname = 'api_navigator.clear_filter'
    bl_label = 'API Nav clear filter'
    
    def execute(self, context):
        bpy.context.window_manager.api_nav_props.filter = ''
        return {'FINISHED'}


class FakeButton(ApiNavigator, bpy.types.Operator):
    """The list is not displayed completely""" # only serve as an indicator
    bl_idname = 'api_navigator.fake_button'
    bl_label = ''


class Subscript(ApiNavigator, bpy.types.Operator):
    """Subscript to this Item"""
    bl_idname = "api_navigator.subscript"
    bl_label = "API Navigator Subscript"
    subscription = bpy.props.StringProperty(name='', default='')
    
    def execute(self, context):
        fill_filter_mem()
        bpy.context.window_manager.api_nav_props.old_path = bpy.context.window_manager.api_nav_props.path = bpy.context.window_manager.api_nav_props.path + '[' + self.subscription + ']'
        update_filter(self)
        self.generate_global_values()
        self.doc_text_datablock()
        return {'FINISHED'}


class Toggle_doc(ApiNavigator, bpy.types.Operator):
    """Toggle on or off api_doc_ Text"""
    bl_idname = 'api_navigator.toggle_doc'
    bl_label = 'Toggle api_doc_'
    
    
    def execute(self, context):
        global last_text
        
        try :
            if bpy.context.space_data.text.name != "api_doc_":
                last_text = bpy.context.space_data.text.name
        except : pass
        
        try :
            text = bpy.data.texts["api_doc_"]
            bpy.data.texts["api_doc_"].clear()
            bpy.data.texts.remove(text)
        except KeyError:
            self.doc_text_datablock()
            return {'FINISHED'}
        
        try :
            text = bpy.data.texts[last_text]
            bpy.context.space_data.text = text
            #line = bpy.ops.text.line_number() # operator doesn't seems to work ???
            #bpy.ops.text.jump(line=line)
            return {'FINISHED'}
        except : pass
        
        bpy.context.space_data.text = None
        return {'FINISHED'}

############ UI Panels ############
    
class OBJECT_PT_api_navigator(ApiNavigator, bpy.types.Panel):
    bl_idname = 'api_navigator'
    bl_space_type = "TEXT_EDITOR"
    bl_region_type = "UI"
    bl_label = "API Navigator"
    bl_options = "DEFAULT_CLOSED"
    
    
    columns = 3


    def iterable_draw(self):
        global tree_level, current_module
        
        iterable = isiterable(current_module)
        
        if iterable:
            iter(current_module)
            current_type = str(module_type)
            
            if current_type == "<class 'str'>":
                return {'FINISHED'}
            
            col = self.layout
            filter = bpy.context.window_manager.api_nav_props.filter
            reduce_to = bpy.context.window_manager.api_nav_props.reduce_to * self.columns
            pages = bpy.context.window_manager.api_nav_props.pages
            page_index = reduce_to*pages
            rank = 0
            count = 0
            i = 0
            filtered = 0
            
            if iterable == 'a':
                current_type.__iter__()
                collection = list(current_module.keys())
                end = collection.__len__()
                box = self.layout.box()
                row = box.row()
                row.label(text="Items", icon="DOTSDOWN")
                box = box.box()
                col = box.column(align=True)
                
                while count < reduce_to and i < end:
                    mod = collection[i]
                    if filtered < page_index:
                        filtered += 1
                        i += 1
                        continue
                    
                    if not (i % self.columns):
                        row = col.row()
                    row.operator('api_navigator.subscript', text=mod, emboss=False).subscription = '"' + mod + '"'
                    filtered += 1
                    i += 1
                    count += 1
            
            elif iterable == 'b':
                box = self.layout.box()
                row = box.row()
                row.label(text="Item Values", icon="OOPS")
                box = box.box()
                col = box.column(align=True)
                collection = list(current_module)
                end = collection.__len__()
                
                while count < reduce_to and i < end:
                    mod = str(collection[i])
                    if filtered < page_index:
                        filtered += 1
                        i += 1
                        continue
                    
                    if not (i % self.columns):
                        row = col.row()
                    row.operator('api_navigator.subscript', text=mod, emboss=False).subscription = str(i)
                    filtered += 1
                    i += 1
                    count += 1
            
            too_long = end > 30
            
            if too_long:
                row = col.row()
                row.prop(bpy.context.window_manager.api_nav_props, 'reduce_to')
                row.operator('api_navigator.fake_button', text='', emboss=False, icon="DOTSDOWN")
                row.prop(bpy.context.window_manager.api_nav_props, 'pages', text='Pages')

        return {'FINISHED'}
    
    
    
    
    def list_draw(self, t, pages, icon, label=None, emboss=False):
        global tree_level, current_module
        
        def reduced(too_long):
            
            if too_long:
                row = col.row()
                row.prop(bpy.context.window_manager.api_nav_props, 'reduce_to')
                row.operator('api_navigator.fake_button', text='', emboss=False, icon="DOTSDOWN")
                row.prop(bpy.context.window_manager.api_nav_props, 'pages', text='Pages')
        
        layout = self.layout
        
        filter = bpy.context.window_manager.api_nav_props.filter
        
        reduce_to = bpy.context.window_manager.api_nav_props.reduce_to * self.columns
        
        page_index = reduce_to*pages
        
        
        len = tree_level[t].__len__()
        too_long = len > reduce_to
        
        if len:
            col = layout.column()
            box = col.box()
            
            row = box.row()
            row.label(text=label, icon=icon)
            
            if t < 2:
                box = box.box()
            row = box.row()
            col = row.column(align=True)
            i = 0
            objects = 0
            count = 0
            filtered = 0
            
            while count < reduce_to and i < len:
                obj = tree_level[t][i]
                
                if filter and filter not in obj:
                    i += 1
                    continue
                elif filtered < page_index:
                    filtered += 1
                    i += 1
                    continue
                
                if not (objects % self.columns):
                    row = col.row()
                if t > 1:
                    row.operator("api_navigator.down", text=obj, emboss=emboss).pointed_module = obj
                elif t == 0:
                    row.operator('api_navigator.subscript', text=str(obj), emboss=False).subscription = '"' + obj + '"'
                else :
                    row.operator('api_navigator.subscript', text=str(obj), emboss=False).subscription = str(i)
                filtered += 1
                i += 1
                objects += 1
                count += 1
            
            reduced(too_long)
        
        return {'FINISHED'}
    
    
    def draw(self, context):
        global tree_level, current_module, module_type, return_report
    
        bpy.ops.api_navigator.update()
        
        st = bpy.context.space_data
        
        ###### layout ######
        layout = self.layout
        col = layout.column()
        layout.label(text='Tree Structure')
        col = layout.column(align=True)
        col.prop(bpy.context.window_manager.api_nav_props, 'path', text='')
        row = col.row()
        row.operator("api_navigator.parent", text="Parent", icon="BACK")
        row.operator("api_navigator.back_to_bpy", text='', emboss=True, icon="FILE_PARENT")
        
        col = layout.column()
        row = col.row(align=True)
        row.prop(bpy.context.window_manager.api_nav_props, 'filter')
        row.operator('api_navigator.clear_filter', text='', icon='PANEL_CLOSE')
        
        col = layout.column()

        pages = bpy.context.window_manager.api_nav_props.pages
        self.list_draw(0, pages, "DOTSDOWN", label="Items")
        self.list_draw(1, pages, "DOTSDOWN", label="Item Values")
        self.list_draw(2, pages, "PACKAGE", label="Modules", emboss=True)
        self.list_draw(3, pages, "WORDWRAP_ON", label="Types", emboss=False)
        self.list_draw(4, pages, "BUTS", label="Properties", emboss=False)
        self.list_draw(5, pages, "OOPS", label="Structs and Functions")
        self.list_draw(6, pages, "SCRIPTWIN", label="Methods and Functions")
        self.list_draw(7, pages, "INFO", label="Attributes")
        self.list_draw(8, pages, "ERROR", label="Inaccessible")


########### Menu functions ###############


def register_keymaps():
    kc = bpy.context.window_manager.keyconfigs['Blender']
    km = kc.keymaps.get("Text")
    if km is None:
         km = kc.keymaps.new(name="Text")
    kmi = km.items.new('api_navigator.toggle_doc', 'ESC', 'PRESS')


def unregister_keymaps():
    km = bpy.data.window_managers["WinMan"].keyconfigs.default.keymaps["Text"]
    kmi = km.items["api_navigator.toggle_doc"]
    km.items.remove(kmi)


def register():
    class ApiNavProps(bpy.types.IDPropertyGroup):
        """
        Fake module like class.
    
        bpy.context.window_manager.api_nav_props
         
        """ 
        pass
    

    addProperties(ApiNavProps)
    register_keymaps()
    #print(get_tree_level())


def unregister():
    unregister_keymaps()
    delProperties()
    


if __name__ == '__main__':
    register()