initial renaming support

parent 5f4d35e2
#!/bin/env python
import os
import tempfile
import re
import logging
from io import open
from io import StringIO
def loadRenamesFile(renamesPath):
"""Loads the content of a rename file"""
renamesRe = re.compile(
r"\s*(?P<old>[a-zA-Z0-9_]+)\s*->\s*(?P<new>[a-zA-Z0-9_]+)\s*$")
renames = {}
with open(renamesPath, encoding='utf-8') as f:
while True:
line = f.readline()
if not line:
break
m = renamesRe.match(line)
if m:
renames[m.group("old")] = m.group("new")
elif not line.isspace():
logging.warn("Unexpected line %r in %s", line, renamesPath)
return renames
def renamesSearchRe(renames):
"""creates a regular expression that matches the words that should be renamed"""
res = StringIO()
res.write(r"\b(")
first = True
for k in renames.keys():
if not first:
res.write("|")
else:
first = False
res.write(k)
res.write(r")\b")
renameReStr = res.getvalue()
return re.compile(renameReStr)
def replaceInFile(filePath, replacements):
"""performs the replacements in the given file"""
renameRe = renamesSearchRe(replacements)
outF = tempfile.NamedTemporaryFile(
mode="w", suffix='', prefix='tmp', dir=os.path.dirname(filePath), delete=False, encoding='utf-8')
didReplace = {}
lineNr = 0
with outF:
with open(filePath, encoding='utf-8') as inF:
while True:
line = inF.readline()
if not line:
break
lineNr += 1
ii = 0
for m in renameRe.finditer(line):
old = m.group(1)
didReplace[old] = didReplace.get(old, []) + [lineNr]
outF.write(line[ii:m.start()])
outF.write(replacements[old])
ii = m.end()
outF.write(line[ii:])
if didReplace:
bkPathBase = filePath + ".bk"
bkPath = bkPathBase
# non atomic (should really create if not there)
ii = 0
while os.path.exists(bkPath):
ii += 1
bkPath = bkPathBase + str(ii)
os.rename(filePath, bkPath)
os.rename(outF.name, filePath)
print("%r: {" % filePath)
for k,v in didReplace.items():
print(k,'->',replacements[k],":",v)
print("}\nBackup in ",bkPath)
if __name__ == "__main__":
import sys
renamesPath = os.path.join(os.path.dirname(__file__), "renames.txt")
renames = loadRenamesFile(renamesPath)
for f in sys.argv[1:]:
try:
replaceInFile(f, renames)
except:
logging.exception("handling file %s", f)
print("DONE")
XC_method -> xc_method
energy_type_van_der_Waals -> energy_type_van_der_waals
XC_functional_weight -> xc_functional_weight
XC_functional_parameters -> xc_functional_parameters
energy_van_der_Waals -> energy_van_der_waals
spin_S2_scf_iteration -> spin_s2_scf_iteration
energy_van_der_Waals_value -> energy_van_der_waals_value
energy_type_XC -> energy_type_xc
section_XC_functionals -> section_xc_functionals
energy_total_T0 -> energy_total_t0
settings_GW -> settings_gw
energy_T0_per_atom -> energy_t0_per_atom
XC_functional -> xc_functional
energy_hartree_fock_X_scaled -> energy_hartree_fock_x_scaled
energy_total_T0_per_atom_scf_iteration -> energy_total_t0_per_atom_scf_iteration
atom_forces_T0 -> atom_forces_t0
van_der_Waals_method -> van_der_waals_method
spin_S2 -> spin_s2
energy_XC_functional -> energy_xc_functional
energy_total_T0_scf_iteration -> energy_total_t0_scf_iteration
XC_functional_name -> xc_functional_name
energy_XC_scf_iteration -> energy_xc_scf_iteration
settings_van_der_Waals -> settings_van_der_waals
energy_C -> energy_c
energy_XC_potential -> energy_xc_potential
atom_forces_T0_raw -> atom_forces_t0_raw
section_energy_van_der_Waals -> section_energy_van_der_waals
XC_method_current -> xc_method_current
settings_Monte_Carlo -> settings_monte_carlo
energy_type_C -> energy_type_c
settings_XC -> settings_xc
energy_van_der_Waals_kind -> energy_van_der_waals_kind
SC_matrix -> sc_matrix
settings_MCSCF -> settings_mcscf
energy_X -> energy_x
energy_XC_potential_scf_iteration -> energy_xc_potential_scf_iteration
settings_XC_functional -> settings_xc_functional
energy_type_X -> energy_type_x
thermodynamical_property_heat_capacity_C_v -> thermodynamical_property_heat_capacity_c_v
energy_XC -> energy_xc
energy_total_T0_per_atom -> energy_total_t0_per_atom
energy_hartree_fock_X -> energy_hartree_fock_x
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment