def download_resume(resume_id, file_format): """Download resume in prescribed format""" # TODO: abstract this; maybe a decorator? resume = Resume.query.filter_by(id=resume_id, user=current_user).first() if not resume or file_format not in ResumeBabel.get_supported_formats(): abort(404) # Abstract resume file operations. Maybe attach to resume object resume_name = '%d.json' % (resume_id) resume_path = os.path.join(app.config['RESUME_FOLDER'], resume_name) file_name = '%d.%s' % (resume_id, file_format) file_path = os.path.join(app.config['RESUME_FOLDER'], file_name) resume_json = None if os.path.isfile(resume_path): resume_json = open(resume_path, 'r').read() if not os.path.isfile(file_path): out_fd = open(file_path, 'w') r = ResumeBabel(resume_json) r.export_resume(out_fd, file_format) out_fd.close() as_attachment = False if request.args.get('download'): as_attachment = True # TODO: mimetype='application/json' ???? return send_from_directory( app.config['RESUME_FOLDER'], file_name, attachment_filename=('resume.' + file_format), cache_timeout=1, as_attachment=as_attachment)
import collections import json import os import sys from resumebabel.resumebabel import ResumeBabel if __name__ == '__main__': input_json = sys.argv[1] output_file = sys.argv[2] # TODO: make sure output_format is not empty output_format = (os.path.splitext(output_file)[1])[1:] resume = open(input_json).read() r = ResumeBabel(resume) out_fd = open(output_file, 'wb') out_fd.write(r.export_resume(output_format)) out_fd.close()