toadstool


Nametoadstool JSON
Version 0.1.0 PyPI version JSON
download
home_pageNone
SummaryPython Load Tools Suite
upload_time2022-12-20 03:33:19
maintainerNone
docs_urlNone
authorNone
requires_python~=3.10
licenseNone
keywords import loader meta sys
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Toadstool

Python load tools -- An opinionated solution for cleanly loading files directly into Python objects.

This library works by creating custom Python finders and loaders and injecting them into the Python meta search path.

The Python import system is explained in great detail [here](https://docs.python.org/3/reference/import.html#the-import-system).

Consider a `json` file `sample.json`

```json
{
    "employee": {
        "name":       "sonoo",
        "salary":      56000,
        "married":    true
    },
    "menu": {
        "id": "file",
        "value": "File",
        "popup": {
          "menuitem": [
            {"value": "New", "onclick": "CreateDoc()"},
            {"value": "Open", "onclick": "OpenDoc()"},
            {"value": "Save", "onclick": "SaveDoc()"}
          ]
        }
      }
}
```

can be loaded and used in the following way:

```python
import toadstool
import sample

# Or import using ToadContext
from toadstool.utils.utils import ToadContext
from toadstool.loaders.json_loader import JsonLoader
with ToadContext(JsonLoader):
  import sample

>>> sample.
sample.employee  sample.json      sample.menu
print(sample.menu)
> {'id': 'file', 'value': 'File', 'popup': {'menuitem': [{'value': 'New', 'onclick': 'CreateDoc()'}, {'value': 'Open', 'onclick': 'OpenDoc()'}, {'value': 'Save', 'onclick': 'SaveDoc()'}]}}
```

## Requirements

Requires Python 3.10

## Install

### Basic Install

`$ python3 -m pip install toadstool`

### Advanced Install

`$ python3 -m pip install toadstool[options]`
Where `options` are any of the following (or combinations thereof):

* `toml` For `.toml` support using `toml` library for Python versions prior to `Python 3.11`, which introduced `tomllib` into the standard library
* `yaml` For `.yaml` support using `PyYaml`
* `gql` For `.gql` or `.graphql` query support using `graphql-core` library

## Usage

To use this module, simply `import toadstool` before any import you wish to use the included loaders for. This can be at the top of your source file or just before any imports for the supported filetypes.

## Loaders

<details>
  <summary>GraphQL (.gql | .graphql)</summary>

Load graphql queries directly as graphql.language.ast.DocumentNode objects from the [GraphQL Core Library](https://github.com/graphql-python/graphql-core/tree/main/src/graphql).  Allows direct importing of queries/mutations/subscriptions/fragments (aka GraphQL operations).

Example:

Given a GraphQL query file names `queries.graphl` or `queries.gql` with the following contents:

```graphql
query HeroComparison($first: Int = 3) {
  leftComparison: hero(episode: EMPIRE) {
    ...comparisonFields
  }
  rightComparison: hero(episode: JEDI) {
    ...comparisonFields
  }
}

fragment comparisonFields on Character {
  name
  friendsConnection(first: $first) {
    totalCount
    edges {
      node {
        name
      }
    }
  }
}

mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
  createReview(episode: $ep, review: $review) {
    stars
    commentary
  }
}
```

then you can import the contents of the file either as a whole module:

```python
import queries
print(queries.__dict__)

>
{'__name__': 'queries', '__doc__': None, '__package__': '', '__loader__': GqlImporter('queries.gql'), '__spec__': ModuleSpec(name='queries', loader=GqlImporter('queries.gql')), 'HeroComparison': DocumentNode, 'operations': {'HeroComparison': DocumentNode}, '__file__': 'queries.gql'}
```

or using specific query names:

```python
from queries import HeroComparison
print(HeroComparison.definitions)

> (OperationDefinitionNode at 0:180, FragmentDefinitionNode at 182:339)
```

Also tracks all operations in a module dict as `queries.operations`

</details>

<details>
  <summary>JSON (.json)</summary>

Loads JSON objects using builtin `json` library. The top-level JSON keys are stored as attirbutes for the module and the whole `json` converted `dict` is stored as `imported_name.json` For example, the following file `sample.json`

```json
{
    "employee": {
        "name":       "sonoo",
        "salary":      56000,
        "married":    true
    },
    "menu": {
        "id": "file",
        "value": "File",
        "popup": {
          "menuitem": [
            {"value": "New", "onclick": "CreateDoc()"},
            {"value": "Open", "onclick": "OpenDoc()"},
            {"value": "Save", "onclick": "SaveDoc()"}
          ]
        }
      }
}
```

can be loaded and used in the following way:

```python
import toadstool
import sample

>>> sample.
sample.employee  sample.json      sample.menu
print(sample.menu)
> {'id': 'file', 'value': 'File', 'popup': {'menuitem': [{'value': 'New', 'onclick': 'CreateDoc()'}, {'value': 'Open', 'onclick': 'OpenDoc()'}, {'value': 'Save', 'onclick': 'SaveDoc()'}]}}
```

You also have the json root object available at `sample.json`:

```python
import toadstool
import sample

print(sample.json)
>{'employee': {'name': 'sonoo', 'salary': 56000, 'married': True}, 'menu': {'id': 'file', 'value': 'File', 'popup': {'menuitem': [{'value': 'New', 'onclick': 'CreateDoc()'}, {'value': 'Open', 'onclick': 'OpenDoc()'}, {'value': 'Save', 'onclick': 'SaveDoc()'}]}}}
```

</details>

<details>
  <summary>TOML (.toml)</summary>

  Loads TOML files such that each top-level table becomes an attribute of the imported module. Also loads the whole TOML file as a dictionary under the `toml` attirbute (which will overwrite any table from the file with the name `toml` as well). For example, if you have `example.toml` with the following contents:

  ```toml
  [project]
  name = 'Toadstool'
  description = 'Python Load Tools Suite'
  readme = 'README.md'
  requires-python = "~=3.10"
  license = { file = 'LICENSE' }
  version = '0.1.0'
  authors = [{ name = 'Andrés Alejos', email = 'acalejos@proton.me' }]
  classifiers = [
      'License :: OSI Approved :: MIT Licens',
      'Programming Language :: Python :: 3',
      'Topic :: Software Development',
      'Topic :: Utilities',
  ]
  keywords = ["import", "loader", "meta", "sys"]
  urls = { Home = "https://github.com/acalejos/toadstool" }

  [project.optional-dependencies]
  gql = ['graphql_core>=3.2.3']
  yaml = ['pyyaml >= 5.3.1']
  toml = ['toml >= 0.10.2;python_version < "3.11"']
  all = ['toadstool[gql]', 'toadstool[yaml]', 'toadstool[toml]']

  [sample]
  name = 'Sample'

  [[Root]]
  name = 'Root'
  ```

  can be loaded and used in the following way:

```python
import toadstool
import example

>>> example.
example.Root     example.project  example.sample   example.toml
```

</details>

<details>
  <summary>YAML (.yaml | .yml)</summary>

  Loads a YAML file, assigning each top-level key as a module attribute (similar to the JSON loader). Also loads the whole YAML definition as a `dict` into the `yaml` attirbute. If the YAML file has more than 1 YAML definition (which is legal within the YAML definition), then only the `yaml` attribute is set, and is a `list[dict]`. Suppose you have a YAML file called `sample.yaml` with the following contents:

```yaml
document: 1
name: 'erik'
```

then you could do the following:

```python
import toadstool
import sample

>>> sample.
sample.document sample.name sample.yaml
```

and suppose you had a similar multifile YAML file:

```yaml
document: 1
name: 'erik'
---
document: 2
name: 'config'
```

then you could do the following:

```python
import toadstool
import sample

>>> sample.
sample.yaml
```

</details>

<details>
  <summary>Config (.ini | .cfg | .config)</summary>

  Uses the `configparser` package from the standard library to load the file and assign any top-level key to the module's attribute, and the entire `ConfigParser` object to the `config` attribute. Suppose you have the following config file `sample.ini`:

  ```ini
  [http]
  port=8080
  username=httpuser
  [https]
  port=8043
  username=httpsuser
  [FTP]
  port=8043
  username=ftpuser
  [database]
  driverclass   = com.mysql.jdbc.Driver
  dbName        = mydatabase
  port          = 3306
  username      = root
  ```

  then you could do the following

  ```python
  import toadstool
  import sample

  >>> sample.
  sample.http  sample.https sample.FTP sample.database sample.config
  ```
</details>

<details>
  <summary>CSV (.csv)</summary>

Loads a CSV file and loads the contents into `rows` and `columns` attributes as `list[list[str]]`.  This will attempt to determine if the CSV contains a header as the first row, and will do the following according to that determination:

If Header:

* Creates attributes `named_rows` and `named_columns`
  * `named_columns`: Each column is a `dict` keyed on the column header and contains the whole column
  * `named_rows`: Each row contains a `dict` with each column entry keyed from its header to the value at that row.
* Populates each column name as an attribute name that will correspond to its respective column
* Populates an attribute `fieldnames` with the header values.

For any CSV:

* Creates a `rows` attirbute which contains a `list[list[str]]` with the CSV content rows (skips header if exists)
* Creates a `columns` attirbute which contains a `list[list[str]]` with the CSV content columns

Suppose you have a file called `sample.csv` with the following contents:

```csv
Index,Customer Id,First Name
1,DD37Cf93aecA6Dc,Sheryl
2,1Ef7b82A4CAAD10,Preston
3,6F94879bDAfE5a6,Roy
4,5Cef8BFA16c5e3c,Linda
5,053d585Ab6b3159,Joanna
```

then you can do the following:

```python
import toadstool
import sample

>>> sample.
sample.First_Name         sample.named_rows         sample.rows
sample.Index              sample.columns            sample.named_columns
sample.Customer_Id        sample.fieldnames
```

</details>

## ToadContext

Located in `toadstool.utils.utils`, ToadContext is a Context Manager to allow imports with `toadstool` loaders without permanently changing the `sys.meta_path`.  Using this, you can import your files the same way as you would using `import toadstool` from within a context without having to actually import `toadstool`. You must explicitly pass loaders you wish to use as arguments.

This context manager does not yield anything, so the proper usage is:

```python
with ToadContext(Loader | list(Loaders)):
    import my_module
```

## Limitations

Toadstool works by injecting the supported `Loader`s into the `sys.meta_path` during Toadstool's module init. The `sys.meta_path` is a list of class instances that contain the `find_spec` and `exec_module` methods. This occurs the first time that Toadstool is imported (upon `init` of Toadstool), after which `toadstool` is cached in the `sys.modules` data structure, which is what is checked when any module is referenced before deferring to the loaders in `sys.meta_path` and searching for the module. Therefore, after the first `import toadstool` that occurs per Python interpreter session, the module `__init__.py` might not be run again and thus if you do anything to alter the `sys.meta_path` after `import toadstool` then the `Loader`s may no longer be in the search path.

One possible solution is to use `importlib.reload(toadstool)` which reruns the module `__init__.py` code (only code not contained in `if __name__ == '__main__'` block).

Another possible solution is to use the `ToadContext` when you wish to import using the Toadstool `Loader`s, This could prove to be redundant, but it also ensures that the `sys.meta_path` is only altered while importing the modules that require `toadstool` and removes the `Loader`s upon existing the context.

## Contributing

Feel free to open feature requests for new features or simply supported file extensions. If you feel so inclined, you can also open up a pull request to merge in your own loader. Creating a new loader is very straight forwards and can be accomplished by:

* Creating a file in `toadstool/loaders` with the name `{filetype}_loader.py`
* Implementing a class that inherits from the `Loader` class located in `toadstool/loaders/base_loader`. Your class must implement the following:

  * Add a class attribute `file_exts` specifying your file extension(s) either as a `str` for a single extension or `list[str]` for multiple extensions. For example:

  ```python
  class CsvLoader(Loader):
    file_exts="csv"
  ```

  ```python
  class GqlLoader(Loader):
    file_exts=["gql","graphql"]
  ```

  * Define `def exec_module(self, module):` where your functionality will be implemented. This will typically look like updating the `module.__dict__` with values according to how you load the file
* If your loader has dependencies:
  * Specify a new optional dependency in `pyrpoject.toml` under `[project.optional-dependencies]`
  * Add your dependency to the `all` optional dependency by adding it as `toadstool[your_dep]`
* Add your loader to the list `all_loaders` in the root `__init__.py` file as a 3-tuple with:
  * Path to your module in dotted notation
  * Name of your loader class
  * Name of dependency from `pyrpoject.toml` if it exists or `None`

Refer to any of the existing loaders in `toadstool/loaders` for examples

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "toadstool",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "~=3.10",
    "maintainer_email": null,
    "keywords": "import,loader,meta,sys",
    "author": null,
    "author_email": "Andr\u00e9s Alejos <acalejos@proton.me>",
    "download_url": "https://files.pythonhosted.org/packages/5b/3b/f0cca0a08446024ce9a60d4769dbf8fa95c7b9171dc6105ce040d7d4820f/toadstool-0.1.0.tar.gz",
    "platform": null,
    "description": "# Toadstool\n\nPython load tools -- An opinionated solution for cleanly loading files directly into Python objects.\n\nThis library works by creating custom Python finders and loaders and injecting them into the Python meta search path.\n\nThe Python import system is explained in great detail [here](https://docs.python.org/3/reference/import.html#the-import-system).\n\nConsider a `json` file `sample.json`\n\n```json\n{\n    \"employee\": {\n        \"name\":       \"sonoo\",\n        \"salary\":      56000,\n        \"married\":    true\n    },\n    \"menu\": {\n        \"id\": \"file\",\n        \"value\": \"File\",\n        \"popup\": {\n          \"menuitem\": [\n            {\"value\": \"New\", \"onclick\": \"CreateDoc()\"},\n            {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},\n            {\"value\": \"Save\", \"onclick\": \"SaveDoc()\"}\n          ]\n        }\n      }\n}\n```\n\ncan be loaded and used in the following way:\n\n```python\nimport toadstool\nimport sample\n\n# Or import using ToadContext\nfrom toadstool.utils.utils import ToadContext\nfrom toadstool.loaders.json_loader import JsonLoader\nwith ToadContext(JsonLoader):\n  import sample\n\n>>> sample.\nsample.employee  sample.json      sample.menu\nprint(sample.menu)\n> {'id': 'file', 'value': 'File', 'popup': {'menuitem': [{'value': 'New', 'onclick': 'CreateDoc()'}, {'value': 'Open', 'onclick': 'OpenDoc()'}, {'value': 'Save', 'onclick': 'SaveDoc()'}]}}\n```\n\n## Requirements\n\nRequires Python 3.10\n\n## Install\n\n### Basic Install\n\n`$ python3 -m pip install toadstool`\n\n### Advanced Install\n\n`$ python3 -m pip install toadstool[options]`\nWhere `options` are any of the following (or combinations thereof):\n\n* `toml` For `.toml` support using `toml` library for Python versions prior to `Python 3.11`, which introduced `tomllib` into the standard library\n* `yaml` For `.yaml` support using `PyYaml`\n* `gql` For `.gql` or `.graphql` query support using `graphql-core` library\n\n## Usage\n\nTo use this module, simply `import toadstool` before any import you wish to use the included loaders for. This can be at the top of your source file or just before any imports for the supported filetypes.\n\n## Loaders\n\n<details>\n  <summary>GraphQL (.gql | .graphql)</summary>\n\nLoad graphql queries directly as graphql.language.ast.DocumentNode objects from the [GraphQL Core Library](https://github.com/graphql-python/graphql-core/tree/main/src/graphql).  Allows direct importing of queries/mutations/subscriptions/fragments (aka GraphQL operations).\n\nExample:\n\nGiven a GraphQL query file names `queries.graphl` or `queries.gql` with the following contents:\n\n```graphql\nquery HeroComparison($first: Int = 3) {\n  leftComparison: hero(episode: EMPIRE) {\n    ...comparisonFields\n  }\n  rightComparison: hero(episode: JEDI) {\n    ...comparisonFields\n  }\n}\n\nfragment comparisonFields on Character {\n  name\n  friendsConnection(first: $first) {\n    totalCount\n    edges {\n      node {\n        name\n      }\n    }\n  }\n}\n\nmutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {\n  createReview(episode: $ep, review: $review) {\n    stars\n    commentary\n  }\n}\n```\n\nthen you can import the contents of the file either as a whole module:\n\n```python\nimport queries\nprint(queries.__dict__)\n\n>\n{'__name__': 'queries', '__doc__': None, '__package__': '', '__loader__': GqlImporter('queries.gql'), '__spec__': ModuleSpec(name='queries', loader=GqlImporter('queries.gql')), 'HeroComparison': DocumentNode, 'operations': {'HeroComparison': DocumentNode}, '__file__': 'queries.gql'}\n```\n\nor using specific query names:\n\n```python\nfrom queries import HeroComparison\nprint(HeroComparison.definitions)\n\n> (OperationDefinitionNode at 0:180, FragmentDefinitionNode at 182:339)\n```\n\nAlso tracks all operations in a module dict as `queries.operations`\n\n</details>\n\n<details>\n  <summary>JSON (.json)</summary>\n\nLoads JSON objects using builtin `json` library. The top-level JSON keys are stored as attirbutes for the module and the whole `json` converted `dict` is stored as `imported_name.json` For example, the following file `sample.json`\n\n```json\n{\n    \"employee\": {\n        \"name\":       \"sonoo\",\n        \"salary\":      56000,\n        \"married\":    true\n    },\n    \"menu\": {\n        \"id\": \"file\",\n        \"value\": \"File\",\n        \"popup\": {\n          \"menuitem\": [\n            {\"value\": \"New\", \"onclick\": \"CreateDoc()\"},\n            {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},\n            {\"value\": \"Save\", \"onclick\": \"SaveDoc()\"}\n          ]\n        }\n      }\n}\n```\n\ncan be loaded and used in the following way:\n\n```python\nimport toadstool\nimport sample\n\n>>> sample.\nsample.employee  sample.json      sample.menu\nprint(sample.menu)\n> {'id': 'file', 'value': 'File', 'popup': {'menuitem': [{'value': 'New', 'onclick': 'CreateDoc()'}, {'value': 'Open', 'onclick': 'OpenDoc()'}, {'value': 'Save', 'onclick': 'SaveDoc()'}]}}\n```\n\nYou also have the json root object available at `sample.json`:\n\n```python\nimport toadstool\nimport sample\n\nprint(sample.json)\n>{'employee': {'name': 'sonoo', 'salary': 56000, 'married': True}, 'menu': {'id': 'file', 'value': 'File', 'popup': {'menuitem': [{'value': 'New', 'onclick': 'CreateDoc()'}, {'value': 'Open', 'onclick': 'OpenDoc()'}, {'value': 'Save', 'onclick': 'SaveDoc()'}]}}}\n```\n\n</details>\n\n<details>\n  <summary>TOML (.toml)</summary>\n\n  Loads TOML files such that each top-level table becomes an attribute of the imported module. Also loads the whole TOML file as a dictionary under the `toml` attirbute (which will overwrite any table from the file with the name `toml` as well). For example, if you have `example.toml` with the following contents:\n\n  ```toml\n  [project]\n  name = 'Toadstool'\n  description = 'Python Load Tools Suite'\n  readme = 'README.md'\n  requires-python = \"~=3.10\"\n  license = { file = 'LICENSE' }\n  version = '0.1.0'\n  authors = [{ name = 'Andr\u00e9s Alejos', email = 'acalejos@proton.me' }]\n  classifiers = [\n      'License :: OSI Approved :: MIT Licens',\n      'Programming Language :: Python :: 3',\n      'Topic :: Software Development',\n      'Topic :: Utilities',\n  ]\n  keywords = [\"import\", \"loader\", \"meta\", \"sys\"]\n  urls = { Home = \"https://github.com/acalejos/toadstool\" }\n\n  [project.optional-dependencies]\n  gql = ['graphql_core>=3.2.3']\n  yaml = ['pyyaml >= 5.3.1']\n  toml = ['toml >= 0.10.2;python_version < \"3.11\"']\n  all = ['toadstool[gql]', 'toadstool[yaml]', 'toadstool[toml]']\n\n  [sample]\n  name = 'Sample'\n\n  [[Root]]\n  name = 'Root'\n  ```\n\n  can be loaded and used in the following way:\n\n```python\nimport toadstool\nimport example\n\n>>> example.\nexample.Root     example.project  example.sample   example.toml\n```\n\n</details>\n\n<details>\n  <summary>YAML (.yaml | .yml)</summary>\n\n  Loads a YAML file, assigning each top-level key as a module attribute (similar to the JSON loader). Also loads the whole YAML definition as a `dict` into the `yaml` attirbute. If the YAML file has more than 1 YAML definition (which is legal within the YAML definition), then only the `yaml` attribute is set, and is a `list[dict]`. Suppose you have a YAML file called `sample.yaml` with the following contents:\n\n```yaml\ndocument: 1\nname: 'erik'\n```\n\nthen you could do the following:\n\n```python\nimport toadstool\nimport sample\n\n>>> sample.\nsample.document sample.name sample.yaml\n```\n\nand suppose you had a similar multifile YAML file:\n\n```yaml\ndocument: 1\nname: 'erik'\n---\ndocument: 2\nname: 'config'\n```\n\nthen you could do the following:\n\n```python\nimport toadstool\nimport sample\n\n>>> sample.\nsample.yaml\n```\n\n</details>\n\n<details>\n  <summary>Config (.ini | .cfg | .config)</summary>\n\n  Uses the `configparser` package from the standard library to load the file and assign any top-level key to the module's attribute, and the entire `ConfigParser` object to the `config` attribute. Suppose you have the following config file `sample.ini`:\n\n  ```ini\n  [http]\n  port=8080\n  username=httpuser\n  [https]\n  port=8043\n  username=httpsuser\n  [FTP]\n  port=8043\n  username=ftpuser\n  [database]\n  driverclass   = com.mysql.jdbc.Driver\n  dbName        = mydatabase\n  port          = 3306\n  username      = root\n  ```\n\n  then you could do the following\n\n  ```python\n  import toadstool\n  import sample\n\n  >>> sample.\n  sample.http  sample.https sample.FTP sample.database sample.config\n  ```\n</details>\n\n<details>\n  <summary>CSV (.csv)</summary>\n\nLoads a CSV file and loads the contents into `rows` and `columns` attributes as `list[list[str]]`.  This will attempt to determine if the CSV contains a header as the first row, and will do the following according to that determination:\n\nIf Header:\n\n* Creates attributes `named_rows` and `named_columns`\n  * `named_columns`: Each column is a `dict` keyed on the column header and contains the whole column\n  * `named_rows`: Each row contains a `dict` with each column entry keyed from its header to the value at that row.\n* Populates each column name as an attribute name that will correspond to its respective column\n* Populates an attribute `fieldnames` with the header values.\n\nFor any CSV:\n\n* Creates a `rows` attirbute which contains a `list[list[str]]` with the CSV content rows (skips header if exists)\n* Creates a `columns` attirbute which contains a `list[list[str]]` with the CSV content columns\n\nSuppose you have a file called `sample.csv` with the following contents:\n\n```csv\nIndex,Customer Id,First Name\n1,DD37Cf93aecA6Dc,Sheryl\n2,1Ef7b82A4CAAD10,Preston\n3,6F94879bDAfE5a6,Roy\n4,5Cef8BFA16c5e3c,Linda\n5,053d585Ab6b3159,Joanna\n```\n\nthen you can do the following:\n\n```python\nimport toadstool\nimport sample\n\n>>> sample.\nsample.First_Name         sample.named_rows         sample.rows\nsample.Index              sample.columns            sample.named_columns\nsample.Customer_Id        sample.fieldnames\n```\n\n</details>\n\n## ToadContext\n\nLocated in `toadstool.utils.utils`, ToadContext is a Context Manager to allow imports with `toadstool` loaders without permanently changing the `sys.meta_path`.  Using this, you can import your files the same way as you would using `import toadstool` from within a context without having to actually import `toadstool`. You must explicitly pass loaders you wish to use as arguments.\n\nThis context manager does not yield anything, so the proper usage is:\n\n```python\nwith ToadContext(Loader | list(Loaders)):\n    import my_module\n```\n\n## Limitations\n\nToadstool works by injecting the supported `Loader`s into the `sys.meta_path` during Toadstool's module init. The `sys.meta_path` is a list of class instances that contain the `find_spec` and `exec_module` methods. This occurs the first time that Toadstool is imported (upon `init` of Toadstool), after which `toadstool` is cached in the `sys.modules` data structure, which is what is checked when any module is referenced before deferring to the loaders in `sys.meta_path` and searching for the module. Therefore, after the first `import toadstool` that occurs per Python interpreter session, the module `__init__.py` might not be run again and thus if you do anything to alter the `sys.meta_path` after `import toadstool` then the `Loader`s may no longer be in the search path.\n\nOne possible solution is to use `importlib.reload(toadstool)` which reruns the module `__init__.py` code (only code not contained in `if __name__ == '__main__'` block).\n\nAnother possible solution is to use the `ToadContext` when you wish to import using the Toadstool `Loader`s, This could prove to be redundant, but it also ensures that the `sys.meta_path` is only altered while importing the modules that require `toadstool` and removes the `Loader`s upon existing the context.\n\n## Contributing\n\nFeel free to open feature requests for new features or simply supported file extensions. If you feel so inclined, you can also open up a pull request to merge in your own loader. Creating a new loader is very straight forwards and can be accomplished by:\n\n* Creating a file in `toadstool/loaders` with the name `{filetype}_loader.py`\n* Implementing a class that inherits from the `Loader` class located in `toadstool/loaders/base_loader`. Your class must implement the following:\n\n  * Add a class attribute `file_exts` specifying your file extension(s) either as a `str` for a single extension or `list[str]` for multiple extensions. For example:\n\n  ```python\n  class CsvLoader(Loader):\n    file_exts=\"csv\"\n  ```\n\n  ```python\n  class GqlLoader(Loader):\n    file_exts=[\"gql\",\"graphql\"]\n  ```\n\n  * Define `def exec_module(self, module):` where your functionality will be implemented. This will typically look like updating the `module.__dict__` with values according to how you load the file\n* If your loader has dependencies:\n  * Specify a new optional dependency in `pyrpoject.toml` under `[project.optional-dependencies]`\n  * Add your dependency to the `all` optional dependency by adding it as `toadstool[your_dep]`\n* Add your loader to the list `all_loaders` in the root `__init__.py` file as a 3-tuple with:\n  * Path to your module in dotted notation\n  * Name of your loader class\n  * Name of dependency from `pyrpoject.toml` if it exists or `None`\n\nRefer to any of the existing loaders in `toadstool/loaders` for examples\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Python Load Tools Suite",
    "version": "0.1.0",
    "split_keywords": [
        "import",
        "loader",
        "meta",
        "sys"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "md5": "40479747d6c425cf8a463a92e24ba5b6",
                "sha256": "81cae6d802f5a2702de614e55888ea58f52000823d4befa220a376f74cdeda63"
            },
            "downloads": -1,
            "filename": "toadstool-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "40479747d6c425cf8a463a92e24ba5b6",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": "~=3.10",
            "size": 12542,
            "upload_time": "2022-12-20T03:33:11",
            "upload_time_iso_8601": "2022-12-20T03:33:11.796668Z",
            "url": "https://files.pythonhosted.org/packages/9a/3a/b1dbbdac4a35904ff79b127826c5145e2d68c9352d29e6d498971e44fcfe/toadstool-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "md5": "e90aa432bfda10f663f730bc72e4b209",
                "sha256": "266007a8fb3bd4e4cf1087791ff6c89b59754ba5d29fff4a8f272a92a124e515"
            },
            "downloads": -1,
            "filename": "toadstool-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "e90aa432bfda10f663f730bc72e4b209",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "~=3.10",
            "size": 13497,
            "upload_time": "2022-12-20T03:33:19",
            "upload_time_iso_8601": "2022-12-20T03:33:19.374986Z",
            "url": "https://files.pythonhosted.org/packages/5b/3b/f0cca0a08446024ce9a60d4769dbf8fa95c7b9171dc6105ce040d7d4820f/toadstool-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-20 03:33:19",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "toadstool"
}
        
Elapsed time: 0.06990s