diff options
| author | Tom Christie | 2013-08-19 08:24:27 +0100 |
|---|---|---|
| committer | Tom Christie | 2013-08-19 08:24:27 +0100 |
| commit | 89b0a539c389477cfd7df7df461868b85f618d95 (patch) | |
| tree | 3b37dd82a657d92a06a229175253dc88c6c8f7f9 /rest_framework/utils/formatting.py | |
| parent | a8aabe23c94b1281ac8ca752a09555c1b0ef163d (diff) | |
| download | django-rest-framework-89b0a539c389477cfd7df7df461868b85f618d95.tar.bz2 | |
Move view name/description functions into public space
Diffstat (limited to 'rest_framework/utils/formatting.py')
| -rw-r--r-- | rest_framework/utils/formatting.py | 36 |
1 files changed, 11 insertions, 25 deletions
diff --git a/rest_framework/utils/formatting.py b/rest_framework/utils/formatting.py index 89a89252..4b59ba84 100644 --- a/rest_framework/utils/formatting.py +++ b/rest_framework/utils/formatting.py @@ -5,12 +5,13 @@ from __future__ import unicode_literals from django.utils.html import escape from django.utils.safestring import mark_safe -from rest_framework.compat import apply_markdown, smart_text -import re +from rest_framework.compat import apply_markdown from rest_framework.settings import api_settings +from textwrap import dedent +import re -def _remove_trailing_string(content, trailing): +def remove_trailing_string(content, trailing): """ Strip trailing component `trailing` from `content` if it exists. Used when generating names from view classes. @@ -20,10 +21,14 @@ def _remove_trailing_string(content, trailing): return content -def _remove_leading_indent(content): +def dedent(content): """ Remove leading indent from a block of text. Used when generating descriptions from docstrings. + + Note that python's `textwrap.dedent` doesn't quite cut it, + as it fails to dedent multiline docstrings that include + unindented text on the initial line. """ whitespace_counts = [len(line) - len(line.lstrip(' ')) for line in content.splitlines()[1:] if line.lstrip()] @@ -32,11 +37,10 @@ def _remove_leading_indent(content): if whitespace_counts: whitespace_pattern = '^' + (' ' * min(whitespace_counts)) content = re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', content) - content = content.strip('\n') - return content + return content.strip() -def _camelcase_to_spaces(content): +def camelcase_to_spaces(content): """ Translate 'CamelCaseNames' to 'Camel Case Names'. Used when generating names from view classes. @@ -54,21 +58,3 @@ def markup_description(description): else: description = escape(description).replace('\n', '<br />') return mark_safe(description) - - -def view_name(instance, view, suffix=None): - name = view.__name__ - name = _remove_trailing_string(name, 'View') - name = _remove_trailing_string(name, 'ViewSet') - name = _camelcase_to_spaces(name) - if suffix: - name += ' ' + suffix - - return name - -def view_description(instance, view, html=False): - description = view.__doc__ or '' - description = _remove_leading_indent(smart_text(description)) - if html: - return markup_description(description) - return description
\ No newline at end of file |
