#!/usr/bin/env python import string,re,os def all_subroutines(interface_in): # remove comments comment_block_exp = re.compile(r'/\*(?:\s|.)*?\*/') subroutine_exp = re.compile(r'subroutine (?:\s|.)*?end subroutine.*') function_exp = re.compile(r'function (?:\s|.)*?end function.*') interface = comment_block_exp.sub('',interface_in) subroutine_list = subroutine_exp.findall(interface) function_list = function_exp.findall(interface) #function_list = [] subroutine_list = subroutine_list + function_list subroutine_list = map(lambda x: string.strip(x),subroutine_list) return subroutine_list def real_convert(val_string): return val_string def complex_convert(val_string): return '(' + val_string + ',0.)' def convert_types(interface_in,converter): regexp = re.compile(r'') interface = interface_in[:] while 1: sub = regexp.search(interface) if sub is None: break converted = converter(sub.group(1)) interface = string.replace(interface,sub.group(),converted) return interface def generic_expand(generic_interface): generic_types ={'s' :('real', 'real', real_convert, 'real'), 'd' :('double precision','double precision',real_convert, 'double precision'), 'c' :('complex', 'complex',complex_convert, 'real'), 'z' :('double complex', 'double complex',complex_convert, 'double precision'), 'sc':('complex', 'real',real_convert, 'real'), 'dz':('double complex', 'double precision',real_convert, 'double precision'), 'cs':('real', 'complex',complex_convert, 'real'), 'zd':('double precision','double complex', complex_convert, 'double precision')} #2. get all subroutines subs = all_subroutines(generic_interface) print len(subs) #loop through the subs type_exp = re.compile(r'') interface = '' for sub in subs: #3. Find the typecodes to use: m = type_exp.search(sub) if m is None: interface = interface + '\n\n' + sub continue type_chars = m.group(1) # get rid of spaces type_chars = string.replace(type_chars,' ','') # get a list of the characters (or character pairs) type_chars = string.split(type_chars,',') #print type_chars # Now get rid of the special tag that contained the types sub = re.sub(type_exp,'',sub) sub_generic = string.strip(sub) for char in type_chars: type_in,type_out,converter, rtype_in = generic_types[char] sub = convert_types(sub_generic,converter) function_def = string.replace(sub,'',char) function_def = string.replace(function_def,'',type_in) #function_def = string.replace(function_def,'',rtype_in) #function_def = string.replace(function_def,'',type_out) interface = interface + '\n\n' + function_def return interface #def interface_to_module(interface_in,module_name,include_list,sdir='.'): def interface_to_module(interface_in,module_name): pre_prefix = "!%f90 -*- f90 -*-\n" # includes = '' # for file in include_list: # f = open(os.path.join(sdir,file)) # includes += f.read(-1) # f.close() # heading and tail of the module definition. file_prefix = "\npython module " + module_name +" ! in\n" \ " interface \n" file_suffix = "\n end interface\n" \ "end module %s" % module_name #return pre_prefix + includes + file_prefix + interface_in + file_suffix return pre_prefix + file_prefix + interface_in + file_suffix def generate_clapack(sdir,output_path='.'): print "generating clapack interface" f = open(os.path.join(sdir,'generic_clapack.pyf')) module_name = 'clapack' generic_interface = f.read(-1) f.close() generic_interface = generic_expand(generic_interface) module_def = interface_to_module(generic_interface,module_name) f = open(os.path.join(output_path,module_name+'.pyf'),'w') f.write(module_def) f.close() def generate_flapack(sdir,output_path='.'): print "generating flapack interface" f = open(os.path.join(sdir,'generic_flapack.pyf')) module_name = 'flapack' generic_interface = f.read(-1) f.close() generic_interface = generic_expand(generic_interface) module_def = interface_to_module(generic_interface,module_name) f = open(os.path.join(output_path,module_name+'.pyf'),'w') f.write(module_def) f.close() def process_all(): # process the standard files. generate_clapack('.') generate_flapack('.') if __name__ == "__main__": process_all()