Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose all arguments in from_* methods #304

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions magic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,50 +184,49 @@ def __del__(self):
_instances = {}


def _get_magic_type(mime):
i = _instances.get(mime)
def _get_magic_type(kwargs):
# Dicts are not hashable but sorting and creating a tuple is a valid hash
key = hash(tuple(sorted(kwargs.items())))
i = _instances.get(key)
if i is None:
i = _instances[mime] = Magic(mime=mime)
i = _instances[key] = Magic(**kwargs)
return i


def from_file(filename, mime=False):
def from_file(filename, **kwargs):
""""
Accepts a filename and returns the detected filetype. Return
value is the mimetype if mime=True, otherwise a human readable
name.
Accepts a filename and returns the detected filetype.
Accepts all arguments to Magic as keyword arguments.

>>> magic.from_file("testdata/test.pdf", mime=True)
'application/pdf'
"""
m = _get_magic_type(mime)
m = _get_magic_type(kwargs)
return m.from_file(filename)


def from_buffer(buffer, mime=False):
def from_buffer(buffer, **kwargs):
"""
Accepts a binary string and returns the detected filetype. Return
value is the mimetype if mime=True, otherwise a human readable
name.
Accepts a binary string and returns the detected filetype.
Accepts all arguments to Magic as keyword arguments.

>>> magic.from_buffer(open("testdata/test.pdf").read(1024))
'PDF document, version 1.2'
"""
m = _get_magic_type(mime)
m = _get_magic_type(kwargs)
return m.from_buffer(buffer)


def from_descriptor(fd, mime=False):
def from_descriptor(fd, **kwargs):
"""
Accepts a file descriptor and returns the detected filetype. Return
value is the mimetype if mime=True, otherwise a human readable
name.
Accepts a file descriptor and returns the detected filetype.
Accepts all arguments to Magic as keyword arguments.

>>> f = open("testdata/test.pdf")
>>> magic.from_descriptor(f.fileno())
'PDF document, version 1.2'
"""
m = _get_magic_type(mime)
m = _get_magic_type(kwargs)
return m.from_descriptor(fd)

from . import loader
Expand Down