home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1410 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  2.7 KB  |  70 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  6. __docformat__ = 'restructuredtext en'
  7. import os
  8. import textwrap
  9. import re
  10. import subprocess
  11. INC = '/usr/include/ImageMagick'
  12.  
  13. def parse_enums(f):
  14.     print '\nParsing:', f
  15.     raw = open(os.path.join(INC, f)).read()
  16.     raw = re.sub('(?s)/\\*.*?\\*/', '', raw)
  17.     raw = re.sub('#.*', '', raw)
  18.     for enum in re.findall('typedef\\s+enum\\s+\\{([^}]+)', raw):
  19.         enum = re.sub('(?s)/\\*.*?\\*/', '', enum)
  20.         for x in enum.splitlines():
  21.             e = x.split(',')[0].strip().split(' ')[0]
  22.             if e:
  23.                 val = get_value(e)
  24.                 print e, val
  25.                 yield (e, val)
  26.                 continue
  27.         
  28.     
  29.  
  30.  
  31. def get_value(const):
  32.     t = '\n    #include <wand/MagickWand.h>\n    #include <stdio.h>\n    int main(int argc, char **argv) {\n    printf("%%d", %s);\n    return 0;\n    }\n    ' % const
  33.     
  34.     try:
  35.         f = _[1]
  36.         f.write(t)
  37.     finally:
  38.         pass
  39.  
  40.     subprocess.check_call([
  41.         'gcc',
  42.         '-I/usr/include/ImageMagick',
  43.         '/tmp/ig.c',
  44.         '-o',
  45.         '/tmp/ig',
  46.         '-lMagickWand'])
  47.     return int(subprocess.Popen([
  48.         '/tmp/ig'], stdout = subprocess.PIPE).communicate()[0].strip())
  49.  
  50.  
  51. def main():
  52.     constants = []
  53.     for x in ('resample', 'image', 'draw', 'distort', 'composite', 'geometry', 'colorspace'):
  54.         constants += list(parse_enums('magick/%s.h' % x))
  55.     
  56.     base = os.path.dirname(__file__)
  57.     constants = [ 'PyModule_AddIntConstant(m, "{0}", {1});'.format(c, v) for c, v in constants ]
  58.     raw = textwrap.dedent('        // Generated by generate.py\n\n        static void magick_add_module_constants(PyObject *m) {\n            %s\n        }\n        ') % '\n    '.join(constants)
  59.     
  60.     try:
  61.         f = _[2]
  62.         f.write(raw)
  63.     finally:
  64.         pass
  65.  
  66.  
  67. if __name__ == '__main__':
  68.     main()
  69.  
  70.