Sfoglia il codice sorgente

Bin: Make sure subcommands inherit the --no-color preload option. Fixes #1799

Ask Solem 11 anni fa
parent
commit
df9c39d989
2 ha cambiato i file con 39 aggiunte e 12 eliminazioni
  1. 25 4
      celery/bin/base.py
  2. 14 8
      celery/bin/celery.py

+ 25 - 4
celery/bin/base.py

@@ -236,8 +236,8 @@ class Command(object):
         self.get_app = get_app or self._get_default_app
         self.stdout = stdout or sys.stdout
         self.stderr = stderr or sys.stderr
-        self.no_color = no_color
-        self.colored = term.colored(enabled=not self.no_color)
+        self._colored = None
+        self._no_color = no_color
         self.quiet = quiet
         if not self.description:
             self.description = self.__doc__
@@ -405,8 +405,10 @@ class Command(object):
         quiet = preload_options.get('quiet')
         if quiet is not None:
             self.quiet = quiet
-        self.colored.enabled = \
-            not preload_options.get('no_color', self.no_color)
+        try:
+            self.no_color = preload_options['no_color']
+        except KeyError:
+            pass
         workdir = preload_options.get('working_directory')
         if workdir:
             os.chdir(workdir)
@@ -593,6 +595,25 @@ class Command(object):
         if body and self.show_body:
             self.out(body)
 
+    @property
+    def colored(self):
+        if self._colored is None:
+            self._colored = term.colored(enabled=not self.no_color)
+        return self._colored
+
+    @colored.setter
+    def colored(self, obj):
+        self._colored = obj
+
+    @property
+    def no_color(self):
+        return self._no_color
+
+    @no_color.setter
+    def no_color(self, value):
+        self._no_color = value
+        if self._colored is not None:
+            self._colored.enabled = not self._no_color
 
 def daemon_options(default_pidfile=None, default_logfile=None):
     return (

+ 14 - 8
celery/bin/celery.py

@@ -611,8 +611,10 @@ class help(Command):
 
     def run(self, *args, **kwargs):
         self.parser.print_help()
-        self.out(HELP.format(prog_name=self.prog_name,
-                             commands=CeleryCommand.list_commands()))
+        self.out(HELP.format(
+            prog_name=self.prog_name,
+            commands=CeleryCommand.list_commands(colored=self.colored),
+        ))
 
         return EX_USAGE
 
@@ -665,6 +667,7 @@ class CeleryCommand(Command):
         try:
             return cls(
                 app=self.app, on_error=self.on_error,
+                no_color=self.no_color, quiet=self.quiet,
                 on_usage_error=partial(self.on_usage_error, command=command),
             ).run_from_argv(self.prog_name, argv[1:], command=argv[0])
         except self.UsageError as exc:
@@ -725,8 +728,9 @@ class CeleryCommand(Command):
             sys.exit(EX_FAILURE)
 
     @classmethod
-    def get_command_info(self, command, indent=0, color=None):
-        colored = term.colored().names[color] if color else lambda x: x
+    def get_command_info(self, command, indent=0, color=None, colored=None):
+        colored = term.colored() if colored is None else colored
+        colored = colored.names[color] if color else lambda x: x
         obj = self.commands[command]
         cmd = 'celery {0}'.format(colored(command))
         if obj.leaf:
@@ -738,14 +742,16 @@ class CeleryCommand(Command):
         ])
 
     @classmethod
-    def list_commands(self, indent=0):
-        white = term.colored().white
+    def list_commands(self, indent=0, colored=None):
+        colored = term.colored() if colored is None else colored
+        white = colored.white
         ret = []
         for cls, commands, color in command_classes:
             ret.extend([
                 text.indent('+ {0}: '.format(white(cls)), indent),
-                '\n'.join(self.get_command_info(command, indent + 4, color)
-                          for command in commands),
+                '\n'.join(
+                    self.get_command_info(command, indent + 4, color, colored)
+                    for command in commands),
                 ''
             ])
         return '\n'.join(ret).strip()