Metadata-Version: 2.1
Name: import-x
Version: 0.1.0
Summary: Import non-python files
Home-page: https://github.com/deepsourcelabs/import-x
License: MIT
Keywords: import machinary,import hook,import-x
Author: Mohit Solanki
Author-email: mohit@deepsource.io
Requires-Python: >=3.6,<4.0
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Project-URL: Documentation, https://github.com/deepsourcelabs/import-x
Project-URL: Repository, https://github.com/deepsourcelabs/import-x
Description-Content-Type: text/x-rst

import-x
########

An ext-tensible loader to import anything like it is a python module.

Supports Python **3.6+**.

Usage
======

Example json file in your path ``foo.json``:

.. code:: json

    {
        "why": "not",
    }

.. code-block:: python

   # Extend the ExtensionLoader and implement 'handle_module' method
   # where you will get a module object and the path to that module.

   >>> from import_x import ExtensionLoader

   >>> class JsonLoader(ExtensionLoader):
        extension = '.json'

        auto_enable = False

        @staticmethod
        def handle_module(module, path):
            """
            Load the json file and set as `data` attribute of the module.
            """
            json_file = Path(path)
            content = json_file.read_text()
            try:
                data = json.loads(content)
            except (json.JSONDecodeError, ValueError):
                data = {}
            module.data = data

    >>> json_imports = JsonLoader()
    >>> with json_imports:
            import foo
    >>> foo.data
    >>> {"why": "not"}

If you want to enable imports automatically without the context_manager then just
do ``auto_enable = True`` in your loader.

