SpectraRust/fix_imports.py
2026-03-25 18:34:41 +08:00

192 lines
7.4 KiB
Python

import os
import re
# Change to the project directory
os.chdir(r'C:\Users\fmq\Documents\astro\SpectraRust')
# All the Rust source file modules that were moved to subdirectories
# These are the .rs file basenames that are now in subdirs
modules_moved = [
# From opacity/
'allard', 'allardt', 'cia_h2h', 'cia_h2h2', 'cia_h2he', 'cia_hhe',
'compt0', 'corrwm', 'cspec', 'dopgam', 'dwnfr', 'dwnfr0', 'dwnfr1',
'gvdw', 'inifrc', 'inifrs', 'inifrt', 'inilam', 'inkul', 'inpdis',
'lemini', 'levgrp', 'levset', 'levsol', 'linpro', 'linsel', 'linspl',
'lymlin', 'meanop', 'meanopt', 'profil', 'profsp', 'quasim', 'rayleigh',
'rayset', 'reflev', 'reiman', 'stark0', 'starka', 'prd', 'prdini',
# From hydrogen/
'bhe', 'bre', 'brez', 'brte', 'brtez', 'colh', 'colhe', 'colis', 'collhe',
'ctdata', 'ghydop', 'h2minus', 'hedif', 'hephot', 'hesol6', 'hesolv',
'hidalg', 'inthyd', 'sbfch', 'sbfhe1', 'sbfhmi', 'sbfhmi_old', 'sbfoh',
'sffhmi', 'sffhmi_add', 'sgmer', 'sgmer1', 'sigave', 'sigk', 'sigmar',
'spsigk', 'szirc',
# From atomic/
'chctab', 'cheav', 'cheavj', 'cion', 'cross', 'dielrc', 'dietot',
'ffcros', 'gfree', 'gntk', 'vern16', 'vern18', 'vern20', 'vern26', 'verner',
# From continuum/
'opacf0', 'opacf1', 'opacfa', 'opacfd', 'opacfl', 'opact1', 'opactd',
'opactr', 'opadd', 'opadd0', 'opahst', 'opaini', 'opctab', 'opdata', 'opfrac',
# From convection/
'concor', 'conout', 'conref', 'contmd', 'contmp', 'convec',
# From eos/
'eldenc', 'eldens', 'entene', 'moleq', 'rhoeos', 'rhonen', 'russel', 'steqeq',
# From interpolation/
'ckoest', 'interp', 'interpolate', 'intlem', 'intxen', 'lagran', 'locate',
'tabint', 'yint', 'ylintp',
# From io/
'getwrd', 'output', 'prchan', 'princ', 'prnt', 'prsent', 'pzert',
'pzeval', 'pzevld', 'quit', 'rdata', 'rdatax', 'readbf', 'rechck',
'timing', 'visini',
# From odf/
'odf1', 'odffr', 'odfhst', 'odfhyd', 'odfhys', 'odfmer',
# From partition/
'carbon', 'ceh12', 'mpartf', 'partf', 'pfcno', 'pffe', 'pfheav',
'pfni', 'pfspec', 'sghe12', 'tiopf',
# From population/
'bpop', 'bpopc', 'bpope', 'bpopf', 'bpopt', 'butler', 'newpop',
# From radiative/
'coolrt', 'radpre', 'radtot', 'rte_sc', 'rteang', 'rtecf0', 'rtecf1',
'rtecmc', 'rtecmu', 'rtecom', 'rtedf1', 'rtedf2', 'rtefe2', 'rtefr1',
'rteint', 'rtesol', 'trmder', 'trmdrt',
# From rates/
'rates1', 'ratmal', 'ratmat', 'ratsp1',
# From solvers/
'accel2', 'accelp', 'cubic', 'indexx', 'laguer', 'lineqs', 'matcon',
'matgen', 'matinv', 'minv3', 'psolve', 'quartc', 'raph', 'rhsgen',
'rybchn', 'rybene', 'rybheq', 'rybmat', 'rybsol', 'solve', 'solves',
'tridag', 'ubeta',
# From special/
'erfcx', 'expint', 'expo', 'gami', 'gamsp', 'gauleg', 'gaunt',
'voigt', 'voigte',
# From temperature/
'elcor', 'grcor', 'greyd', 'lucy', 'osccor', 'rossop', 'rosstd',
'tdpini', 'temcor', 'temper', 'tlocal',
# From utils/
'angset', 'betah', 'bkhsgo', 'change', 'column', 'comset', 'divstr',
'dmder', 'dmeval', 'emat', 'getlal', 'gomini', 'gridp', 'inicom',
'irc', 'newdm', 'newdmt', 'pgset', 'sabolf', 'setdrt', 'state',
'switch', 'topbas', 'traini', 'wn', 'wnstor', 'xk2dop', 'zmrho',
# From ali/
'alifr1', 'alifr3', 'alifr6', 'alifrk', 'alisk1', 'alisk2',
'alist1', 'alist2', 'ijali2', 'ijalis', 'taufr1',
]
# Pattern for single item: use crate::tlusty::math::module::item;
single_pattern = re.compile(
r'use crate::tlusty::math::(' + '|'.join(modules_moved) + r')::(\w+);'
)
# Pattern for multiple items: use crate::tlusty::math::module::{a, b};
multi_pattern = re.compile(
r'use crate::tlusty::math::(' + '|'.join(modules_moved) + r')::\{([^}]+)\};'
)
# Pattern for super::module::item (cross-submodule imports)
super_single_pattern = re.compile(
r'use super::(' + '|'.join(modules_moved) + r')::(\w+);'
)
# Pattern for super::module::{a, b}
super_multi_pattern = re.compile(
r'use super::(' + '|'.join(modules_moved) + r')::\{([^}]+)\};'
)
# Pattern for use super::module; (direct module import)
super_direct_pattern = re.compile(
r'use super::(' + '|'.join(modules_moved) + r');'
)
# Pattern for use super::{module1, module2, ...}
super_brace_pattern = re.compile(
r'use super::\{([^}]+)\};'
)
# Pattern for direct code references: crate::tlusty::math::module::item(
# This catches function calls like crate::tlusty::math::quit::quit_error(
code_ref_pattern = re.compile(
r'crate::tlusty::math::(' + '|'.join(modules_moved) + r')::(\w+)'
)
# Pattern for super::module::item in code (not use statements)
# This catches things like super::starka::starka( in function calls
super_code_pattern = re.compile(
r'super::(' + '|'.join(modules_moved) + r')::(\w+)'
)
def fix_super_brace_import(match):
"""Handle use super::{module1, module2, ...}"""
items = match.group(1)
# Split by comma and process each item
parts = [p.strip() for p in items.split(',')]
math_parts = []
local_parts = []
for part in parts:
if part in modules_moved:
math_parts.append(part)
else:
local_parts.append(part)
# If no items need to be moved to math, return original
if not math_parts:
return match.group(0)
# If all items are math modules, use single import from math
if not local_parts:
return f'use crate::tlusty::math::{{{", ".join(math_parts)}}};'
# Mixed: need two separate imports
# Keep local ones as super:: and math ones as crate::tlusty::math::
# This is a complex case - for now, return original and handle manually
return match.group(0)
def fix_file(path):
try:
with open(path, 'r', encoding='utf-8') as f:
content = f.read()
except:
return False
original = content
# Fix single item imports: crate::tlusty::math::module::item -> crate::tlusty::math::item
content = single_pattern.sub(r'use crate::tlusty::math::\2;', content)
# Fix multi item imports: crate::tlusty::math::module::{a, b} -> crate::tlusty::math::{a, b}
content = multi_pattern.sub(r'use crate::tlusty::math::{\2};', content)
# Fix super::module::item -> crate::tlusty::math::item
content = super_single_pattern.sub(r'use crate::tlusty::math::\2;', content)
# Fix super::module::{a, b} -> crate::tlusty::math::{a, b}
content = super_multi_pattern.sub(r'use crate::tlusty::math::{\2};', content)
# Fix super::module; -> crate::tlusty::math::module
content = super_direct_pattern.sub(r'use crate::tlusty::math::\1;', content)
# Fix super::{module1, module2, ...} -> crate::tlusty::math::{module1, module2, ...}
content = super_brace_pattern.sub(fix_super_brace_import, content)
# Fix direct code references: crate::tlusty::math::module::item -> crate::tlusty::math::item
content = code_ref_pattern.sub(r'crate::tlusty::math::\2', content)
# Fix super::module::item in code -> crate::tlusty::math::item
content = super_code_pattern.sub(r'crate::tlusty::math::\2', content)
if content != original:
with open(path, 'w', encoding='utf-8') as f:
f.write(content)
return True
return False
count = 0
for root, dirs, files in os.walk('src/tlusty'):
for f in files:
if f.endswith('.rs'):
path = os.path.join(root, f)
if fix_file(path):
count += 1
print(f"Fixed: {path}")
print(f"\nTotal files fixed: {count}")