bash/python-engine: Refactor common code into a base class