Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/rest_framework/compat.py: 34%
92 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1"""
2The `compat` module provides support for backwards compatibility with older
3versions of Django/Python, and compatibility wrappers around optional packages.
4"""
5from django.conf import settings
6from django.views.generic import View
9def unicode_http_header(value):
10 # Coerce HTTP header value to unicode.
11 if isinstance(value, bytes):
12 return value.decode('iso-8859-1')
13 return value
16def distinct(queryset, base):
17 if settings.DATABASES[queryset.db]["ENGINE"] == "django.db.backends.oracle":
18 # distinct analogue for Oracle users
19 return base.filter(pk__in=set(queryset.values_list('pk', flat=True)))
20 return queryset.distinct()
23# django.contrib.postgres requires psycopg2
24try:
25 from django.contrib.postgres import fields as postgres_fields
26except ImportError:
27 postgres_fields = None
30# coreapi is required for CoreAPI schema generation
31try:
32 import coreapi
33except ImportError:
34 coreapi = None
36# uritemplate is required for OpenAPI and CoreAPI schema generation
37try:
38 import uritemplate
39except ImportError:
40 uritemplate = None
43# coreschema is optional
44try:
45 import coreschema
46except ImportError:
47 coreschema = None
50# pyyaml is optional
51try:
52 import yaml
53except ImportError:
54 yaml = None
57# requests is optional
58try:
59 import requests
60except ImportError:
61 requests = None
64# PATCH method is not implemented by Django
65if 'patch' not in View.http_method_names: 65 ↛ 66line 65 didn't jump to line 66, because the condition on line 65 was never true
66 View.http_method_names = View.http_method_names + ['patch']
69# Markdown is optional (version 3.0+ required)
70try:
71 import markdown
73 HEADERID_EXT_PATH = 'markdown.extensions.toc'
74 LEVEL_PARAM = 'baselevel'
76 def apply_markdown(text):
77 """
78 Simple wrapper around :func:`markdown.markdown` to set the base level
79 of '#' style headers to <h2>.
80 """
81 extensions = [HEADERID_EXT_PATH]
82 extension_configs = {
83 HEADERID_EXT_PATH: {
84 LEVEL_PARAM: '2'
85 }
86 }
87 md = markdown.Markdown(
88 extensions=extensions, extension_configs=extension_configs
89 )
90 md_filter_add_syntax_highlight(md)
91 return md.convert(text)
92except ImportError:
93 apply_markdown = None
94 markdown = None
97try:
98 import pygments
99 from pygments.formatters import HtmlFormatter
100 from pygments.lexers import TextLexer, get_lexer_by_name
102 def pygments_highlight(text, lang, style):
103 lexer = get_lexer_by_name(lang, stripall=False)
104 formatter = HtmlFormatter(nowrap=True, style=style)
105 return pygments.highlight(text, lexer, formatter)
107 def pygments_css(style):
108 formatter = HtmlFormatter(style=style)
109 return formatter.get_style_defs('.highlight')
111except ImportError:
112 pygments = None
114 def pygments_highlight(text, lang, style):
115 return text
117 def pygments_css(style):
118 return None
120if markdown is not None and pygments is not None: 120 ↛ 124line 120 didn't jump to line 124, because the condition on line 120 was never true
121 # starting from this blogpost and modified to support current markdown extensions API
122 # https://zerokspot.com/weblog/2008/06/18/syntax-highlighting-in-markdown-with-pygments/
124 import re
126 from markdown.preprocessors import Preprocessor
128 class CodeBlockPreprocessor(Preprocessor):
129 pattern = re.compile(
130 r'^\s*``` *([^\n]+)\n(.+?)^\s*```', re.M | re.S)
132 formatter = HtmlFormatter()
134 def run(self, lines):
135 def repl(m):
136 try:
137 lexer = get_lexer_by_name(m.group(1))
138 except (ValueError, NameError):
139 lexer = TextLexer()
140 code = m.group(2).replace('\t', ' ')
141 code = pygments.highlight(code, lexer, self.formatter)
142 code = code.replace('\n\n', '\n \n').replace('\n', '<br />').replace('\\@', '@')
143 return '\n\n%s\n\n' % code
144 ret = self.pattern.sub(repl, "\n".join(lines))
145 return ret.split("\n")
147 def md_filter_add_syntax_highlight(md):
148 md.preprocessors.register(CodeBlockPreprocessor(), 'highlight', 40)
149 return True
150else:
151 def md_filter_add_syntax_highlight(md):
152 return False
155# `separators` argument to `json.dumps()` differs between 2.x and 3.x
156# See: https://bugs.python.org/issue22767
157SHORT_SEPARATORS = (',', ':')
158LONG_SEPARATORS = (', ', ': ')
159INDENT_SEPARATORS = (',', ': ')