Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. from io import BytesIO
  2. import pickle
  3. import tarfile
  4.  
  5.  
  6. def _is_tar(tar_candidate):
  7. return type(tar_candidate) == tarfile.TarFile
  8.  
  9.  
  10. def _write_tar(tar, name, get_data):
  11. data = get_data()
  12. info = tarfile.TarInfo(name=name)
  13. info.size=len(data)
  14. tar.addfile(tarinfo=info, fileobj=BytesIO(data))
  15.  
  16.  
  17. def _with_tar(stream, action, mode='w'):
  18. if _is_tar(stream):
  19. tar = stream
  20. else:
  21. tar = tarfile.TarFile(stream, mode=mode)
  22. action_response = action(tar)
  23. if not _is_tar(stream):
  24. tar.close()
  25. return action_response
  26.  
  27.  
  28. def _read_tar(input_, name):
  29. if _is_tar(input_):
  30. tar = input_
  31. else:
  32. tar = tarfile.open(input_, mode='r')
  33. data = pickle.load(tar.extractfile(name))
  34. if not _is_tar(input_):
  35. tar.close()
  36. return data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement