Added -f and -D, moved checks to be more efficient

pull/11/head
Nicholas Hope 2022-08-09 13:02:20 -04:00
parent 0e224d46ab
commit 8bcfd0329a
1 changed files with 40 additions and 28 deletions

View File

@ -40,15 +40,6 @@ def writeOutput(scriptString, className):
global targetDir
chunksize = 2**20 # 1Mb maximum cfg file size
chunk = 1
# Make sure the target exists before we try to use it
if os.path.isdir( targetDir ) == False:
try:
os.mkdir( targetDir )
if args.debug:
print( f'DEBUG: created {targetDir}')
except Exception as fileExcept:
print( f'WARN: Failed to create {targetDir}: {fileExcept.strerror}\nUsing current directory instead.' )
targetDir = '.'
# If the string is more than 1048576 bytes, we need divide it into files that each
# are less than 1048576 bytes
chunksneeded = int( 1 + len(scriptString) / chunksize )
@ -103,6 +94,16 @@ def writeOutput(scriptString, className):
def parseConfig(config):
"""With validated data structure, write out all the files."""
global targetDir
# Make sure the target exists before we try to use it
if os.path.isdir( targetDir ) == False:
try:
os.mkdir( targetDir )
if args.debug:
print( f'DEBUG: created {targetDir}')
except Exception as fileExcept:
print( f'WARN: Failed to create {targetDir}: {fileExcept.strerror}\nUsing current directory instead.' )
targetDir = '.'
for currentClass in config:
classDict = config[currentClass]
stringToWrite = tfscript.makeCFG(classDict)
@ -120,42 +121,53 @@ def main():
help="Enable debugging messages.")
parser.add_argument( '-n', '--dry-run', action='store_true',
help="Parse input file, but don't write anything.")
parser.add_argument( '--directory', action='store', type=str,
help='Change output directory')
parser.add_argument( '-f', '--force', action='store_true',
help="Force tfscript to continue until catastrophic failure")
parser.add_argument( '-D', '--directory', action='store', type=str,
help="Change output directory")
# positional argument: first non-hyphenated argument is input file
parser.add_argument( 'infile', type=argparse.FileType('r'),
help='File containing YAML to convert.')
args = parser.parse_args()
if args.directory is None:
if args.directory is not None:
targetDir = args.directory
else:
systemName = getOS()
if systemName == "Darwin":
if float( '.'.join(release().split('.')[0:2]) ) >= 10.15:
print(
"As of macOS Catalina (v10.15), 32-bit applications "
"such as tf2 do not work, so tfscript does not function",
file=sys.stderr
)
return 2
targetDir = os.path.expanduser("~/Library/Application Support/Steam/")
if not args.force:
print(
"As of macOS Catalina (v10.15), 32-bit applications "
"such as tf2 do not work, so tfscript does not function",
file=sys.stderr
)
else:
targetDir = os.path.expanduser("~/Library/Application Support/Steam/")
if systemName == "Windows":
# Oh god why do we have to use the registry!
elif systemName == "Windows":
# oh god why do we have to use the registry
accessReg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
accessKey = winreg.OpenKey(accessReg, "SOFTWARE\\WOW6432Node\\Valve\\Steam\\")
targetDir = winreg.QueryValue(accessKey, "InstallPath")
if systemName == "Linux":
elif systemName == "Linux":
targetDir = os.path.expanduser("~/.local/Steam")
if systemName == "Java":
elif systemName == "Java":
print("Java-based OSes are not supported yet by tfscript.", file=sys.stderr)
if targetDir != "":
# Supported OS: add steamapps path
if targetDir[-1] != '/':
targetDir += '/'
targetDir += "steamapps/common/Team Fortress 2/tf/cfg"
elif args.force:
# Unsupported OS but -f specified
targetDir = os.path.expanduser('.')
else:
# Unsupported OS and not forced to continue
return 2
if targetDir[-1] != '/': targetDir += '/'
targetDir += "steamapps/common/Team Fortress 2/tf/cfg"
else:
targetDir = args.directory
print(targetDir)
parseFile(args.infile)
return 0