1
1
# Code generator to generate code for everything contained in COM type
2
2
# libraries.
3
3
from __future__ import print_function
4
+ import ctypes
5
+ import keyword
6
+ import logging
4
7
import os
5
8
import sys
9
+ import textwrap
6
10
if sys .version_info >= (3 , 0 ):
7
11
import io
8
12
else :
9
13
import cStringIO as io
10
- import keyword
11
- import ctypes
12
- import textwrap
13
14
14
- from comtypes .tools import tlbparser , typedesc
15
15
import comtypes
16
+ from comtypes import TYPE_CHECKING
17
+ from comtypes .tools import tlbparser , typedesc
16
18
import comtypes .typeinfo
17
19
20
+ if TYPE_CHECKING :
21
+ from typing import List , Union as _UnionT
22
+
23
+
18
24
version = comtypes .__version__
19
25
20
- import logging
21
26
logger = logging .getLogger (__name__ )
22
27
23
28
__warn_on_munge__ = __debug__
@@ -343,14 +348,13 @@ def _to_docstring(self, orig, depth=1):
343
348
repled = orig .replace ("\\ " , r"\\" ).replace ("\" " , r"'" )
344
349
return '%s"""%s"""' % (indent , repled )
345
350
346
- _arraytypes = 0
347
351
def ArrayType (self , tp ):
348
- self . _arraytypes += 1
352
+ # type: (typedesc.ArrayType) -> None
349
353
self .generate (get_real_type (tp .typ ))
350
354
self .generate (tp .typ )
351
355
352
- _enumvalues = 0
353
356
def EnumValue (self , tp ):
357
+ # type: (typedesc.EnumValue) -> None
354
358
self .last_item_class = False
355
359
value = int (tp .value )
356
360
if keyword .iskeyword (tp .name ):
@@ -360,11 +364,9 @@ def EnumValue(self, tp):
360
364
tp_name = self ._to_type_name (tp )
361
365
print ("%s = %d" % (tp_name , value ), file = self .stream )
362
366
self .names .add (tp_name )
363
- self ._enumvalues += 1
364
367
365
- _enumtypes = 0
366
368
def Enumeration (self , tp ):
367
- self . _enumtypes += 1
369
+ # type: (typedesc.Enumeration) -> None
368
370
self .last_item_class = False
369
371
if tp .name :
370
372
print ("# values for enumeration '%s'" % tp .name , file = self .stream )
@@ -380,10 +382,9 @@ def Enumeration(self, tp):
380
382
print ("%s = c_int # enum" % tp .name , file = self .stream )
381
383
self .names .add (tp .name )
382
384
383
- _typedefs = 0
384
385
def Typedef (self , tp ):
385
- self . _typedefs += 1
386
- if type (tp .typ ) in (typedesc .Structure , typedesc .Union ):
386
+ # type: (typedesc.Typedef) -> None
387
+ if isinstance (tp .typ , (typedesc .Structure , typedesc .Union ) ):
387
388
self .generate (tp .typ .get_head ())
388
389
self .more .add (tp .typ )
389
390
else :
@@ -398,9 +399,11 @@ def Typedef(self, tp):
398
399
self .names .add (tp .name )
399
400
400
401
def FundamentalType (self , item ):
402
+ # type: (typedesc.FundamentalType) -> None
401
403
pass # we should check if this is known somewhere
402
404
403
405
def StructureHead (self , head ):
406
+ # type: (typedesc.StructureHead) -> None
404
407
for struct in head .struct .bases :
405
408
self .generate (struct .get_head ())
406
409
self .more .add (struct )
@@ -479,17 +482,20 @@ def StructureHead(self, head):
479
482
print (file = self .stream )
480
483
self .names .add (head .struct .name )
481
484
482
- _structures = 0
483
485
def Structure (self , struct ):
484
- self . _structures += 1
486
+ # type: (typedesc.Structure) -> None
485
487
self .generate (struct .get_head ())
486
488
self .generate (struct .get_body ())
487
489
488
- Union = Structure
490
+ def Union (self , union ):
491
+ # type: (typedesc.Union) -> None
492
+ self .generate (union .get_head ())
493
+ self .generate (union .get_body ())
489
494
490
495
def StructureBody (self , body ):
491
- fields = []
492
- methods = []
496
+ # type: (typedesc.StructureBody) -> None
497
+ fields = [] # type: List[typedesc.Field]
498
+ methods = [] # type: List[typedesc.Method]
493
499
for m in body .struct .members :
494
500
if type (m ) is typedesc .Field :
495
501
fields .append (m )
@@ -596,8 +602,7 @@ def StructureBody(self, body):
596
602
# top-level typedesc generators
597
603
#
598
604
def TypeLib (self , lib ):
599
- # lib.name, lib.gui, lib.major, lib.minor, lib.doc
600
-
605
+ # type: (typedesc.TypeLib) -> None
601
606
# Hm, in user code we have to write:
602
607
# class MyServer(COMObject, ...):
603
608
# _com_interfaces_ = [MyTypeLib.IInterface]
@@ -624,25 +629,27 @@ def TypeLib(self, lib):
624
629
print (file = self .stream )
625
630
626
631
def External (self , ext ):
632
+ # type: (typedesc.External) -> None
627
633
modname = name_wrapper_module (ext .tlib )
628
634
if modname not in self .imports :
629
635
self .externals .append (ext .tlib )
630
636
self .imports .add (modname )
631
637
632
638
def Constant (self , tp ):
639
+ # type: (typedesc.Constant) -> None
633
640
self .last_item_class = False
634
641
print ("%s = %r # Constant %s" % (tp .name ,
635
642
tp .value ,
636
643
self ._to_type_name (tp .typ )), file = self .stream )
637
644
self .names .add (tp .name )
638
645
639
646
def SAFEARRAYType (self , sa ):
647
+ # type: (typedesc.SAFEARRAYType) -> None
640
648
self .generate (sa .typ )
641
649
self .imports .add ("comtypes.automation" , "_midlSAFEARRAY" )
642
650
643
- _pointertypes = 0
644
651
def PointerType (self , tp ):
645
- self . _pointertypes += 1
652
+ # type: (typedesc.PointerType) -> None
646
653
if type (tp .typ ) is typedesc .ComInterface :
647
654
# this defines the class
648
655
self .generate (tp .typ .get_head ())
@@ -667,6 +674,7 @@ def PointerType(self, tp):
667
674
self .declarations .add ("WSTRING" , "c_wchar_p" )
668
675
669
676
def CoClass (self , coclass ):
677
+ # type: (typedesc.CoClass) -> None
670
678
self .imports .add ("comtypes" , "GUID" )
671
679
self .imports .add ("comtypes" , "CoClass" )
672
680
if not self .last_item_class :
@@ -719,11 +727,13 @@ def CoClass(self, coclass):
719
727
self .names .add (coclass .name )
720
728
721
729
def ComInterface (self , itf ):
730
+ # type: (typedesc.ComInterface) -> None
722
731
self .generate (itf .get_head ())
723
732
self .generate (itf .get_body ())
724
733
self .names .add (itf .name )
725
734
726
735
def _is_enuminterface (self , itf ):
736
+ # type: (typedesc.ComInterface) -> bool
727
737
# Check if this is an IEnumXXX interface
728
738
if not itf .name .startswith ("IEnum" ):
729
739
return False
@@ -734,6 +744,7 @@ def _is_enuminterface(self, itf):
734
744
return True
735
745
736
746
def ComInterfaceHead (self , head ):
747
+ # type: (typedesc.ComInterfaceHead) -> None
737
748
if head .itf .name in self .known_symbols :
738
749
return
739
750
base = head .itf .base
@@ -786,6 +797,7 @@ def ComInterfaceHead(self, head):
786
797
print (file = self .stream )
787
798
788
799
def ComInterfaceBody (self , body ):
800
+ # type: (typedesc.ComInterfaceBody) -> None
789
801
# The base class must be fully generated, including the
790
802
# _methods_ list.
791
803
self .generate (body .itf .base )
@@ -856,11 +868,13 @@ def ComInterfaceBody(self, body):
856
868
print ("#" , file = self .stream )
857
869
858
870
def DispInterface (self , itf ):
871
+ # type: (typedesc.DispInterface) -> None
859
872
self .generate (itf .get_head ())
860
873
self .generate (itf .get_body ())
861
874
self .names .add (itf .name )
862
875
863
876
def DispInterfaceHead (self , head ):
877
+ # type: (typedesc.DispInterfaceHead) -> None
864
878
self .generate (head .itf .base )
865
879
basename = self ._to_type_name (head .itf .base )
866
880
@@ -883,6 +897,7 @@ def DispInterfaceHead(self, head):
883
897
print (file = self .stream )
884
898
885
899
def DispInterfaceBody (self , body ):
900
+ # type: (typedesc.DispInterfaceBody) -> None
886
901
# make sure we can generate the body
887
902
for m in body .itf .members :
888
903
if isinstance (m , typedesc .DispMethod ):
@@ -913,6 +928,7 @@ def DispInterfaceBody(self, body):
913
928
# non-toplevel method generators
914
929
#
915
930
def make_ComMethod (self , m , isdual ):
931
+ # type: (typedesc.ComMethod, bool) -> None
916
932
self .imports .add ("comtypes" , "COMMETHOD" )
917
933
# typ, name, idlflags, default
918
934
if isdual :
@@ -1028,6 +1044,7 @@ def make_ComMethod(self, m, isdual):
1028
1044
print (" )," , file = self .stream )
1029
1045
1030
1046
def make_DispMethod (self , m ):
1047
+ # type: (typedesc.DispMethod) -> None
1031
1048
self .imports .add ("comtypes" , "DISPMETHOD" )
1032
1049
self .imports .add ("comtypes" , "dispid" )
1033
1050
idlflags = [dispid (m .dispid )] + m .idlflags
@@ -1094,6 +1111,7 @@ def make_DispMethod(self, m):
1094
1111
print (" )," , file = self .stream )
1095
1112
1096
1113
def make_DispProperty (self , prop ):
1114
+ # type: (typedesc.DispProperty) -> None
1097
1115
self .imports .add ("comtypes" , "DISPPROPERTY" )
1098
1116
self .imports .add ("comtypes" , "dispid" )
1099
1117
idlflags = [dispid (prop .dispid )] + prop .idlflags
0 commit comments