filetype.utils module
# -*- coding: utf-8 -*-
def get_signature_bytes(path):
"""
Reads file from disk and returns the first 256 bytes
of data representing the magic number header signature.
:param path str
:rtype bytearray
"""
with open(path, 'rb') as f:
return bytearray(f.read(256))
def signature(array):
"""
Returns the first 256 bytes of the given bytearray
as part of the file header signature.
:param array bytearray
:rtype bytearray
"""
length = len(array)
index = 256 if length > 256 else length
return array[:index]
def get_bytes(obj):
"""
Infers the input type and reads the first 256 bytes,
returning a sliced bytearray.
:param obj obj
:rtype bytearray
"""
kind = type(obj)
if kind == bytearray:
return signature(obj)
if kind == str:
return get_signature_bytes(obj)
if kind is bytes:
return signature(bytearray(obj))
raise TypeError('Unsupported type as file input: %s' % kind)
Functions
def get_bytes(
obj)
Infers the input type and reads the first 256 bytes, returning a sliced bytearray.
:param obj obj :rtype bytearray
def get_bytes(obj):
"""
Infers the input type and reads the first 256 bytes,
returning a sliced bytearray.
:param obj obj
:rtype bytearray
"""
kind = type(obj)
if kind == bytearray:
return signature(obj)
if kind == str:
return get_signature_bytes(obj)
if kind is bytes:
return signature(bytearray(obj))
raise TypeError('Unsupported type as file input: %s' % kind)
def get_signature_bytes(
path)
Reads file from disk and returns the first 256 bytes of data representing the magic number header signature.
:param path str :rtype bytearray
def get_signature_bytes(path):
"""
Reads file from disk and returns the first 256 bytes
of data representing the magic number header signature.
:param path str
:rtype bytearray
"""
with open(path, 'rb') as f:
return bytearray(f.read(256))
def signature(
array)
Returns the first 256 bytes of the given bytearray as part of the file header signature.
:param array bytearray :rtype bytearray
def signature(array):
"""
Returns the first 256 bytes of the given bytearray
as part of the file header signature.
:param array bytearray
:rtype bytearray
"""
length = len(array)
index = 256 if length > 256 else length
return array[:index]