1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#!/usr/bin/env python3
#
# Utility script to insert an xml snippet into an existing document
#
# Re-implements xml_insert.sh in python with extra options to send the
# output to a new file.
#
# Copyright John Marshall 2020
#
from __future__ import print_function
import os
import sys
import argparse
class Args():
def __init__(self):
parser = argparse.ArgumentParser()
parser.add_argument(
'--output',
metavar='OUTPUT_FILE',
help='output file - otherwise output to STDOUT'
)
parser.add_argument(
'--insert',
action='append',
nargs=2,
required=True,
metavar=('TAG', 'INSERT_FILE'),
help='insert file tag and file name'
)
parser.add_argument(
'FILE',
metavar='INPUT_FILE',
help='input file'
)
self.input = os.path.realpath(parser.parse_args().FILE)
if parser.parse_args().output:
self.output = os.path.realpath(parser.parse_args().output)
else:
self.output = None
self.inserts = parser.parse_args().insert
def main():
args = Args()
try:
in_file = open(args.input, mode='r', encoding='utf-8')
except IOError:
print('cannot access input file ' + args.input,
file=sys.stderr)
sys.exit(1)
doc = in_file.read().splitlines()
in_file.close()
# get insert files
inserts = args.inserts
for insert in inserts:
ins_tag = '<!--' + insert[0] + '-->'
# find tag instances in input file
indices = []
for i, line in enumerate(doc):
if ins_tag in line:
indices.append(i)
if not indices:
print(ins_tag + ' not in input file - skipping',
file=sys.stderr)
continue
# read in insert file
try:
ins_file = open(os.path.realpath(insert[1]), mode='r', encoding='utf-8')
except IOError:
print ('cannot open insert file %s - skipping' % insert[1],
file=sys.stderr)
continue
if ins_file:
ins_doc = ins_file.read().splitlines()
ins_file.close()
# insert in reverse order so that remaining inert positions
# remain valid
for index in reversed(indices):
doc[index+1:index+1] = ins_doc
# output to file or stdout
if args.output:
try:
out_file = open(os.path.realpath(args.output), mode='w', encoding='utf-8')
except IOError:
print('cannot open output file %s' % args.output)
sys.exit(1)
else:
out_file = sys.stdout
for line in doc:
print(line, file=out_file)
sys.exit(0)
if __name__ == "__main__":
main()
|