Ejemplo n.º 1
0
    def test_writer_indented(self):
        writer = PythonWriter()
        writer.write_line('def a()')
        with writer.indent():
            writer.write_line('x=1')
            writer.write_line('y=2')

        self.assertEqual(writer.get_string(), 'def a()\n\tx=1\n\ty=2\n')
Ejemplo n.º 2
0
 def test_writer_class_def(self):
     writer = PythonWriter()
     with writer.write_class('a'):
         with writer.write_def('__init__', ['self', 'i']):
             writer.write_line('self.i=i')
     self.assertEqual(
         writer.get_string(),
         '\n\nclass a:\n\tdef __init__(self, i):\n\t\tself.i=i\n\t\n')
Ejemplo n.º 3
0
 def test_writer_for(self):
     writer = PythonWriter()
     with writer.write_for('i', 'range(10)'):
         writer.write_line('j=i+1')
     self.assertEqual(writer.get_string(), 'for i in range(10):\n\tj=i+1\n')
Ejemplo n.º 4
0
 def test_writer_if(self):
     writer = PythonWriter()
     with writer.write_if('i==10'):
         writer.write_line('j=i+1')
     self.assertEqual(writer.get_string(), 'if i==10:\n\tj=i+1\n')
Ejemplo n.º 5
0
 def test_writer_while(self):
     writer = PythonWriter()
     with writer.write_while('i==10'):
         writer.write_line('i=i+1')
     self.assertEqual(writer.get_string(), 'while i==10:\n\ti=i+1\n')
Ejemplo n.º 6
0
 def test_writer_escape(self):
     writer = PythonWriter()
     s = writer.escaped('""""')
     writer.write_line(f'x="{s}"')
     self.assertEqual(writer.get_string(), 'x="\\\"\\\"\\\"\\\""')
Ejemplo n.º 7
0
 def test_writer_unindented(self):
     writer = PythonWriter()
     writer.write_line('x=1')
     writer.write_line('y=2')
     self.assertEqual(writer.get_string(), 'x=1\ny=2')
Ejemplo n.º 8
0
    def build_wrapper_for_structs_and_functions(self, header_name, enums,
                                                structs, functions):
        """Creates wrapper file for given list of FunctionDeclaration objects"""
        with open(header_name + '.py', 'w+') as f:
            writer = PythonWriter()
            if not self.wrap_dynamic_lib:
                writer.write_line('import warnings')
                writer.write_line('from .lib import _' + header_name)
                writer.write_line('from cffi import FFI')
                writer.write_line(f'ffi = _{header_name}.ffi')
            else:
                writer.write_line('import warnings')
                writer.write_line('from cffi import FFI')
                writer.write_line('ffi = FFI()')

                _ = writer.escaped('\n'.join(decl.declaration_string
                                             for decl in enums))
                struct_decls = writer.escaped('\n'.join(decl.declaration_string
                                                        for decl in structs))
                func_decls = writer.escaped('\n'.join(decl.declaration_string
                                                      for decl in functions))
                writer.write_line(f'ffi.cdef("""{struct_decls}""")')
                writer.write_line(f'ffi.cdef("""{func_decls}""")')
                writer.write_line('import os')
                writer.write_line('')

            writer.write_line('')

            self._build_wrapper_for_structs_and_functions(
                writer, header_name, enums, structs, functions)

            writer.write_line('')

            f.write(writer.get_string())