File indexing completed on 2024-04-28 16:13:13

0001 #!/usr/bin/python
0002 
0003 from sys import argv, stdout, stderr
0004 import xml.dom.minidom
0005 
0006 from libglibcodegen import NS_TP, get_docstring, \
0007         get_descendant_text, get_by_path
0008 
0009 class Generator(object):
0010     def __init__(self, prefix, implfile, declfile, dom):
0011         self.prefix = prefix + '_'
0012         self.impls = open(implfile, 'w')
0013         self.decls = open(declfile, 'w')
0014         self.spec = get_by_path(dom, "spec")[0]
0015 
0016     def h(self, code):
0017         self.decls.write(code.encode('utf-8'))
0018 
0019     def c(self, code):
0020         self.impls.write(code.encode('utf-8'))
0021 
0022     def __call__(self):
0023         for f in self.h, self.c:
0024             self.do_header(f)
0025         self.do_body()
0026 
0027     # Header
0028     def do_header(self, f):
0029         f('/* Generated from: ')
0030         f(get_descendant_text(get_by_path(self.spec, 'title')))
0031         version = get_by_path(self.spec, "version")
0032         if version:
0033             f(' version ' + get_descendant_text(version))
0034         f('\n\n')
0035         for copyright in get_by_path(self.spec, 'copyright'):
0036             f(get_descendant_text(copyright))
0037             f('\n')
0038         f('\n')
0039         f(get_descendant_text(get_by_path(self.spec, 'license')))
0040         f(get_descendant_text(get_by_path(self.spec, 'docstring')))
0041         f("""
0042  */
0043 
0044 """)
0045 
0046     # Body
0047     def do_body(self):
0048         for iface in self.spec.getElementsByTagName('interface'):
0049             self.do_iface(iface)
0050 
0051     def do_iface(self, iface):
0052         parent_name = get_by_path(iface, '../@name')
0053         self.h("""\
0054 /**
0055  * %(IFACE_DEFINE)s:
0056  *
0057  * The interface name "%(name)s"
0058  */
0059 #define %(IFACE_DEFINE)s \\
0060 "%(name)s"
0061 """ % {'IFACE_DEFINE' : (self.prefix + 'IFACE_' + \
0062             parent_name).upper().replace('/', ''),
0063        'name' : iface.getAttribute('name')})
0064 
0065         self.h("""
0066 /**
0067  * %(IFACE_QUARK_DEFINE)s:
0068  *
0069  * Expands to a call to a function that returns a quark for the interface \
0070 name "%(name)s"
0071  */
0072 #define %(IFACE_QUARK_DEFINE)s \\
0073   (%(iface_quark_func)s ())
0074 
0075 GQuark %(iface_quark_func)s (void);
0076 
0077 """ % {'IFACE_QUARK_DEFINE' : (self.prefix + 'IFACE_QUARK_' + \
0078             parent_name).upper().replace('/', ''),
0079        'iface_quark_func' : (self.prefix + 'iface_quark_' + \
0080             parent_name).lower().replace('/', ''),
0081        'name' : iface.getAttribute('name')})
0082 
0083         self.c("""\
0084 GQuark
0085 %(iface_quark_func)s (void)
0086 {
0087   static GQuark quark = 0;
0088 
0089   if (G_UNLIKELY (quark == 0))
0090     {
0091       quark = g_quark_from_static_string ("%(name)s");
0092     }
0093 
0094   return quark;
0095 }
0096 
0097 """ % {'iface_quark_func' : (self.prefix + 'iface_quark_' + \
0098             parent_name).lower().replace('/', ''),
0099        'name' : iface.getAttribute('name')})
0100 
0101         for prop in iface.getElementsByTagNameNS(None, 'property'):
0102             self.decls.write("""
0103 /**
0104  * %(IFACE_PREFIX)s_%(PROP_UC)s:
0105  *
0106  * The fully-qualified property name "%(name)s.%(prop)s"
0107  */
0108 #define %(IFACE_PREFIX)s_%(PROP_UC)s \\
0109 "%(name)s.%(prop)s"
0110 """ % {'IFACE_PREFIX' : (self.prefix + 'PROP_' + \
0111                 parent_name).upper().replace('/', ''),
0112            'PROP_UC': prop.getAttributeNS(NS_TP, "name-for-bindings").upper(),
0113            'name' : iface.getAttribute('name'),
0114            'prop' : prop.getAttribute('name'),
0115            })
0116 
0117 if __name__ == '__main__':
0118     argv = argv[1:]
0119     Generator(argv[0], argv[1], argv[2], xml.dom.minidom.parse(argv[3]))()