File size: 9,513 Bytes
d12bc25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import glob
import os
import pyclbr

import afxres
import commctrl
import pywin.framework.scriptutils
import regutil
import win32api
import win32con
import win32ui
from pywin.mfc import dialog

from . import hierlist


class HLIErrorItem(hierlist.HierListItem):
    def __init__(self, text):
        self.text = text
        hierlist.HierListItem.__init__(self)

    def GetText(self):
        return self.text


class HLICLBRItem(hierlist.HierListItem):
    def __init__(self, name, file, lineno, suffix=""):
        # If the 'name' object itself has a .name, use it.  Not sure
        # how this happens, but seems pyclbr related.
        # See PyWin32 bug 817035
        self.name = getattr(name, "name", name)
        self.file = file
        self.lineno = lineno
        self.suffix = suffix

    def __lt__(self, other):
        return self.name < other.name

    def __eq__(self, other):
        return self.name == other.name

    def GetText(self):
        return self.name + self.suffix

    def TakeDefaultAction(self):
        if self.file:
            pywin.framework.scriptutils.JumpToDocument(
                self.file, self.lineno, bScrollToTop=1
            )
        else:
            win32ui.SetStatusText("The source of this object is unknown")

    def PerformItemSelected(self):
        if self.file is None:
            msg = "%s - source can not be located." % (self.name,)
        else:
            msg = "%s defined at line %d of %s" % (self.name, self.lineno, self.file)
        win32ui.SetStatusText(msg)


class HLICLBRClass(HLICLBRItem):
    def __init__(self, clbrclass, suffix=""):
        try:
            name = clbrclass.name
            file = clbrclass.file
            lineno = clbrclass.lineno
            self.super = clbrclass.super
            self.methods = clbrclass.methods
        except AttributeError:
            name = clbrclass
            file = lineno = None
            self.super = []
            self.methods = {}
        HLICLBRItem.__init__(self, name, file, lineno, suffix)

    def GetSubList(self):
        ret = []
        for c in self.super:
            ret.append(HLICLBRClass(c, " (Parent class)"))
        for meth, lineno in self.methods.items():
            ret.append(HLICLBRMethod(meth, self.file, lineno, " (method)"))
        return ret

    def IsExpandable(self):
        return len(self.methods) + len(self.super)

    def GetBitmapColumn(self):
        return 21


class HLICLBRFunction(HLICLBRClass):
    def GetBitmapColumn(self):
        return 22


class HLICLBRMethod(HLICLBRItem):
    def GetBitmapColumn(self):
        return 22


class HLIModuleItem(hierlist.HierListItem):
    def __init__(self, path):
        hierlist.HierListItem.__init__(self)
        self.path = path

    def GetText(self):
        return os.path.split(self.path)[1] + " (module)"

    def IsExpandable(self):
        return 1

    def TakeDefaultAction(self):
        win32ui.GetApp().OpenDocumentFile(self.path)

    def GetBitmapColumn(self):
        col = 4  # Default
        try:
            if win32api.GetFileAttributes(self.path) & win32con.FILE_ATTRIBUTE_READONLY:
                col = 5
        except win32api.error:
            pass
        return col

    def GetSubList(self):
        mod, path = pywin.framework.scriptutils.GetPackageModuleName(self.path)
        win32ui.SetStatusText("Building class list - please wait...", 1)
        win32ui.DoWaitCursor(1)
        try:
            try:
                reader = pyclbr.readmodule_ex  # Post 1.5.2 interface.
                extra_msg = " or functions"
            except AttributeError:
                reader = pyclbr.readmodule
                extra_msg = ""
            data = reader(mod, [path])
            if data:
                ret = []
                for item in data.values():
                    if (
                        item.__class__ != pyclbr.Class
                    ):  # ie, it is a pyclbr Function instance (only introduced post 1.5.2)
                        ret.append(HLICLBRFunction(item, " (function)"))
                    else:
                        ret.append(HLICLBRClass(item, " (class)"))
                ret.sort()
                return ret
            else:
                return [HLIErrorItem("No Python classes%s in module." % (extra_msg,))]
        finally:
            win32ui.DoWaitCursor(0)
            win32ui.SetStatusText(win32ui.LoadString(afxres.AFX_IDS_IDLEMESSAGE))


def MakePathSubList(path):
    ret = []
    for filename in glob.glob(os.path.join(path, "*")):
        if os.path.isdir(filename) and os.path.isfile(
            os.path.join(filename, "__init__.py")
        ):
            ret.append(HLIDirectoryItem(filename, os.path.split(filename)[1]))
        else:
            if os.path.splitext(filename)[1].lower() in [".py", ".pyw"]:
                ret.append(HLIModuleItem(filename))
    return ret


class HLIDirectoryItem(hierlist.HierListItem):
    def __init__(self, path, displayName=None, bSubDirs=0):
        hierlist.HierListItem.__init__(self)
        self.path = path
        self.bSubDirs = bSubDirs
        if displayName:
            self.displayName = displayName
        else:
            self.displayName = path

    def IsExpandable(self):
        return 1

    def GetText(self):
        return self.displayName

    def GetSubList(self):
        ret = MakePathSubList(self.path)
        if (
            os.path.split(self.path)[1] == "win32com"
        ):  # Complete and utter hack for win32com.
            try:
                path = win32api.GetFullPathName(
                    os.path.join(self.path, "..\\win32comext")
                )
                ret = ret + MakePathSubList(path)
            except win32ui.error:
                pass
        return ret


class HLIProjectRoot(hierlist.HierListItem):
    def __init__(self, projectName, displayName=None):
        hierlist.HierListItem.__init__(self)
        self.projectName = projectName
        self.displayName = displayName or projectName

    def GetText(self):
        return self.displayName

    def IsExpandable(self):
        return 1

    def GetSubList(self):
        paths = regutil.GetRegisteredNamedPath(self.projectName)
        pathList = paths.split(";")
        if len(pathList) == 1:  # Single dir - dont bother putting the dir in
            ret = MakePathSubList(pathList[0])
        else:
            ret = list(map(HLIDirectoryItem, pathList))
        return ret


class HLIRoot(hierlist.HierListItem):
    def __init__(self):
        hierlist.HierListItem.__init__(self)

    def IsExpandable(self):
        return 1

    def GetSubList(self):
        keyStr = regutil.BuildDefaultPythonKey() + "\\PythonPath"
        hKey = win32api.RegOpenKey(regutil.GetRootKey(), keyStr)
        try:
            ret = []
            ret.append(HLIProjectRoot("", "Standard Python Library"))  # The core path.
            index = 0
            while 1:
                try:
                    ret.append(HLIProjectRoot(win32api.RegEnumKey(hKey, index)))
                    index = index + 1
                except win32api.error:
                    break
            return ret
        finally:
            win32api.RegCloseKey(hKey)


class dynamic_browser(dialog.Dialog):
    style = win32con.WS_OVERLAPPEDWINDOW | win32con.WS_VISIBLE
    cs = (
        win32con.WS_CHILD
        | win32con.WS_VISIBLE
        | commctrl.TVS_HASLINES
        | commctrl.TVS_LINESATROOT
        | commctrl.TVS_HASBUTTONS
    )

    dt = [
        ["Python Projects", (0, 0, 200, 200), style, None, (8, "MS Sans Serif")],
        ["SysTreeView32", None, win32ui.IDC_LIST1, (0, 0, 200, 200), cs],
    ]

    def __init__(self, hli_root):
        dialog.Dialog.__init__(self, self.dt)
        self.hier_list = hierlist.HierListWithItems(hli_root, win32ui.IDB_BROWSER_HIER)
        self.HookMessage(self.on_size, win32con.WM_SIZE)

    def OnInitDialog(self):
        self.hier_list.HierInit(self)
        return dialog.Dialog.OnInitDialog(self)

    def on_size(self, params):
        lparam = params[3]
        w = win32api.LOWORD(lparam)
        h = win32api.HIWORD(lparam)
        self.GetDlgItem(win32ui.IDC_LIST1).MoveWindow((0, 0, w, h))


def BrowseDialog():
    root = HLIRoot()
    if not root.IsExpandable():
        raise TypeError(
            "Browse() argument must have __dict__ attribute, or be a Browser supported type"
        )

    dlg = dynamic_browser(root)
    dlg.CreateWindow()


def DockableBrowserCreator(parent):
    root = HLIRoot()
    hl = hierlist.HierListWithItems(root, win32ui.IDB_BROWSER_HIER)

    style = (
        win32con.WS_CHILD
        | win32con.WS_VISIBLE
        | win32con.WS_BORDER
        | commctrl.TVS_HASLINES
        | commctrl.TVS_LINESATROOT
        | commctrl.TVS_HASBUTTONS
    )

    control = win32ui.CreateTreeCtrl()
    control.CreateWindow(style, (0, 0, 150, 300), parent, win32ui.IDC_LIST1)
    list = hl.HierInit(parent, control)
    return control


def DockablePathBrowser():
    import pywin.docking.DockingBar

    bar = pywin.docking.DockingBar.DockingBar()
    bar.CreateWindow(
        win32ui.GetMainFrame(), DockableBrowserCreator, "Path Browser", 0x8E0A
    )
    bar.SetBarStyle(
        bar.GetBarStyle()
        | afxres.CBRS_TOOLTIPS
        | afxres.CBRS_FLYBY
        | afxres.CBRS_SIZE_DYNAMIC
    )
    bar.EnableDocking(afxres.CBRS_ALIGN_ANY)
    win32ui.GetMainFrame().DockControlBar(bar)


# The "default" entry point
Browse = DockablePathBrowser