File indexing completed on 2024-04-28 08:54:37

0001 #!/usr/bin/python
0002 
0003 from sys import argv, stdout, stderr
0004 import xml.dom.minidom
0005 
0006 from libtpcodegen import file_set_contents, u
0007 from libglibcodegen import NS_TP, get_docstring, \
0008         get_descendant_text, get_by_path
0009 
0010 class Generator(object):
0011     def __init__(self, prefix, implfile, declfile, dom):
0012         self.prefix = prefix + '_'
0013 
0014         assert declfile.endswith('.h')
0015         docfile = declfile[:-2] + '-gtk-doc.h'
0016 
0017         self.implfile = implfile
0018         self.declfile = declfile
0019         self.docfile = docfile
0020 
0021         self.impls = []
0022         self.decls = []
0023         self.docs = []
0024         self.spec = get_by_path(dom, "spec")[0]
0025 
0026     def h(self, code):
0027         self.decls.append(code)
0028 
0029     def c(self, code):
0030         self.impls.append(code)
0031 
0032     def d(self, code):
0033         self.docs.append(code)
0034 
0035     def __call__(self):
0036         for f in self.h, self.c:
0037             self.do_header(f)
0038         self.do_body()
0039 
0040         file_set_contents(self.implfile, u('').join(self.impls).encode('utf-8'))
0041         file_set_contents(self.declfile, u('').join(self.decls).encode('utf-8'))
0042         file_set_contents(self.docfile, u('').join(self.docs).encode('utf-8'))
0043 
0044     # Header
0045     def do_header(self, f):
0046         f('/* Generated from: ')
0047         f(get_descendant_text(get_by_path(self.spec, 'title')))
0048         version = get_by_path(self.spec, "version")
0049         if version:
0050             f(' version ' + get_descendant_text(version))
0051         f('\n\n')
0052         for copyright in get_by_path(self.spec, 'copyright'):
0053             f(get_descendant_text(copyright))
0054             f('\n')
0055         f('\n')
0056         f(get_descendant_text(get_by_path(self.spec, 'license')))
0057         f(get_descendant_text(get_by_path(self.spec, 'docstring')))
0058         f("""
0059  */
0060 
0061 #include <glib.h>
0062 """)
0063 
0064     # Body
0065     def do_body(self):
0066         for iface in self.spec.getElementsByTagName('interface'):
0067             self.do_iface(iface)
0068 
0069     def do_iface(self, iface):
0070         parent_name = get_by_path(iface, '../@name')
0071         self.d("""\
0072 /**
0073  * %(IFACE_DEFINE)s:
0074  *
0075  * The interface name "%(name)s"
0076  */
0077 """ % {'IFACE_DEFINE' : (self.prefix + 'IFACE_' + \
0078             parent_name).upper().replace('/', ''),
0079        'name' : iface.getAttribute('name')})
0080 
0081         self.h("""
0082 #define %(IFACE_DEFINE)s \\
0083 "%(name)s"
0084 """ % {'IFACE_DEFINE' : (self.prefix + 'IFACE_' + \
0085             parent_name).upper().replace('/', ''),
0086        'name' : iface.getAttribute('name')})
0087 
0088         self.d("""
0089 /**
0090  * %(IFACE_QUARK_DEFINE)s:
0091  *
0092  * Expands to a call to a function that returns a quark for the interface \
0093 name "%(name)s"
0094  */
0095 """ % {'IFACE_QUARK_DEFINE' : (self.prefix + 'IFACE_QUARK_' + \
0096             parent_name).upper().replace('/', ''),
0097        'iface_quark_func' : (self.prefix + 'iface_quark_' + \
0098             parent_name).lower().replace('/', ''),
0099        'name' : iface.getAttribute('name')})
0100 
0101         self.h("""
0102 #define %(IFACE_QUARK_DEFINE)s \\
0103   (%(iface_quark_func)s ())
0104 
0105 GQuark %(iface_quark_func)s (void);
0106 
0107 """ % {'IFACE_QUARK_DEFINE' : (self.prefix + 'IFACE_QUARK_' + \
0108             parent_name).upper().replace('/', ''),
0109        'iface_quark_func' : (self.prefix + 'iface_quark_' + \
0110             parent_name).lower().replace('/', ''),
0111        'name' : iface.getAttribute('name')})
0112 
0113         self.c("""\
0114 GQuark
0115 %(iface_quark_func)s (void)
0116 {
0117   static GQuark quark = 0;
0118 
0119   if (G_UNLIKELY (quark == 0))
0120     {
0121       quark = g_quark_from_static_string ("%(name)s");
0122     }
0123 
0124   return quark;
0125 }
0126 
0127 """ % {'iface_quark_func' : (self.prefix + 'iface_quark_' + \
0128             parent_name).lower().replace('/', ''),
0129        'name' : iface.getAttribute('name')})
0130 
0131         for prop in iface.getElementsByTagNameNS(None, 'property'):
0132             self.d("""
0133 /**
0134  * %(IFACE_PREFIX)s_%(PROP_UC)s:
0135  *
0136  * The fully-qualified property name "%(name)s.%(prop)s"
0137  */
0138 """ % {'IFACE_PREFIX' : (self.prefix + 'PROP_' + \
0139                 parent_name).upper().replace('/', ''),
0140            'PROP_UC': prop.getAttributeNS(NS_TP, "name-for-bindings").upper(),
0141            'name' : iface.getAttribute('name'),
0142            'prop' : prop.getAttribute('name'),
0143            })
0144 
0145             self.h("""
0146 #define %(IFACE_PREFIX)s_%(PROP_UC)s \\
0147 "%(name)s.%(prop)s"
0148 """ % {'IFACE_PREFIX' : (self.prefix + 'PROP_' + \
0149                 parent_name).upper().replace('/', ''),
0150            'PROP_UC': prop.getAttributeNS(NS_TP, "name-for-bindings").upper(),
0151            'name' : iface.getAttribute('name'),
0152            'prop' : prop.getAttribute('name'),
0153            })
0154 
0155 
0156         for prop in iface.getElementsByTagNameNS(NS_TP, 'contact-attribute'):
0157             self.d("""
0158 /**
0159  * %(TOKEN_PREFIX)s_%(TOKEN_UC)s:
0160  *
0161  * The fully-qualified contact attribute token name "%(name)s/%(prop)s"
0162  */
0163 """ % {'TOKEN_PREFIX' : (self.prefix + 'TOKEN_' + \
0164                 parent_name).upper().replace('/', ''),
0165            'TOKEN_UC': prop.getAttributeNS(None, "name").upper().replace("-", "_").replace(".", "_"),
0166            'name' : iface.getAttribute('name'),
0167            'prop' : prop.getAttribute('name'),
0168            })
0169 
0170             self.h("""
0171 #define %(TOKEN_PREFIX)s_%(TOKEN_UC)s \\
0172 "%(name)s/%(prop)s"
0173 """ % {'TOKEN_PREFIX' : (self.prefix + 'TOKEN_' + \
0174                 parent_name).upper().replace('/', ''),
0175            'TOKEN_UC': prop.getAttributeNS(None, "name").upper().replace("-", "_").replace(".", "_"),
0176            'name' : iface.getAttribute('name'),
0177            'prop' : prop.getAttribute('name'),
0178            })
0179 
0180         for prop in iface.getElementsByTagNameNS(NS_TP, 'hct'):
0181             if (prop.getAttribute('is-family') != "yes"):
0182                 self.d("""
0183 /**
0184  * %(TOKEN_PREFIX)s_%(TOKEN_UC)s:
0185  *
0186  * The fully-qualified capability token name "%(name)s/%(prop)s"
0187  */
0188 """ % {'TOKEN_PREFIX' : (self.prefix + 'TOKEN_' + \
0189                 parent_name).upper().replace('/', ''),
0190            'TOKEN_UC': prop.getAttributeNS(None, "name").upper().replace("-", "_").replace(".", "_"),
0191            'name' : iface.getAttribute('name'),
0192            'prop' : prop.getAttribute('name'),
0193            })
0194 
0195                 self.h("""
0196 #define %(TOKEN_PREFIX)s_%(TOKEN_UC)s \\
0197 "%(name)s/%(prop)s"
0198 """ % {'TOKEN_PREFIX' : (self.prefix + 'TOKEN_' + \
0199                 parent_name).upper().replace('/', ''),
0200            'TOKEN_UC': prop.getAttributeNS(None, "name").upper().replace("-", "_").replace(".", "_"),
0201            'name' : iface.getAttribute('name'),
0202            'prop' : prop.getAttribute('name'),
0203            })
0204 
0205 if __name__ == '__main__':
0206     argv = argv[1:]
0207     Generator(argv[0], argv[1], argv[2], xml.dom.minidom.parse(argv[3]))()