-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgirepo.py
More file actions
969 lines (704 loc) · 37 KB
/
girepo.py
File metadata and controls
969 lines (704 loc) · 37 KB
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
# functions to access class structure members using ctypes
# for gobject introspection
from __future__ import print_function
import sys
import os
import pdb
import gi
import gi.repository
from gi.repository import GObject
gi.require_version('GIRepository', '2.0')
from gi.repository import GIRepository
import ctypes
from ctypes.util import find_library
libgirepositorynm = find_library("libgirepository")
libgirepository = ctypes.CDLL(libgirepositorynm)
class GITypeTag(object):
GI_TYPE_TAG_VOID = 0
GI_TYPE_TAG_BOOLEAN = 1
GI_TYPE_TAG_INT8 = 2
GI_TYPE_TAG_UINT8 = 3
GI_TYPE_TAG_INT16 = 4
GI_TYPE_TAG_UINT16 = 5
GI_TYPE_TAG_INT32 = 6
GI_TYPE_TAG_UINT32 = 7
GI_TYPE_TAG_INT64 = 8
GI_TYPE_TAG_UINT64 = 9
GI_TYPE_TAG_FLOAT = 10
GI_TYPE_TAG_DOUBLE = 11
GI_TYPE_TAG_GTYPE = 12
GI_TYPE_TAG_UTF8 = 13
GI_TYPE_TAG_FILENAME = 14
# /* Non-basic types; compare with G_TYPE_TAG_IS_BASIC */
GI_TYPE_TAG_ARRAY = 15
GI_TYPE_TAG_INTERFACE = 16
GI_TYPE_TAG_GLIST = 17
GI_TYPE_TAG_GSLIST = 18
GI_TYPE_TAG_GHASH = 19
GI_TYPE_TAG_ERROR = 20
# /* Another basic type */
GI_TYPE_TAG_UNICHAR = 21
# /* Note - there is currently only room for 32 tags */
# special tag for potiner
GI_TYPE_TAG_POINTER = 31
class GIArrayType(object):
GI_ARRAY_TYPE_C = 0
GI_ARRAY_TYPE_ARRAY = 1
GI_ARRAY_TYPE_PTR_ARRAY = 2
GI_ARRAY_TYPE_BYTE_ARRAY = 3
class GIInfoType(object):
GI_INFO_TYPE_INVALID = 0
GI_INFO_TYPE_FUNCTION = 1
GI_INFO_TYPE_CALLBACK = 2
GI_INFO_TYPE_STRUCT = 3
GI_INFO_TYPE_BOXED = 4
GI_INFO_TYPE_ENUM = 5 # /* 5 */
GI_INFO_TYPE_FLAGS = 6
GI_INFO_TYPE_OBJECT = 7
GI_INFO_TYPE_INTERFACE = 8
GI_INFO_TYPE_CONSTANT = 9
GI_INFO_TYPE_INVALID_0 = 10 # /* 10 */
GI_INFO_TYPE_UNION = 11
GI_INFO_TYPE_VALUE = 12
GI_INFO_TYPE_SIGNAL = 13
GI_INFO_TYPE_VFUNC = 14
GI_INFO_TYPE_PROPERTY = 15 # /* 15 */
GI_INFO_TYPE_FIELD = 16
GI_INFO_TYPE_ARG = 17
GI_INFO_TYPE_TYPE = 18
GI_INFO_TYPE_UNRESOLVED = 19
class GIFieldInfoOpaque(ctypes.Structure):
pass
libgirepository.g_field_info_get_offset.argtypes = [ ctypes.c_void_p ]
#libgirepository.g_field_info_get_offset.argtypes = [ ctypes.POINTER(GIFieldInfoOpaque) ]
libgirepository.g_field_info_get_offset.restype = ctypes.c_int
libgirepository.g_field_info_get_type.argtypes = [ ctypes.c_void_p ]
#libgirepository.g_field_info_get_type.argtypes = [ ctypes.POINTER(GIFieldInfoOpaque) ]
libgirepository.g_field_info_get_type.restype = ctypes.c_void_p
class GITypeInfoOpaque(ctypes.Structure):
pass
libgirepository.g_type_info_get_tag.argtypes = [ ctypes.c_void_p ]
#libgirepository.g_type_info_get_tag.argtypes = [ ctypes.POINTER(GITypeInfoOpaque) ]
libgirepository.g_type_info_get_tag.restype = ctypes.c_uint
libgirepository.g_type_info_get_array_type.argtypes = [ ctypes.c_void_p ]
#libgirepository.g_type_info_get_array_type.argtypes = [ ctypes.POINTER(GITypeInfoOpaque) ]
libgirepository.g_type_info_get_array_type.restype = ctypes.c_uint
libgirepository.g_type_info_is_pointer.argtypes = [ ctypes.c_void_p ]
#libgirepository.g_type_info_is_pointer.argtypes = [ ctypes.POINTER(GITypeInfoOpaque) ]
libgirepository.g_type_info_is_pointer.restype = ctypes.c_uint
class GIBaseInfoOpaque(ctypes.Structure):
pass
libgirepository.g_type_info_get_interface.argtypes = [ ctypes.c_void_p ]
#libgirepository.g_type_info_get_interface.argtypes = [ ctypes.POINTER(GITypeInfoOpaque) ]
libgirepository.g_type_info_get_interface.restype = ctypes.c_void_p
#libgirepository.g_type_info_get_interface.restype = ctypes.POINTER(GIBaseInfoOpaque)
libgirepository.g_base_info_get_type.argtypes = [ ctypes.c_void_p ]
#libgirepository.g_base_info_get_type.argtypes = [ ctypes.POINTER(GIBaseInfoOpaque) ]
libgirepository.g_base_info_get_type.restype = ctypes.c_uint
class GIStructInfoOpaque(ctypes.Structure):
pass
#libgirepository.g_struct_info_set_size.argtypes = [ ctypes.c_void_p ]
##libgirepository.g_struct_info_set_size.argtypes = [ ctypes.POINTER(GIStructInfoOpaque) ]
#libgirepository.g_struct_info_set_size.restype = ctypes.c_uint
class GIUnionInfoOpaque(ctypes.Structure):
pass
#libgirepository.g_union_info_set_size.argtypes = [ ctypes.c_void_p ]
##libgirepository.g_union_info_set_size.argtypes = [ ctypes.POINTER(GIUnionInfoOpaque) ]
#libgirepository.g_union_info_set_size.restype = ctypes.c_uint
def get_field_info (field_info):
fieldinfo_ptr = id(field_info)
#print("fieldinfo 0x%x"%fieldinfo_ptr)
pygibaseinfo_ptr = ctypes.cast( fieldinfo_ptr, ctypes.POINTER(PyGIBaseInfo) )
#print("baseinfo ptr 0x%x"%pygibaseinfo_ptr)
#print("baseinfo ptr 0x%x"%ctypes.addressof(pygibaseinfo_ptr.contents))
#print("type %x"%pygibaseinfo_ptr.contents.ob_type)
#print("info %x"%pygibaseinfo_ptr.contents.info)
return pygibaseinfo_ptr.contents.info
def get_field_type_tag (field_info_obj):
type_info_ptr = libgirepository.g_field_info_get_type(field_info_obj)
#print("type ptr %x"%type_info_ptr)
chkptr = libgirepository.g_type_info_is_pointer(type_info_ptr)
#pdb.set_trace()
# what to do about pointer type??
# make a special type - hopefully not found in C!!
# WE DO NOT HANDLE THIS RIGHT YET!!
#if chkptr:
# field_type_tag = GITypeTag.GI_TYPE_TAG_POINTER
#else:
if True:
field_type_tag = libgirepository.g_type_info_get_tag(type_info_ptr)
#print("type tag %d"%field_type_tag)
if field_type_tag == GITypeTag.GI_TYPE_TAG_INTERFACE:
field_interface_ptr = libgirepository.g_type_info_get_interface(type_info_ptr)
info_type = libgirepository.g_base_info_get_type(field_interface_ptr)
print("info type %d"%info_type)
field_type_tag = GITypeTag.GI_TYPE_TAG_INTERFACE*1000 + info_type
return field_type_tag
# these are internal definitions - only use if really need to!!
class GIRealInfo(ctypes.Structure):
_fields_ = [ ("type", ctypes.c_int),
("ref_count", ctypes.c_int),
("repository", ctypes.c_void_p),
("container", ctypes.c_void_p),
("typelib", ctypes.c_void_p),
("offset", ctypes.c_uint),
("type_is_embedded", ctypes.c_uint),
("reserved2", ctypes.c_void_p*4),
]
class GIUnresolvedInfo(ctypes.Structure):
_fields_ = [ ("type", ctypes.c_int),
("ref_count", ctypes.c_int),
("repository", ctypes.c_void_p),
("container", ctypes.c_void_p),
("name", ctypes.c_char_p),
("namespace", ctypes.c_char_p),
]
# unfortunately I cant see any way to get round defining a structure
# type to get the info pointer
class PyGIBaseInfo(ctypes.Structure):
_fields_ = [ ("ob_refcnt", ctypes.c_void_p),
("ob_type", ctypes.c_void_p),
("info", ctypes.c_void_p),
]
# well its problematic to store/access string pointers
# this should get us access to the string address which we can stuff in
ctypes.pythonapi.PyUnicode_AsUTF8.argtypes = (ctypes.py_object,)
ctypes.pythonapi.PyUnicode_AsUTF8.restype = ctypes.c_void_p
ctypes.pythonapi.PyUnicode_FromString.argtypes = (ctypes.c_void_p,)
ctypes.pythonapi.PyUnicode_FromString.restype = ctypes.py_object
# now getting some understanding of metaclasses
# - they may be useful here
# define a metaclass object which is used to modify creation of class objects
class GirMeta(gi.types.GObjectMeta):
def __new__ (mcls, classname, bases, attrs):
# code to operate on the arguments
if not '__girmetaclass__' in attrs:
raise AttributeError("__girmetaclass__ attribute MUST be defined")
# it appears we cannot use other functions defined in the Meta class
# - claims need an instance as first argument - mcls is not an instance
# (we could of course define functions inside functions I suppose)
#flds = attrs['__girmetaclass__'].__dict__['__info__'].get_fields()
pdb.set_trace()
# now generate class
# do we need to use the parent meta class new here?? or always type??
# or should we be using init??
#return type.__new__(mcls, classname, bases, attrs)
return gi.types.GObjectMeta.__new__(mcls, classname, bases, attrs)
# I think we need an explicit Meta class for the Plugin
# - we need to fixup the plugin_name - and be careful not to override
# virtual functions - how to detect??
# OK so understand difference between __new__ and __init__
# __new__ - at this point the class has not yet been created - the argument
# is the metaclass
# __init__ - by this point the class has been created so the first argument
# is the new class - ALMOST!!
# NOTA BENE - the new class not fully created till AFTER the
# class super __init__ call!!!
# this is VERY important when subclassing Gir wrapped GTypes!!
# this is getting more confusing - it looks as though we cannot change
# the attributes at the init stage
# so we need to fixup the attributes in the __new__ phase
# but we cannot set the class pointer till the init phase!!
# because we interpose a Python wrapping class we need 2 metaclass objects
# GncPluginMeta is used in the GncPluginPython definition to define
# access to the C private data - we only define the __new__ function
# so Im still confused about subclassing a class with a __metaclass__
# - the subclass still seems to call the parent __metaclass__ __new__ if the subclass
# has no __metaclass__
# I thought we define the python class variables we need in the parent class
# - but if we have done that we dont need to redo this in the subclass (the parent
# class is now defined with the updated class variables)
# problem is if we forget to add the __metaclass__ in the subclass the parent
# __metaclass__ is called - how to detect this??
# also means in the subclass __metaclass__ we dont call the parent __new__
# - again would just redo whats been done
# should we just ignore the parent metaclass and only update the parent class
# variables when define the subclass - this just does not seem right??
# so it appears that making a python subclass of a python subclass of the GncPlugin GObject
# we will always have to subclass the GncPlugin GObject directly
# - for common code we make the current python subclass a mixin class
class GncPluginMeta(gi.types.GObjectMeta):
def __new__ (mcls, classname, bases, attrs):
# code to operate on the arguments
print("GncPluginMeta new called:",str(mcls),classname,str(bases),str(attrs))
#pdb.set_trace()
if not '__girmetaclass__' in attrs:
pdb.set_trace()
# NOTA BENE - the correct order for multiple classes is base class last
if len(bases) > 1:
raise AttributeError("__metaclass__ multiple bases not implemented")
if isinstance(bases[-1], GncPluginMeta):
raise AttributeError("__metaclass__ class attribute MUST be defined")
raise AttributeError("__girmetaclass__ class attribute MUST be defined")
# it appears we cannot use other functions defined in the Meta class
# - claims need an instance as first argument - mcls is not an instance
# (we could of course define functions inside functions I suppose)
# Im going to skip virtual functions(callback type - any others??)
flds = attrs['__girmetaclass__'].__dict__['__info__'].get_fields()
newattr = {}
for fldobj in flds:
if fldobj.get_name() in attrs:
clsfldobj = GObjectField(fldobj, initial_value=attrs[fldobj.get_name()])
if hasattr(clsfldobj,'get_value'):
newattr[fldobj.get_name()] = clsfldobj
else:
clsfldobj = GObjectField(fldobj)
if hasattr(clsfldobj,'get_value'):
newattr[fldobj.get_name()] = clsfldobj
if not 'plugin_name' in attrs:
raise AttributeError("plugin_name class attribute MUST be defined")
#pdb.set_trace()
attrs['ClassVariables'] = GObjectClass(newattr.items())
attrs['plugin_name'] = newattr['plugin_name']
# now generate class
# do we need to use the parent meta class new here?? or always type??
# or should we be using init??
#return type.__new__(mcls, classname, bases, attrs)
return super(GncPluginMeta, mcls).__new__(mcls,classname, bases, attrs)
#return gi.types.GObjectMeta.__new__(mcls, classname, bases, attrs)
def __init__ (cls, name, bases, attrs):
print("GncPluginMeta init called:",str(cls),name,str(bases),str(attrs))
# NOTA BENE - for GType subclassing the class structure address returned at this point
# is STILL the parent GType class structure!!
# we need the cls variable AFTER the super call!!
# so this is how to get the address of the class structure!!
print("python meta gtype klass %s address %x"%(str(cls),hash(GObject.type_class_peek(cls))))
super(GncPluginMeta, cls).__init__(name, bases, attrs)
print("python meta gtype klass %s address %x"%(str(cls),hash(GObject.type_class_peek(cls))))
#pdb.set_trace()
# we delay this till subclass for subclass of subclass
## we have deleted plugin_name from attrs above!!
#cls.plugin_name.set_klass_pointer(cls)
#plugin_name = cls.plugin_name.field_value
#print("python gtype plugin_name",plugin_name)
#cls.plugin_name.set_value(plugin_name)
# and GncPluginSubClassMeta is used in a subclass of GncPluginPython to actually set the
# variables - we need __new__ here as we need to substitute the initial definition
class GncPluginSubClassMeta(GncPluginMeta):
def __new__ (mcls, classname, bases, attrs):
print("GncPluginSubClassMeta new called:",str(mcls),classname,str(bases),str(attrs))
#pdb.set_trace()
if len(bases) > 1:
raise AttributeError("__metaclass__ multiple bases not implemented")
if not 'plugin_name' in attrs:
raise AttributeError("plugin_name not defined for subclass - must be defined")
# this assumes no multiple bases - base[-1] seems to be immediate superclass
bases[-1].plugin_name.set_field_value(attrs['plugin_name'])
# we need to delete this from attrs otherwise the new plain string definition
# overrides the superclass (GncPluginPython) definition
del attrs['plugin_name']
# well doing GncPluginMeta.__new__ does call the supermetaclass __new__
# - but this has already been done - whats the right way??
# just use type call??
#return GncPluginMeta.__new__(mcls, classname, bases, attrs)
#return type.__new__(mcls, classname, bases, attrs)
return gi.types.GObjectMeta.__new__(mcls, classname, bases, attrs)
def __init__ (cls, name, bases, attrs):
print("GncPluginSubClassMeta init called:",str(cls),name,str(bases),str(attrs))
# NOTA BENE - for GType subclassing the class structure address returned at this point
# is STILL the parent GType class structure!!
# we need the cls variable AFTER the super call!!
# so this is how to get the address of the class structure!!
print("python sub gtype klass %s address %x"%(str(cls),hash(GObject.type_class_peek(cls))))
super(GncPluginSubClassMeta, cls).__init__(name, bases, attrs)
print("python sub gtype klass %s address %x"%(str(cls),hash(GObject.type_class_peek(cls))))
#pdb.set_trace()
# we have deleted plugin_name from attrs above!!
cls.plugin_name.set_klass_pointer(cls)
plugin_name = cls.plugin_name.field_value
cls.plugin_name.set_value(plugin_name)
class GncPluginTryMeta(gi.types.GObjectMeta):
def __new__ (mcls, classname, bases, attrs):
# code to operate on the arguments
print("GncPluginTryMeta new called:",str(mcls),classname,str(bases),str(attrs))
return super(GncPluginTryMeta, mcls).__new__(mcls,classname, bases, attrs)
#return gi.types.GObjectMeta.__new__(mcls, classname, bases, attrs)
def __init__ (cls, name, bases, attrs):
print("GncPluginTryMeta init called:",str(cls),name,str(bases),str(attrs))
print("python meta gtype klass %s address %x"%(str(cls),hash(GObject.type_class_peek(cls))))
super(GncPluginTryMeta, cls).__init__(name, bases, attrs)
print("python meta gtype klass %s address %x"%(str(cls),hash(GObject.type_class_peek(cls))))
# this is for the GncPluginPage class - essentially a duplicate of GncPluginMeta
class GncPluginPageMeta(gi.types.GObjectMeta):
def __new__ (mcls, classname, bases, attrs):
# code to operate on the arguments
print("GncPluginPageMeta new called:",str(mcls),classname,str(bases),str(attrs))
#pdb.set_trace()
if not '__girmetaclass__' in attrs:
#pdb.set_trace()
# NOTA BENE - the correct order for multiple classes is base class last
if len(bases) > 1:
raise AttributeError("__metaclass__ multiple bases not implemented")
if isinstance(bases[-1], GncPluginPageMeta):
raise AttributeError("__metaclass__ class attribute MUST be defined")
raise AttributeError("__girmetaclass__ class attribute MUST be defined")
# it appears we cannot use other functions defined in the Meta class
# - claims need an instance as first argument - mcls is not an instance
# (we could of course define functions inside functions I suppose)
# Im going to skip virtual functions(callback type - any others??)
flds = attrs['__girmetaclass__'].__dict__['__info__'].get_fields()
newattr = {}
for fldobj in flds:
if fldobj.get_name() in attrs:
clsfldobj = GObjectField(fldobj, initial_value=attrs[fldobj.get_name()])
if hasattr(clsfldobj,'get_value'):
newattr[fldobj.get_name()] = clsfldobj
else:
clsfldobj = GObjectField(fldobj)
if hasattr(clsfldobj,'get_value'):
newattr[fldobj.get_name()] = clsfldobj
if not 'plugin_name' in attrs:
raise AttributeError("plugin_name class attribute MUST be defined")
if not 'tab_icon' in attrs:
raise AttributeError("tab_icon class attribute MUST be defined")
#pdb.set_trace()
attrs['ClassVariables'] = GObjectClass(newattr.items())
attrs['plugin_name'] = newattr['plugin_name']
attrs['tab_icon'] = newattr['tab_icon']
# now generate class
# do we need to use the parent meta class new here?? or always type??
# or should we be using init??
#return type.__new__(mcls, classname, bases, attrs)
return super(GncPluginPageMeta, mcls).__new__(mcls,classname, bases, attrs)
#return gi.types.GObjectMeta.__new__(mcls, classname, bases, attrs)
def __init__ (cls, name, bases, attrs):
print("GncPluginPageMeta init called:",str(cls),name,str(bases),str(attrs))
# NOTA BENE - for GType subclassing the class structure address returned at this point
# is STILL the parent GType class structure!!
# we need the cls variable AFTER the super call!!
# so this is how to get the address of the class structure!!
print("python meta gtype klass %s address %x"%(str(cls),hash(GObject.type_class_peek(cls))))
super(GncPluginPageMeta, cls).__init__(name, bases, attrs)
print("python meta gtype klass %s address %x"%(str(cls),hash(GObject.type_class_peek(cls))))
#pdb.set_trace()
# we delay this till subclass for subclass of subclass
## we have deleted plugin_name from attrs above!!
#cls.plugin_name.set_klass_pointer(cls)
#plugin_name = cls.plugin_name.field_value
#print("python gtype plugin_name",plugin_name)
#cls.plugin_name.set_value(plugin_name)
#cls.tab_icon.set_klass_pointer(cls)
#tab_icon = cls.tab_icon.field_value
#print("python gtype tab_icon",tab_icon)
#cls.tab_icon.set_value(tab_icon)
# and GncPluginPageSubClassMeta is used in a subclass of GncPluginPagePython to actually set the
# variables - we need __new__ here as we need to substitute the initial definition
class GncPluginPageSubClassMeta(GncPluginPageMeta):
def __new__ (mcls, classname, bases, attrs):
print("GncPluginPageSubClassMeta new called:",str(mcls),classname,str(bases),str(attrs))
#pdb.set_trace()
if len(bases) > 1:
raise AttributeError("__metaclass__ multiple bases not implemented")
if not 'plugin_name' in attrs:
raise AttributeError("plugin_name not defined for subclass - must be defined")
# this assumes no multiple bases - base[-1] seems to be immediate superclass
bases[-1].plugin_name.set_field_value(attrs['plugin_name'])
bases[-1].tab_icon.set_field_value(attrs['tab_icon'])
# we need to delete this from attrs otherwise the new plain string definition
# overrides the superclass (GncPluginPagePython) definition
del attrs['plugin_name']
del attrs['tab_icon']
# well doing GncPluginPageSubClassMeta.__new__ does call the supermetaclass __new__
# - but this has already been done - whats the right way??
# just use type call??
#return GncPluginPageSubClassMeta.__new__(mcls, classname, bases, attrs)
#return type.__new__(mcls, classname, bases, attrs)
return gi.types.GObjectMeta.__new__(mcls, classname, bases, attrs)
def __init__ (cls, name, bases, attrs):
print("GncPluginPageSubClassMeta init called:",str(cls),name,str(bases),str(attrs))
# NOTA BENE - for GType subclassing the class structure address returned at this point
# is STILL the parent GType class structure!!
# we need the cls variable AFTER the super call!!
# so this is how to get the address of the class structure!!
print("python sub gtype klass %s address %x"%(str(cls),hash(GObject.type_class_peek(cls))))
super(GncPluginPageSubClassMeta, cls).__init__(name, bases, attrs)
print("python sub gtype klass %s address %x"%(str(cls),hash(GObject.type_class_peek(cls))))
#pdb.set_trace()
# we have deleted plugin_name from attrs above!!
cls.plugin_name.set_klass_pointer(cls)
plugin_name = cls.plugin_name.field_value
cls.plugin_name.set_value(plugin_name)
cls.tab_icon.set_klass_pointer(cls)
tab_icon = cls.tab_icon.field_value
cls.tab_icon.set_value(tab_icon)
# class definition for python 2.7 using the meta class
#class NewGObject(object):
# __metaclass__ = GirMeta
# __girmetaclass__ = NewGObject.GObjectClass
# etc
# class definition for python 3 using the meta class
#class NewGObject(object, metaclass=GirMeta):
# __girmetaclass__ = NewGObject.GObjectClass
# etc
# create a container object for storing GType class variables
# should I use a namedtuple
# for namedtuple we need to know variable names at definition time
#namedtuple('GObjectClass')
class GObjectClass(object):
def __init__ (self, key_values):
for key,val in key_values:
setattr(self, key, val)
# this class handles a GType field variables
class GObjectField(object):
# this object is instantiated for each field name
def __init__ (self, fldobj, offset_adjust_hack=0, check_adjust_hack=0, initial_value=None):
self.field_name = fldobj.get_name()
self.field_info = fldobj
#pdb.set_trace()
field_info_obj = get_field_info(self.field_info)
self.offset = libgirepository.g_field_info_get_offset(field_info_obj)
print("offset %d"%self.offset)
#pdb.set_trace()
self.field_type_tag = get_field_type_tag(field_info_obj)
print("type tag %d"%self.field_type_tag)
#pdb.set_trace()
# DANGEROUS - only one of instance_ptr or klass_ptr should be set!!
self.instance_ptr = None
self.klass_ptr = None
self.value_pointer = None
## do we assign these as defaults??
#self.get_value = None
#self.set_value = None
# DANGER WILL ROBINSON!!
# this is extreme hackery - because of issues of introspection and
# bit field sizes which lead to wrong field offsets this allows
# the offset to be adjusted when computing field pointer values
# the check_adjust_hack is used to try and detect if the offset is
# needed - its values is checked against the original offset
self.offset_adjust_hack = offset_adjust_hack
self.check_adjust_hack = check_adjust_hack
# now we need to create the get_value/set_value functions
if self.field_type_tag == GITypeTag.GI_TYPE_TAG_UTF8:
self.get_value = self.get_utf8_value
self.set_value = self.set_utf8_value
elif self.field_type_tag == GITypeTag.GI_TYPE_TAG_UINT32:
self.get_value = self.get_uint32_value
self.set_value = self.set_uint32_value
elif self.field_type_tag == GITypeTag.GI_TYPE_TAG_VOID:
# so far this seen for a virtual function call recreate_page which
# creates a new GncPluginPage in gnc_plugin_page.h
# ignore for the moment??
pass
elif self.field_type_tag == GITypeTag.GI_TYPE_TAG_ARRAY:
type_info_ptr = libgirepository.g_field_info_get_type(field_info_obj)
print("type ptr %x"%type_info_ptr)
array_type = libgirepository.g_type_info_get_array_type(type_info_ptr)
# what to do for this??
self.get_value = self.get_object_value
self.set_value = self.set_object_value
elif self.field_type_tag >= GITypeTag.GI_TYPE_TAG_INTERFACE*1000:
if self.field_type_tag == GITypeTag.GI_TYPE_TAG_INTERFACE*1000+GIInfoType.GI_INFO_TYPE_STRUCT:
# this appears to be used for basic GObjects
self.get_value = self.get_object_value
self.set_value = self.set_object_value
elif self.field_type_tag == GITypeTag.GI_TYPE_TAG_INTERFACE*1000+GIInfoType.GI_INFO_TYPE_OBJECT:
# this appears to be used for basic GObjects
self.get_value = self.get_object_value
self.set_value = self.set_object_value
elif self.field_type_tag == GITypeTag.GI_TYPE_TAG_INTERFACE*1000+GIInfoType.GI_INFO_TYPE_CALLBACK:
# for the moment Im skipping callbacks/virtual functions as those should be
# correctly available as virtual functions
# by not defining the get_value/set_value attribute use this to check to exclude
# from class variables
pass
else:
pdb.set_trace()
raise RuntimeError("Unimplemented info type tag %d for %s"%(self.field_type_tag,self.field_name))
else:
pdb.set_trace()
raise RuntimeError("Unimplemented type tag %d for %s"%(self.field_type_tag,self.field_name))
print("added class variable",self.field_name,self.field_type_tag)
# we cannot set the value yet as class not implemented
# - just save it
if initial_value != None:
self.field_value = initial_value
#pdb.set_trace()
def __get__ (self, obj, objtype=None):
#pdb.set_trace()
if obj == None:
return self
if self.get_value == None:
raise AttributeError("unreadable GObject class attribute %s"%self.field_name)
return self.get_value()
def __set__ (self, obj, value):
#pdb.set_trace()
if self.set_value == None:
raise AttributeError("can't set GObject class attribute %s"%self.field_name)
return self.set_value(value)
def __delete__ (self, obj):
# we cant delete these attributes!!
raise AttributeError("can't delete GObject class attribute %s"%self.field_name)
def set_instance_pointer (self, ginstance):
self.instance_pointer = hash(ginstance)
print("python ginstance %s address %x"%(str(ginstance),hash(ginstance)))
if self.offset_adjust_hack != 0:
if self.offset != self.check_adjust_hack:
pdb.set_trace()
raise ValueError("Field offset is wrong - possible bad version of GType %s - may need to regenerate gir files"%self.field_name)
self.value_pointer = self.instance_pointer + self.offset_adjust_hack + self.offset
print("python value address %x"%self.value_pointer)
def set_klass_pointer (self, gtype):
# so this is how to get the address of the class structure!!
self.klass_ptr = hash(GObject.type_class_peek(gtype))
print("python gtype klass %s address %x"%(str(gtype),hash(GObject.type_class_peek(gtype))))
if self.offset_adjust_hack != 0:
if self.offset != self.check_adjust_hack:
raise ValueError("Field offset is wrong - possible bad version of GType %s - may need to regenerate gir files"%self.field_name)
self.value_pointer = self.klass_ptr + self.offset_adjust_hack + self.offset
print("python value address %x"%self.value_pointer)
def set_field_value (self, new_value):
# a direct set
self.field_value = new_value
def get_utf8_value (self):
print("called get_utf8_value")
print("python value address %x"%self.value_pointer)
#pdb.set_trace()
# need to check if returning addresses or python values!!!
strval = ctypes.cast(self.value_pointer, ctypes.POINTER(ctypes.c_char_p)).contents.value
utf8val = ctypes.pythonapi.PyUnicode_FromString(strval)
return utf8val
def set_utf8_value (self, new_value):
# for the moment this is limited to string type
print("called set_utf8_value")
#pdb.set_trace()
print("python value address %x"%self.value_pointer)
# save string to ensure not GCed
self.field_value = new_value
#pdb.set_trace()
if new_value == None:
new_value_str_ptr = 0
else:
# typeerror I think is correct exception to raise here
if not isinstance(new_value,str):
raise TypeError("argument value is not a string type - must be a string type")
# this was sneakily using the address of the string object as the C pointer
# we need to correct this to use the python object directly
#new_value_str_ptr = ctypes.pythonapi.PyUnicode_AsUTF8(id(new_value))
new_value_str_ptr = ctypes.pythonapi.PyUnicode_AsUTF8(new_value)
print("python str address %x"%new_value_str_ptr)
# finally - this is how we do pointer assignment given plain addresses
# we must cast to a pointer type - its confusing because the pointed to type is also a generic
# pointer!!
ctypes.cast(self.value_pointer, ctypes.POINTER(ctypes.c_void_p)).contents.value = new_value_str_ptr
return
def get_object_value (self):
print("called get_object_value")
print("python value address %x"%self.value_pointer)
#pdb.set_trace()
objval = ctypes.cast(self.value_pointer, ctypes.POINTER(ctypes.c_void_p)).contents.value
return objval
def set_object_value (self, new_value):
print("called set_object_value")
print("python value address %x"%self.value_pointer)
## typeerror I think is correct exception to raise here
#if not isinstance(new_value,str):
# raise TypeError("argument value is not a string type - must be a string type")
# what is the address to use -
pdb.set_trace()
#if isinstance(new_value, ctypes.c_void_p):
# new_value_ptr = ctypes.c_void_p
new_value_ptr = new_value
# finally - this is how we do pointer assignment given plain addresses
# we must cast to a pointer type - its confusing because the pointed to type is also a generic
# pointer!!
ctypes.cast(self.value_pointer, ctypes.POINTER(ctypes.c_void_p)).contents.value = new_value_ptr
return
def get_uint32_value (self):
print("called get_uint32_value")
print("python value address %x"%self.value_pointer)
pdb.set_trace()
uintval = ctypes.cast(self.value_pointer, ctypes.POINTER(ctypes.c_uint)).contents.value
return uintval
def set_uint32_value (self, new_value):
print("called set_object_value")
print("python value address %x"%self.value_pointer)
## typeerror I think is correct exception to raise here
## test for positive or hex map??
#if not isinstance(new_value,int) or new_value < 0:
# raise TypeError("argument value is not a valid positive integer")
# finally - this is how we do pointer assignment given plain addresses
# we must cast to a pointer type
ctypes.cast(self.value_pointer, ctypes.POINTER(ctypes.c_uint)).contents.value = new_value
return
# hmm - looks like we need both the GType (or a GObject instantiation) and its class structure
# definition from the gir file
# yes - hash on the klass object is not the same as the
# hash on the type_class_peek of the gobject
@classmethod
def SetupClass (cls, gtype, klass, field_name, offset_adjust_hack=0, check_adjust_hack=0):
#Try.PluginClass.__dict__['__info__'].get_fields()[2]
flds = klass.__dict__['__info__'].get_fields()
fndfld = None
for fldobj in flds:
if fldobj.get_name() == field_name:
fndfld = fldobj
break
if fndfld == None:
raise RuntimeError("Class %s does not contain the field name %s"%(str(klass),field_name))
newobj = cls(fldobj, offset_adjust_hack=offset_adjust_hack, check_adjust_hack=check_adjust_hack)
newobj.set_klass_pointer(gtype)
return newobj
@classmethod
def Setup (cls, ginstance, field_name, offset_adjust_hack=0, check_adjust_hack=0):
#pdb.set_trace()
flds = ginstance.__class__.__info__.get_fields()
fndfld = None
for fldobj in flds:
if fldobj.get_name() == field_name:
fndfld = fldobj
break
if fndfld == None:
raise RuntimeError("Class %s does not contain the field name %s"%(str(klass),field_name))
newobj = cls(fldobj, offset_adjust_hack=offset_adjust_hack, check_adjust_hack=check_adjust_hack)
newobj.set_instance_pointer(ginstance)
return newobj
def BaseInfoCast (intype):
# see if we can make a type caster
# apparently we can - this works!!
# - but we also need an inverse version
# - in which we take a BaseInfo object and re-cast to
# one of the others - for functions which return BaseInfo
# objects
pdb.set_trace()
intype_ptr = id(intype)
print("intype 0x%x"%intype_ptr)
pyinfo_ptr = ctypes.cast( intype_ptr, ctypes.POINTER(PyGIBaseInfo) )
#print("baseinfo ptr 0x%x"%pyinfo_ptr)
print("baseinfo ptr 0x%x"%ctypes.addressof(pyinfo_ptr.contents))
print("type %x"%pyinfo_ptr.contents.ob_type)
print("info %x"%pyinfo_ptr.contents.info)
newbaseinfo = gi.repository.GIRepository.BaseInfo()
base_ptr = id(newbaseinfo)
print("intype 0x%x"%base_ptr)
newpygibaseinfo_ptr = ctypes.cast( base_ptr, ctypes.POINTER(PyGIBaseInfo) )
#print("newbaseinfo ptr 0x%x"%newpygibaseinfo_ptr)
print("newbaseinfo ptr 0x%x"%ctypes.addressof(newpygibaseinfo_ptr.contents))
print("newtype %x"%newpygibaseinfo_ptr.contents.ob_type)
print("newinfo %x"%newpygibaseinfo_ptr.contents.info)
newpygibaseinfo_ptr.contents.info = pyinfo_ptr.contents.info
return newbaseinfo
def access_class_data (gtype):
# so this is how to get the address of the class structure!!
# note gtype is a GObject type - NOT the GObjectClass type!!
klass_ptr = hash(GObject.type_class_peek(gtype))
return klass_ptr
def main ():
from gi.repository import Gtk
# so this is how to get the address of the class structure!!
#trymod_klass = hash(GObject.type_class_peek(Try.Plugin))
#print("python gobject trymod klass address %x"%hash(GObject.type_class_peek(Try.Plugin)))
pdb.set_trace()
gobjflds = Gtk.Bin.__info__.get_fields()
#gobjflds = Gtk.Container.__info__.get_fields()
for gobjfld in gobjflds:
print("gobjfld",gobjfld)
print("gobjfld",gobjfld.get_name())
field_info_obj = get_field_info(gobjfld)
offset = libgirepository.g_field_info_get_offset(field_info_obj)
print("gobjfld offset",offset)
#g_field_get_value(gobjfld, trymod_klass)
#g_field_get_value(Try.PluginClass.__dict__['__info__'].get_fields()[1], trymod_klass)
#g_field_get_value(Try.PluginClass.__dict__['__info__'].get_fields()[2], trymod_klass)
pass
if __name__ == '__main__':
main()