home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / lib / site-packages / gtk-2.0 / codegen / override.py < prev    next >
Encoding:
Python Source  |  2007-11-01  |  9.0 KB  |  282 lines

  1. # -*- Mode: Python; py-indent-offset: 4 -*-
  2.  
  3. # this file contains code for loading up an override file.  The override file
  4. # provides implementations of functions where the code generator could not
  5. # do its job correctly.
  6.  
  7. import fnmatch
  8. import os
  9. import re
  10. import string
  11. import sys
  12.  
  13. def class2cname(klass, method):
  14.     c_name = ''
  15.     for c in klass:
  16.         if c.isupper():
  17.             c_name += '_' + c.lower()
  18.         else:
  19.             c_name += c
  20.     return c_name[1:] + '_'  + method
  21.  
  22. import_pat = re.compile(r'\s*import\s+(\S+)\.([^\s.]+)\s+as\s+(\S+)')
  23.  
  24. class Overrides:
  25.     def __init__(self, filename=None):
  26.         self.modulename = None
  27.         self.ignores = {}
  28.         self.glob_ignores = []
  29.         self.type_ignores = {}
  30.         self.overrides = {}
  31.         self.overridden = {}
  32.         self.kwargs = {}
  33.         self.noargs = {}
  34.         self.onearg = {}
  35.         self.staticmethod = {}
  36.         self.classmethod = {}
  37.         self.startlines = {}
  38.         self.override_attrs = {}
  39.         self.override_slots = {}
  40.         self.headers = ''
  41.         self.body = ''
  42.         self.init = ''
  43.         self.imports = []
  44.         self.defines = {}
  45.         self.functions = {}
  46.         self.newstyle_constructors = {}
  47.         self.dynamicnamespace = False
  48.         if filename:
  49.             self.handle_file(filename)
  50.  
  51.     def handle_file(self, filename):
  52.         oldpath = os.getcwd()
  53.  
  54.         fp = open(filename, 'r')
  55.         dirname = os.path.dirname(os.path.abspath(filename))
  56.  
  57.         if dirname != oldpath:
  58.             os.chdir(dirname)
  59.  
  60.         # read all the components of the file ...
  61.         bufs = []
  62.         startline = 1
  63.         lines = []
  64.         line = fp.readline()
  65.         linenum = 1
  66.         while line:
  67.             if line == '%%\n' or line == '%%':
  68.                 if lines:
  69.                     bufs.append((string.join(lines, ''), startline))
  70.                 startline = linenum + 1
  71.                 lines = []
  72.             else:
  73.                 lines.append(line)
  74.             line = fp.readline()
  75.             linenum = linenum + 1
  76.         if lines:
  77.             bufs.append((string.join(lines, ''), startline))
  78.         if not bufs: return
  79.  
  80.         for buf, startline in bufs:
  81.             self.__parse_override(buf, startline, filename)
  82.  
  83.         os.chdir(oldpath)
  84.  
  85.     def __parse_override(self, buffer, startline, filename):
  86.         pos = string.find(buffer, '\n')
  87.         if pos >= 0:
  88.             line = buffer[:pos]
  89.             rest = buffer[pos+1:]
  90.         else:
  91.             line = buffer ; rest = ''
  92.         words = string.split(line)
  93.         command = words[0]
  94.         if (command == 'ignore' or
  95.             command == 'ignore-' + sys.platform):
  96.             "ignore/ignore-platform [functions..]"
  97.             for func in words[1:]:
  98.                 self.ignores[func] = 1
  99.             for func in string.split(rest):
  100.                 self.ignores[func] = 1
  101.         elif (command == 'ignore-glob' or
  102.               command == 'ignore-glob-' + sys.platform):
  103.             "ignore-glob/ignore-glob-platform [globs..]"
  104.             for func in words[1:]:
  105.                 self.glob_ignores.append(func)
  106.             for func in string.split(rest):
  107.                 self.glob_ignores.append(func)
  108.         elif (command == 'ignore-type' or
  109.               command == 'ignore-type-' + sys.platform):
  110.             "ignore-type/ignore-type-platform [typenames..]"
  111.             for typename in words[1:]:
  112.                 self.type_ignores[typename] = 1
  113.             for typename in string.split(rest):
  114.                 self.type_ignores[typename] = 1
  115.         elif command == 'override':
  116.             "override function/method [kwargs|noargs|onearg] [staticmethod|classmethod]"
  117.             func = words[1]
  118.             if 'kwargs' in words[1:]:
  119.                 self.kwargs[func] = 1
  120.             elif 'noargs' in words[1:]:
  121.                 self.noargs[func] = 1
  122.             elif 'onearg' in words[1:]:
  123.                 self.onearg[func] = True
  124.  
  125.             if 'staticmethod' in words[1:]:
  126.                 self.staticmethod[func] = True
  127.             elif 'classmethod' in words[1:]:
  128.                 self.classmethod[func] = True
  129.             if func in self.overrides:
  130.                 raise RuntimeError("Function %s is being overridden more than once" % (func,))
  131.             self.overrides[func] = rest
  132.             self.startlines[func] = (startline + 1, filename)
  133.         elif command == 'override-attr':
  134.             "override-slot Class.attr"
  135.             attr = words[1]
  136.             self.override_attrs[attr] = rest
  137.             self.startlines[attr] = (startline + 1, filename)
  138.         elif command == 'override-slot':
  139.             "override-slot Class.slot"
  140.             slot = words[1]
  141.             self.override_slots[slot] = rest
  142.             self.startlines[slot] = (startline + 1, filename)
  143.         elif command == 'headers':
  144.             "headers"
  145.             self.headers = '%s\n#line %d "%s"\n%s' % \
  146.                            (self.headers, startline + 1, filename, rest)
  147.         elif command == 'body':
  148.             "body"
  149.             self.body = '%s\n#line %d "%s"\n%s' % \
  150.                            (self.body, startline + 1, filename, rest)
  151.         elif command == 'init':
  152.             "init"
  153.             self.init = '%s\n#line %d "%s"\n%s' % \
  154.                         (self.init, startline + 1, filename, rest)
  155.         elif command == 'modulename':
  156.             "modulename name"
  157.             self.modulename = words[1]
  158.         elif command == 'include':
  159.             "include filename"
  160.             for filename in words[1:]:
  161.                 self.handle_file(filename)
  162.             for filename in string.split(rest):
  163.                 self.handle_file(filename)
  164.         elif command == 'import':
  165.             "import module1 [\n module2, \n module3 ...]"
  166.             for line in string.split(buffer, '\n'):
  167.                 match = import_pat.match(line)
  168.                 if match:
  169.                     self.imports.append(match.groups())
  170.         elif command == 'define':
  171.             "define funcname [kwargs|noargs|onearg] [classmethod|staticmethod]"
  172.             "define Class.method [kwargs|noargs|onearg] [classmethod|staticmethod]"
  173.             func = words[1]
  174.             klass = None
  175.             if func.find('.') != -1:
  176.                 klass, func = func.split('.', 1)
  177.  
  178.                 if not self.defines.has_key(klass):
  179.                     self.defines[klass] = {}
  180.                 self.defines[klass][func] = rest
  181.             else:
  182.                 self.functions[func] = rest
  183.  
  184.             if 'kwargs' in words[1:]:
  185.                 self.kwargs[func] = 1
  186.             elif 'noargs' in words[1:]:
  187.                 self.noargs[func] = 1
  188.             elif 'onearg' in words[1:]:
  189.                 self.onearg[func] = 1
  190.  
  191.             if 'staticmethod' in words[1:]:
  192.                 self.staticmethod[func] = True
  193.             elif 'classmethod' in words[1:]:
  194.                 self.classmethod[func] = True
  195.  
  196.             self.startlines[func] = (startline + 1, filename)
  197.  
  198.         elif command == 'new-constructor':
  199.             "new-constructor GType"
  200.             gtype, = words[1:]
  201.             self.newstyle_constructors[gtype] = True
  202.         elif command == 'options':
  203.             for option in words[1:]:
  204.                 if option == 'dynamicnamespace':
  205.                     self.dynamicnamespace = True
  206.  
  207.     def is_ignored(self, name):
  208.         if self.ignores.has_key(name):
  209.             return 1
  210.         for glob in self.glob_ignores:
  211.             if fnmatch.fnmatchcase(name, glob):
  212.                 return 1
  213.         return 0
  214.  
  215.     def is_type_ignored(self, name):
  216.         return name in self.type_ignores
  217.  
  218.     def is_overriden(self, name):
  219.         return self.overrides.has_key(name)
  220.  
  221.     def is_already_included(self, name):
  222.         return self.overridden.has_key(name)
  223.  
  224.     def override(self, name):
  225.         self.overridden[name] = 1
  226.         return self.overrides[name]
  227.  
  228.     def define(self, klass, name):
  229.         self.overridden[class2cname(klass, name)] = 1
  230.         return self.defines[klass][name]
  231.  
  232.     def function(self, name):
  233.         return self.functions[name]
  234.  
  235.     def getstartline(self, name):
  236.         return self.startlines[name]
  237.  
  238.     def wants_kwargs(self, name):
  239.         return self.kwargs.has_key(name)
  240.  
  241.     def wants_noargs(self, name):
  242.         return self.noargs.has_key(name)
  243.  
  244.     def wants_onearg(self, name):
  245.         return self.onearg.has_key(name)
  246.  
  247.     def is_staticmethod(self, name):
  248.         return self.staticmethod.has_key(name)
  249.  
  250.     def is_classmethod(self, name):
  251.         return self.classmethod.has_key(name)
  252.  
  253.     def attr_is_overriden(self, attr):
  254.         return self.override_attrs.has_key(attr)
  255.  
  256.     def attr_override(self, attr):
  257.         return self.override_attrs[attr]
  258.  
  259.     def slot_is_overriden(self, slot):
  260.         return self.override_slots.has_key(slot)
  261.  
  262.     def slot_override(self, slot):
  263.         return self.override_slots[slot]
  264.  
  265.     def get_headers(self):
  266.         return self.headers
  267.  
  268.     def get_body(self):
  269.         return self.body
  270.  
  271.     def get_init(self):
  272.         return self.init
  273.  
  274.     def get_imports(self):
  275.         return self.imports
  276.  
  277.     def get_defines_for(self, klass):
  278.         return self.defines.get(klass, {})
  279.  
  280.     def get_functions(self):
  281.         return self.functions
  282.