util.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import os
  2. import hashlib
  3. import json
  4. import copy
  5. def md5_for_file(path, block_size=2**20):
  6. md5 = hashlib.md5()
  7. with open(path) as f:
  8. while True:
  9. data = f.read(block_size)
  10. if not data:
  11. break
  12. md5.update(data)
  13. return md5.hexdigest()
  14. def mkdir(newdir):
  15. """works the way a good mkdir should :)
  16. - already exists, silently complete
  17. - regular file in the way, raise an exception
  18. - parent directory(ies) does not exist, make them as well
  19. """
  20. if os.path.isdir(newdir):
  21. pass
  22. elif os.path.isfile(newdir):
  23. raise OSError("a file with the same name as the desired " \
  24. "dir, '{0}', already exists.".format(newdir))
  25. else:
  26. head, tail = os.path.split(newdir)
  27. if head and not os.path.isdir(head):
  28. mkdir(head)
  29. #print "mkdir {0}.format(repr(newdir))
  30. if tail:
  31. os.mkdir(newdir)
  32. class JSONEncodable(object):
  33. """An inheritance mixin to make a class encodable to JSON"""
  34. def to_dict(self):
  35. d = copy.copy(self.__dict__)
  36. for key,value in d.items():
  37. if hasattr(value,"to_dict"):
  38. d[key] = value.to_dict()
  39. return d
  40. def to_json(self):
  41. return json.dumps(self.to_dict(),sort_keys=True,indent=4)