Debian Codegraph @

envisage/examples/demo/MOTD/acme/motd/motd_plugin.py

raw
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 'Message of the Day' plugin """
12
13
14 # In the interest of lazy loading you should only import from the following
15 # packages at the module level of a plugin::
16 #
17 # - envisage
18 # - traits
19 #
20 # Eveything else should be imported when it is actually required.
21
22
23 from traits.api import Instance, List, on_trait_change
24
25 # Enthought library imports.
26 from envisage.api import ExtensionPoint, Plugin, ServiceOffer
27
28
29 class MOTDPlugin(Plugin):
30 """The 'Message of the Day' plugin.
31
32 This plugin simply prints the 'Message of the Day' to stdout.
33
34 """
35
36 # The Ids of the extension points that this plugin offers.
37 MESSAGES = "acme.motd.messages"
38
39 # The Ids of the extension points that this plugin contributes to.
40 SERVICE_OFFERS = "envisage.service_offers"
41
42 #### 'IPlugin' interface ##################################################
43
44 # The plugin's unique identifier.
45 id = "acme.motd"
46
47 # The plugin's name (suitable for displaying to the user).
48 name = "MOTD"
49
50 #### Extension points offered by this plugin ##############################
51
52 # The messages extension point.
53 #
54 # Notice that we use the string name of the 'IMessage' interface rather
55 # than actually importing it. This makes sure that the import only happens
56 # when somebody actually gets the contributions to the extension point.
57 messages = ExtensionPoint(
58 List(Instance("acme.motd.api.IMessage")),
59 id=MESSAGES,
60 desc="""
61
62 This extension point allows you to contribute messages to the 'Message
63 Of The Day'.
64
65 """,
66 )
67
68 #### Contributions to extension points made by this plugin ################
69
70 service_offers = List(contributes_to=SERVICE_OFFERS)
71
72 def _service_offers_default(self):
73 """Trait initializer."""
74
75 # Register the protocol as a string containing the actual module path
76 # and *not* a module path that goes via an 'api.py' file as this does
77 # not match what Python thinks the module is! This allows the service
78 # to be looked up by passing either the exact same string, or the
79 # actual protocol object itself (the latter is the preferred way of
80 # doing it!).
81 motd_service_offer = ServiceOffer(
82 protocol="acme.motd.i_motd.IMOTD",
83 factory=self._create_motd_service,
84 )
85
86 return [motd_service_offer]
87
88 ###########################################################################
89 # Private interface.
90 ###########################################################################
91
92 def _create_motd_service(self):
93 """Factory method for the 'MOTD' service."""
94
95 # Only do imports when you need to! This makes sure that the import
96 # only happens when somebody needs an 'IMOTD' service.
97 from acme.motd.motd import MOTD
98
99 return MOTD(messages=self.messages)
100
101 # This plugin does all of its work in this method which gets called when
102 # the application has started all of its plugins.
103 @on_trait_change("application:started")
104 def _print_motd(self):
105 """Print the 'Message of the Day' to stdout!"""
106
107 # Note that we always offer the service via its name, but look it up
108 # via the actual protocol.
109 from acme.motd.api import IMOTD
110
111 # Lookup the MOTD service.
112 motd = self.application.get_service(IMOTD)
113
114 # Get the message of the day...
115 message = motd.motd()
116
117 # ... and print it.
118 print('\n"%s"\n\n- %s' % (message.text, message.author))