def construct_extension_sources(modulename, parse_files, include_dirs, build_dir): """ Construct wrapper sources. """ from py_wrap import PythonWrapperModule f90_modules = [] external_subprograms = [] for filename in parse_files: if not os.path.isfile(filename): sys.stderr.write('No or not a file %r. Skipping.\n' % (filename)) continue sys.stderr.write('Parsing %r..\n' % (filename)) for block in parse(filename, include_dirs=include_dirs).content: if isinstance(block, Module): f90_modules.append(block) elif isinstance(block, (Subroutine, Function)): external_subprograms.append(block) else: sys.stderr.write("Unhandled structure: %r\n" % (block.__class__)) module_infos = [] for block in f90_modules: wrapper = PythonWrapperModule(block.name) wrapper.add(block) c_code = wrapper.c_code() f_code = '! -*- f90 -*-\n' + wrapper.fortran_code() c_fn = os.path.join(build_dir,'%smodule.c' % (block.name)) f_fn = os.path.join(build_dir,'%s_f_wrappers_f2py.f90' % (block.name)) f = open(c_fn,'w') f.write(c_code) f.close() f = open(f_fn,'w') f.write(f_code) f.close() #f_lib = '%s_f_wrappers_f2py' % (block.name) module_info = {'name':block.name, 'c_sources':[c_fn], 'f_sources':[f_fn], 'language':'f90'} module_infos.append(module_info) if external_subprograms: wrapper = PythonWrapperModule(modulename) for block in external_subprograms: wrapper.add(block) c_code = wrapper.c_code() f_code = wrapper.fortran_code() c_fn = os.path.join(build_dir,'%smodule.c' % (modulename)) ext = '.f' language = 'f77' if wrapper.isf90: f_code = '! -*- f90 -*-\n' + f_code ext = '.f90' language = 'f90' f_fn = os.path.join(build_dir,'%s_f_wrappers_f2py%s' % (modulename, ext)) f = open(c_fn,'w') f.write(c_code) f.close() f = open(f_fn,'w') f.write(f_code) f.close() module_info = {'name':modulename, 'c_sources':[c_fn], 'f_sources':[f_fn], 'language':language} module_infos.append(module_info) return module_infos
def dump_signature(sys_argv): """ Read Fortran files and dump the signatures to file or stdout. XXX: Not well tested. """ signature_output = get_signature_output(sys_argv) # initialize output stream if signature_output in ['stdout','stderr']: output_stream = getattr(sys, signature_output) modulename = get_option_value(sys_argv,'-m','untitled','unknown') else: name,ext = os.path.splitext(signature_output) if ext != '.pyf': signature_output += '.pyf' if os.path.isfile(signature_output): overwrite = get_option(sys_argv, '--overwrite-signature', False) if not overwrite: print >> sys.stderr, 'Signature file %r exists. '\ 'Use --overwrite-signature to overwrite.' % (signature_output) sys.exit() modulename = get_option_value(sys_argv,'-m',os.path.basename(name), os.path.basename(name)) output_stream = open(signature_output,'w') flag = 'file' file_names = [] only_names = [] skip_names = [] options = [] for word in sys_argv: if word=='': pass elif word=='only:': flag = 'only' elif word=='skip:': flag = 'skip' elif word==':': flag = 'file' elif word.startswith('--'): options.append(word) else: {'file': file_names,'only': only_names, 'skip': skip_names}[flag].append(word) if options: sys.stderr.write('Unused options: %s\n' % (', '.join(options))) output_stream.write('''! -*- f90 -*- ! Note: the context of this file is case sensitive. ''') output_stream.write('PYTHON MODULE %s\n' % (modulename)) output_stream.write(' INTERFACE\n\n') for filename in file_names: if not os.path.isfile(filename): sys.stderr.write('No or not a file %r. Skipping.\n' % (filename)) continue sys.stderr.write('Parsing %r..\n' % (filename)) block = parse(filename) if block is None: sys.exit(1) output_stream.write('! File: %s, source mode = %r\n' % (filename, block.reader.mode)) if block.content and isinstance(block.content[0],PythonModule): for subblock in block.content[0].content[0].content: if isinstance(subblock, EndStatement): break output_stream.write(subblock.topyf(' ')+'\n') else: output_stream.write(block.topyf(' ')+'\n') output_stream.write(' END INTERFACE\n') output_stream.write('END PYTHON MODULE %s\n' % (modulename)) if signature_output not in ['stdout','stderr']: output_stream.close() return
def construct_extension_sources(modulename, parse_files, include_dirs, build_dir): """ Construct wrapper sources. """ from py_wrap import PythonWrapperModule f90_modules = [] external_subprograms = [] for filename in parse_files: if not os.path.isfile(filename): sys.stderr.write('No or not a file %r. Skipping.\n' % (filename)) continue sys.stderr.write('Parsing %r..\n' % (filename)) for block in parse(filename, include_dirs=include_dirs).content: if isinstance(block, Module): f90_modules.append(block) elif isinstance(block, (Subroutine, Function)): external_subprograms.append(block) else: sys.stderr.write("Unhandled structure: %r\n" % (block.__class__)) module_infos = [] for block in f90_modules: wrapper = PythonWrapperModule(block.name) wrapper.add(block) c_code = wrapper.c_code() f_code = '! -*- f90 -*-\n' + wrapper.fortran_code() c_fn = os.path.join(build_dir, '%smodule.c' % (block.name)) f_fn = os.path.join(build_dir, '%s_f_wrappers_f2py.f90' % (block.name)) f = open(c_fn, 'w') f.write(c_code) f.close() f = open(f_fn, 'w') f.write(f_code) f.close() #f_lib = '%s_f_wrappers_f2py' % (block.name) module_info = { 'name': block.name, 'c_sources': [c_fn], 'f_sources': [f_fn], 'language': 'f90' } module_infos.append(module_info) if external_subprograms: wrapper = PythonWrapperModule(modulename) for block in external_subprograms: wrapper.add(block) c_code = wrapper.c_code() f_code = wrapper.fortran_code() c_fn = os.path.join(build_dir, '%smodule.c' % (modulename)) ext = '.f' language = 'f77' if wrapper.isf90: f_code = '! -*- f90 -*-\n' + f_code ext = '.f90' language = 'f90' f_fn = os.path.join(build_dir, '%s_f_wrappers_f2py%s' % (modulename, ext)) f = open(c_fn, 'w') f.write(c_code) f.close() f = open(f_fn, 'w') f.write(f_code) f.close() module_info = { 'name': modulename, 'c_sources': [c_fn], 'f_sources': [f_fn], 'language': language } module_infos.append(module_info) return module_infos
def dump_signature(sys_argv): """ Read Fortran files and dump the signatures to file or stdout. XXX: Not well tested. """ signature_output = get_signature_output(sys_argv) # initialize output stream if signature_output in ["stdout", "stderr"]: output_stream = getattr(sys, signature_output) modulename = get_option_value(sys_argv, "-m", "untitled", "unknown") else: name, ext = os.path.splitext(signature_output) if ext != ".pyf": signature_output += ".pyf" if os.path.isfile(signature_output): overwrite = get_option(sys_argv, "--overwrite-signature", False) if not overwrite: print >> sys.stderr, "Signature file %r exists. " "Use --overwrite-signature to overwrite." % ( signature_output ) sys.exit() modulename = get_option_value(sys_argv, "-m", os.path.basename(name), os.path.basename(name)) output_stream = open(signature_output, "w") flag = "file" file_names = [] only_names = [] skip_names = [] options = [] for word in sys_argv: if word == "": pass elif word == "only:": flag = "only" elif word == "skip:": flag = "skip" elif word == ":": flag = "file" elif word.startswith("--"): options.append(word) else: {"file": file_names, "only": only_names, "skip": skip_names}[flag].append(word) if options: sys.stderr.write("Unused options: %s\n" % (", ".join(options))) output_stream.write( """! -*- f90 -*- ! Note: the context of this file is case sensitive. """ ) output_stream.write("PYTHON MODULE %s\n" % (modulename)) output_stream.write(" INTERFACE\n\n") for filename in file_names: if not os.path.isfile(filename): sys.stderr.write("No or not a file %r. Skipping.\n" % (filename)) continue sys.stderr.write("Parsing %r..\n" % (filename)) block = parse(filename) if block is None: sys.exit(1) output_stream.write("! File: %s, source mode = %r\n" % (filename, block.reader.mode)) if block.content and isinstance(block.content[0], PythonModule): for subblock in block.content[0].content[0].content: if isinstance(subblock, EndStatement): break output_stream.write(subblock.topyf(" ") + "\n") else: output_stream.write(block.topyf(" ") + "\n") output_stream.write(" END INTERFACE\n") output_stream.write("END PYTHON MODULE %s\n" % (modulename)) if signature_output not in ["stdout", "stderr"]: output_stream.close() return
def dump_signature(sys_argv): """ Read Fortran files and dump the signatures to file or stdout. XXX: Not well tested. """ signature_output = get_signature_output(sys_argv) # initialize output stream if signature_output in ['stdout', 'stderr']: output_stream = getattr(sys, signature_output) modulename = get_option_value(sys_argv, '-m', 'untitled', 'unknown') else: name, ext = os.path.splitext(signature_output) if ext != '.pyf': signature_output += '.pyf' if os.path.isfile(signature_output): overwrite = get_option(sys_argv, '--overwrite-signature', False) if not overwrite: print >> sys.stderr, 'Signature file %r exists. '\ 'Use --overwrite-signature to overwrite.' % (signature_output) sys.exit() modulename = get_option_value(sys_argv, '-m', os.path.basename(name), os.path.basename(name)) output_stream = open(signature_output, 'w') flag = 'file' file_names = [] only_names = [] skip_names = [] options = [] for word in sys_argv: if word == '': pass elif word == 'only:': flag = 'only' elif word == 'skip:': flag = 'skip' elif word == ':': flag = 'file' elif word.startswith('--'): options.append(word) else: { 'file': file_names, 'only': only_names, 'skip': skip_names }[flag].append(word) if options: sys.stderr.write('Unused options: %s\n' % (', '.join(options))) output_stream.write('''! -*- f90 -*- ! Note: the context of this file is case sensitive. ''') output_stream.write('PYTHON MODULE %s\n' % (modulename)) output_stream.write(' INTERFACE\n\n') for filename in file_names: if not os.path.isfile(filename): sys.stderr.write('No or not a file %r. Skipping.\n' % (filename)) continue sys.stderr.write('Parsing %r..\n' % (filename)) block = parse(filename) if block is None: sys.exit(1) output_stream.write('! File: %s, source mode = %r\n' % (filename, block.reader.mode)) if block.content and isinstance(block.content[0], PythonModule): for subblock in block.content[0].content[0].content: if isinstance(subblock, EndStatement): break output_stream.write(subblock.topyf(' ') + '\n') else: output_stream.write(block.topyf(' ') + '\n') output_stream.write(' END INTERFACE\n') output_stream.write('END PYTHON MODULE %s\n' % (modulename)) if signature_output not in ['stdout', 'stderr']: output_stream.close() return