Generate project name based on current dir

This commit is contained in:
Ben Firshman 2013-12-19 15:32:24 +00:00
parent c488710625
commit d6db049b42
2 changed files with 18 additions and 5 deletions

View File

@ -1,6 +1,7 @@
from docker import Client from docker import Client
import logging import logging
import os import os
import re
import yaml import yaml
from ..service_collection import ServiceCollection from ..service_collection import ServiceCollection
@ -21,7 +22,19 @@ class Command(DocoptCommand):
@cached_property @cached_property
def service_collection(self): def service_collection(self):
config = yaml.load(open('plum.yml')) config = yaml.load(open('plum.yml'))
return ServiceCollection.from_config(self.client, config) return ServiceCollection.from_config(
config,
client=self.client,
project=self.project
)
@cached_property
def project(self):
project = os.path.basename(os.getcwd())
project = re.sub(r'[^a-zA-Z0-9]', '', project)
if not project:
project = 'default'
return project
@cached_property @cached_property
def formatter(self): def formatter(self):

View File

@ -14,7 +14,7 @@ def sort_service_dicts(services):
class ServiceCollection(list): class ServiceCollection(list):
@classmethod @classmethod
def from_dicts(cls, client, service_dicts): def from_dicts(cls, service_dicts, client, project='default'):
""" """
Construct a ServiceCollection from a list of dicts representing services. Construct a ServiceCollection from a list of dicts representing services.
""" """
@ -26,16 +26,16 @@ class ServiceCollection(list):
for name in service_dict.get('links', []): for name in service_dict.get('links', []):
links.append(collection.get(name)) links.append(collection.get(name))
del service_dict['links'] del service_dict['links']
collection.append(Service(client=client, links=links, **service_dict)) collection.append(Service(client=client, project=project, links=links, **service_dict))
return collection return collection
@classmethod @classmethod
def from_config(cls, client, config): def from_config(cls, config, client, project='default'):
dicts = [] dicts = []
for name, service in config.items(): for name, service in config.items():
service['name'] = name service['name'] = name
dicts.append(service) dicts.append(service)
return cls.from_dicts(client, dicts) return cls.from_dicts(dicts, client, project)
def get(self, name): def get(self, name):
for service in self: for service in self: