|
| 1 | + |
| 2 | +import re |
| 3 | +import copy |
| 4 | +from platform import os |
| 5 | + |
| 6 | +def refactor_interfaces(file_name,interface_module): |
| 7 | + |
| 8 | + # Parse whole file |
| 9 | + file_body = [] |
| 10 | + comment_block = False |
| 11 | + comment_body = [] |
| 12 | + is_sub = False |
| 13 | + is_fun = False |
| 14 | + is_interface = False |
| 15 | + |
| 16 | + # FiLoad whole file; split by lines; join concatenation lines |
| 17 | + with open(os.path.join(file_name), 'r') as file: |
| 18 | + # Create an empty list to store the lines |
| 19 | + |
| 20 | + # Iterate over the lines of the file |
| 21 | + for line in file: |
| 22 | + |
| 23 | + lsl = line.strip() |
| 24 | + |
| 25 | + is_comment = lsl.startswith('!>') |
| 26 | + if not interface_module: |
| 27 | + is_sub = bool(re.match(r'(?:.)*subroutine\s+stdlib_(\w+)',line)) |
| 28 | + is_fun = bool(re.match(r'(?:.)*function stdlib_(\w+)',line)) |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + else: |
| 33 | + is_interface = lsl.startswith('interface') |
| 34 | + |
| 35 | + if is_comment: |
| 36 | + # Start saving this new comment block |
| 37 | + if not comment_block: comment_body = [] |
| 38 | + |
| 39 | + |
| 40 | + # At the beginnging of a comment block, do not include empty comments |
| 41 | + if lsl=='!> !' or lsl=='!>': |
| 42 | + comment_block = False |
| 43 | + line = '' |
| 44 | + else: |
| 45 | + comment_block = True |
| 46 | + comment_body.append(line) |
| 47 | + |
| 48 | + elif is_interface or is_sub or is_fun: |
| 49 | + # Comment is over and we're now at an interface: append interface line, follow |
| 50 | + # documentaion |
| 51 | + file_body.append(line) |
| 52 | + |
| 53 | + if is_interface: |
| 54 | + interface_name = re.search(r'interface (\w+)',line).group(1) |
| 55 | + elif is_sub: |
| 56 | + print(line) |
| 57 | + interface_name = re.search(r'(?:.)*subroutine\s+stdlib_(\w+)',line).group(1) |
| 58 | + elif is_fun: |
| 59 | + print(line) |
| 60 | + |
| 61 | + interface_name = re.search(r'(?:.)*function stdlib_(\w+)',line).group(1) |
| 62 | + |
| 63 | + axpy = interface_name.strip().upper() |
| 64 | + search_label = r'!> '+axpy+r':\s*' |
| 65 | + |
| 66 | + if not comment_body is None: |
| 67 | + for k in range(len(comment_body)): |
| 68 | + |
| 69 | + nointerf = re.sub(search_label,r'!> '+axpy+' ',comment_body[k]) |
| 70 | + nointerf = re.sub(r'!> ',r'!! ',nointerf) |
| 71 | + file_body.append(nointerf) |
| 72 | + |
| 73 | + comment_body = [] |
| 74 | + |
| 75 | + else: |
| 76 | + # Regular body: just append line |
| 77 | + file_body.append(line) |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + # print file out |
| 82 | + fid = open(file_name,"w") |
| 83 | + |
| 84 | + # Header |
| 85 | + fid.write(''.join(file_body)) |
| 86 | + fid.close() |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +# Run refactor |
| 91 | +refactor_interfaces('stdlib_linalg_blas.fypp',True) |
| 92 | +refactor_interfaces('stdlib_linalg_blas_aux.fypp',False) |
| 93 | +refactor_interfaces('stdlib_linalg_blas_s.fypp',False) |
| 94 | +refactor_interfaces('stdlib_linalg_blas_d.fypp',False) |
| 95 | +refactor_interfaces('stdlib_linalg_blas_q.fypp',False) |
| 96 | +refactor_interfaces('stdlib_linalg_blas_c.fypp',False) |
| 97 | +refactor_interfaces('stdlib_linalg_blas_z.fypp',False) |
| 98 | +refactor_interfaces('stdlib_linalg_blas_w.fypp',False) |
| 99 | +refactor_interfaces('stdlib_linalg_lapack.fypp',True) |
| 100 | +refactor_interfaces('stdlib_linalg_lapack_aux.fypp',False) |
| 101 | +refactor_interfaces('stdlib_linalg_lapack_s.fypp',False) |
| 102 | +refactor_interfaces('stdlib_linalg_lapack_d.fypp',False) |
| 103 | +refactor_interfaces('stdlib_linalg_lapack_q.fypp',False) |
| 104 | +refactor_interfaces('stdlib_linalg_lapack_c.fypp',False) |
| 105 | +refactor_interfaces('stdlib_linalg_lapack_z.fypp',False) |
| 106 | +refactor_interfaces('stdlib_linalg_lapack_w.fypp',False) |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
0 commit comments