cmagic
Advanced tools
| from os import getenv, path | ||
| from glob import glob | ||
| from itertools import chain | ||
| __cached_db_path = None | ||
| def find_db(): | ||
| global __cached_db_path | ||
| if __cached_db_path: | ||
| return __cached_db_path | ||
| env_path = getenv("MAGIC") | ||
| if env_path and path.exists(env_path): | ||
| __cached_db_path = env_path | ||
| return __cached_db_path | ||
| candidates = list( | ||
| chain( | ||
| glob("/usr/share/misc/magic.*"), | ||
| glob("/usr/lib/file/magic.*"), | ||
| glob("/usr/local/Cellar/libmagic/*/share/misc/magic.mgc"), | ||
| ) | ||
| ) | ||
| if not candidates: | ||
| raise RuntimeError("Magic database was not found") | ||
| __cached_db_path = candidates[0] | ||
| return __cached_db_path |
| Metadata-Version: 2.1 | ||
| Name: cmagic | ||
| Version: 0.3.2 | ||
| Version: 0.4.0 | ||
| Summary: Python wrapper for libmagic | ||
@@ -10,6 +10,11 @@ Home-page: UNKNOWN | ||
| Project-URL: Documentation, https://github.com/mosquito/cmagic/ | ||
| Project-URL: Source, https://github.com/mosquito/cmagic | ||
| Project-URL: Source, https://github.com/mosquito/cmagic/ | ||
| Project-URL: Tracker, https://github.com/mosquito/cmagic/issues | ||
| Project-URL: Say Thanks!, https://saythanks.io/to/me%40mosquito.su | ||
| Description: cmagic | ||
| ====== | ||
| .. image:: https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg | ||
| :target: https://saythanks.io/to/me@mosquito.su | ||
| .. image:: https://github.com/mosquito/cmagic/workflows/tox/badge.svg | ||
@@ -49,5 +54,6 @@ :target: https://github.com/mosquito/cmagic/actions?query=workflow%3Atox | ||
| # Database path from MAGIC environment variable | ||
| m = cmagic.Magic() | ||
| if m.check(cmagic.MAGIC_DB): | ||
| if m.check(): | ||
| print("Database is ok") | ||
@@ -59,5 +65,75 @@ | ||
| # 'ASCII text' | ||
| m.guess_bytes("hello world") | ||
| # 'ASCII text' | ||
| # Setting flags | ||
| m.set_flags(mime_type=True) | ||
| m = cmagic.Magic( | ||
| # Setting flags here | ||
| mime_type=True | ||
| ) | ||
| # Trying to find database on the standard paths | ||
| m.load(cmagic.find_db()) | ||
| m.guess_file("/etc/hosts") | ||
| # 'text/plain' | ||
| m.guess_bytes("hello world") | ||
| # 'text/plain' | ||
| asyncio example | ||
| +++++++++++++++ | ||
| .. code-block:: python | ||
| import asyncio | ||
| import cmagic | ||
| from threading import local | ||
| magic_tls = local() | ||
| def get_instance(): | ||
| global magic_tls | ||
| if not hasattr(magic_tls, "instance"): | ||
| m = cmagic.Magic(mime_type=True) | ||
| m.load(cmagic.find_db()) | ||
| magic_tls.instance = m | ||
| return magic_tls.instance | ||
| async def guess_file(fname): | ||
| loop = asyncio.get_event_loop() | ||
| def run(): | ||
| m = get_instance() | ||
| return m.guess_file(fname) | ||
| return await loop.run_in_executor(None, run) | ||
| async def guess_bytes(payload): | ||
| loop = asyncio.get_event_loop() | ||
| def run(): | ||
| m = get_instance() | ||
| return m.guess_bytes(payload) | ||
| return await loop.run_in_executor(None, run) | ||
| if __name__ == "__main__": | ||
| print(asyncio.run(guess_file("/etc/hosts"))) | ||
| # text/plain | ||
| print(asyncio.run(guess_bytes(b"\0\0\0\0\0\0\0"))) | ||
| # application/octet-stream | ||
| Installation | ||
@@ -64,0 +140,0 @@ ------------ |
@@ -8,2 +8,3 @@ MANIFEST.in | ||
| cmagic/py.typed | ||
| cmagic/utils.py | ||
| cmagic/version.py | ||
@@ -10,0 +11,0 @@ cmagic.egg-info/PKG-INFO |
@@ -1,2 +0,6 @@ | ||
| from .version import __author__, __version__ # NOQA | ||
| from ._magic import Magic # NOQA | ||
| from ._magic import Magic | ||
| from .utils import find_db | ||
| from .version import __author__, __version__ | ||
| __all__ = ("Magic", "__author__", "__version__", "find_db") |
+2
-20
@@ -147,5 +147,3 @@ #define PY_SSIZE_T_CLEAN | ||
| Py_BEGIN_ALLOW_THREADS; | ||
| result = magic_load(self->cookie, db_path); | ||
| Py_END_ALLOW_THREADS; | ||
@@ -169,11 +167,5 @@ if (result) { | ||
| int result; | ||
| if (db_path == NULL) db_path = getenv("MAGIC"); | ||
| Py_BEGIN_ALLOW_THREADS; | ||
| result = magic_check(self->cookie, db_path); | ||
| Py_END_ALLOW_THREADS; | ||
| if (result) Py_RETURN_FALSE; | ||
| if (magic_check(self->cookie, db_path)) Py_RETURN_FALSE; | ||
| Py_RETURN_TRUE; | ||
@@ -193,9 +185,3 @@ } | ||
| int result; | ||
| Py_BEGIN_ALLOW_THREADS; | ||
| result = magic_compile(self->cookie, db_path); | ||
| Py_END_ALLOW_THREADS; | ||
| if (result) Py_RETURN_FALSE; | ||
| if (magic_compile(self->cookie, db_path)) Py_RETURN_FALSE; | ||
| Py_RETURN_TRUE; | ||
@@ -214,5 +200,3 @@ } | ||
| Py_BEGIN_ALLOW_THREADS; | ||
| result = magic_file(self->cookie, filename); | ||
| Py_END_ALLOW_THREADS; | ||
@@ -238,5 +222,3 @@ if (result == NULL) { | ||
| char * result; | ||
| Py_BEGIN_ALLOW_THREADS; | ||
| result = magic_buffer(self->cookie, payload, length); | ||
| Py_END_ALLOW_THREADS; | ||
@@ -243,0 +225,0 @@ if (result == NULL) { |
@@ -8,3 +8,3 @@ author_info = (("Dmitry Orlov", "me@mosquito.su"),) | ||
| version_info = (0, 3, 2) | ||
| version_info = (0, 4, 0) | ||
@@ -11,0 +11,0 @@ __author__ = ", ".join("{} <{}>".format(*info) for info in author_info) |
+79
-3
| Metadata-Version: 2.1 | ||
| Name: cmagic | ||
| Version: 0.3.2 | ||
| Version: 0.4.0 | ||
| Summary: Python wrapper for libmagic | ||
@@ -10,6 +10,11 @@ Home-page: UNKNOWN | ||
| Project-URL: Documentation, https://github.com/mosquito/cmagic/ | ||
| Project-URL: Source, https://github.com/mosquito/cmagic | ||
| Project-URL: Source, https://github.com/mosquito/cmagic/ | ||
| Project-URL: Tracker, https://github.com/mosquito/cmagic/issues | ||
| Project-URL: Say Thanks!, https://saythanks.io/to/me%40mosquito.su | ||
| Description: cmagic | ||
| ====== | ||
| .. image:: https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg | ||
| :target: https://saythanks.io/to/me@mosquito.su | ||
| .. image:: https://github.com/mosquito/cmagic/workflows/tox/badge.svg | ||
@@ -49,5 +54,6 @@ :target: https://github.com/mosquito/cmagic/actions?query=workflow%3Atox | ||
| # Database path from MAGIC environment variable | ||
| m = cmagic.Magic() | ||
| if m.check(cmagic.MAGIC_DB): | ||
| if m.check(): | ||
| print("Database is ok") | ||
@@ -59,5 +65,75 @@ | ||
| # 'ASCII text' | ||
| m.guess_bytes("hello world") | ||
| # 'ASCII text' | ||
| # Setting flags | ||
| m.set_flags(mime_type=True) | ||
| m = cmagic.Magic( | ||
| # Setting flags here | ||
| mime_type=True | ||
| ) | ||
| # Trying to find database on the standard paths | ||
| m.load(cmagic.find_db()) | ||
| m.guess_file("/etc/hosts") | ||
| # 'text/plain' | ||
| m.guess_bytes("hello world") | ||
| # 'text/plain' | ||
| asyncio example | ||
| +++++++++++++++ | ||
| .. code-block:: python | ||
| import asyncio | ||
| import cmagic | ||
| from threading import local | ||
| magic_tls = local() | ||
| def get_instance(): | ||
| global magic_tls | ||
| if not hasattr(magic_tls, "instance"): | ||
| m = cmagic.Magic(mime_type=True) | ||
| m.load(cmagic.find_db()) | ||
| magic_tls.instance = m | ||
| return magic_tls.instance | ||
| async def guess_file(fname): | ||
| loop = asyncio.get_event_loop() | ||
| def run(): | ||
| m = get_instance() | ||
| return m.guess_file(fname) | ||
| return await loop.run_in_executor(None, run) | ||
| async def guess_bytes(payload): | ||
| loop = asyncio.get_event_loop() | ||
| def run(): | ||
| m = get_instance() | ||
| return m.guess_bytes(payload) | ||
| return await loop.run_in_executor(None, run) | ||
| if __name__ == "__main__": | ||
| print(asyncio.run(guess_file("/etc/hosts"))) | ||
| # text/plain | ||
| print(asyncio.run(guess_bytes(b"\0\0\0\0\0\0\0"))) | ||
| # application/octet-stream | ||
| Installation | ||
@@ -64,0 +140,0 @@ ------------ |
+75
-1
| cmagic | ||
| ====== | ||
| .. image:: https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg | ||
| :target: https://saythanks.io/to/me@mosquito.su | ||
| .. image:: https://github.com/mosquito/cmagic/workflows/tox/badge.svg | ||
@@ -38,5 +41,6 @@ :target: https://github.com/mosquito/cmagic/actions?query=workflow%3Atox | ||
| # Database path from MAGIC environment variable | ||
| m = cmagic.Magic() | ||
| if m.check(cmagic.MAGIC_DB): | ||
| if m.check(): | ||
| print("Database is ok") | ||
@@ -48,5 +52,75 @@ | ||
| # 'ASCII text' | ||
| m.guess_bytes("hello world") | ||
| # 'ASCII text' | ||
| # Setting flags | ||
| m.set_flags(mime_type=True) | ||
| m = cmagic.Magic( | ||
| # Setting flags here | ||
| mime_type=True | ||
| ) | ||
| # Trying to find database on the standard paths | ||
| m.load(cmagic.find_db()) | ||
| m.guess_file("/etc/hosts") | ||
| # 'text/plain' | ||
| m.guess_bytes("hello world") | ||
| # 'text/plain' | ||
| asyncio example | ||
| +++++++++++++++ | ||
| .. code-block:: python | ||
| import asyncio | ||
| import cmagic | ||
| from threading import local | ||
| magic_tls = local() | ||
| def get_instance(): | ||
| global magic_tls | ||
| if not hasattr(magic_tls, "instance"): | ||
| m = cmagic.Magic(mime_type=True) | ||
| m.load(cmagic.find_db()) | ||
| magic_tls.instance = m | ||
| return magic_tls.instance | ||
| async def guess_file(fname): | ||
| loop = asyncio.get_event_loop() | ||
| def run(): | ||
| m = get_instance() | ||
| return m.guess_file(fname) | ||
| return await loop.run_in_executor(None, run) | ||
| async def guess_bytes(payload): | ||
| loop = asyncio.get_event_loop() | ||
| def run(): | ||
| m = get_instance() | ||
| return m.guess_bytes(payload) | ||
| return await loop.run_in_executor(None, run) | ||
| if __name__ == "__main__": | ||
| print(asyncio.run(guess_file("/etc/hosts"))) | ||
| # text/plain | ||
| print(asyncio.run(guess_bytes(b"\0\0\0\0\0\0\0"))) | ||
| # application/octet-stream | ||
| Installation | ||
@@ -53,0 +127,0 @@ ------------ |
+3
-1
@@ -38,3 +38,5 @@ import os | ||
| "Documentation": "https://github.com/mosquito/cmagic/", | ||
| "Source": "https://github.com/mosquito/cmagic", | ||
| "Source": "https://github.com/mosquito/cmagic/", | ||
| "Tracker": "https://github.com/mosquito/cmagic/issues", | ||
| "Say Thanks!": "https://saythanks.io/to/me%40mosquito.su", | ||
| }, | ||
@@ -41,0 +43,0 @@ packages=[module_name], |
Alert delta unavailable
Currently unable to show alert delta for PyPI packages.
39027
20.35%16
6.67%250
12.11%