File: pi_generator.py

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (688 lines) | stat: -rw-r--r-- 28,237 bytes parent folder | download
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
#---------------------------------------------------------------------------
# Name:        etgtools/pi_generator.py
# Author:      Robin Dunn
#
# Created:     18-Oct-2011
# Copyright:   (c) 2011-2020 by Total Control Software
# License:     wxWindows License
#---------------------------------------------------------------------------

"""
This generator will create "Python Interface" files, which define a skeleton
version of the classes, functions, attributes, docstrings, etc. as Python
code. This is useful for enabling some introspection of things located in
extension modules where there is less information available for
introspection. The .pi files are used by WingIDE for assisting with code
completion, displaying docstrings in the source assistant panel, etc.

NOTE: PyCharm has a similar feature but the file extension is .pyi in that
case.  For now we'll just make a copy of the .pi file, but PyCharm also
supports Python 3.5 style type annotations in the interface files so we may
want to add some type info to that version of the file eventually...
"""

import sys, os, re
from typing import Optional, Union
import etgtools.extractors as extractors
import etgtools.generators as generators
from etgtools.generators import nci, Utf8EncodingStream, textfile_open
from etgtools.tweaker_tools import FixWxPrefix, magicMethods, \
                                   guessTypeInt, guessTypeFloat, guessTypeStr


phoenixRoot = os.path.abspath(os.path.split(__file__)[0]+'/..')

#---------------------------------------------------------------------------

SKIP_PI_FILE = True

header_pi = """\
# -*- coding: utf-8 -*-
#---------------------------------------------------------------------------
# This file is generated by wxPython's PI generator.  Do not edit by hand.
#
# The *.pi files are used by WingIDE to provide more information than it is
# able to glean from introspection of extension types and methods.  They are
# not intended to be imported, executed or used for any other purpose other
# than providing info to the IDE.  If you don't use WingIDE you can safely
# ignore this file.
#
# See: https://wingware.com/doc/edit/helping-wing-analyze-code
#
# Copyright: (c) 2020 by Total Control Software
# License:   wxWindows License
#---------------------------------------------------------------------------

"""

SKIP_PYI_FILE = False

header_pyi = """\
# -*- coding: utf-8 -*-
#---------------------------------------------------------------------------
# This file is generated by wxPython's PI generator.  Do not edit by hand.
#
# The *.pyi files are used by PyCharm and other development tools to provide
# more information, such as PEP 484 type hints, than it is able to glean from
# introspection of extension types and methods.  They are not intended to be
# imported, executed or used for any other purpose other than providing info
# to the tools. If you don't use use a tool that makes use of .pyi files then
# you can safely ignore this file.
#
# See: https://www.python.org/dev/peps/pep-0484/
#      https://www.jetbrains.com/help/pycharm/2016.1/type-hinting-in-pycharm.html
#
# Copyright: (c) 2020 by Total Control Software
# License:   wxWindows License
#---------------------------------------------------------------------------

"""

typing_imports = """\
from __future__ import annotations
from datetime import datetime, date
from enum import IntEnum, IntFlag, auto
from typing import (Any, overload, TypeAlias, Generic,
    Union, Optional, List, Tuple, Callable
)
try:
    from typing import ParamSpec
except ImportError:
    from typing_extensions import ParamSpec

_TwoInts: TypeAlias = Tuple[int, int]
_ThreeInts: TypeAlias = Tuple[int, int, int]
_FourInts: TypeAlias = Tuple[int, int, int, int]
_TwoFloats: TypeAlias = Tuple[float, float]
_FourFloats: TypeAlias = Tuple[float, float, float, float]

"""

#---------------------------------------------------------------------------

def piIgnored(obj):
    return getattr(obj, 'piIgnored', False)

#---------------------------------------------------------------------------

class PiWrapperGenerator(generators.WrapperGeneratorBase, FixWxPrefix):

    def generate(self, module, destFile=None):
        stream = Utf8EncodingStream()

        # process the module object and its child objects
        self.generateModule(module, stream)

        # Write the contents of the stream to the destination file
        if not destFile:
            name = module.module
            if name.startswith('_'):
                name = name[1:]
            destFile = os.path.join(phoenixRoot, 'wx', name)

        destFile_pi = destFile + '.pi'
        destFile_pyi = destFile + '.pyi'

        def _checkAndWriteHeader(destFile, header, docstring):
            if not os.path.exists(destFile):
                # create the file and write the header
                with textfile_open(destFile, 'wt') as f:
                    f.write(header)
                    if docstring:
                        f.write('\n"""\n%s"""\n' % docstring)

        if not SKIP_PI_FILE:
            _checkAndWriteHeader(destFile_pi, header_pi, module.docstring)
            self.writeSection(destFile_pi, 'typing-imports', typing_imports, at_end=False)
            self.writeSection(destFile_pi, module.name, stream.getvalue())

        if not SKIP_PYI_FILE:
            _checkAndWriteHeader(destFile_pyi, header_pyi, module.docstring)
            self.writeSection(destFile_pyi, 'typing-imports', typing_imports, at_end=False)
            self.writeSection(destFile_pyi, module.name, stream.getvalue())


    def writeSection(self, destFile, sectionName, sectionText, at_end = True):
        """
        Read all the lines from destFile, remove those currently between
        begin/end markers for sectionName (if any), and write the lines back
        to the file with the new text in sectionText.
        `at_end` determines where in the file the section is added when missing
        """
        sectionBeginLine = -1
        sectionEndLine = -1
        sectionBeginMarker = '#-- begin-%s --#' % sectionName
        sectionEndMarker = '#-- end-%s --#' % sectionName

        with textfile_open(destFile, 'rt') as fid:
            lines = fid.readlines()
        for idx, line in enumerate(lines):
            if line.startswith(sectionBeginMarker):
                sectionBeginLine = idx
            if line.startswith(sectionEndMarker):
                sectionEndLine = idx

        if sectionBeginLine == -1:
            if at_end:
                # not there already, add to the end
                lines.append(sectionBeginMarker + '\n')
                lines.append(sectionText)
                lines.append(sectionEndMarker + '\n')
            else:
                # not there already, add to the beginning
                # Skip the header
                idx = 0
                for idx, line in enumerate(lines):
                    if not line.startswith('#'):
                        break
                lines[idx+1:idx+1] = [
                    sectionBeginMarker + '\n',
                    sectionText,
                    sectionEndMarker + '\n',
                ]
        else:
            # replace the existing lines
            lines[sectionBeginLine+1:sectionEndLine] = [sectionText]

        with textfile_open(destFile, 'wt') as f:
            f.writelines(lines)

    #-----------------------------------------------------------------------
    def generateModule(self, module, stream):
        """
        Generate code for each of the top-level items in the module.
        """
        assert isinstance(module, extractors.ModuleDef)
        self.isCore = module.module == '_core'

        for item in module.imports:
            if item.startswith('_'):
                item = item[1:]
            if item == 'core':
                continue
            stream.write('import wx.%s\n' % item)

        # Move all PyCode items with an order value to the beginning of the
        # list as they most likely should appear before everything else.
        pycode = list()
        for item in module:
            if isinstance(item, extractors.PyCodeDef) and item.order is not None:
                pycode.append(item)
        for item in pycode:
            module.items.remove(item)
        module.items = pycode + module.items

        methodMap = {
            extractors.ClassDef         : self.generateClass,
            extractors.DefineDef        : self.generateDefine,
            extractors.FunctionDef      : self.generateFunction,
            extractors.EnumDef          : self.generateEnum,
            extractors.GlobalVarDef     : self.generateGlobalVar,
            extractors.TypedefDef       : self.generateTypedef,
            extractors.WigCode          : self.generateWigCode,
            extractors.PyCodeDef        : self.generatePyCode,
            extractors.PyFunctionDef    : self.generatePyFunction,
            extractors.PyClassDef       : self.generatePyClass,
            extractors.CppMethodDef     : self.generateCppMethod,
            extractors.CppMethodDef_sip : self.generateCppMethod_sip,
            }

        for item in module:
            if item.ignored or piIgnored(item):
                continue
            function = methodMap[item.__class__]
            function(item, stream)


    #-----------------------------------------------------------------------
    def generateEnum(self, enum, stream, indent=''):
        assert isinstance(enum, extractors.EnumDef)
        if enum.ignored or piIgnored(enum):
            return
        # These enum classes aren't actually accessible from the real wx
        # module, o we need to prepend _. But we want to make a type alias to
        # the non-prefixed name, so method signatures can reference it without
        # any special code, and also allow bare ints as inputs.
        if '@' in enum.name or not enum.name:
            # Anonymous enum
            enum_name = f"_enum_{enum.name.replace('@', '').strip()}"
            alias = ''
        else:
            alias = self.fixWxPrefix(enum.name)
            enum_name = f'_{alias}'
        if 'Flags' in enum_name:
            enum_type = 'IntFlag'
        else:
            enum_type = 'IntEnum'
        # Create the enum definition
        stream.write(f'\n{indent}class {enum_name}({enum_type}):\n')
        for v in enum.items:
            if v.ignored or piIgnored(v):
                continue
            name = v.pyName or v.name
            stream.write(f'{indent}    {name} = auto()\n')
        # Create the alias if needed
        if alias:
            stream.write(f'{indent}{alias}: TypeAlias = Union[{enum_name}, int]\n')
        # And bring the enum members into global scope.  We can't use
        # enum.global_enum for this because:
        #  1. It's only available on Python 3.11+
        #  2. FixWxPrefix wouldn't be able to pick up the names, since it's
        #     detecting based on AST parsing, not runtime changes (which
        #     enum.global_enum performs).
        for v in enum.items:
            if v.ignored or piIgnored(v):
                continue
            name = v.pyName or v.name
            stream.write(f'{indent}{name} = {enum_name}.{name}\n')

    #-----------------------------------------------------------------------
    def generateGlobalVar(self, globalVar, stream):
        assert isinstance(globalVar, extractors.GlobalVarDef)
        if globalVar.ignored or piIgnored(globalVar):
            return
        name = globalVar.pyName or globalVar.name
        valTyp = 'Any'
        if guessTypeInt(globalVar):
            valTyp = 'int'
        elif guessTypeFloat(globalVar):
            valTyp = 'float'
        elif guessTypeStr(globalVar):
            valTyp = 'str'
        elif globalVar.type:
            valTyp = self.cleanType(globalVar.type) or valTyp
        stream.write(f'{name}: {valTyp}\n')

    #-----------------------------------------------------------------------
    def generateDefine(self, define, stream):
        assert isinstance(define, extractors.DefineDef)
        if define.ignored or piIgnored(define):
            return
        # we're assuming that all #defines that are not ignored are integer or string values
        name = define.pyName or define.name
        if '"' in define.value:
            stream.write(f'{name}: str\n')
        elif define.value in ('true', 'false'):
            stream.write(f'{name}: bool\n')
        else:
            stream.write(f'{name}: int\n')

    #-----------------------------------------------------------------------
    def generateTypedef(self, typedef, stream, indent=''):
        assert isinstance(typedef, extractors.TypedefDef)
        if typedef.ignored or piIgnored(typedef):
            return

        # If it's not a template instantiation, or has not been flagged by
        # the tweaker script that it should be treated as a class, then just
        # ignore the typedef and return.
        if not ('<' in typedef.type and '>' in typedef.type) and not typedef.docAsClass:
            return

        # Otherwise write a mock class for it that combines the template and class.
        # First, extract the info we need.
        if typedef.docAsClass:
            bases = [self.fixWxPrefix(b, True) for b in typedef.bases]
            name = self.fixWxPrefix(typedef.name)

        elif '<' in typedef.type and '>' in typedef.type:
            t = typedef.type.replace('>', '')
            t = t.replace(' ', '')
            bases = t.split('<')
            bases = (self.fixWxPrefix(b, True) for b in bases)
            bases = [b.replace('*', '') for b in bases] # fix for RichTextLine*
            name = self.fixWxPrefix(typedef.name)

        # Now write the Python equivalent class for the typedef
        if not bases:
            bases = ['object']  # this should not happen, but just in case...
        stream.write('%sclass %s(%s):\n' % (indent, name, ', '.join(bases)))
        indent2 = indent + ' '*4
        if typedef.briefDoc:
            stream.write('%s"""\n' % indent2)
            stream.write(nci(typedef.briefDoc, len(indent2)))
            stream.write('%s"""\n' % indent2)
        else:
            stream.write('%spass\n\n' % indent2)


    #-----------------------------------------------------------------------
    def generateWigCode(self, wig, stream, indent=''):
        assert isinstance(wig, extractors.WigCode)
        # write nothing for this one


    #-----------------------------------------------------------------------
    def generatePyCode(self, pc, stream, indent=''):
        assert isinstance(pc, extractors.PyCodeDef)
        code = pc.code
        if hasattr(pc, 'klass'):
            code = code.replace(pc.klass.pyName+'.', '')
        stream.write('\n')
        stream.write(nci(code, len(indent)))

    #-----------------------------------------------------------------------
    def generatePyFunction(self, pf, stream, indent=''):
        assert isinstance(pf, extractors.PyFunctionDef)
        stream.write('\n')
        if pf.deprecated:
            stream.write('%s@wx.deprecated\n' % indent)
        if pf.isStatic:
            stream.write('%s@staticmethod\n' % indent)
        stream.write('%sdef %s%s:\n' % (indent, pf.name, pf.argsString))
        indent2 = indent + ' '*4
        if pf.briefDoc:
            stream.write('%s"""\n' % indent2)
            stream.write(nci(pf.briefDoc, len(indent2)))
            stream.write('%s"""\n' % indent2)
        stream.write('%spass\n' % indent2)

    #-----------------------------------------------------------------------
    def generatePyClass(self, pc, stream, indent=''):
        assert isinstance(pc, extractors.PyClassDef)

        # write the class declaration and docstring
        if pc.deprecated:
            stream.write('%s@wx.deprecated\n' % indent)
        stream.write('%sclass %s' % (indent, pc.name))
        if pc.bases:
            stream.write('(%s):\n' % ', '.join(pc.bases))
        else:
            stream.write(':\n')
        indent2 = indent + ' '*4
        if pc.briefDoc:
            stream.write('%s"""\n' % indent2)
            stream.write(nci(pc.briefDoc, len(indent2)))
            stream.write('%s"""\n' % indent2)

        # these are the only kinds of items allowed to be items in a PyClass
        dispatch = {
            extractors.PyFunctionDef    : self.generatePyFunction,
            extractors.PyPropertyDef    : lambda a,b,c: self.generatePyProperty(pc, a, b, c),
            extractors.PyCodeDef        : self.generatePyCode,
            extractors.PyClassDef       : self.generatePyClass,
        }
        for item in pc.items:
            item.klass = pc
            f = dispatch[item.__class__]
            f(item, stream, indent2)



    #-----------------------------------------------------------------------
    def generateFunction(self, function, stream, is_overload=False):
        assert isinstance(function, extractors.FunctionDef)
        if not function.pyName:
            return
        if not is_overload and function.hasOverloads():
            for f in function.overloads:
                self.generateFunction(f, stream, True)
            stream.write('\n@overload')
        elif is_overload:
            stream.write('\n@overload')
        if not function.signature:
            function.makePyArgsString()
        assert function.signature is not None
        for line in function.signature.definition_lines():
            stream.write(f'\n{line}')
        if is_overload:
            stream.write('    ...\n')
        else:
            stream.write('    """\n')
            stream.write(nci(function.pyDocstring, 4))
            stream.write('    """\n')


    def generateParameters(self, parameters, stream, indent):
        def _lastParameter(idx):
            if idx == len(parameters)-1:
                return True
            for i in range(idx+1, len(parameters)):
                if not (parameters[i].ignored or piIgnored(parameters[i])):
                    return False
            return True

        for idx, param in enumerate(parameters):
            if param.ignored or piIgnored(param):
                continue
            stream.write(param.name)
            if param.default:
                stream.write('=%s' % param.default)
            if not _lastParameter(idx):
                stream.write(', ')


    #-----------------------------------------------------------------------
    def generateClass(self, klass, stream, indent=''):
        assert isinstance(klass, extractors.ClassDef)
        if klass.ignored or piIgnored(klass):
            return

        # check if there is a pi-customized version of the base class names
        if hasattr(klass, 'piBases'):
            bases = klass.piBases

        else:
            # check if it's a template with the template parameter as the base class
            bases = klass.bases[:]
            for tp in klass.templateParams:
                if tp in bases:
                    bases.remove(tp)

        # write class declaration
        klassName = klass.pyName or klass.name
        stream.write('\n%sclass %s' % (indent, klassName))
        if bases:
            stream.write('(')
            bases = [self.fixWxPrefix(b, True) for b in bases]
            stream.write(', '.join(bases))
            stream.write(')')
        stream.write(':\n')
        indent2 = indent + ' '*4

        # docstring
        stream.write('%s"""\n' % indent2)
        stream.write(nci(klass.pyDocstring, len(indent2)))
        stream.write('%s"""\n' % indent2)

        # generate nested classes
        for item in klass.innerclasses:
            self.generateClass(item, stream, indent2)

        # Split the items into public and protected groups
        enums = [i for i in klass if
                     isinstance(i, extractors.EnumDef) and
                     i.protection == 'public']
        ctors = [i for i in klass if
                     isinstance(i, extractors.MethodDef) and
                     i.protection == 'public' and (i.isCtor or i.isDtor)]
        public = [i for i in klass if i.protection == 'public' and
                     i not in ctors and i not in enums]
        protected = [i for i in klass if i.protection == 'protected']

        dispatch = {
            extractors.MemberVarDef     : self.generateMemberVar,
            extractors.TypedefDef       : lambda a,b,c: None,
            extractors.PropertyDef      : lambda a,b,c: self.generateProperty(klass, a, b, c),
            extractors.PyPropertyDef    : lambda a,b,c: self.generatePyProperty(klass, a, b, c),
            extractors.MethodDef        : self.generateMethod,
            extractors.EnumDef          : self.generateEnum,
            extractors.CppMethodDef     : self.generateCppMethod,
            extractors.CppMethodDef_sip : self.generateCppMethod_sip,
            extractors.PyMethodDef      : self.generatePyMethod,
            extractors.PyCodeDef        : self.generatePyCode,
            extractors.WigCode          : self.generateWigCode,
            }

        for item in enums:
            item.klass = klass
            self.generateEnum(item, stream, indent2)

        for item in ctors:
            if item.isCtor:
                item.klass = klass
                self.generateMethod(item, stream, indent2,
                                    name='__init__', docstring=klass.pyDocstring,
                                    is_top_level_init=klass.is_top_level())

        for item in public:
            item.klass = klass
            f = dispatch[item.__class__]
            f(item, stream, indent2)

        for item in protected:
            item.klass = klass
            f = dispatch[item.__class__]
            f(item, stream, indent2)

        stream.write('%s# end of class %s\n\n' % (indent, klassName))


    def find_method(self, klass: extractors.ClassDef, method_name: str) -> Optional[extractors.MethodDef]:
        methods = (i for i in klass if isinstance(i, extractors.MethodDef) and not i.isCtor and not i.isDtor)
        for method in methods:
            name = method.name or method.pyName
            if name == method_name:
                return method
        return None


    def generateMemberVar(self, memberVar, stream, indent):
        assert isinstance(memberVar, extractors.MemberVarDef)
        if memberVar.ignored or piIgnored(memberVar):
            return
        member_type = memberVar.type
        if member_type:
            member_type = self.cleanType(member_type)
        if not member_type: # Unknown type for the member variable
            member_type = 'Any'
        stream.write(f'{indent}{memberVar.name}: {member_type}\n')


    def generateProperty(self, klass, prop, stream, indent):
        assert isinstance(prop, extractors.PropertyDef)
        self._generateProperty(klass, prop, stream, indent)


    def generatePyProperty(self, klass, prop, stream, indent):
        assert isinstance(prop, extractors.PyPropertyDef)
        self._generateProperty(klass, prop, stream, indent)

    def _generateProperty(self, klass: extractors.ClassDef, prop: Union[extractors.PyPropertyDef, extractors.PropertyDef], stream, indent: str):
        if prop.ignored or piIgnored(prop):
            return
        value_type = ''
        if prop.getter:
            getter = self.find_method(klass, prop.getter)
            if getter and getter.signature:
                value_type = getter.signature.return_type
        if prop.setter:
            setter = self.find_method(klass, prop.setter)
            if setter and setter.signature:
                value_type = setter.signature[0].type_hint
        if prop.setter and prop.getter:
            if value_type:
                stream.write(f'{indent}@property\n')
                stream.write(f'{indent}def {prop.name}(self) -> {value_type}: ...\n')
                stream.write(f'{indent}@{prop.name}.setter\n')
                stream.write(f'{indent}def {prop.name}(self, value: {value_type}, /) -> None: ...\n')
            else:
                stream.write(f'{indent}{prop.name} = property({prop.getter}, {prop.setter})\n')
        elif prop.getter:
            if value_type:
                stream.write(f'{indent}@property\n')
                stream.write(f'{indent}def {prop.name}(self) -> {value_type}: ...\n')
            else:
                stream.write(f'{indent}{prop.name} = property({prop.getter})\n')
        elif prop.setter:
            # Can't use the decorator syntax in this situation
            stream.write(f'{indent}{prop.name} = property(fset={prop.setter})\n') 


    def generateMethod(self, method, stream, indent, name=None, docstring=None, is_overload=False, is_top_level_init=False):
        """Write the python declaration for a method (type-stub or otherwise):
        method: MethodDef holding information about the method
        stream: output stream to write to
        indent: indentation level to use when writing
        name: name of the method, if wanting to override what is identified in `method`
        docstring: docstring to use, if wanting to override what is in method.pyDocString
        is_overload: If this declaration should be marked with `@typing.overload`
        is_top_level_init: If this class is a subclass of wx.TopLevelWindow and is an __init__ method, to apply the
            transformation `parent: <WindowType>` -> `parent: Optional[<WindowType>]`, because TopLevelWindow
            allows for a `None` parent.
        """
        assert isinstance(method, extractors.MethodDef)
        for m in method.all():  # use the first not ignored if there are overloads
            if not m.ignored or piIgnored(m):
                method = m
                break
        else:
            return
        if method.isDtor:
            return

        # write the method declaration
        if not is_overload and method.hasOverloads():
            for m in method.overloads:
                self.generateMethod(m, stream, indent, name, None, True, is_top_level_init)
            stream.write(f'\n{indent}@overload')
        elif is_overload:
            stream.write(f'\n{indent}@overload')
        if not method.signature:
            method.makePyArgsString()
        assert method.signature is not None
        if name is not None:
            method.signature.method_name = name
        if is_top_level_init and 'parent' in method.signature:
            method.signature['parent'].make_optional()
        for line in method.signature.definition_lines():
            stream.write(f'\n{indent}{line}')
        stream.write('\n')
        indent2 = indent + ' '*4

        # docstring
        if is_overload:
            stream.write(f'{indent2}...\n')
        else:
            if not docstring:
                if hasattr(method, 'pyDocstring'):
                    docstring = method.pyDocstring
                else:
                    docstring = ""
            stream.write('%s"""\n' % indent2)
            if docstring.strip():
                stream.write(nci(docstring, len(indent2)))
            stream.write('%s"""\n' % indent2)



    def generateCppMethod(self, method, stream, indent=''):
        assert isinstance(method, extractors.CppMethodDef)
        self.generateMethod(method, stream, indent)


    def generateCppMethod_sip(self, method, stream, indent=''):
        assert isinstance(method, extractors.CppMethodDef_sip)
        self.generateMethod(method, stream, indent)


    def generatePyMethod(self, pm, stream, indent):
        assert isinstance(pm, extractors.PyMethodDef)
        if pm.ignored or piIgnored(pm):
            return
        if pm.isStatic:
            stream.write('\n%s@staticmethod' % indent)
        stream.write('\n%sdef %s' % (indent, pm.name))
        stream.write(getattr(pm, 'piArgsString', pm.argsString))
        stream.write(':\n')
        indent2 = indent + ' '*4

        stream.write('%s"""\n' % indent2)
        stream.write(nci(pm.pyDocstring, len(indent2)))
        stream.write('%s"""\n' % indent2)




#---------------------------------------------------------------------------
#---------------------------------------------------------------------------