forked from mindflayer/python-mocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompat.py
More file actions
37 lines (27 loc) · 900 Bytes
/
Copy pathcompat.py
File metadata and controls
37 lines (27 loc) · 900 Bytes
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
import codecs
import os
import shlex
ENCODING = os.getenv("MOCKET_ENCODING", "utf-8")
text_type = str
byte_type = bytes
basestring = (str,)
def encode_to_bytes(s, encoding=ENCODING):
if isinstance(s, text_type):
s = s.encode(encoding)
return byte_type(s)
def decode_from_bytes(s, encoding=ENCODING):
if isinstance(s, byte_type):
s = codecs.decode(s, encoding, "ignore")
return text_type(s)
def shsplit(s):
s = decode_from_bytes(s)
return shlex.split(s)
def do_the_magic(lib_magic, body): # pragma: no cover
if hasattr(lib_magic, "from_buffer"):
# PyPI python-magic
return lib_magic.from_buffer(body, mime=True)
# file's builtin python wrapper
# used by https://www.archlinux.org/packages/community/any/python-mocket/
_magic = lib_magic.open(lib_magic.MAGIC_MIME_TYPE)
_magic.load()
return _magic.buffer(body)