Reinventing the Wheel
The first step was to pick a language, and because this is meant to be a learning experience, I decided to go with Python. I haven’t used Python much and it makes a lot of sense for this project, so that’s what I’m going with.
The next step, for me, was to learn a bit about Python’s file I/O. Ideally, this will be a command line tool where you can give it a .sb2 and it will spit out a .html (the html file will contain everything in it, for easy project portability).
I managed, after a few attempts, to read the project JSON and print it out into the console:
import zipfile zf = zipfile.ZipFile('myProjectName.sb2', 'r') print zf.read('project.json')
myProjectName.sb2 isn’t really an acceptable file name, so I had to let users input their own. I added a command line argument to fix that:
import zipfile, argparse parser = argparse.ArgumentParser() parser.add_argument('filename', type=str) args = parser.parse_args() zf = zipfile.ZipFile(args.filename, 'r') print zf.read('project.json')
Now you can run
python compile.py yourproject.sb2 to see the json.
The last thing I wanted to do before taking a break was parse the json. It turned out to be surprisingly easy:
import zipfile, argparse, json parser = argparse.ArgumentParser() parser.add_argument('filename', type=str) args = parser.parse_args() zf = zipfile.ZipFile(args.filename, 'r') projectJSON = zf.read('project.json') print json.loads(projectJSON)["costumes"]["costumeName"]
Then we can get the name of the first backdrop:
python compile.py project.sb2 > backdrop1
So far this project has been coming along fairly well! There’s nothing too interesting going on just yet, but I’m having fun, and that was really the goal in the first place.