Where did I get this magic module from initially?

This commit is contained in:
Geoffrey Frogeye 2021-06-13 11:42:37 +02:00
parent 59aaf63d4a
commit fb6cfce656
Signed by: geoffrey
GPG key ID: C72403E7F82E6AD8
2 changed files with 10 additions and 11 deletions

View file

@ -19,21 +19,21 @@ import magic
path = sys.argv[1]
# Getting the MIME type
ishttp = path.startswith('http')
ishttp = path.startswith("http")
buf = None
if ishttp:
buf = urllib.request.urlopen(path)
chunk = buf.read(1024)
mime = magic.from_buffer(chunk, mime=True)
fmagic = magic.detect_from_content(chunk)
else:
assert os.path.isfile(path), f"Not a file: {path}"
path = os.path.realpath(path)
mime = magic.from_file(path, mime=True)
mime = tuple(mime.split('/'))
fmagic = magic.detect_from_filename(path)
mime = tuple(fmagic.mime_type.split("/"))
assert len(mime) == 2
graphical = os.environ.get('DISPLAY')
graphical = os.environ.get("DISPLAY")
# Some energumens
if mime[0] == "application" and mime[1] in ("json", "javascript"):
@ -46,7 +46,7 @@ isterm = False # Executable should run in a terminal
if mime[0] == "text":
if not ishttp:
ex = os.environ.get('VISUAL' if graphical else 'EDITOR', None)
ex = os.environ.get("VISUAL" if graphical else "EDITOR", None)
isterm = True
elif mime[0] == "image":
ex = "feh"
@ -62,14 +62,14 @@ tmp = None
if ex:
if forcelocal and ishttp:
assert buf
tmp = tempfile.NamedTemporaryFile(prefix='o')
tmp = tempfile.NamedTemporaryFile(prefix="o")
tmp.write(chunk)
tmp.write(buf.read())
path = tmp.name
else:
ex = 'xdg-open'
ex = "xdg-open"
if ishttp:
ex = os.environ.get('BROWSER', ex)
ex = os.environ.get("BROWSER", ex)
if buf:
buf.close()

View file

@ -7,7 +7,6 @@ import logging
import magic
import typing
import coloredlogs
import enum
# TODO Able to ignore extensions everywhere
@ -139,7 +138,7 @@ class TreeExtractor:
filepath = os.path.join(real_root, name)
with open(filepath, "rb") as filedesc:
header = filedesc.read(1024)
mime = magic.from_buffer(header, mime=True)
mime = magic.detect_from_content(header).mime_type
archive_type = None
for archtyp in self.ARCHIVE_TYPES: