Pārlūkot izejas kodu

Generate project name based on current dir

Ben Firshman 12 gadi atpakaļ
vecāks
revīzija
d6db049b42
2 mainītis faili ar 18 papildinājumiem un 5 dzēšanām
  1. 14 1
      plum/cli/command.py
  2. 4 4
      plum/service_collection.py

+ 14 - 1
plum/cli/command.py

@@ -1,6 +1,7 @@
 from docker import Client
 import logging
 import os
+import re
 import yaml
 
 from ..service_collection import ServiceCollection
@@ -21,7 +22,19 @@ class Command(DocoptCommand):
     @cached_property
     def service_collection(self):
         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
     def formatter(self):

+ 4 - 4
plum/service_collection.py

@@ -14,7 +14,7 @@ def sort_service_dicts(services):
 
 class ServiceCollection(list):
     @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.
         """
@@ -26,16 +26,16 @@ class ServiceCollection(list):
                 for name in service_dict.get('links', []):
                     links.append(collection.get(name))
                 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
 
     @classmethod
-    def from_config(cls, client, config):
+    def from_config(cls, config, client, project='default'):
         dicts = []
         for name, service in config.items():
             service['name'] = name
             dicts.append(service)
-        return cls.from_dicts(client, dicts)
+        return cls.from_dicts(dicts, client, project)
 
     def get(self, name):
         for service in self: