Added support for argument parsing and temp files.
parent
7f47395ff2
commit
a289440187
|
@ -2,7 +2,7 @@ import keywords
|
|||
|
||||
def regularClass(cfg):
|
||||
ret = ''
|
||||
for key, data in cfg["scout"].items():
|
||||
for key, data in cfg.items():
|
||||
ret += keywords.branch("scout", key, data)
|
||||
return ret
|
||||
|
||||
|
|
38
cli.py
38
cli.py
|
@ -1,22 +1,17 @@
|
|||
# https://www.w3schools.io/file/yaml-arrays/
|
||||
import sys
|
||||
import yaml
|
||||
import argparse
|
||||
import tempfile
|
||||
|
||||
import classes
|
||||
import verify
|
||||
|
||||
def main(argv):
|
||||
if len(argv) > 1:
|
||||
parseFile(argv[1])
|
||||
else:
|
||||
raise Exception("Error: must provide a source file")
|
||||
|
||||
def parseFile(fileName):
|
||||
def parseFile(inputFile):
|
||||
config = {}
|
||||
|
||||
with open(fileName, 'r') as file:
|
||||
config = yaml.safe_load(file)
|
||||
|
||||
config = yaml.safe_load(inputFile)
|
||||
|
||||
aliases = bool("aliases" in config)
|
||||
|
||||
# See verify.py
|
||||
|
@ -24,15 +19,18 @@ def parseFile(fileName):
|
|||
if "errors" in config:
|
||||
for e in config["errors"]:
|
||||
print(e,file=sys.stderr)
|
||||
return
|
||||
# return
|
||||
|
||||
parseConfig(config)
|
||||
|
||||
def parseConfig(config):
|
||||
for currentClass in ["scout", "soldier", "pyro", "demo", "heavy", "sniper", "default"]:
|
||||
if currentClass in config:
|
||||
outfile = open("cfg/temp_"+currentClass+".cfg", "w")
|
||||
outfile = tempfile.NamedTemporaryFile( prefix=currentClass, delete=False )
|
||||
print( 'created {} '.format(outfile.name))
|
||||
outfile.write(classes.regularClass(config.pop(currentClass)))
|
||||
os.replace(outfile, './{}_nscript.txt'.format(currentClass))
|
||||
close(outfile)
|
||||
|
||||
if "engi" in config:
|
||||
outfile = open("cfg/temp_engi.cfg", "w")
|
||||
|
@ -54,4 +52,18 @@ def parseConfig(config):
|
|||
outfile.write( classes.getDefaultConfig(config) )
|
||||
config.pop("default")
|
||||
|
||||
main(sys.argv)
|
||||
# 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)
|
Loading…
Reference in New Issue