ua-nick / fleep-py

File format determination library for Python
https://pypi.python.org/pypi/fleep
MIT License
262 stars 40 forks source link
extension file-extensions file-format file-format-detection file-type-detection file-types fileformat filetype magic-number magic-numbers mimetype

fleep

File format determination library for Python

|pypi version| |python version| |license|

Getting Started

fleep is a library that determines file format by file signature (also known as "magic number").

Installation

You can install fleep using pip. Simply run in CLI:

::

pip install fleep

Requirements

In Use

fleep has main function get() that determines file format. It takes byte sequence (128 bytes are enough) as an argument and returns an instance of class Info with the following arguments:

You may presume that first element in list will be the most suitable.

Also an instance of class Info has the following methods:

There are 3 more functions:

Examples

You can find examples in examples folder. There is a simple example:

.. code:: python

import fleep

with open("png_image", "rb") as file:
    info = fleep.get(file.read(128))

print(info.type)  # prints ['raster-image']
print(info.extension)  # prints ['png']
print(info.mime)  # prints ['image/png']

print(info.type_matches("raster-image"))  # prints True
print(info.extension_matches("gif"))  # prints False
print(info.mime_matches("image/png"))  # prints True

Tests

You can find tests in tests folder. There are results of speed test:

Workstation

Results

Note: 0.0012345 -> time in seconds

+-----------+---------+-----------+-----------+ | Iteration | Minimum | Maximum | Average | +===========+=========+===========+===========+ | 1 | 0.0 | 0.0005312 | 0.0000851 | +-----------+---------+-----------+-----------+ | 2 | 0.0 | 0.0005360 | 0.0000858 | +-----------+---------+-----------+-----------+ | 3 | 0.0 | 0.0005236 | 0.0000837 | +-----------+---------+-----------+-----------+ | 4 | 0.0 | 0.0005035 | 0.0000833 | +-----------+---------+-----------+-----------+ | 5 | 0.0 | 0.0005295 | 0.0000835 | +-----------+---------+-----------+-----------+

Supported Formats

There is a list of supported formats (in alphabetical order):

Raster Image:

Raw Image:

Vector Image:

3D Image:

Audio:

Video:

Document:

Archive:

Executable:

Font:

System:

Database:

Changelog

You can find changelog in CHANGELOG.md file.

License

This project is licensed under the MIT License.

Authors

Mykyta Paliienko - GitHub profile_

.. _GitHub profile: https://github.com/floyernick

.. |pypi version| image:: https://img.shields.io/badge/pypi-v1.0.1-blue.svg :target: https://pypi.python.org/pypi/fleep .. |python version| image:: https://img.shields.io/badge/python-3-blue.svg .. |license| image:: https://img.shields.io/badge/license-MIT-blue.svg :target: https://github.com/floyernick/fleep/blob/master/LICENSE