Skip to main content

Automation Mojo Extension Package

Project description

DEPRICATED - THIS MODULE WILL BE REMOVED

This package will be removed on Aug 1st, 2025, the root namespace ‘mojo.*’ conflicts with the ‘mojo’ programming language. For information about using the newer packages, contact Automation Mojo at:

automationx@automationmojo.com

Automation Mojo Extension Package

This is a python package that provides a mechanism for extending other python packages. This package is different from other python extension packages in that it uses the python Protocol typing in order to query module hierarchies for extensions.

Declaring an Extension Protocol

For example, if we want to be able to create instance of object like these from a factory.

class Hey:
    def __str__(self):
        return "Hey"

class Ho:
    def __str__(self):
        return "Ho"


# The following class defines a protocol that defines an extenstion type.
# Extensions

class MyExtTypeProtocol(ExtProtocol):

    ext_protocol_name = "mojo-myextypeprotocol"

    @classmethod
    def give_me_a_hey(cls):
        ...

    @classmethod
    def give_me_a_ho(cls):
        ...

Implementing an Extension Protocol

The code below is implementing the extension protocol defined above. When a class implements an extension protocol, it will inherit from the protocol it is implementing. By inheriting from the protocol, it pulls in the ext_protocol_name variable which ensures that the derived type is declared to implement a given protocol.

Another important thing to look at in the code below is the class variable PRECEDENCE. The PRECEDENCE number indicates to the SuperFactory which extensions to return when an extension is queried based on precedence of overload and relevance. The higher number precedence is considered by the SuperFactory to have the most relevance.

class MyExtTypeFactory(ExtFactory, MyExtTypeProtocol):

    PRECEDENCE = 10

    @classmethod
    def give_me_a_hey(cls):
        return Hey

    @classmethod
    def give_me_a_ho(cls):
        return Ho

Configuration for Custom Extensions

In order to be able to extend packages, you must tell the mojo-extension code where the root packages are that need to be searched for extension factories. Then what we do is we register the root modules under which the factory types will be found.

Setting the MJR_CONFIGURED_FACTORY_MODULES Variable from Python

from mojo.extension.extensionconfiguration import ExtensionConfiguration
from mojo.extension.wellknown import ConfiguredSuperFactorySingleton

ExtensionConfiguration.MJR_CONFIGURED_FACTORY_MODULES = [
        "mypkg.factories",
    ]

Setting the MJR_CONFIGURED_FACTORY_MODULES Environment Variable

MJR_CONFIGURED_FACTORY_MODULES=mypkg.a.factories,mypkg.b.factories

Setting the MJR_CONFIGURED_FACTORY_MODULES in the Startup Config

[MOJO-EXTENSION]
MJR_CONFIGURED_FACTORY_MODULES=mypkg.a.factories,mypkg.b.factories

Loading Custom Factories

In order to load extension factories, we utilize the ConfiguredSuperFactorySingleton singleton object that is maintained by the mojo-extension package. You can get a reference to the super factory singleton by using code similar to the code below:

from mojo.extension.wellknown import ConfiguredSuperFactorySingleton

superfactory = ConfiguredSuperFactorySingleton()

Then when we want to get the type from the extension, we utilize the protocol that was declared and ask for the type using the function on the protocol that will return the type.

hey_type = self._super_factory.get_override_types_by_order(MyExtTypeProtocol.give_me_a_hey)
ho_type = self._super_factory.get_override_types_by_order(MyExtTypeProtocol.give_me_a_ho)

hey = hey_type()
ho = ho_type()

print("")
print(f"{hey}... {ho}... {hey}... {ho}...")

References

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

mojo_extension-2.0.6.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

mojo_extension-2.0.6-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file mojo_extension-2.0.6.tar.gz.

File metadata

  • Download URL: mojo_extension-2.0.6.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.11.0-24-generic

File hashes

Hashes for mojo_extension-2.0.6.tar.gz
Algorithm Hash digest
SHA256 754cf0667d3f100af3bc0ba1e71fa5249134e5b3a7b82e4ccaa9f0ace8680585
MD5 4e5da462193390ee14191c6a78fe9208
BLAKE2b-256 bb9d69948f37c177e80037bfcd08da1d32ce101257f5ca9ec3719591cb057eb4

See more details on using hashes here.

File details

Details for the file mojo_extension-2.0.6-py3-none-any.whl.

File metadata

  • Download URL: mojo_extension-2.0.6-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.11.0-24-generic

File hashes

Hashes for mojo_extension-2.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 79c3455b737775daf96cd244fcd4d98b4e3b2151dc974985c19132382c8db57b
MD5 95339a37df3e7f975f2016f15121a2da
BLAKE2b-256 d9289afc436abcda012a20bf0f28e7dea7d86baacd4025bf5318c1f6a70fc42b

See more details on using hashes here.

Supported by

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