utils.py 983 B

12345678910111213141516171819202122232425262728293031323334353637
  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. save = []
  11. for k in history.iterkeys():
  12. url, ref, dfile = k # unpack
  13. save.append({
  14. 'url': url,
  15. 'ref': ref,
  16. 'dfile': dfile,
  17. 'img': history[k]
  18. })
  19. with open(target, 'w') as f:
  20. f.write(json.dumps(save))
  21. def load_history(target='/opt/stackbrew/history.json'):
  22. history = {}
  23. try:
  24. with open(target, 'r') as f:
  25. savefile = json.loads(f.read())
  26. for item in savefile:
  27. history[(item['url'], item['ref'], item['dfile'])] = item['img']
  28. return history
  29. except IOError:
  30. return {}