Skip to main content

Allow loading non Python module formats as modules

Project description

Allow loading non Python module formats as modules.

Install

Use pip for installing:

$ pip install abm

Usage

Once installed, you can activate abm by importing abm.activate:

from abm import activate

Now you can register new loaders by doing:

from abm.loaders import IniLoader
IniLoader.register()

Since now, you can load *.ini files as if they were modules:

# config.ini
[section]
option = value
import config
assert(config['example'] is not None)
assert(config['example']['option'] is 'value')

Writing a loader

Extend the base loader AbmLoader provided in abm.loaders and implement create_module and execute_module methods. Provide the extension class member to allow automatic registration:

from configparser import ConfigParser
from types import ModuleType
from abm.loaders import AbmLoader


class IniLoader(AbmLoader):

    extensions = ('.ini', )

    def __init__(self, name, path):
        self.file_path = path

    def create_module(self, spec):
        module = ConfigModule(spec.name)
        self.init_module_attrs(spec, module)
        return module

    def exec_module(self, module):
        module.read(self.file_path)
        return module


class ConfigModule(ModuleType, ConfigParser):

    def __init__(self, specname):
        ModuleType.__init__(self, specname)
        ConfigParser.__init__(self)

Loaders are initialized passing the name of the module in the form:

'path.to.the.module'

And its absolute path.

Implementing create_module

create_module function should produce a module of the correct type. Nothing more. This method is passed with the module specification object used to find the module:

def create_module(self, spec)
    module = ConfigModule(spec.name)
    self.init_module_attrs(spec, module)
    return module

Implementing execute_module

execute_module function should contain the code for loading the contents of the module:

def execute_module(self, module):
    module.read(self.file_path)
    return module

A good tip for determining how to implement this method is imagining you trigger a reload of the module: the code syncing the module contents with the file is what you should put here.

How does it work

Extension mechanism work by monkeypatching the FileFinder class in charge of reading Python several format modules from the local file system.

Internally, FileFinder uses file loaders to read the several formats of Python modules identified by their file extension. Although these classes are public, FileFinder does not expose any extension mechanism to link new extensions with new loaders.

In the spirit of sys.path_hooks and other extension hooks, activating abm will expose a dictionary in sys.abm_hooks to register new loaders dynamically. For instance:

import sys
from abm.loaders import IniLoader
from abm.core import activate

activate()
sys.abm_hooks['.ini'] = IniLoader

It works by turning the internal instance attribute _loaders of FileFinder instances into a class property. Setting the property will diverge the new value to a different attribute while reading the value will combine the original one with the extensions in sys.abm_hooks.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

abm-0.2.0.tar.gz (6.2 kB view details)

Uploaded Source

File details

Details for the file abm-0.2.0.tar.gz.

File metadata

  • Download URL: abm-0.2.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5

File hashes

Hashes for abm-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0b0bd2825ca72a82ec32577d6ff73c8b4dcbdb111aca3586cb6c4b9fa8eda68b
MD5 08d93c58e0256c0ffcb396d98083eb83
BLAKE2b-256 ce2e50d07ecbda9594c2e275a14f1b34ead17da838951fecc6e92e66b0f683e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page