Check ./cfg directory exists before using.

pull/11/head
Paco Hope 2022-06-19 20:02:44 -04:00
parent 5328f486fa
commit 5fbd0f75a2
1 changed files with 13 additions and 2 deletions

15
cli.py
View File

@ -27,16 +27,27 @@ def parseFile(inputFile):
def parseConfig(config):
"""With validated data structure, write out all the files."""
global args
prefix = './cfg'
# Make sure ./cfg exists before we try to use it
if os.path.isdir( prefix ) == False:
try:
os.mkdir( prefix )
args.debug and print( f'DEBUG: created {prefix}')
except Exception as fileExcept:
print( f'WARN: Failed to create {prefix}: {fileExcept.strerror}\nUsing current directory instead.' )
prefix = '.'
for currentClass in config:
outfile = tempfile.NamedTemporaryFile( prefix=currentClass, delete=False )
if args.debug:
print( f'created {outfile.name} ')
print( f'DEBUG: created temporary {outfile.name} ')
classDict = config[currentClass]
stringToWrite = tfscript.makeCFG(classDict)
outfile.write(stringToWrite.encode("utf8"))
os.replace(outfile.name, f'./cfg/{currentClass}_nscript.txt')
os.replace(outfile.name, f'{prefix}/{currentClass}_nscript.txt')
args.debug and print( f'DEBUG: Created {prefix}/{currentClass}_nscript.txt')
outfile.close()
# Main function