command.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from __future__ import unicode_literals
  2. from __future__ import absolute_import
  3. from ..packages.docker import Client
  4. import errno
  5. import logging
  6. import os
  7. import re
  8. import yaml
  9. from ..project import Project
  10. from .docopt_command import DocoptCommand
  11. from .formatter import Formatter
  12. from .utils import cached_property, docker_url
  13. log = logging.getLogger(__name__)
  14. class Command(DocoptCommand):
  15. base_dir = '.'
  16. @cached_property
  17. def client(self):
  18. return Client(docker_url())
  19. @cached_property
  20. def project(self):
  21. try:
  22. yaml_path = os.path.join(self.base_dir, 'fig.yml')
  23. config = yaml.load(open(yaml_path))
  24. except IOError as e:
  25. if e.errno == errno.ENOENT:
  26. log.error("Can't find %s. Are you in the right directory?", os.path.basename(e.filename))
  27. else:
  28. log.error(e)
  29. exit(1)
  30. return Project.from_config(self.project_name, config, self.client)
  31. @cached_property
  32. def project_name(self):
  33. project = os.path.basename(os.getcwd())
  34. project = re.sub(r'[^a-zA-Z0-9]', '', project)
  35. if not project:
  36. project = 'default'
  37. return project
  38. @cached_property
  39. def formatter(self):
  40. return Formatter()