utils.py 563 B

1234567891011121314151617181920212223
  1. import json
  2. def resp(app, data=None, code=200, headers=None):
  3. if not headers:
  4. headers = {}
  5. if 'Content-Type' not in headers:
  6. headers['Content-Type'] = 'application/json'
  7. data = json.dumps(data)
  8. return app.make_response((data, code, headers))
  9. def save_history(history, target='/opt/stackbrew/history.json'):
  10. with open(target, 'w') as f:
  11. f.write(json.dumps(history))
  12. def load_history(target='/opt/stackbrew/history.json'):
  13. try:
  14. with open(target, 'r') as f:
  15. return json.loads(f.read())
  16. except IOError:
  17. return {}