mirror of
https://github.com/httpie/cli.git
synced 2025-06-20 09:37:45 +02:00
Annotate formatters and processing
This commit is contained in:
parent
d603502960
commit
30624e66ec
@ -1,5 +1,6 @@
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
import json
|
import json
|
||||||
|
from typing import Type
|
||||||
|
|
||||||
import pygments.lexer
|
import pygments.lexer
|
||||||
import pygments.token
|
import pygments.token
|
||||||
@ -13,6 +14,7 @@ from pygments.lexers.text import HttpLexer as PygmentsHttpLexer
|
|||||||
from pygments.util import ClassNotFound
|
from pygments.util import ClassNotFound
|
||||||
|
|
||||||
from httpie.compat import is_windows
|
from httpie.compat import is_windows
|
||||||
|
from httpie.context import Environment
|
||||||
from httpie.plugins import FormatterPlugin
|
from httpie.plugins import FormatterPlugin
|
||||||
|
|
||||||
|
|
||||||
@ -40,8 +42,13 @@ class ColorFormatter(FormatterPlugin):
|
|||||||
"""
|
"""
|
||||||
group_name = 'colors'
|
group_name = 'colors'
|
||||||
|
|
||||||
def __init__(self, env, explicit_json=False,
|
def __init__(
|
||||||
color_scheme=DEFAULT_STYLE, **kwargs):
|
self,
|
||||||
|
env: Environment,
|
||||||
|
explicit_json=False,
|
||||||
|
color_scheme=DEFAULT_STYLE,
|
||||||
|
**kwargs
|
||||||
|
):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
if not env.colors:
|
if not env.colors:
|
||||||
@ -63,14 +70,14 @@ class ColorFormatter(FormatterPlugin):
|
|||||||
self.formatter = formatter
|
self.formatter = formatter
|
||||||
self.http_lexer = http_lexer
|
self.http_lexer = http_lexer
|
||||||
|
|
||||||
def format_headers(self, headers):
|
def format_headers(self, headers: str) -> str:
|
||||||
return pygments.highlight(
|
return pygments.highlight(
|
||||||
code=headers,
|
code=headers,
|
||||||
lexer=self.http_lexer,
|
lexer=self.http_lexer,
|
||||||
formatter=self.formatter,
|
formatter=self.formatter,
|
||||||
).strip()
|
).strip()
|
||||||
|
|
||||||
def format_body(self, body, mime):
|
def format_body(self, body: str, mime: str) -> str:
|
||||||
lexer = self.get_lexer_for_body(mime, body)
|
lexer = self.get_lexer_for_body(mime, body)
|
||||||
if lexer:
|
if lexer:
|
||||||
body = pygments.highlight(
|
body = pygments.highlight(
|
||||||
@ -80,21 +87,29 @@ class ColorFormatter(FormatterPlugin):
|
|||||||
)
|
)
|
||||||
return body.strip()
|
return body.strip()
|
||||||
|
|
||||||
def get_lexer_for_body(self, mime, body):
|
def get_lexer_for_body(
|
||||||
|
self, mime: str,
|
||||||
|
body: str
|
||||||
|
) -> Type[pygments.lexer.Lexer]:
|
||||||
return get_lexer(
|
return get_lexer(
|
||||||
mime=mime,
|
mime=mime,
|
||||||
explicit_json=self.explicit_json,
|
explicit_json=self.explicit_json,
|
||||||
body=body,
|
body=body,
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_style_class(self, color_scheme):
|
@staticmethod
|
||||||
|
def get_style_class(color_scheme: str) -> Type[pygments.style.Style]:
|
||||||
try:
|
try:
|
||||||
return pygments.styles.get_style_by_name(color_scheme)
|
return pygments.styles.get_style_by_name(color_scheme)
|
||||||
except ClassNotFound:
|
except ClassNotFound:
|
||||||
return Solarized256Style
|
return Solarized256Style
|
||||||
|
|
||||||
|
|
||||||
def get_lexer(mime, explicit_json=False, body=''):
|
def get_lexer(
|
||||||
|
mime: str,
|
||||||
|
explicit_json=False,
|
||||||
|
body=''
|
||||||
|
) -> Type[pygments.lexer.Lexer]:
|
||||||
|
|
||||||
# Build candidate mime type and lexer names.
|
# Build candidate mime type and lexer names.
|
||||||
mime_types, lexer_names = [mime], []
|
mime_types, lexer_names = [mime], []
|
||||||
|
@ -3,7 +3,7 @@ from httpie.plugins import FormatterPlugin
|
|||||||
|
|
||||||
class HeadersFormatter(FormatterPlugin):
|
class HeadersFormatter(FormatterPlugin):
|
||||||
|
|
||||||
def format_headers(self, headers):
|
def format_headers(self, headers: str) -> str:
|
||||||
"""
|
"""
|
||||||
Sorts headers by name while retaining relative
|
Sorts headers by name while retaining relative
|
||||||
order of multiple headers with the same name.
|
order of multiple headers with the same name.
|
||||||
|
@ -9,7 +9,7 @@ DEFAULT_INDENT = 4
|
|||||||
|
|
||||||
class JSONFormatter(FormatterPlugin):
|
class JSONFormatter(FormatterPlugin):
|
||||||
|
|
||||||
def format_body(self, body, mime):
|
def format_body(self, body: str, mime: str) -> str:
|
||||||
maybe_json = [
|
maybe_json = [
|
||||||
'json',
|
'json',
|
||||||
'javascript',
|
'javascript',
|
||||||
|
@ -40,12 +40,12 @@ class Formatting:
|
|||||||
if p.enabled:
|
if p.enabled:
|
||||||
self.enabled_plugins.append(p)
|
self.enabled_plugins.append(p)
|
||||||
|
|
||||||
def format_headers(self, headers):
|
def format_headers(self, headers: str) -> str:
|
||||||
for p in self.enabled_plugins:
|
for p in self.enabled_plugins:
|
||||||
headers = p.format_headers(headers)
|
headers = p.format_headers(headers)
|
||||||
return headers
|
return headers
|
||||||
|
|
||||||
def format_body(self, content, mime):
|
def format_body(self, content: str, mime: str) -> str:
|
||||||
if is_valid_mime(mime):
|
if is_valid_mime(mime):
|
||||||
for p in self.enabled_plugins:
|
for p in self.enabled_plugins:
|
||||||
content = p.format_body(content, mime)
|
content = p.format_body(content, mime)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user