cmagic


Namecmagic JSON
Version 1.0.3 PyPI version JSON
download
home_pagehttps://github.com/mosquito/cmagic/
SummaryPython wrapper for libmagic
upload_time2023-03-10 13:43:27
maintainer
docs_urlNone
authorDmitry Orlov <me@mosquito.su>
requires_python>=3.7, <4
licenseApache Software License
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            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
    :target: https://github.com/mosquito/cmagic/actions?query=workflow%3Atox
    :alt: Github Actions

.. image:: https://img.shields.io/pypi/v/cmagic.svg
    :target: https://pypi.python.org/pypi/cmagic/
    :alt: Latest Version

.. image:: https://img.shields.io/pypi/wheel/cmagic.svg
    :target: https://pypi.python.org/pypi/cmagic/

.. image:: https://img.shields.io/pypi/pyversions/cmagic.svg
    :target: https://pypi.python.org/pypi/cmagic/

.. image:: https://img.shields.io/pypi/l/cmagic.svg
    :target: https://pypi.python.org/pypi/cmagic/

Python wrapper for libmagic.

Usage
-----

.. code-block::

   # MacOS
   $ export MAGIC=/usr/local/Cellar/libmagic/5.39/share/misc/magic.mgc

   # Ubuntu
   $ export MAGIC=/usr/lib/file/magic.mgc

.. code-block:: python

   import cmagic

   # Database path from MAGIC environment variable
   m = cmagic.Magic()

   if m.check():
      print("Database is ok")

   m.load()

   m.guess_file("/etc/hosts")
   # '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
------------

Ubuntu/Debian
+++++++++++++

.. code-block:: bash

   apt-get install -y libmagic1 libmagic-mgc   # when using manilinux wheel
   apt-get install -y libmagic-dev             # for building from sources
   python3 -m pip install cmagic


Centos
++++++

.. code-block:: bash

   yum install -y file-libs            # when using manilinux wheel
   yum install -y file-devel           # for building from sources
   python3 -m pip install cmagic


MacOS
+++++

.. code-block:: bash

   brew install libmagic
   export CFLAGS="-I$(brew --prefix libmagic)/include" LDFLAGS="-L$(brew --prefix libmagic)/lib"
   python3 -m pip install cmagic



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/mosquito/cmagic/",
    "name": "cmagic",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7, <4",
    "maintainer_email": "",
    "keywords": "",
    "author": "Dmitry Orlov <me@mosquito.su>",
    "author_email": "me@mosquito.su",
    "download_url": "https://files.pythonhosted.org/packages/62/a5/7efff6bc4c6832adbfea118c059d3069bcf2e27741eef49635d70ba090d3/cmagic-1.0.3.tar.gz",
    "platform": null,
    "description": "cmagic\n======\n\n.. image:: https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg\n   :target: https://saythanks.io/to/me@mosquito.su\n\n.. image:: https://github.com/mosquito/cmagic/workflows/tox/badge.svg\n    :target: https://github.com/mosquito/cmagic/actions?query=workflow%3Atox\n    :alt: Github Actions\n\n.. image:: https://img.shields.io/pypi/v/cmagic.svg\n    :target: https://pypi.python.org/pypi/cmagic/\n    :alt: Latest Version\n\n.. image:: https://img.shields.io/pypi/wheel/cmagic.svg\n    :target: https://pypi.python.org/pypi/cmagic/\n\n.. image:: https://img.shields.io/pypi/pyversions/cmagic.svg\n    :target: https://pypi.python.org/pypi/cmagic/\n\n.. image:: https://img.shields.io/pypi/l/cmagic.svg\n    :target: https://pypi.python.org/pypi/cmagic/\n\nPython wrapper for libmagic.\n\nUsage\n-----\n\n.. code-block::\n\n   # MacOS\n   $ export MAGIC=/usr/local/Cellar/libmagic/5.39/share/misc/magic.mgc\n\n   # Ubuntu\n   $ export MAGIC=/usr/lib/file/magic.mgc\n\n.. code-block:: python\n\n   import cmagic\n\n   # Database path from MAGIC environment variable\n   m = cmagic.Magic()\n\n   if m.check():\n      print(\"Database is ok\")\n\n   m.load()\n\n   m.guess_file(\"/etc/hosts\")\n   # 'ASCII text'\n   m.guess_bytes(\"hello world\")\n   # 'ASCII text'\n\n   # Setting flags\n   m.set_flags(mime_type=True)\n\n\n   m = cmagic.Magic(\n      # Setting flags here\n      mime_type=True\n   )\n\n   # Trying to find database on the standard paths\n   m.load(cmagic.find_db())\n\n   m.guess_file(\"/etc/hosts\")\n   # 'text/plain'\n\n   m.guess_bytes(\"hello world\")\n   # 'text/plain'\n\n\nasyncio example\n+++++++++++++++\n\n.. code-block:: python\n\n   import asyncio\n   import cmagic\n   from threading import local\n\n\n   magic_tls = local()\n\n\n   def get_instance():\n       global magic_tls\n\n       if not hasattr(magic_tls, \"instance\"):\n           m = cmagic.Magic(mime_type=True)\n           m.load(cmagic.find_db())\n           magic_tls.instance = m\n\n       return magic_tls.instance\n\n\n   async def guess_file(fname):\n       loop = asyncio.get_event_loop()\n\n       def run():\n           m = get_instance()\n           return m.guess_file(fname)\n\n       return await loop.run_in_executor(None, run)\n\n\n   async def guess_bytes(payload):\n       loop = asyncio.get_event_loop()\n\n       def run():\n           m = get_instance()\n           return m.guess_bytes(payload)\n\n       return await loop.run_in_executor(None, run)\n\n\n   if __name__ == \"__main__\":\n       print(asyncio.run(guess_file(\"/etc/hosts\")))\n       # text/plain\n       print(asyncio.run(guess_bytes(b\"\\0\\0\\0\\0\\0\\0\\0\")))\n       # application/octet-stream\n\n\nInstallation\n------------\n\nUbuntu/Debian\n+++++++++++++\n\n.. code-block:: bash\n\n   apt-get install -y libmagic1 libmagic-mgc   # when using manilinux wheel\n   apt-get install -y libmagic-dev             # for building from sources\n   python3 -m pip install cmagic\n\n\nCentos\n++++++\n\n.. code-block:: bash\n\n   yum install -y file-libs            # when using manilinux wheel\n   yum install -y file-devel           # for building from sources\n   python3 -m pip install cmagic\n\n\nMacOS\n+++++\n\n.. code-block:: bash\n\n   brew install libmagic\n   export CFLAGS=\"-I$(brew --prefix libmagic)/include\" LDFLAGS=\"-L$(brew --prefix libmagic)/lib\"\n   python3 -m pip install cmagic\n\n\n",
    "bugtrack_url": null,
    "license": "Apache Software License",
    "summary": "Python wrapper for libmagic",
    "version": "1.0.3",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "62a57efff6bc4c6832adbfea118c059d3069bcf2e27741eef49635d70ba090d3",
                "md5": "6034485f6085f1b1bc12a74958326780",
                "sha256": "acd50451ffcba1b484defb9d6041ac11c3c4c002e7ec82d3d179446186393f5b"
            },
            "downloads": -1,
            "filename": "cmagic-1.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "6034485f6085f1b1bc12a74958326780",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7, <4",
            "size": 12533,
            "upload_time": "2023-03-10T13:43:27",
            "upload_time_iso_8601": "2023-03-10T13:43:27.781500Z",
            "url": "https://files.pythonhosted.org/packages/62/a5/7efff6bc4c6832adbfea118c059d3069bcf2e27741eef49635d70ba090d3/cmagic-1.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-10 13:43:27",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "mosquito",
    "github_project": "cmagic",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "cmagic"
}
        
Elapsed time: 0.04333s