Skip to content

Commit f18270c

Browse files
committed
Add README
1 parent 31d6ea6 commit f18270c

File tree

4 files changed

+83
-16
lines changed

4 files changed

+83
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ __pypackages__
77
_build
88
.vscode
99
.pdm.toml
10+
.python-version

README.md

Lines changed: 0 additions & 13 deletions
This file was deleted.

README.rst

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
======
2+
Defity
3+
======
4+
5+
6+
.. image:: https://madewithlove.vercel.app/vn?heart=true&colorA=%23ffcd00&colorB=%23da251d
7+
.. image:: https://badgen.net/pypi/v/defity
8+
:target: https://pypi.org/project/defity
9+
10+
Speedy Python library to determine MIME type of file.
11+
12+
13+
.. image:: skunk.svg
14+
:alt: logo
15+
16+
17+
Defity (**De**\tect **fi**\le **ty**\pe) is a library for Python application to guess file type in a reliable way, not based on filename extension ( *\*.png*, *\*.pdf*), but on actual file content. It is like what |file|_ command and |libmagic|_ library do, but with different strategy.
18+
19+
20+
Usage
21+
-----
22+
23+
.. code-block:: python
24+
25+
>>> import defity
26+
>>> defity.from_file('path/to/landscape.png')
27+
'image/png'
28+
>>> with open('path/to/landscape.png', 'rb') as f:
29+
... defity.from_file(f)
30+
...
31+
'image/png'
32+
33+
>>> defity.from_bytes(b'some-binary-content')
34+
'image/png'
35+
36+
37+
How different with libmagic-based ones?
38+
---------------------------------------
39+
40+
There are many Python libraries also do the same thing, most of them are based on wellknown *libmagic*. Defity is based on Rust |tree_magic_mini|_ library, which in turn is a fork of |tree_magic|_ , another Rust library. Quote from ``tree_magic`` to see how it differs from ``libmagic``:
41+
42+
Unlike the typical approach that libmagic and file(1) uses, this loads all the file types in a tree based on subclasses. (EX: application/vnd.openxmlformats-officedocument.wordprocessingml.document (MS Office 2007) subclasses application/zip which subclasses application/octet-stream) Then, instead of checking the file against every file type, it can traverse down the tree and only check the file types that make sense to check. (After all, the fastest check is the check that never gets run.)
43+
44+
This library also provides the ability to check if a file is a certain type without going through the process of checking it against every file type.
45+
46+
47+
And what ``tree_magic_mini`` has improved over ``tree_magic``:
48+
49+
Reduced copying and memory allocation, for a slight increase in speed and decrease in memory use.
50+
51+
52+
So, ``Defity`` should have better performance than other libraries for the same purpose.
53+
54+
Another advantage is that, ``Defity`` is static linked to the underlying Rust library, not depend on discrete *libmagic.so*. It will be easier to deploy to cloud function platforms, where you don't have control over what system libraries is present there.
55+
56+
``Defity`` is, unfortunately, only available for Linux, because the underlying ``tree_magic_mini`` only supports FreeDesktop's `MIME database <mime_db_>`_.
57+
58+
59+
Credit
60+
------
61+
62+
63+
* Author: `Nguyễn Hồng Quân <author_>`_.
64+
* Free icon is made by `Vitaly Gorbachev <vitaly_>`_ from `flaticon.com`_.
65+
66+
67+
.. |file| replace:: ``file``
68+
.. _file: https://helpmanual.io/man1/file
69+
.. |libmagic| replace:: ``libmagic``
70+
.. _libmagic: https://helpmanual.io/man3/libmagic
71+
.. |tree_magic_mini| replace:: ``tree_magic_mini``
72+
.. _tree_magic_mini: https://crates.io/crates/tree_magic_mini
73+
.. |tree_magic| replace:: ``tree_magic``
74+
.. _tree_magic: https://crates.io/crates/tree_magic
75+
.. _mime_db: https://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec/
76+
.. _author: https://quan.hoabinh.vn
77+
.. _vitaly: https://www.flaticon.com/authors/vitaly-gorbachev
78+
.. _flaticon.com: https://www.flaticon.com/free-icon/skunk_2301541

pyproject.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ authors = [
77
maintainers = [
88
{name = "Nguyễn Hồng Quân", email = "[email protected]"},
99
]
10-
readme = "README.md"
10+
readme = "README.rst"
1111
requires-python = ">=3.6"
1212
license = {file = "LICENSE"}
1313
classifiers = [
1414
"Development Status :: 4 - Beta",
15-
"Programming Language :: Python"
15+
"Programming Language :: Python",
16+
"Operating System :: POSIX :: Linux"
1617
]
1718

1819
[project.urls]
@@ -33,7 +34,7 @@ sdist-include = ["Cargo.lock"]
3334
strip = true
3435

3536
# We don't use Poetry to build and publish packages (do with Maturin instead).
36-
# But we use it to setup virtualenv in GH Action,
37+
# But we use Poetry to setup virtualenv in GH Action,
3738
# and it requires at least these information.
3839
[tool.poetry]
3940
name = "defity"

0 commit comments

Comments
 (0)