Welcome to mirror list, hosted at ThFree Co, Russian Federation.

development_api_navigator.py - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 599f6f4ed96798bf8565c04c7a0c427b9e42ca38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# ##### 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, 4),
    "blender": (2, 57, 0),
    "location": "Text Editor > Properties > API Navigator Panel",
    "description": "Allows exploration of the python api via the user interface",
    "warning": "",
    "wiki_url": "https://wiki.blender.org/index.php/Extensions:2.6/Py/"
                "Scripts/Text_Editor/API_Navigator",
    "category": "Development",
}

"""
    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
from bpy.types import (
        Operator,
        Panel,
        PropertyGroup,
        )
from bpy.props import (
        BoolVectorProperty,
        StringProperty,
        IntProperty,
        PointerProperty,
        )
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):
    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):
    global root_module, tree_level, root_m_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():

    path = bpy.context.window_manager.api_nav_props.path

    def 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':
                    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, SyntaxError):
                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"""
    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():
    """Update the filter according to the current path"""
    global filter_mem

    try:
        bpy.context.window_manager.api_nav_props.filters = filter_mem[
                                                            bpy.context.window_manager.api_nav_props.path
                                                            ]
    except:
        bpy.context.window_manager.api_nav_props.filters = ''


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

    filters = bpy.context.window_manager.api_nav_props.filters
    if filters:
        filter_mem[bpy.context.window_manager.api_nav_props.old_path] = \
                bpy.context.window_manager.api_nav_props.filters
    else:
        filter_mem.pop(bpy.context.window_manager.api_nav_props.old_path, None)


# #### API Navigator parent class ######
class ApiNavigator():
    """Parent class for API Navigator"""

    @staticmethod
    def generate_global_values():
        """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

        ApiNavigator.generate_api_doc()
        return {'FINISHED'}

    @staticmethod
    def generate_api_doc():
        """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'}

    @staticmethod
    def doc_text_datablock():
        """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 ###########
def api_update(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()
        ApiNavigator.generate_global_values()
        ApiNavigator.doc_text_datablock()


class Update(ApiNavigator, Operator):
    """Update the tree structure"""
    bl_idname = "api_navigator.update"
    bl_label = "API Navigator Update"

    def execute(self, context):
        api_update()
        return {'FINISHED'}


class BackToBpy(ApiNavigator, 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.generate_global_values()
        self.doc_text_datablock()
        return {'FINISHED'}


class Down(ApiNavigator, Operator):
    """Go to this Module"""
    bl_idname = "api_navigator.down"
    bl_label = "API Navigator Down"

    pointed_module = 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.generate_global_values()
        self.doc_text_datablock()
        return {'FINISHED'}


class Parent(ApiNavigator, 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.generate_global_values()
            self.doc_text_datablock()

        return {'FINISHED'}


class ClearFilter(ApiNavigator, 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.filters = ''
        return {'FINISHED'}


class Subscript(ApiNavigator, Operator):
    """Subscript to this Item"""
    bl_idname = "api_navigator.subscript"
    bl_label = "API Navigator Subscript"

    subscription = 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.generate_global_values()
        self.doc_text_datablock()
        return {'FINISHED'}


class Toggle_doc(ApiNavigator, 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
            return {'FINISHED'}
        except:
            pass

        bpy.context.space_data.text = None

        return {'FINISHED'}


# ######### UI Panels ############
class OBJECT_PT_api_navigator(ApiNavigator, 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):
        # Note: Currently unused method
        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
            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
            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=True).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=True).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.label(text="", 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=True):
        global tree_level, current_module

        def reduced(row, too_long):
            if row and too_long:
                sub_row = row.row(align=True)
                sub_row.prop(bpy.context.window_manager.api_nav_props, "reduce_to")
                sub_row.prop(bpy.context.window_manager.api_nav_props, "pages", text="Pages")

        layout = self.layout
        filters = bpy.context.window_manager.api_nav_props.filters
        reduce_to = bpy.context.window_manager.api_nav_props.reduce_to * self.columns
        page_index = reduce_to * pages
        show_panel_elements = bpy.context.window_manager.api_nav_props.panel_toggle[t] if \
                              0 <= t < 10 else True

        lenght = tree_level[t].__len__()
        too_long = lenght > reduce_to

        if lenght:
            col = layout.column()
            box = col.box()

            title_box = box.row(align=True)
            title_box.prop(bpy.context.window_manager.api_nav_props,
                           "panel_toggle", text="", index=t if 0 <= t < 10 else 0)
            title_box.label(text=label, icon=icon)
            reduced(box, too_long)

            if show_panel_elements:
                if t < 2:
                    box = box.box()
                row = box.row()
                col = row.column(align=True)
                i = 0
                objects, count, filtered = 0, 0, 0

                while count < reduce_to and i < lenght:
                    obj = tree_level[t][i]

                    if filters and filters not in obj:
                        i += 1
                        continue
                    elif filtered < page_index:
                        filtered += 1
                        i += 1
                        continue

                    if not (objects % self.columns):
                        row = col.row(align=True)
                    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=emboss).subscription = '"' + obj + '"'
                    else:
                        row.operator("api_navigator.subscript",
                                     text=str(obj), emboss=emboss).subscription = str(i)
                    filtered += 1
                    i += 1
                    objects += 1
                    count += 1

        return {'FINISHED'}

    def draw(self, context):
        global tree_level, current_module, module_type, return_report

        api_update(context)

        layout = self.layout
        layout.label(text="Tree Structure:")

        col = layout.column(align=True)
        col.prop(bpy.context.window_manager.api_nav_props, 'path', text='')

        row = col.row(align=True)
        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, "filters", text="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=True)
        self.list_draw(4, pages, "BUTS", label="Properties", emboss=True)
        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")


# ###### Properties #######
class ApiNavProps(PropertyGroup):
    """
    Fake module like class.

    bpy.context.window_manager.api_nav_props
    """
    path = StringProperty(
            name="Path",
            description="Enter bpy.ops.api_navigator to see the documentation",
            default="bpy"
            )
    old_path = StringProperty(
            name="Old Path",
            default=""
            )
    filters = StringProperty(
            name="Filters",
            description="Filter the resulting modules",
            default=""
            )
    reduce_to = IntProperty(
            name="Reduce to",
            description="Display a maximum number of x entries by pages",
            default=10,
            min=1
            )
    pages = IntProperty(
            name="Pages",
            description="Display a Page",
            default=0,
            min=0
            )
    panel_toggle = BoolVectorProperty(
            name="Tab",
            description="Expand/Collapse UI elements",
            default=(True,) * 9,
            size=9,
            )


# ######## Register #########
def register_keymaps():
    kc = bpy.context.window_manager.keyconfigs.addon
    if kc:
        km = kc.keymaps.new(name="Text", space_type='TEXT_EDITOR')
        km.keymap_items.new('api_navigator.toggle_doc', 'ESC', 'PRESS')


def unregister_keymaps():
    kc = bpy.context.window_manager.keyconfigs.addon
    if kc:
        km = kc.keymaps["Text"]
        kmi = km.keymap_items["api_navigator.toggle_doc"]
        km.keymap_items.remove(kmi)


def register():
    bpy.utils.register_module(__name__)
    bpy.types.WindowManager.api_nav_props = PointerProperty(
                                                type=ApiNavProps,
                                                name="API Nav Props",
                                                description=""
                                                )
    register_keymaps()


def unregister():
    unregister_keymaps()
    del bpy.types.WindowManager.api_nav_props

    bpy.utils.unregister_module(__name__)


if __name__ == '__main__':
    register()