-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreview_formatter.py
59 lines (42 loc) · 1.81 KB
/
preview_formatter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import json
import re
from string_helper import truncate
class FormatterInterface:
def match(self, content_type) -> bool:
pass
def format(self, text) -> str | None:
pass
class JsonFormatter(FormatterInterface):
def __init__(self, max_lines: int = 1000, max_columns: int = 500):
self._max_lines = max_lines
self._max_columns = max_columns
def match(self, content_type) -> bool:
return re.match(r'\b(application|text)/json\b', content_type.lower()) is not None
def format(self, content: bytes) -> str | None:
try:
parsed_data = json.loads(content.decode('utf-8'))
return truncate(json.dumps(parsed_data, indent=2, ensure_ascii=False), lines=self._max_lines,
columns=self._max_columns)
except(json.JSONDecodeError, UnicodeDecodeError):
return None
class TextFormatter(FormatterInterface):
def __init__(self, max_lines: int = 1000, max_columns: int = 500):
self._max_lines = max_lines
self._max_columns = max_columns
def match(self, content_type) -> bool:
return re.match(r'^text/', content_type.lower()) is not None
def format(self, content: bytes) -> str | None:
try:
return truncate(content.decode('utf-8'), lines=self._max_lines, columns=self._max_columns)
except UnicodeDecodeError:
return None
class Formatter:
def __init__(self):
self.formatters = [JsonFormatter(), TextFormatter()]
def format(self, content_type: str, content: bytes):
for formatter in self.formatters:
if formatter.match(content_type):
formatted_content = formatter.format(content)
if formatted_content is not None:
return formatted_content
return None