Skip to main content

Generic backend system to use throughout Armstrong

Project description

TravisCI status Coverage status PyPI Version License

Generic backend system to use throughout Armstrong


Dynamically load a Python module at runtime and use it as if you’d hardcoded the module directly. This allows flexibility. It’s polymorphism in action.

Why? In the Armstrong internals we do a bunch of stuff via backends. If you want to do that stuff differently, make a class with the same interface and provide your class as the backend. Armstrong will work its magic the way you want. In many cases, Armstrong ships with support for multiple common scenarios (implemented as backends) and you can pick the one that fits your needs.

Following the Django paradigm, create a key = value in where the value is a string or a list of strings of full, dotted, Python import paths. That module will be imported at runtime and used exactly as if you had instantiated it directly. An example:

# hello/
class Hello(object):
    def hi(self):
        print("Hello world!")

# hello/
class Hello(object):
    def hi(self):
        print("Hello Armstrong!")

#  <-- armstrong.utils.backends uses Django settings by default
HELLO_CLASS = "hello.armstrong.Hello"

# or in a console
>>> from armstrong.utils.backends import GenericBackend
>>> hello = GenericBackend("HELLO_CLASS").get_backend()
>>> hello.hi()
Hello Armstrong!

A default can be provided and the process works like the standard Python dict.get() where if the key doesn’t exist in the settings, there’s a fallback. (This is how Armstrong specifies its defaults so you aren’t required to change your if satisfied with the default behavior.):

>>> backend = GenericBackend("MISSING_KEY", defaults="")
>>> hello = backend.get_backend()
>>> hello.hi()
Hello world!

Calling get_backend() is the equivalent of instantiation. So whenever you’re ready to use the dynamically loaded class, call get_backend. Pass in any parameters you’d normally use. Think of it as __init__. These are the same:

GenericBackend("HELLO_CLASS").get_backend(1, two=2)
Hello(1, two=2)

You can pass in a different settings module with the settings kwarg if you want the backend loader to look somewhere other than Django settings.

Multiple backends

Another powerful feature? Feeding in multiple possible backends. Armstrong will perform the action you want by going down the list of backends stopping at the first one that does its job. If the backend’s method raises a BackendDidNotHandle exception, Armstrong will try the next backend. A pseudo code example:

default_backends = ["myapp.backends.TwitterBackend",
backend = GenericBackend("SOCIAL_NETWORKS", defaults=default_backends)

class TwitterBackend(object):
    def post(msg):
        if not self.user.has_account:
            raise BackendDidNotHandle("No account for that user")

social_network = backend.get_backend(user)"Armstrong is pretty sweet you guys")

Writing Backends

Backends are classes. GenericBackend is a way to dynamically load those classes. Beyond using get_backend to handle the creation of the backend, you treat it as if you were calling it directly.

If you are using multiple backends, all attributes (and methods) accessed on the backend are proxied to handle the dispatching. To have a backend abdicate and have the loader use the next backend in the list, have the backend method raise armstrong.utils.backends.BackendDidNotHandle.

Installation & Configuration

Supports Django 1.3, 1.4, 1.5, 1.6, 1.7 on Python 2.6 and 2.7.

  1. pip install armstrong.utils.backends


Development occurs on Github. Participation is welcome!

  • Found a bug? File it on Github Issues. Include as much detail as you can and make sure to list the specific component since we use a centralized, project-wide issue tracker.

  • Testing? pip install tox and run tox

  • Have code to submit? Fork the repo, consolidate your changes on a topic branch and create a pull request. The package provides tools for testing, coverage and South migration as well as making it very easy to run a full Django environment with this component’s settings.

  • Questions, need help, discussion? Use our Google Group mailing list.

State of Project

Armstrong is an open-source news platform that is freely available to any organization. It is the result of a collaboration between the Texas Tribune and Bay Citizen and a grant from the John S. and James L. Knight Foundation. Armstrong is available as a complete bundle and as individual, stand-alone components.

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

armstrong.utils.backends-1.1.1.tar.gz (9.2 kB view hashes)

Uploaded source

Supported by

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