sip_tools.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import os
  2. from common import *
  3. class SIPBuilder:
  4. def __init__(self, doxyparse, outputdir):
  5. self.doxyparser = doxyparse
  6. self.output_dir = outputdir
  7. def make_bindings(self):
  8. output_dir = os.path.abspath(os.path.join(self.output_dir, "sip"))
  9. if not os.path.exists(output_dir):
  10. os.makedirs(output_dir)
  11. for aclass in self.doxyparser.classes:
  12. if aclass.name in excluded_classes:
  13. print "Skipping %s" % aclass.name
  14. continue
  15. header_name = aclass.name[2:].lower()
  16. filename = os.path.join(output_dir, "_" + header_name + ".sip")
  17. enums_text = make_enums(aclass)
  18. method_text = self.make_sip_methods(aclass)
  19. base_class = get_first_value(aclass.bases)
  20. if base_class != "":
  21. base_class = ": %s" % base_class
  22. text = """
  23. %s
  24. class %s %s
  25. {
  26. %%TypeHeaderCode
  27. #include <%s>
  28. %%End
  29. public:
  30. %s
  31. };
  32. """ % (enums_text, aclass.name, base_class, get_first_value(aclass.includes), method_text)
  33. afile = open(filename, "wb")
  34. afile.write(text)
  35. afile.close()
  36. def make_sip_methods(self, aclass):
  37. retval = ""
  38. for amethod in aclass.constructors + aclass.methods:
  39. transfer = ""
  40. # FIXME: we need to come up with a way of filtering the methods out by various criteria
  41. # including parameters and method name, and how to deal with overloads
  42. if aclass.name in ignored_methods:
  43. should_ignore = False
  44. for method in ignored_methods[aclass.name]:
  45. print "method = %s" % method
  46. if method == amethod.name:
  47. params = ignored_methods[aclass.name][method]
  48. should_ignore = True
  49. for i in xrange(len(params)):
  50. if i >= len(amethod.params):
  51. should_ignore = False
  52. break
  53. elif amethod.params[i]["type"] != params[i]:
  54. print "param type = %s, amethod.param type = %s" % (params[i], amethod.params[i]["type"])
  55. should_ignore = False
  56. break
  57. if should_ignore:
  58. continue
  59. # We need to let SIP know when wx is responsible for deleting the object.
  60. # We do this if the class is derived from wxWindow, since wxTLW manages child windows
  61. # and wxApp deletes all wxTLWs on shutdown
  62. if amethod in aclass.constructors and self.doxyparser.is_derived_from_base(aclass, "wxWindow"):
  63. transfer = "/Transfer/"
  64. if amethod.name.startswith("operator"):
  65. continue
  66. retval += " %s %s%s%s;\n\n" % (amethod.return_type.replace("virtual ", ""), amethod.name, amethod.argsstring, transfer)
  67. return retval