1
- from .preprocessor import FortranPreprocessor
2
1
import re
3
2
3
+ from .preprocessor import FortranPreprocessor
4
4
from .smartopen import smart_open
5
5
6
6
UNIT_REGEX = re .compile (
@@ -50,20 +50,20 @@ def __init__(
50
50
contents = f .read ()
51
51
52
52
preprocessor = FortranPreprocessor ()
53
+ macros = macros or {}
53
54
54
- if macros :
55
- if isinstance (macros , dict ):
56
- for k , v in macros .items ():
57
- preprocessor .define ("{} {}" .format (k , v ))
58
- else :
59
- if not isinstance (macros , list ):
60
- macros = [macros ]
61
- for macro in macros :
62
- if "=" in macro :
63
- temp = macro .split ("=" )
64
- preprocessor .define ("{} {}" .format (* temp ))
65
- else :
66
- preprocessor .define (macro )
55
+ if isinstance (macros , dict ):
56
+ for k , v in macros .items ():
57
+ preprocessor .define (f"{ k } { v } " )
58
+ else :
59
+ if not isinstance (macros , list ):
60
+ macros = [macros ]
61
+ for macro in macros :
62
+ if "=" in macro :
63
+ key , value = macro .split ("=" )
64
+ preprocessor .define (f"{ key } { value } " )
65
+ else :
66
+ preprocessor .define (macro )
67
67
68
68
if cpp_includes :
69
69
if not isinstance (cpp_includes , list ):
@@ -81,7 +81,7 @@ def __str__(self):
81
81
return self .filename
82
82
83
83
def __repr__ (self ):
84
- return "FortranFile('{}')" . format ( self .filename )
84
+ return f "FortranFile('{ self .filename } ')"
85
85
86
86
def get_modules (self , contents , macros = None ):
87
87
"""Return all the modules or programs that are in the file
@@ -96,22 +96,17 @@ def get_modules(self, contents, macros=None):
96
96
ends = []
97
97
98
98
for num , line in enumerate (contents ):
99
- unit = re .match (UNIT_REGEX , line )
100
- end = re .match (END_REGEX , line )
101
- if unit :
99
+ if unit := UNIT_REGEX .match (line ):
102
100
found_units .append (unit )
103
101
starts .append (num )
104
- if end :
102
+ if end := END_REGEX . match ( line ) :
105
103
ends .append (num )
106
104
107
105
if found_units :
108
106
if (len (found_units ) != len (starts )) or (len (starts ) != len (ends )):
109
- error_string = (
110
- "Unmatched start/end of modules in {} ({} begins/{} ends)" .format (
111
- self .filename , len (starts ), len (ends )
112
- )
107
+ raise ValueError (
108
+ f"Unmatched start/end of modules in { self .filename } ({ len (starts )} begins/{ len (ends )} ends)"
113
109
)
114
- raise ValueError (error_string )
115
110
for unit , start , end in zip (found_units , starts , ends ):
116
111
name = unit .group ("modname" )
117
112
mod = FortranModule (
@@ -167,9 +162,7 @@ def __str__(self):
167
162
return self .name
168
163
169
164
def __repr__ (self ):
170
- return "FortranModule({}, '{}', '{}')" .format (
171
- self .unit_type , self .name , self .source_file .filename
172
- )
165
+ return f"FortranModule({ self .unit_type } , '{ self .name } ', '{ self .source_file .filename } ')"
173
166
174
167
def get_uses (self , contents , macros = None ):
175
168
"""Return which modules are used in the file after expanding macros
@@ -183,8 +176,7 @@ def get_uses(self, contents, macros=None):
183
176
uses = []
184
177
185
178
for line in contents [self .defined_at : self .end ]:
186
- found = re .match (USE_REGEX , line )
187
- if found :
179
+ if found := USE_REGEX .match (line ):
188
180
uses .append (found .group ("moduse" ).strip ().lower ())
189
181
190
182
# Remove duplicates
0 commit comments