本文整理汇总了Python中rest_framework.utils.formatting.dedent函数的典型用法代码示例。如果您正苦于以下问题:Python dedent函数的具体用法?Python dedent怎么用?Python dedent使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了dedent函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_docstring_is_not_stripped_by_get_description
def test_docstring_is_not_stripped_by_get_description():
class ExampleDocstringAPIView(APIView):
"""
=== title
* item a
* item a-a
* item a-b
* item b
- item 1
- item 2
code block begin
code
code
code
code block end
the end
"""
def get(self, *args, **kwargs):
pass
def post(self, request, *args, **kwargs):
pass
view = ExampleDocstringAPIView()
schema = view.schema
descr = schema.get_description('example', 'get')
# the first and last character are '\n' correctly removed by get_description
assert descr == formatting.dedent(ExampleDocstringAPIView.__doc__[1:][:-1])
示例2: get_notes
def get_notes(self):
"""
Returns the body of the docstring trimmed before any parameters are
listed. First, get the class docstring and then get the method's. The
methods will always inherit the class comments.
"""
docstring = ""
class_docs = self.callback.__doc__ or ''
class_docs = smart_text(class_docs)
class_docs = IntrospectorHelper.strip_yaml_from_docstring(class_docs)
class_docs = IntrospectorHelper.strip_params_from_docstring(class_docs)
method_docs = self.get_docs()
if class_docs is not None:
docstring += class_docs + " \n"
if method_docs is not None:
method_docs = formatting.dedent(smart_text(method_docs))
method_docs = IntrospectorHelper.strip_yaml_from_docstring(
method_docs
)
method_docs = IntrospectorHelper.strip_params_from_docstring(
method_docs
)
docstring += '\n' + method_docs
# Markdown is optional
if apply_markdown:
docstring = apply_markdown(docstring)
else:
docstring = docstring.replace("\n\n", "<br/>")
return docstring
示例3: get_description
def get_description(self, path, method, view):
"""
Determine a link description.
This will be based on the method docstring if one exists,
or else the class docstring.
"""
method_name = getattr(view, 'action', method.lower())
method_docstring = getattr(view, method_name, None).__doc__
if method_docstring:
# An explicit docstring on the method or action.
return formatting.dedent(smart_text(method_docstring))
description = view.get_view_description()
lines = [line.strip() for line in description.splitlines()]
current_section = ''
sections = {'': ''}
for line in lines:
if header_regex.match(line):
current_section, seperator, lead = line.partition(':')
sections[current_section] = lead.strip()
else:
sections[current_section] += line + '\n'
header = getattr(view, 'action', method.lower())
if header in sections:
return sections[header].strip()
if header in self.coerce_method_names:
if self.coerce_method_names[header] in sections:
return sections[self.coerce_method_names[header]].strip()
return sections[''].strip()
示例4: get_view_description
def get_view_description(view_cls, html=False):
"""
Given a view class, return a textual description to represent the view.
This name is used in the browsable API, and in OPTIONS responses.
This function is the default for the `VIEW_DESCRIPTION_FUNCTION` setting.
"""
description = view_cls.__doc__ or ''
description = formatting.dedent(smart_text(description))
if hasattr(getattr(view_cls, 'serializer_class', 'None'), 'Meta'):
doc_url = get_doc_url(
'api',
'{0}s'.format(
view_cls.serializer_class.Meta.model.__name__.lower()
)
)
else:
doc_url = get_doc_url('api')
if html:
return (
formatting.markup_description(description) +
mark_safe(DOC_TEXT.format(doc_url))
)
return description
示例5: get_view_doc
def get_view_doc(view, html=True):
"""
Build view documentation. Return in html format.
If you want in markdown format, use html=False
"""
try:
description = view.__doc__ or ''
description = formatting.dedent(smart_text(description))
# include filters in description
filter_fields = get_filter_fields(view)
if filter_fields:
filter_doc = ['\n\n\n## Filters', '']
for f in filter_fields:
filter_doc.append('- `%s`' % f)
description += '\n'.join(filter_doc)
# replace {api_url} by current base url
api_url = "/api"
description = description.replace('{api_url}', api_url)
if html:
description = formatting.markup_description(description)
return description
except:
import traceback
traceback.print_exc()
raise
示例6: get_notes
def get_notes(self):
"""
Returns the body of the docstring trimmed before any parameters are
listed. First, get the class docstring and then get the method's. The
methods will always inherit the class comments.
"""
docstring = ""
class_docs = get_view_description(self.callback)
class_docs = IntrospectorHelper.strip_yaml_from_docstring(class_docs)
class_docs = IntrospectorHelper.strip_params_from_docstring(class_docs)
method_docs = self.get_docs()
if class_docs is not None:
docstring += class_docs + " \n"
if method_docs is not None:
method_docs = formatting.dedent(smart_text(method_docs))
method_docs = IntrospectorHelper.strip_yaml_from_docstring(
method_docs
)
method_docs = IntrospectorHelper.strip_params_from_docstring(
method_docs
)
docstring += '\n' + method_docs
docstring = docstring.strip()
return do_markdown(docstring)
示例7: get_restructuredtext
def get_restructuredtext(view_cls, html=False):
from docutils import core
description = view_cls.__doc__ or ''
description = formatting.dedent(smart_text(description))
if html:
parts = core.publish_parts(source=description, writer_name='html')
html = parts['body_pre_docinfo'] + parts['fragment']
return mark_safe(html)
return description
示例8: get_view_description
def get_view_description(self, html=False):
description = self.__doc__ or """
Returns a nested list of objects that would be also be deleted when
this object is deleted.
"""
description = formatting.dedent(description)
if html:
return formatting.markup_description(description)
return description
示例9: get_view_description
def get_view_description(view_cls, html=False):
"""
Given a view class, return a textual description to represent the view.
This name is used in the browsable API, and in OPTIONS responses.
This function is the default for the `VIEW_DESCRIPTION_FUNCTION` setting.
"""
description = view_cls.__doc__ or ''
description = formatting.dedent(smart_text(description))
if html:
return formatting.markup_description(description)
return description
示例10: format_docstring
def format_docstring(self, view, method, docstring):
macros = settings.BROWSABLE_DOCUMENT_MACROS
if view:
macros['FILTERS'] = get_filters(view)
if '%(SERIALIZER)s' in docstring:
macros['SERIALIZER'] = get_serializer(view, include_read_only=True)
if '%(WRITABLE_SERIALIZER)s' in docstring:
macros['WRITABLE_SERIALIZER'] = get_serializer(view, include_read_only=False)
string = formatting.dedent(docstring)
formatted = string % macros
formatted = self.substitute_urls(view, method, formatted)
string = smart_text(formatted)
return formatting.markup_description(string)
示例11: get_view_description
def get_view_description(view_cls, html=False):
from django_markwhat.templatetags.markup import restructuredtext
description = view_cls.__doc__ or ''
description = formatting.dedent(smart_text(description))
if html:
rst_doc = getattr(view_cls, 'rst_doc', None)
if rst_doc:
if isinstance(rst_doc, str):
path = os.path.dirname(sys.modules[view_cls.__module__].__file__)
path = os.path.join(path, rst_doc)
with open(path) as rst:
return restructuredtext(rst.read())
return restructuredtext(description)
return formatting.markup_description(description)
return description
示例12: get_view_description
def get_view_description(cls, html=False):
description = ''
if getattr(cls, 'get_description', None):
cache_key = cls.__name__
cached_description = cache.get(cache_key)
if not cached_description:
description = cls.get_description()
description = formatting.dedent(smart_text(description))
# Cache for 1 hour (if we update description, it will take 1 hour to show)
cache.set(cache_key, description, 60*60)
else:
description = cached_description
if html:
return formatting.markup_description(description)
return description
示例13: get_view_description
def get_view_description(view, html=False):
"""
Given a view class, return a textual description to represent the view.
This name is used in the browsable API, and in OPTIONS responses.
This function is the default for the `VIEW_DESCRIPTION_FUNCTION` setting.
"""
# Description may be set by some Views, such as a ViewSet.
description = getattr(view, 'description', None)
if description is None:
description = view.__class__.__doc__ or ''
description = formatting.dedent(smart_text(description))
if html:
return formatting.markup_description(description)
return description
示例14: get_description
def get_description(self, path, method):
"""
Determine a link description.
This will be based on the method docstring if one exists,
or else the class docstring.
"""
view = self.view
method_name = getattr(view, 'action', method.lower())
method_docstring = getattr(view, method_name, None).__doc__
if method_docstring:
# An explicit docstring on the method or action.
return self._get_description_section(view, method.lower(), formatting.dedent(smart_text(method_docstring)))
else:
return self._get_description_section(view, getattr(view, 'action', method.lower()), view.get_view_description())
示例15: load_obj_from_docstring
def load_obj_from_docstring(self, docstring):
"""Loads YAML from docstring"""
# update one dict by other recursively
def recursive_update(obj, other):
for key, value in other.iteritems():
if key in obj and key != 'overwrite':
# if value is dictionary we need to update it
if isinstance(value, dict) and not value.get('overwrite', False):
recursive_update(obj[key], other[key])
# if value is a list we need to extend it
elif isinstance(value, list):
obj[key].extend(value)
else:
obj[key] = value
else:
obj[key] = value
if not docstring:
return {}
split_lines = trim_docstring(docstring).split('\n')
# Cut YAML from rest of docstring
for index, line in enumerate(split_lines):
line = line.strip()
if line.startswith('---'):
cut_from = index
break
else:
return {}
yaml_string = formatting.dedent("\n".join(split_lines[cut_from:]))
try:
yaml_obj = yaml.load(yaml_string)
# if is parent view specified, we need to get docs from parent view
if 'inherit_docs_from' in yaml_obj:
parent_class = self._load_class(yaml_obj['inherit_docs_from'], self.method_introspector.callback)
parent_docs = self.method_introspector.get_inherited_docs(parent_class)
parent_obj = self.load_obj_from_docstring(docstring=parent_docs)
recursive_update(parent_obj, yaml_obj)
yaml_obj = parent_obj
except yaml.YAMLError as e:
self.yaml_error = e
return {}
return yaml_obj