Skip to content

Commit 44fd439

Browse files
committed
Fix linting issues in gen_thermochem.py
- Move imports to top-level to fix import-outside-toplevel warnings - Remove trailing whitespace - Remove trailing newlines - Add docstring to main() function - Add encoding parameter to file open call - Rename exception variable from 'e' to 'exc' Pylint now rates the script at 10.00/10.
1 parent a18b712 commit 44fd439

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

toolchain/scripts/gen_thermochem.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,22 @@
88
import argparse
99
import sys
1010

11+
try:
12+
import cantera as ct
13+
except ImportError as exc:
14+
print("ERROR: cantera Python package is required for chemistry.", file=sys.stderr)
15+
print("Install it with: pip install cantera", file=sys.stderr)
16+
raise ImportError("cantera is required for chemistry code generation") from exc
17+
18+
try:
19+
import pyrometheus as pyro
20+
except ImportError as exc:
21+
print("ERROR: pyrometheus Python package is required for chemistry.", file=sys.stderr)
22+
print("Install it with: pip install pyrometheus", file=sys.stderr)
23+
raise ImportError("pyrometheus is required for chemistry code generation") from exc
24+
1125
def main():
26+
"""Generate m_thermochem.f90 using Pyrometheus and Cantera."""
1227
parser = argparse.ArgumentParser(
1328
description="Generate Fortran thermochemistry module via Pyrometheus"
1429
)
@@ -21,46 +36,31 @@ def main():
2136
)
2237
args = parser.parse_args()
2338

24-
try:
25-
import cantera as ct
26-
except ImportError:
27-
print("ERROR: cantera Python package is required for chemistry.", file=sys.stderr)
28-
print("Install it with: pip install cantera", file=sys.stderr)
29-
sys.exit(1)
30-
31-
try:
32-
import pyrometheus as pyro
33-
except ImportError:
34-
print("ERROR: pyrometheus Python package is required for chemistry.", file=sys.stderr)
35-
print("Install it with: pip install pyrometheus", file=sys.stderr)
36-
sys.exit(1)
37-
3839
# Load the Cantera solution
3940
# If no mechanism file is specified or it's empty, use Cantera's default h2o2.yaml
4041
mech_file = args.mech if args.mech else "h2o2.yaml"
41-
42+
4243
try:
4344
sol = ct.Solution(mech_file)
44-
except Exception as e:
45-
print(f"ERROR: Failed to load Cantera mechanism '{mech_file}': {e}", file=sys.stderr)
45+
except Exception as exc:
46+
print(f"ERROR: Failed to load Cantera mechanism '{mech_file}': {exc}", file=sys.stderr)
4647
sys.exit(1)
4748

4849
# Generate Fortran code using Pyrometheus
4950
try:
5051
code = pyro.FortranCodeGenerator().generate(args.module, sol)
51-
except Exception as e:
52-
print(f"ERROR: Failed to generate Fortran code: {e}", file=sys.stderr)
52+
except Exception as exc:
53+
print(f"ERROR: Failed to generate Fortran code: {exc}", file=sys.stderr)
5354
sys.exit(1)
5455

5556
# Write to output file
5657
try:
57-
with open(args.out, 'w') as f:
58+
with open(args.out, 'w', encoding='utf-8') as f:
5859
f.write(code)
5960
print(f"Successfully generated {args.out} from {mech_file}")
60-
except Exception as e:
61-
print(f"ERROR: Failed to write output file '{args.out}': {e}", file=sys.stderr)
61+
except Exception as exc:
62+
print(f"ERROR: Failed to write output file '{args.out}': {exc}", file=sys.stderr)
6263
sys.exit(1)
6364

6465
if __name__ == "__main__":
6566
main()
66-

0 commit comments

Comments
 (0)