utils.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from __future__ import unicode_literals
  2. from __future__ import absolute_import
  3. from __future__ import division
  4. import datetime
  5. import os
  6. import socket
  7. from .errors import UserError
  8. def cached_property(f):
  9. """
  10. returns a cached property that is calculated by function f
  11. http://code.activestate.com/recipes/576563-cached-property/
  12. """
  13. def get(self):
  14. try:
  15. return self._property_cache[f]
  16. except AttributeError:
  17. self._property_cache = {}
  18. x = self._property_cache[f] = f(self)
  19. return x
  20. except KeyError:
  21. x = self._property_cache[f] = f(self)
  22. return x
  23. return property(get)
  24. def yesno(prompt, default=None):
  25. """
  26. Prompt the user for a yes or no.
  27. Can optionally specify a default value, which will only be
  28. used if they enter a blank line.
  29. Unrecognised input (anything other than "y", "n", "yes",
  30. "no" or "") will return None.
  31. """
  32. answer = raw_input(prompt).strip().lower()
  33. if answer == "y" or answer == "yes":
  34. return True
  35. elif answer == "n" or answer == "no":
  36. return False
  37. elif answer == "":
  38. return default
  39. else:
  40. return None
  41. # http://stackoverflow.com/a/5164027
  42. def prettydate(d):
  43. diff = datetime.datetime.utcnow() - d
  44. s = diff.seconds
  45. if diff.days > 7 or diff.days < 0:
  46. return d.strftime('%d %b %y')
  47. elif diff.days == 1:
  48. return '1 day ago'
  49. elif diff.days > 1:
  50. return '{0} days ago'.format(diff.days)
  51. elif s <= 1:
  52. return 'just now'
  53. elif s < 60:
  54. return '{0} seconds ago'.format(s)
  55. elif s < 120:
  56. return '1 minute ago'
  57. elif s < 3600:
  58. return '{0} minutes ago'.format(s/60)
  59. elif s < 7200:
  60. return '1 hour ago'
  61. else:
  62. return '{0} hours ago'.format(s/3600)
  63. def mkdir(path, permissions=0o700):
  64. if not os.path.exists(path):
  65. os.mkdir(path)
  66. os.chmod(path, permissions)
  67. return path
  68. def docker_url():
  69. return os.environ.get('DOCKER_HOST')
  70. def split_buffer(reader, separator):
  71. """
  72. Given a generator which yields strings and a separator string,
  73. joins all input, splits on the separator and yields each chunk.
  74. Unlike string.split(), each chunk includes the trailing
  75. separator, except for the last one if none was found on the end
  76. of the input.
  77. """
  78. buffered = str('')
  79. separator = str(separator)
  80. for data in reader:
  81. buffered += data
  82. while True:
  83. index = buffered.find(separator)
  84. if index == -1:
  85. break
  86. yield buffered[:index+1]
  87. buffered = buffered[index+1:]
  88. if len(buffered) > 0:
  89. yield buffered