forked from mindflayer/python-mocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompat.py
35 lines (24 loc) · 806 Bytes
/
compat.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
from __future__ import annotations
import codecs
import os
import shlex
from typing import Final
import puremagic
ENCODING: Final[str] = os.getenv("MOCKET_ENCODING", "utf-8")
def encode_to_bytes(s: str | bytes, encoding: str = ENCODING) -> bytes:
if isinstance(s, str):
s = s.encode(encoding)
return bytes(s)
def decode_from_bytes(s: str | bytes, encoding: str = ENCODING) -> str:
if isinstance(s, bytes):
s = codecs.decode(s, encoding, "ignore")
return str(s)
def shsplit(s: str | bytes) -> list[str]:
s = decode_from_bytes(s)
return shlex.split(s)
def do_the_magic(body):
try:
magic = puremagic.magic_string(body)
except puremagic.PureError:
magic = []
return magic[0].mime_type if len(magic) else "application/octet-stream"