<commit_before>#!/usr/bin/env python

'''Generate a Python ctypes wrapper file for a header file.

Usage example::
    wrap.py -lGL -oGL.py /usr/include/GL/gl.h

    >>> from GL import *

'''

__docformat__ = 'restructuredtext'
__version__ = '$Id$'

from ctypesparser import *

class CtypesWrapper(CtypesParser):
    def __init__(self, library, file):
        super(CtypesWrapper, self).__init__()
        self.library = library
        self.file = file
        self.all_names = []
        self.known_types = {}

    def wrap(self, filename, source=None):
        self.filename = filename
        self.print_preamble()
        self.parse(filename, source)
        self.print_epilogue()

    def does_emit(self, symbol, filename):
        return filename == self.filename

    def print_preamble(self):
        import textwrap
        import time
        print >> self.file, textwrap.dedent("""
            '''Wrapper for %(library)s
            
            Generated %(date)s by %(class)s.  
            Do not modify this file.
            '''

            __docformat__ =  'restructuredtext'
            __version__ = '$Id$'

            import ctypes
            from ctypes import *
            from ctypes.util import find_library as _find_library

            _libpath = _find_library(%(library)r)
            if not _libpath:
                raise ImportError('Could not locate %(library)s library')
            _lib = cdll.LoadLibrary(_libpath)

            _int_types = (c_int16, c_int32)
            if hasattr(ctypes, 'c_int64'):
                # Some builds of ctypes apparently do not have c_int64
                # defined; it's a pretty good bet that these builds do not
                # have 64-bit pointers.
                _int_types += (ctypes.c_int64,)
            for t in _int_types:
                if sizeof(t) == sizeof(c_size_t):
                    c_ptrdiff_t = t
        """ % {
            'library': self.library,
            'date': time.ctime(),
            'class': self.__class__.__name__,
        }).lstrip()

    def print_epilogue(self):
        print >> self.file, '__all__ = [%s]' % \
            ','.join([repr(n) for n in self.all_names])

    def handle_ctypes_constant(self, name, value, filename, lineno):
        if self.does_emit(name, filename):
            print >> self.file, '%s = %r' % (name, value),
            print >> self.file, '\t# %s:%d' % (filename, lineno)
            self.all_names.append(name)<commit_msg>Wrap __all__<commit_after>#!/usr/bin/env python

'''Generate a Python ctypes wrapper file for a header file.

Usage example::
    wrap.py -lGL -oGL.py /usr/include/GL/gl.h

    >>> from GL import *

'''

__docformat__ = 'restructuredtext'
__version__ = '$Id$'

from ctypesparser import *
import textwrap

class CtypesWrapper(CtypesParser):
    def __init__(self, library, file):
        super(CtypesWrapper, self).__init__()
        self.library = library
        self.file = file
        self.all_names = []
        self.known_types = {}

    def wrap(self, filename, source=None):
        self.filename = filename
        self.print_preamble()
        self.parse(filename, source)
        self.print_epilogue()

    def does_emit(self, symbol, filename):
        return filename == self.filename

    def print_preamble(self):
        import textwrap
        import time
        print >> self.file, textwrap.dedent("""
            '''Wrapper for %(library)s
            
            Generated %(date)s by %(class)s.  
            Do not modify this file.
            '''

            __docformat__ =  'restructuredtext'
            __version__ = '$Id$'

            import ctypes
            from ctypes import *
            from ctypes.util import find_library as _find_library

            _libpath = _find_library(%(library)r)
            if not _libpath:
                raise ImportError('Could not locate %(library)s library')
            _lib = cdll.LoadLibrary(_libpath)

            _int_types = (c_int16, c_int32)
            if hasattr(ctypes, 'c_int64'):
                # Some builds of ctypes apparently do not have c_int64
                # defined; it's a pretty good bet that these builds do not
                # have 64-bit pointers.
                _int_types += (ctypes.c_int64,)
            for t in _int_types:
                if sizeof(t) == sizeof(c_size_t):
                    c_ptrdiff_t = t
        """ % {
            'library': self.library,
            'date': time.ctime(),
            'class': self.__class__.__name__,
        }).lstrip()

    def print_epilogue(self):
        print >> self.file
        print >> self.file,  '\n'.join(textwrap.wrap(
            '__all__ = [%s]' % ', '.join([repr(n) for n in self.all_names]),
            width=78,
            break_long_words=False))

    def handle_ctypes_constant(self, name, value, filename, lineno):
        if self.does_emit(name, filename):
            print >> self.file, '%s = %r' % (name, value),
            print >> self.file, '\t# %s:%d' % (filename, lineno)
            self.all_names.append(name)
{'fraction_non_alphanumeric': 0.11299546440544271, 'fraction_numerical': 0.005127193847367383, 'mean_word_length': 1.8916761687571266, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6239504', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1493, 'n_words': 383}