This script looks for patterns or literal strings in the input files and replaces them with the corresponding text.
Provided the following as input:
- a list of search and replace patterns (in an Excel file)
- a folder containing text-based files (e.g. XML)
For every file, the script will:
- look for every search pattern
- replace it with the replacement pattern
- write an output file in the output folder specified
Clone this repo and change directory to it:
gh repo clone capstanlqc/source-xml-linter
cd source-xml-linter
Install a virtual environment in the root folder of the repo (only once):
python -m venv venv
Activate the virtual environment (before every time you run the script):
source venv/bin/activate
Install all dependencies in the virtual environment:
pip install -r requirements.txt
Run the code (see below for details):
python str_subs.py \
-i /path/input/folder \
-o /path/to_output/folder \
-c /path/to/config.xlsx
You may exit the virtual environment when you're done running the code:
deactivate
The help will show you what input parameters are needed:
$ python str_subs.py --help
usage: str_subs.py [-h] [-V] [-i INPUT] [-o OUTPUT] [-c CONFIG]
String substitution in XML files
options:
-h, --help show this help message and exit
-V, --version show program version
-i INPUT, --input INPUT
specify path to the folder containing the files to be processed
-o OUTPUT, --output OUTPUT
specify path to the folder where the processed files should be saved
-c CONFIG, --config CONFIG
specify path to the config file containing patterns etc.
There are three different config files for:
- new content
- trend content
- XYZ batch
In practice, that means running three different commands depending on the files that need to be linted and signed off.
For new content:
python $app/str_subs.py -i $tolint_new -o $linted_new -c $app/config.xlsx
For trend content:
python $app/str_subs.py -i $tolint_trend -o $linted_trend -c $app/config_trend.xlsx
For XYZ batch:
python $app/str_subs.py -i $tolint_xyz -o $linted_xyz -c $app/config_xyz.xlsx
A log file will be written in logs with an account of what was done.
Tentative todo list:
- Parse XML input file and run the substution only inside the text node (e.g.
<label>
) - Add timestamp to log filename
- Make it a requirement that argument paths are absolute paths