Beispiel #1
0
    def __init__(self,
                 mixin_modules=None,
                 binding_module='mesh.standard.python'):
        if isinstance(mixin_modules, basestring):
            mixin_modules = mixin_modules.split(' ')

        self.binding_module = binding_module
        self.formatter = StructureFormatter()
        self.mixin_modules = mixin_modules
Beispiel #2
0
    def __init__(self, mixin_modules=None, binding_module='mesh.standard.python'):
        if isinstance(mixin_modules, basestring):
            mixin_modules = mixin_modules.split(' ')

        self.binding_module = binding_module
        self.formatter = StructureFormatter()
        self.mixin_modules = mixin_modules
Beispiel #3
0
class BindingGenerator(object):
    """Generates python bindings for one or more mesh bundles.

    :param list mixin_modules: Optional, default is ``None``; a ``list`` of one or
        more mixin modules, specified as dotted package paths, to evaluate when
        generating bindings.

    :param str binding_module: Optional, default is ``mesh.standard.python``; the
        dotted package path of the module which should be used as the basis
        for any generated bindings.

    :param str specification_var: Optional, default is ``specification``; the
        name which should be used for the bundle specification in the
        generated bindings.
    """

    CONSTRUCTOR_PARAMS = ('mixin_modules', 'binding_module')
    MODULE_TMPL = get_package_data('mesh.binding', 'templates/module.py.tmpl')

    def __init__(self, mixin_modules=None, binding_module='mesh.standard.python'):
        if isinstance(mixin_modules, basestring):
            mixin_modules = mixin_modules.split(' ')

        self.binding_module = binding_module
        self.formatter = StructureFormatter()
        self.mixin_modules = mixin_modules

    def generate(self, bundle):
        if isinstance(bundle, basestring):
            bundle = import_object(bundle)

        source = self._generate_binding(bundle)
        return '%s.py' % bundle.name, source

    def generate_dynamically(self, bundle):
        if isinstance(bundle, basestring):
            bundle = import_object(bundle)

        source = self._generate_binding(bundle)
        module = new_module(bundle.name)

        exec source in module.__dict__
        return module

    def _generate_binding(self, bundle):
        specification = self.formatter.format(bundle.describe())
        mixins, mixin_classes = self._generate_mixins()

        return self.MODULE_TMPL % {
            'binding_module': self.binding_module,
            'mixins': mixins,
            'mixin_classes': mixin_classes,
            'specification': specification,
        }

    def _generate_mixins(self):
        if not self.mixin_modules:
            return '', ''

        mixins = []
        mixin_classes = []

        for name in self.mixin_modules:
            module = import_object(name)
            for attr in dir(module):
                value = getattr(module, attr)
                try:
                    targets = value.mixin
                except Exception:
                    continue
                mixins.append(getsource(value))
                mixin_classes.append(attr)

        return '\n'.join(mixins), ', '.join(mixin_classes)
Beispiel #4
0
 def run(self, runtime):
     description = self['bundle'].describe(self['targets'])
     content = StructureFormatter().format(description)
     self['path'].write_bytes(content)
Beispiel #5
0
class BindingGenerator(object):
    """Generates python bindings for one or more mesh bundles.

    :param list mixin_modules: Optional, default is ``None``; a ``list`` of one or
        more mixin modules, specified as dotted package paths, to evaluate when
        generating bindings.

    :param str binding_module: Optional, default is ``mesh.standard.python``; the
        dotted package path of the module which should be used as the basis
        for any generated bindings.

    :param str specification_var: Optiona, default is ``specification``; the
        name which should be used for the bundle specification in the
        generated bindings.
    """

    CONSTRUCTOR_PARAMS = ('mixin_modules', 'binding_module')
    MODULE_TMPL = get_package_data('mesh.binding', 'templates/module.py.tmpl')

    def __init__(self,
                 mixin_modules=None,
                 binding_module='mesh.standard.python'):
        if isinstance(mixin_modules, basestring):
            mixin_modules = mixin_modules.split(' ')

        self.binding_module = binding_module
        self.formatter = StructureFormatter()
        self.mixin_modules = mixin_modules

    def generate(self, bundle):
        if isinstance(bundle, basestring):
            bundle = import_object(bundle)

        source = self._generate_binding(bundle)
        return '%s.py' % bundle.name, source

    def generate_dynamically(self, bundle):
        if isinstance(bundle, basestring):
            bundle = import_object(bundle)

        source = self._generate_binding(bundle)
        module = new_module(bundle.name)

        exec source in module.__dict__
        return module

    def _generate_binding(self, bundle):
        specification = self.formatter.format(bundle.describe())
        mixins, mixin_classes = self._generate_mixins()

        return self.MODULE_TMPL % {
            'binding_module': self.binding_module,
            'mixins': mixins,
            'mixin_classes': mixin_classes,
            'specification': specification,
        }

    def _generate_mixins(self):
        if not self.mixin_modules:
            return '', ''

        mixins = []
        mixin_classes = []

        for name in self.mixin_modules:
            module = import_object(name)
            for attr in dir(module):
                value = getattr(module, attr)
                try:
                    targets = value.mixin
                except Exception:
                    continue
                mixins.append(getsource(value))
                mixin_classes.append(attr)

        return '\n'.join(mixins), ', '.join(mixin_classes)