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

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 

7 

8 

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 

14 

15 

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() 

21 

22 

23# django.contrib.postgres requires psycopg2 

24try: 

25 from django.contrib.postgres import fields as postgres_fields 

26except ImportError: 

27 postgres_fields = None 

28 

29 

30# coreapi is required for CoreAPI schema generation 

31try: 

32 import coreapi 

33except ImportError: 

34 coreapi = None 

35 

36# uritemplate is required for OpenAPI and CoreAPI schema generation 

37try: 

38 import uritemplate 

39except ImportError: 

40 uritemplate = None 

41 

42 

43# coreschema is optional 

44try: 

45 import coreschema 

46except ImportError: 

47 coreschema = None 

48 

49 

50# pyyaml is optional 

51try: 

52 import yaml 

53except ImportError: 

54 yaml = None 

55 

56 

57# requests is optional 

58try: 

59 import requests 

60except ImportError: 

61 requests = None 

62 

63 

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'] 

67 

68 

69# Markdown is optional (version 3.0+ required) 

70try: 

71 import markdown 

72 

73 HEADERID_EXT_PATH = 'markdown.extensions.toc' 

74 LEVEL_PARAM = 'baselevel' 

75 

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 

95 

96 

97try: 

98 import pygments 

99 from pygments.formatters import HtmlFormatter 

100 from pygments.lexers import TextLexer, get_lexer_by_name 

101 

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) 

106 

107 def pygments_css(style): 

108 formatter = HtmlFormatter(style=style) 

109 return formatter.get_style_defs('.highlight') 

110 

111except ImportError: 

112 pygments = None 

113 

114 def pygments_highlight(text, lang, style): 

115 return text 

116 

117 def pygments_css(style): 

118 return None 

119 

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/ 

123 

124 import re 

125 

126 from markdown.preprocessors import Preprocessor 

127 

128 class CodeBlockPreprocessor(Preprocessor): 

129 pattern = re.compile( 

130 r'^\s*``` *([^\n]+)\n(.+?)^\s*```', re.M | re.S) 

131 

132 formatter = HtmlFormatter() 

133 

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&nbsp;\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") 

146 

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 

153 

154 

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 = (',', ': ')