Browse Source

Utils: Moves simple_format -> .utils.text

Ask Solem 8 years ago
parent
commit
c21d6a9c17
2 changed files with 31 additions and 27 deletions
  1. 1 26
      celery/utils/__init__.py
  2. 30 1
      celery/utils/text.py

+ 1 - 26
celery/utils/__init__.py

@@ -17,7 +17,6 @@ import traceback
 import warnings
 import datetime
 
-from collections import Callable
 from functools import partial
 from pprint import pprint
 
@@ -28,6 +27,7 @@ from celery.exceptions import CPendingDeprecationWarning, CDeprecationWarning
 from celery.five import WhateverIO, items, reraise, string_t
 
 from .functional import memoize
+from .text import simple_format
 
 __all__ = ['worker_direct', 'warn_deprecated', 'deprecated', 'lpmerge',
            'is_iterable', 'isatty', 'cry', 'maybe_reraise', 'strtobool',
@@ -47,12 +47,6 @@ DEPRECATION_FMT = """
     version {removal}. {alternative}
 """
 
-UNKNOWN_SIMPLE_FORMAT_KEY = """
-Unknown format %{0} in string {1!r}.
-Possible causes: Did you forget to escape the expand sign (use '%%{0!r}'),
-or did you escape and the value was expanded twice? (%%N -> %N -> %hostname)?
-""".strip()
-
 #: Billiard sets this when execv is enabled.
 #: We use it to find out the name of the original ``__main__``
 #: module, so that we can properly rewrite the name of the
@@ -69,7 +63,6 @@ WORKER_DIRECT_QUEUE_FORMAT = '{hostname}.dq2'
 NODENAME_SEP = '@'
 
 NODENAME_DEFAULT = 'celery'
-RE_FORMAT = re.compile(r'%(\w)')
 
 gethostname = memoize(1, Cache=dict)(socket.gethostname)
 
@@ -377,24 +370,6 @@ def host_format(s, host=None, name=None, **extra):
     return simple_format(s, keys)
 
 
-def simple_format(s, keys, pattern=RE_FORMAT, expand=r'\1'):
-    if s:
-        keys.setdefault('%', '%')
-
-        def resolve(match):
-            key = match.expand(expand)
-            try:
-                resolver = keys[key]
-            except KeyError:
-                raise ValueError(UNKNOWN_SIMPLE_FORMAT_KEY.format(key, s))
-            if isinstance(resolver, Callable):
-                return resolver()
-            return resolver
-
-        return pattern.sub(resolve, s)
-    return s
-
-
 # ------------------------------------------------------------------------ #
 # > XXX Compat
 from .log import LOG_LEVELS     # noqa

+ 30 - 1
celery/utils/text.py

@@ -8,6 +8,9 @@
 """
 from __future__ import absolute_import, unicode_literals
 
+import re
+
+from collections import Callable
 from functools import partial
 from textwrap import fill
 
@@ -19,9 +22,17 @@ __all__ = [
     'abbr', 'abbrtask', 'dedent', 'dedent_initial',
     'ensure_newlines', 'ensure_sep',
     'fill_paragraphs', 'indent', 'join',
-    'pluralize', 'pretty', 'str_to_list', 'truncate',
+    'pluralize', 'pretty', 'str_to_list', 'simple_format', 'truncate',
 ]
 
+UNKNOWN_SIMPLE_FORMAT_KEY = """
+Unknown format %{0} in string {1!r}.
+Possible causes: Did you forget to escape the expand sign (use '%%{0!r}'),
+or did you escape and the value was expanded twice? (%%N -> %N -> %hostname)?
+""".strip()
+
+RE_FORMAT = re.compile(r'%(\w)')
+
 
 def str_to_list(s):
     if isinstance(s, string_t):
@@ -105,3 +116,21 @@ def pretty(value, width=80, nl_width=80, sep='\n', **kw):
 
 def match_case(s, other):
     return s.upper() if other.isupper() else s.lower()
+
+
+def simple_format(s, keys, pattern=RE_FORMAT, expand=r'\1'):
+    if s:
+        keys.setdefault('%', '%')
+
+        def resolve(match):
+            key = match.expand(expand)
+            try:
+                resolver = keys[key]
+            except KeyError:
+                raise ValueError(UNKNOWN_SIMPLE_FORMAT_KEY.format(key, s))
+            if isinstance(resolver, Callable):
+                return resolver()
+            return resolver
+
+        return pattern.sub(resolve, s)
+    return s