| 1 |
# (C) Copyright 2007-2025 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
""" The Envisage version of the old chestnut. """ |
| 12 |
|
| 13 |
from traits.api import List, Str |
| 14 |
|
| 15 |
# Enthought library imports. |
| 16 |
from envisage.api import Application, ExtensionPoint, Plugin |
| 17 |
|
| 18 |
|
| 19 |
class HelloWorld(Plugin): |
| 20 |
"""The 'Hello World' plugin. |
| 21 |
|
| 22 |
This plugin offers a single extension point 'greetings' which is a list of |
| 23 |
greetings, one of which is used to produce the '<greeting> World' message |
| 24 |
when the plugin is started. |
| 25 |
|
| 26 |
""" |
| 27 |
|
| 28 |
# This tells us that the plugin offers the 'greetings' extension point, |
| 29 |
# and that plugins that want to contribute to it must each provide a list |
| 30 |
# of strings (Str). |
| 31 |
greetings = ExtensionPoint( |
| 32 |
List(Str), id="greetings", desc='Greetings for "Hello World"' |
| 33 |
) |
| 34 |
|
| 35 |
# Plugin's have two important lifecyle methods, 'start' and 'stop'. These |
| 36 |
# methods are called automatically by Envisage when the application is |
| 37 |
# started and stopped respectively. |
| 38 |
def start(self): |
| 39 |
"""Start the plugin.""" |
| 40 |
|
| 41 |
# Standard library imports. |
| 42 |
# |
| 43 |
# We put this import here just to emphasize that it is only used in |
| 44 |
# this specific plugin. |
| 45 |
import random |
| 46 |
|
| 47 |
print("{} World!".format(random.choice(self.greetings))) |
| 48 |
|
| 49 |
|
| 50 |
class Greetings(Plugin): |
| 51 |
"""A plugin that contributes to the 'greetings' extension point.""" |
| 52 |
|
| 53 |
# This tells us that the plugin contributes the value of this trait to the |
| 54 |
# 'greetings' extension point. |
| 55 |
greetings = List(["Hello", "G'day"], contributes_to="greetings") |
| 56 |
|
| 57 |
|
| 58 |
class MoreGreetings(Plugin): |
| 59 |
"""Another plugin that contributes to the 'greetings' extension point.""" |
| 60 |
|
| 61 |
# This tells us that the plugin contributes the value of this trait to the |
| 62 |
# 'greetings' extension point. |
| 63 |
greetings = List(contributes_to="greetings") |
| 64 |
|
| 65 |
# This shows how you can use a standard trait initializer to populate the |
| 66 |
# list dynamically. |
| 67 |
def _greetings_default(self): |
| 68 |
"""Trait initializer.""" |
| 69 |
|
| 70 |
extensions = [ |
| 71 |
"The %s application says %s" % (self.application.id, greeting) |
| 72 |
for greeting in ["Bonjour", "Hola"] |
| 73 |
] |
| 74 |
|
| 75 |
return extensions |
| 76 |
|
| 77 |
|
| 78 |
# Application entry point. |
| 79 |
if __name__ == "__main__": |
| 80 |
# Create the application. |
| 81 |
# |
| 82 |
# An application is simply a collection of plugins. In this case we |
| 83 |
# specify the plugins explicitly, but the mechanism for finding plugins |
| 84 |
# is configurable by setting the application's 'plugin_manager' trait. |
| 85 |
application = Application( |
| 86 |
id="hello.world", plugins=[HelloWorld(), Greetings(), MoreGreetings()] |
| 87 |
) |
| 88 |
|
| 89 |
# Run it! |
| 90 |
application.run() |