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
| import os
import json
import argparse
import sc2reader
from spawningtool.parser import parse_replay
# Argparse gives the tool a nice *nix style help page for when you forget how to use it.
parser = argparse.ArgumentParser(description="Convert replays to json build files")
parser.add_argument('outdir', metavar='OUTPUT_DIRECTORY', type=str, nargs=1,
help='Directory to write json files to.')
parser.add_argument('paths', metavar='SOURCE_PATH', type=str, nargs='+',
help='paths to search for replay files')
args = parser.parse_args()
# For each source path provided
for path in args.paths:
# Get all the .SC2Replay files you can find and order them for consistency
for filepath in sorted(sc2reader.utils.get_files(path)):
# For large batch jobs it is helpful to keep a log of what we are doing
# If we have a failure during processing we know that the previous filename
# was the cause of the issue and can debug or exclude it from the batch.
print("Processing {0}".format(filepath))
# Parse them with spawningtool to get datastructure with all the relevant game/build data
data = parse_replay(filepath)
# Extract the name of the file from the full path
filename, extension = os.path.splitext(os.path.basename(filepath))
# Write the data to the output directory with a .json extension instead of .SC2Replay
with open(os.path.join(args.outdir[0], filename+".json"), 'w') as outfile:
json.dump(data, outfile) |
Partager