types.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """
  2. Types for objects parsed from the configuration.
  3. """
  4. from __future__ import absolute_import
  5. from __future__ import unicode_literals
  6. from collections import namedtuple
  7. from compose.config.errors import ConfigurationError
  8. class VolumeFromSpec(namedtuple('_VolumeFromSpec', 'source mode')):
  9. @classmethod
  10. def parse(cls, volume_from_config):
  11. parts = volume_from_config.split(':')
  12. if len(parts) > 2:
  13. raise ConfigurationError(
  14. "volume_from {} has incorrect format, should be "
  15. "service[:mode]".format(volume_from_config))
  16. if len(parts) == 1:
  17. source = parts[0]
  18. mode = 'rw'
  19. else:
  20. source, mode = parts
  21. return cls(source, mode)
  22. def parse_restart_spec(restart_config):
  23. if not restart_config:
  24. return None
  25. parts = restart_config.split(':')
  26. if len(parts) > 2:
  27. raise ConfigurationError(
  28. "Restart %s has incorrect format, should be "
  29. "mode[:max_retry]" % restart_config)
  30. if len(parts) == 2:
  31. name, max_retry_count = parts
  32. else:
  33. name, = parts
  34. max_retry_count = 0
  35. return {'Name': name, 'MaximumRetryCount': int(max_retry_count)}