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
|
#!/usr/bin/python
from xml.etree import ElementTree
import sys
import re
import os
from optparse import OptionParser
import pwd
import datetime
class ParamSpec:
def __init__(self, name, nick, desc, flags, **kwargs):
self.name = name
self.nick = nick
self.desc = desc
self.flags = flags
self.args = []
def finalizer(self, container):
return None
def get_value(self, val, container):
return 'g_value_set_pointer (%s, %s->%s);' % (val, container, self.cname())
def set_value(self, val, container):
return '%s->%s = g_value_get_pointer (%s);' % (container, self.cname(), val)
def prop_enum(self):
return 'PROP_' + self.name.upper().replace('-', '_')
def cname(self):
return self.name.replace('-', '_')
def ctype(self):
return 'gpointer'
def spec_name(self):
name = self.__class__.__name__
if name.startswith('ParamSpec'):
name = name[len('ParamSpec'):]
return name.lower()
def format_str(self, s):
if s.startswith('"') or s.startswith('_("'):
return s
return '"%s"' % (s.replace('"', '\\"'),)
def __str__(self):
name = "g_param_spec_" + self.spec_name()
indent = " " * (len(name) + 2)
argstr = ''
if self.args:
ret = (",\n%s" % (indent,)).join(str(x) for x in self.args)
argstr = "\n%s%s," % (indent, ret)
flags = (' |\n%s' % (indent,)).join(x.strip() for x in self.flags.split('|'))
return "%s (%s,\n%s%s,\n%s%s,%s\n%s%s)" % (name,
self.format_str(self.name),
indent,
self.format_str(self.nick),
indent,
self.format_str(self.desc),
argstr,
indent,
flags)
def write(self):
delim = "\n" + (" " * 33)
spec = delim.join(str(self).splitlines())
return """
g_object_class_install_property (object_class,
%s,
%s);""" % (self.prop_enum(), spec)
class ParamSpecTyped(ParamSpec):
def __init__(self, name, nick, desc, flags, **kwargs):
ParamSpec.__init__(self, name, nick, desc, flags, **kwargs)
if 'ctype' in kwargs:
self._ctype = kwargs['ctype']
else:
parts = kwargs['gtype'].split('_')
del parts[1]
self._ctype = '%s *' % (''.join(x.title() for x in parts),)
self.args.append(kwargs['gtype'])
def ctype(self):
return self._ctype
class ParamSpecBoolean(ParamSpec):
def __init__(self, name, nick, desc, flags, **kwargs):
ParamSpec.__init__(self, name, nick, desc, flags, **kwargs)
if 'default' in kwargs:
if kwargs['default'].lower() in ['true', '1']:
self.args = ['TRUE']
else:
self.args = ['FALSE']
else:
self.args = ['FALSE']
def get_value(self, val, container):
return 'g_value_set_boolean (%s, %s->%s);' % (val, container, self.cname())
def set_value(self, val, container):
return '%s->%s = g_value_get_boolean (%s);' % (container, self.cname(), val)
def ctype(self):
return 'gboolean'
class ParamSpecBoxed(ParamSpecTyped):
def __init__(self, name, nick, desc, flags, **kwargs):
ParamSpecTyped.__init__(self, name, nick, desc, flags, **kwargs)
def finalizer(self, container):
return 'if (%s->%s != NULL)\n{\n\tg_boxed_free (%s->%s);\n}' % (container, self.cname(), container, self.cname())
def get_value(self, val, container):
return 'g_value_set_boxed (%s, %s->%s);' % (val, container, self.cname())
def set_value(self, val, container):
return '%s->%s = g_value_dup_boxed (%s);' % (container, self.cname(), val)
class ParamSpecNumeric(ParamSpec):
def __init__(self, name, nick, desc, flags, **kwargs):
ParamSpec.__init__(self, name, nick, desc, flags, **kwargs)
if 'min' in kwargs:
minv = kwargs['min']
else:
minv = self.min_value()
if 'max' in kwargs:
maxv = kwargs['max']
else:
maxv = self.max_value()
if 'default' in kwargs:
default = kwargs['default']
else:
default = self.default_value()
self.args = [minv, maxv, default]
def min_value(self):
return '0'
def max_value(self):
return '0'
def default_value(self):
return '0'
class ParamSpecDouble(ParamSpecNumeric):
def __init__(self, name, nick, desc, flags, **kwargs):
ParamSpecNumeric.__init__(self, name, nick, desc, flags, **kwargs)
def min_value(self):
return 'G_MINDOUBLE'
def max_value(self):
return 'G_MAXDOUBLE'
def ctype(self):
return 'gdouble'
def get_value(self, val, container):
return 'g_value_set_double (%s, %s->%s);' % (val, container, self.cname())
def set_value(self, val, container):
return '%s->%s = g_value_get_double (%s);' % (container, self.cname(), val)
class ParamSpecEnum(ParamSpecTyped):
def __init__(self, name, nick, desc, flags, **kwargs):
ParamSpec.__init__(self, name, nick, desc, flags, **kwargs)
if 'default' in kwargs:
default = kwargs['default']
else:
default = '0'
self.args.append(default)
def get_value(self, val, container):
return 'g_value_set_enum (%s, %s->%s);' % (val, container, self.cname())
def set_value(self, val, container):
return '%s->%s = g_value_get_enum (%s);' % (container, self.cname(), val)
class ParamSpecFlags(ParamSpecEnum):
def __init__(self, name, nick, desc, flags, **kwargs):
ParamSpecEnum.__init__(self, name, nick, desc, flags, **kwargs)
def get_value(self, val, container):
return 'g_value_set_flags (%s, %s->%s);' % (val, container, self.cname())
def set_value(self, val, container):
return '%s->%s = g_value_get_flags (%s);' % (container, self.cname(), val)
class ParamSpecFloat(ParamSpecNumeric):
def __init__(self, name, nick, desc, flags, **kwargs):
ParamSpecNumeric.__init__(self, name, nick, desc, flags, **kwargs)
def min_value(self):
return 'G_MINFLOAT'
def max_value(self):
return 'G_MAXFLOAT'
def ctype(self):
return 'gfloat'
def get_value(self, val, container):
return 'g_value_set_float (%s, %s->%s);' % (val, container, self.cname())
def set_value(self, val, container):
return '%s->%s = g_value_get_float (%s);' % (container, self.cname(), val)
class ParamSpecInt(ParamSpecNumeric):
def __init__(self, name, nick, desc, flags, **kwargs):
ParamSpecNumeric.__init__(self, name, nick, desc, flags, **kwargs)
def min_value(self):
return 'G_MININT'
def max_value(self):
return 'G_MAXINT'
def ctype(self):
return 'gint'
def get_value(self, val, container):
return 'g_value_set_int (%s, %s->%s);' % (val, container, self.cname())
def set_value(self, val, container):
return '%s->%s = g_value_get_int (%s);' % (container, self.cname(), val)
class ParamSpecUInt(ParamSpecNumeric):
def __init__(self, name, nick, desc, flags, **kwargs):
ParamSpecNumeric.__init__(self, name, nick, desc, flags, **kwargs)
def min_value(self):
return '0'
def max_value(self):
return 'G_MAXUINT'
def ctype(self):
return 'guint'
def get_value(self, val, container):
return 'g_value_set_uint (%s, %s->%s);' % (val, container, self.cname())
def set_value(self, val, container):
return '%s->%s = g_value_get_uint (%s);' % (container, self.cname(), val)
class ParamSpecObject(ParamSpecTyped):
def __init__(self, name, nick, desc, flags, **kwargs):
ParamSpecTyped.__init__(self, name, nick, desc, flags, **kwargs)
def finalizer(self, container):
return 'if (%s->%s)\n{\n\tg_object_unref (%s->%s);\n}' % (container, self.cname(), container, self.cname())
def get_value(self, val, container):
return 'g_value_set_object (%s, %s->%s);' % (val, container, self.cname())
def set_value(self, val, container):
return '%s->%s = g_value_dup_object (%s);' % (container, self.cname(), val)
class ParamSpecPointer(ParamSpec):
def __init__(self, name, nick, desc, flags, **kwargs):
ParamSpec.__init__(self, name, nick, desc, flags, **kwargs)
class ParamSpecString(ParamSpec):
def __init__(self, name, nick, desc, flags, **kwargs):
ParamSpec.__init__(self, name, nick, desc, flags, **kwargs)
if 'default' in kwargs:
default = kwargs['default']
else:
default = 'NULL'
if not default.startswith('"') and not default.startswith('_("') and default != 'NULL':
default = '"' + default.replace('"', '\\"') + '"'
self.args.append(default)
def ctype(self):
return 'gchar *'
def finalizer(self, container):
return 'g_free (%s->%s);' % (container, self.cname())
def get_value(self, val, container):
return 'g_value_set_string (%s, %s->%s);' % (val, container, self.cname())
def set_value(self, val, container):
return '%s->%s = g_value_dup_string (%s);' % (container, self.cname(), val)
_prop_types = {
'boolean': ParamSpecBoolean,
'boxed': ParamSpecBoxed,
'double': ParamSpecDouble,
'enum': ParamSpecEnum,
'flags': ParamSpecFlags,
'float': ParamSpecFloat,
'int': ParamSpecInt,
'object': ParamSpecObject,
'pointer': ParamSpecPointer,
'string': ParamSpecString,
'uint': ParamSpecUInt
}
GPL = """
/*
* #FILENAME
* This file is part of #PROGRAM
*
* Copyright (C) #YEAR - #AUTHOR
*
* #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.
*
* #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 #PROGRAM; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
"""
LGPL = """
/*
* #FILENAME
* This file is part of #PROGRAM
*
* Copyright (C) #YEAR - #AUTHOR
*
* ${2} is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* #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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
"""
class Include:
def __init__(self, elem):
self.path = elem.text
self.is_system = elem.get('system') != None
def __str__(self):
if self.is_system:
return '#include <%s>' % (self.path,)
else:
return '#include "%s"' % (self.path,)
class Message:
def __init__(self, elem):
self.name = elem.get('name')
self.namespace = elem.get('namespace')
if not self.name:
sys.stderr.write("Name for message is required...\n")
return
if not self.namespace:
sys.stderr.write("Namespace for message is required (%s)...\n" % (self.name,))
self.properties = []
self.includes = []
for prop in elem.findall('property'):
self.add_prop(prop)
for inc in elem.findall('include'):
self.includes.append(Include(inc))
self.setup()
def setup(self):
parts = [x.group(0) for x in re.finditer('[A-Z]+[a-z0-9]*', self.name)]
self.ns_upper = self.namespace.upper()
self.ns_lower = self.namespace.lower()
self.name_upper = '_'.join(x.upper() for x in parts)
self.name_lower = self.name_upper.lower()
self.cname_upper = '%s_%s' % (self.ns_upper, self.name_upper)
self.cname_lower = self.cname_upper.lower()
self.ctype = '%s_%s_%s' % (self.ns_upper, 'TYPE', self.name_upper)
self.cclass = '%s%sClass' % (self.namespace, self.name)
self.cobj = '%s%s' % (self.namespace, self.name)
self.filename = '%s-%s' % (self.ns_lower, '-'.join(x.lower() for x in parts))
def normalize_name(self, name):
if not name:
return name
return name.replace('_', '-')
def parse_flags(self, flags):
return ' | '.join('G_PARAM_' + x.strip().replace('-', '_').upper() for x in flags.split('|'))
def add_prop(self, elem):
name = self.normalize_name(elem.get('name'))
typ = elem.get('type')
if not name:
sys.stderr.write("Name for property not specified...\n")
return False
if not typ:
sys.stderr.write("Type for property `%s' not specified...\n" % (name,))
return False
if not typ in _prop_types:
sys.stderr.write("Invalid property type `%s'...\n" % (typ,))
blurb = elem.get('blurb')
nick = elem.get('nick')
flags = elem.get('flags')
if not flags:
flags = 'G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS'
else:
flags = self.parse_flags(flags)
if not nick:
nick = ' '.join(map(lambda x: x.title(), name.split('-')))
if not blurb:
blurb = str(nick)
attr = elem.attrib
for k in ['blurb', 'nick', 'flags', 'name', 'type']:
if k in attr:
del attr[k]
self.properties.append(_prop_types[typ](name, nick, blurb, flags, **attr))
def _write(self, *args):
if len(args) > 1:
self.f.write(args[0] % args[1:])
elif len(args) > 0:
self.f.write(args[0])
self.f.write("\n")
def _write_license(self, text, options, filename):
text = text.replace('#PROGRAM', options.program)
text = text.replace('#YEAR', str(datetime.date.today().year))
text = text.replace('#AUTHOR', options.author)
text = text.replace('#FILENAME', filename)
self._write(text)
def _write_gpl(self, options, filename):
self._write_license(GPL, options, filename)
def _write_lgpl(self, options, filename):
self._write_license(LGPL, options, filename)
def write_header(self, outdir, options):
fname = '%s.h' % (self.filename,)
self.f = file(os.path.join(outdir, fname), 'w')
if options.gpl:
self._write_gpl(options, fname)
elif options.lgpl:
self._write_lgpl(options, fname)
self._write("#ifndef __%s_H__", self.cname_upper)
self._write("#define __%s_H__", self.cname_upper)
self._write()
self._write("#include <gedit/gedit-message.h>\n")
self._write("G_BEGIN_DECLS\n")
alignon = len('%s_IS_%s_CLASS(klass)' % (self.ns_upper, self.name_upper))
alignsec = ' ' * (alignon + len('#define '))
self._write("#define %s (%s_get_type ())", self.ctype.ljust(alignon), self.cname_lower)
self._write("#define %s (G_TYPE_CHECK_INSTANCE_CAST ((obj),\\\n%s%s,\\\n%s%s))",
('%s(obj)' % (self.cname_upper,)).ljust(alignon),
alignsec, self.ctype,
alignsec, self.cobj)
self._write("#define %s (G_TYPE_CHECK_INSTANCE_CAST ((obj),\\\n%s%s,\\\n%s%s))",
('%s_CONST(obj)' % (self.cname_upper,)).ljust(alignon),
alignsec, self.ctype,
alignsec, '%s const' % (self.cobj,))
self._write("#define %s (G_TYPE_CHECK_CLASS_CAST ((klass),\\\n%s%s,\\\n%s%s))",
('%s_CLASS(klass)' % (self.cname_upper,)).ljust(alignon),
alignsec, self.ctype,
alignsec, self.cclass)
self._write("#define %s (G_TYPE_CHECK_INSTANCE_TYPE ((obj),\\\n%s%s))",
('%s_IS_%s(obj)' % (self.ns_upper, self.name_upper)).ljust(alignon),
alignsec, self.ctype)
self._write("#define %s (G_TYPE_CHECK_CLASS_TYPE ((klass),\\\n%s%s))",
('%s_IS_%s_CLASS(klass)' % (self.ns_upper, self.name_upper)).ljust(alignon),
alignsec, self.ctype)
self._write("#define %s (G_TYPE_INSTANCE_GET_CLASS ((obj),\\\n%s%s,\\\n%s%s))",
('%s_GET_CLASS(obj)' % (self.cname_upper,)).ljust(alignon),
alignsec, self.ctype,
alignsec, self.cclass)
thepriv = '%sPrivate' % (self.cobj,)
alignon = len(thepriv)
self._write()
self._write('typedef struct _%s %s;', self.cobj.ljust(alignon), self.cobj)
self._write('typedef struct _%s %s;', self.cclass.ljust(alignon), self.cclass)
self._write('typedef struct _%s %s;', thepriv.ljust(alignon), thepriv)
self._write()
self._write('struct _%s', self.cobj)
self._write('{')
self._write('\tGeditMessage parent;')
self._write()
self._write('\t%s *priv;', thepriv)
self._write('};')
self._write()
self._write('struct _%s', self.cclass)
self._write('{')
self._write('\tGeditMessageClass parent_class;')
self._write('};')
self._write()
self._write('GType %s_get_type (void) G_GNUC_CONST;', self.cname_lower)
self._write()
self._write('G_END_DECLS')
self._write()
self._write("#endif /* __%s_H__ */", self.cname_upper)
self.f.close()
def needs_finalize(self):
for prop in self.properties:
out = prop.finalizer('ignore')
if out:
return True
return False
def write_body(self, outdir, options):
fname = '%s.c' % (self.filename,)
self.f = file(os.path.join(outdir, fname), 'w')
if options.gpl:
self._write_gpl(options, fname)
elif options.lgpl:
self._write_lgpl(options, fname)
self._write("#ifdef HAVE_CONFIG_H")
self._write("#include <config.h>")
self._write("#endif")
self._write()
self._write('#include "%s.h"', self.filename)
for inc in self.includes:
self._write('%s', inc)
self._write()
self._write('#define %s_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), %s, %sPrivate))',
self.cname_upper, self.ctype, self.cobj)
self._write()
self._write('enum')
self._write('{')
self._write('\tPROP_0,')
self._write()
for prop in self.properties:
self._write('\t%s,', prop.prop_enum())
self._write('};')
self._write()
self._write('struct _%sPrivate', self.cobj)
self._write('{')
for prop in self.properties:
ct = prop.ctype()
if not ct.endswith('*'):
ct += ' '
self._write('\t%s%s;', ct, prop.cname())
self._write('};')
self._write()
self._write('G_DEFINE_TYPE (%s, %s, GEDIT_TYPE_MESSAGE)', self.cobj, self.cname_lower)
self._write()
if self.needs_finalize():
self._write('static void')
self._write('%s_finalize (GObject *obj)', self.cname_lower)
self._write('{')
self._write('\t%s *msg = %s (obj);', self.cobj, self.cname_upper)
self._write()
haswritten = False
for prop in self.properties:
out = prop.finalizer('msg->priv')
if out:
haswritten = True
self._write('\t%s', '\n\t'.join(out.splitlines()))
if haswritten:
self._write()
self._write('\tG_OBJECT_CLASS (%s_parent_class)->finalize (obj);', self.cname_lower)
self._write('}')
self._write()
ll = ' ' * (len(self.cname_lower))
self._write('static void')
self._write('%s_get_property (GObject *obj,', self.cname_lower)
self._write('%s guint prop_id,', ll)
self._write('%s GValue *value,', ll)
self._write('%s GParamSpec *pspec)', ll)
self._write('{')
self._write('\t%s *msg;', self.cobj)
self._write()
self._write('\tmsg = %s (obj);', self.cname_upper)
self._write()
self._write('\tswitch (prop_id)')
self._write('\t{')
for prop in self.properties:
self._write('\t\tcase %s:', prop.prop_enum())
self._write('\t\t\t%s', prop.get_value('value', 'msg->priv'))
self._write('\t\t\tbreak;')
self._write('\t}')
self._write('}')
self._write()
self._write('static void')
self._write('%s_set_property (GObject *obj,', self.cname_lower)
self._write('%s guint prop_id,', ll)
self._write('%s GValue const *value,', ll)
self._write('%s GParamSpec *pspec)', ll)
self._write('{')
self._write('\t%s *msg;', self.cobj)
self._write()
self._write('\tmsg = %s (obj);', self.cname_upper)
self._write()
self._write('\tswitch (prop_id)')
self._write('\t{')
for prop in self.properties:
self._write('\t\tcase %s:', prop.prop_enum())
out = prop.finalizer('msg->priv')
if out:
out = '\n\t\t\t'.join(out.splitlines())
self._write('\t\t{')
self._write('\t\t\t%s' % (out,))
self._write('\t\t\t%s', prop.set_value('value', 'msg->priv'))
self._write('\t\t\tbreak;')
if out:
self._write('\t\t}')
self._write('\t}')
self._write('}')
self._write()
self._write('static void')
self._write('%s_class_init (%s *klass)', self.cname_lower, self.cclass)
self._write('{')
self._write('\tGObjectClass *object_class = G_OBJECT_CLASS(klass);')
self._write()
if self.needs_finalize():
self._write('\tobject_class->finalize = %s_finalize;', self.cname_lower)
self._write()
self._write('\tobject_class->get_property = %s_get_property;', self.cname_lower)
self._write('\tobject_class->set_property = %s_set_property;', self.cname_lower)
self._write()
pp = 'g_object_class_install_property ('
prefix = '\t%s' % (' ' * len(pp),)
for prop in self.properties:
out = str(prop)
out = ("\n%s" % (prefix,)).join(out.splitlines())
self._write('\tg_object_class_install_property (object_class,\n%s%s,', prefix, prop.prop_enum())
self._write('%s%s);', prefix, out)
self._write()
self._write('\tg_type_class_add_private (object_class, sizeof (%sPrivate));', self.cobj)
self._write('}')
self._write()
self._write('static void')
self._write('%s_init (%s *message)', self.cname_lower, self.cobj)
self._write('{')
self._write('\tmessage->priv = %s_GET_PRIVATE (message);', self.cname_upper)
self._write('}')
self.f.close()
class Generator:
def __init__(self, filename, options):
self.filename = filename
self.doc = ElementTree.parse(filename)
self.options = options
def write_message(self, outputdir, elem):
msg = Message(elem)
msg.write_header(outputdir, options)
msg.write_body(outputdir, options)
def write(self, outputdir = '.'):
for message in self.doc.findall('message'):
self.write_message(outputdir, message)
author = pwd.getpwuid(os.getuid()).pw_gecos.split(',')[0]
parser = OptionParser()
parser.add_option("-o", "--output-directory", dest="output", help="Output directory", default=".")
parser.add_option("-g", "--gpl", dest="gpl", action="store_true", help="Add GPL stub to generated files", default=False)
parser.add_option("-l", "--lgpl", dest="lgpl", action="store_true", help="Add LGPL stub to generated files", default=False)
parser.add_option("-p", "--program", dest="program", help="Program name used in license snippet", default="program")
parser.add_option("-a", "--author", dest="author", help="Author used in license snippet", default=author)
(options, args) = parser.parse_args()
for arg in args:
generator = Generator(arg, options)
generator.write(options.output)
# vi:ex:ts=4:et
|