54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
# https://www.w3schools.io/file/yaml-arrays/
|
|
import sys
|
|
import os
|
|
import yaml
|
|
import argparse
|
|
import tempfile
|
|
|
|
import classes
|
|
import verify
|
|
|
|
def parseFile(inputFile):
|
|
config = {}
|
|
|
|
config = yaml.safe_load(inputFile)
|
|
|
|
aliases = bool("aliases" in config)
|
|
|
|
# See verify.py
|
|
config = verify.verifyConfig(config)
|
|
if "errors" in config:
|
|
for e in config["errors"]:
|
|
print(e,file=sys.stderr)
|
|
else:
|
|
parseConfig(config)
|
|
|
|
def parseConfig(config):
|
|
global args
|
|
for currentClass in config:
|
|
outfile = tempfile.NamedTemporaryFile( prefix=currentClass, delete=False )
|
|
if args.debug:
|
|
print( 'created {} '.format(outfile.name))
|
|
|
|
classDict = config[currentClass]
|
|
stringToWrite = classes.makeCFG(classDict)
|
|
outfile.write(stringToWrite.encode("utf8"))
|
|
|
|
os.replace(outfile.name, './cfg/{}_nscript.txt'.format(currentClass))
|
|
outfile.close()
|
|
|
|
# Main function
|
|
if __name__ == "__main__":
|
|
# Handle command line
|
|
parser = argparse.ArgumentParser(
|
|
description="Parse YAML file and produce TF2 config script."
|
|
)
|
|
parser.add_argument( '-d', '--debug', action='store_true',
|
|
help="Enable debugging messages.")
|
|
parser.add_argument( '-n', '--dry-run', action='store_true',
|
|
help="Parse input file, but don't write anything.")
|
|
# positional argument: first non-hyphenated argument is input file
|
|
parser.add_argument( 'infile', type=argparse.FileType('r'),
|
|
help='File containing YAML to convert. Default: "example.yaml"')
|
|
args = parser.parse_args()
|
|
parseFile(args.infile) |