Where did I get this magic module from initially?
This commit is contained in:
parent
59aaf63d4a
commit
fb6cfce656
|
@ -19,21 +19,21 @@ import magic
|
||||||
path = sys.argv[1]
|
path = sys.argv[1]
|
||||||
|
|
||||||
# Getting the MIME type
|
# Getting the MIME type
|
||||||
ishttp = path.startswith('http')
|
ishttp = path.startswith("http")
|
||||||
|
|
||||||
buf = None
|
buf = None
|
||||||
if ishttp:
|
if ishttp:
|
||||||
buf = urllib.request.urlopen(path)
|
buf = urllib.request.urlopen(path)
|
||||||
chunk = buf.read(1024)
|
chunk = buf.read(1024)
|
||||||
mime = magic.from_buffer(chunk, mime=True)
|
fmagic = magic.detect_from_content(chunk)
|
||||||
else:
|
else:
|
||||||
assert os.path.isfile(path), f"Not a file: {path}"
|
assert os.path.isfile(path), f"Not a file: {path}"
|
||||||
path = os.path.realpath(path)
|
path = os.path.realpath(path)
|
||||||
mime = magic.from_file(path, mime=True)
|
fmagic = magic.detect_from_filename(path)
|
||||||
mime = tuple(mime.split('/'))
|
mime = tuple(fmagic.mime_type.split("/"))
|
||||||
assert len(mime) == 2
|
assert len(mime) == 2
|
||||||
|
|
||||||
graphical = os.environ.get('DISPLAY')
|
graphical = os.environ.get("DISPLAY")
|
||||||
|
|
||||||
# Some energumens
|
# Some energumens
|
||||||
if mime[0] == "application" and mime[1] in ("json", "javascript"):
|
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 mime[0] == "text":
|
||||||
if not ishttp:
|
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
|
isterm = True
|
||||||
elif mime[0] == "image":
|
elif mime[0] == "image":
|
||||||
ex = "feh"
|
ex = "feh"
|
||||||
|
@ -62,14 +62,14 @@ tmp = None
|
||||||
if ex:
|
if ex:
|
||||||
if forcelocal and ishttp:
|
if forcelocal and ishttp:
|
||||||
assert buf
|
assert buf
|
||||||
tmp = tempfile.NamedTemporaryFile(prefix='o')
|
tmp = tempfile.NamedTemporaryFile(prefix="o")
|
||||||
tmp.write(chunk)
|
tmp.write(chunk)
|
||||||
tmp.write(buf.read())
|
tmp.write(buf.read())
|
||||||
path = tmp.name
|
path = tmp.name
|
||||||
else:
|
else:
|
||||||
ex = 'xdg-open'
|
ex = "xdg-open"
|
||||||
if ishttp:
|
if ishttp:
|
||||||
ex = os.environ.get('BROWSER', ex)
|
ex = os.environ.get("BROWSER", ex)
|
||||||
if buf:
|
if buf:
|
||||||
buf.close()
|
buf.close()
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ import logging
|
||||||
import magic
|
import magic
|
||||||
import typing
|
import typing
|
||||||
import coloredlogs
|
import coloredlogs
|
||||||
import enum
|
|
||||||
|
|
||||||
# TODO Able to ignore extensions everywhere
|
# TODO Able to ignore extensions everywhere
|
||||||
|
|
||||||
|
@ -139,7 +138,7 @@ class TreeExtractor:
|
||||||
filepath = os.path.join(real_root, name)
|
filepath = os.path.join(real_root, name)
|
||||||
with open(filepath, "rb") as filedesc:
|
with open(filepath, "rb") as filedesc:
|
||||||
header = filedesc.read(1024)
|
header = filedesc.read(1024)
|
||||||
mime = magic.from_buffer(header, mime=True)
|
mime = magic.detect_from_content(header).mime_type
|
||||||
|
|
||||||
archive_type = None
|
archive_type = None
|
||||||
for archtyp in self.ARCHIVE_TYPES:
|
for archtyp in self.ARCHIVE_TYPES:
|
||||||
|
|
Loading…
Reference in a new issue