bronx.syntax.externalcode

A handy class that checks that an external code import worked properly.

If not, a decorator is provided to disable portions of code.

Example of a simple use:

from bronx.syntax.externalcode import ExternalCodeImportChecker

echecker = ExternalCodeImportChecker('anypackage')
with echecker:
    import anypackage

print('Did it work ? {!s}'.format(echecker.is_available()))

@echecker.disabled_if_unavailable
def my_function_based_on_anypackage()
    return anypackage.sayhi()

@echecker.disabled_if_unavailable
class MyClassBasedOnAnypackage(object)

    def sayhi(self)
        return anypackage.sayhi()

# A call to my_function_based_on_anypackage or any attempt to create a
# MyClassBasedOnAnypackage object will lead to a ExternalCodeUnavailableError
# error if the import statement failed.

Example of that also checks a version number:

from bronx.syntax.externalcode import ExternalCodeImportChecker

echecker = ExternalCodeImportChecker('anypackage')
with echecker as echecker_register:
    import anypackage
    echecker_register.update(version=anypackage.__version__,
                             otherthing=anypackage.otherthing)

# Ensure that the package is here, that version >= 1.0.0 and otherthings==1
@echecker.disabled_if_unavailable(version='1.0.0', otherthing=1)
def my_function_based_on_anypackage()
    return anypackage.sayhi()

Classes

class bronx.syntax.externalcode.ExternalCodeImportChecker(nickname='external')[source]

Bases: object

Catches any import error and allow for the developer to test whether it succeeded or not.

See the example above.

Parameters:

nickname (str) – The name of the external code to be imported

disabled_if_unavailable(*kargs, **kwargs)[source]

This decorator disables the provided object if the external code is unavailable.

Parameters:

kwargs – A dictionary of requirements to check for

is_available(**kwargs)[source]

Is it ok ?

Parameters:

kwargs – A dictionary of requirements to check for

Exceptions

exception bronx.syntax.externalcode.ExternalCodeUnavailableError[source]

Bases: Exception

Raised by the decorated function/class whenever the import did not succeed.