Changes between Version 30 and Version 31 of TracDev/ComponentArchitecture
- Timestamp:
- Feb 8, 2015, 12:15:13 PM (9 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TracDev/ComponentArchitecture
v30 v31 1 = Trac Component Architecture =2 3 As the heart of Trac, [source:/trunk/trac/core.py#latest trac.core] implements a minimal '' component kernel'' that allows components to easily extend each others' functionality. It provides a ''"meta-plugin-API"'': every component can easily offer its own plugin API by declaring ''"extension points"''.4 5 == What is a Component? ==6 7 For our purposes, a ''component'' is an object that provides a certain type of service within the context of the application. There is at most one instance of any component: components are singletons. That implies that a component does '''not''' map to an entity of the application's object model; instead, components represent functional subsystems.8 9 Components can declare '' "extension points"'' that other components can “plug in”to. This allows one component to enhance the functionality of the component it extends, without the extended component even knowing that the extending component exists. All that is needed is that the original component exposes – and uses – one or more extension points.1 = Trac Component Architecture 2 3 As the heart of Trac, [source:/trunk/trac/core.py#latest trac.core] implements a minimal '''component kernel''' that allows components to easily extend each others' functionality. It provides a meta-plugin-API: every component can easily offer its own plugin API by declaring '''extension points'''. 4 5 == What is a Component? 6 7 A '''component''' is an object that provides a certain type of service within the context of the application. There is at most one instance of any component: components are singletons. That implies that a component does '''not''' map to an entity of the application's object model; instead, components represent functional subsystems. 8 9 Components can declare '''extension points''' that other components can plug in to. This allows one component to enhance the functionality of the component it extends, without the extended component even knowing that the extending component exists. All that is needed is that the original component exposes – and uses – one or more extension points. 10 10 11 11 [[Image(xtnpt.png)]] 12 12 13 A component can extend any number of other components and still offer its own extension points. This allows a plugin to itself offer a plugin API (i.e. extension point). This feature is the basis for a plugin-based architecture.14 15 The actual functionality and APIs are defined by individual components. The component kernel provides the “magic glue” to hook the different subsystems together –without them necessarily knowing about each other.16 17 == Public classes ==13 A component can extend any number of other components and still offer its own extension points. This allows a plugin to itself offer a plugin API, ie extension point. This feature is the basis for a plugin-based architecture. 14 15 The actual functionality and APIs are defined by individual components. The component kernel provides the glue to hook the different subsystems together, without them necessarily knowing about each other. 16 17 == Public classes 18 18 19 19 `trac.core.ComponentManager` [[BR]] 20 Manages component life cycle, instantiating registered components on demand 20 Manages component life cycle, instantiating registered components on demand. 21 21 22 22 `trac.core.Component` [[BR]] … … 31 31 [[Image(comparch.png)]] 32 32 33 == Declaring a component ==33 == Declaring a component 34 34 35 35 The simplest possible component is an empty class derived from `trac.core.Component`: … … 51 51 }}} 52 52 53 Remember that components follow the singleton pattern, but component managers do not. There is only one active instance of any component per component manager. The component constructor is “magic” in that it checks with the component manager whether there´s already an active instance before allocating a new instance. If the component was already instantiated, the existing instance is returned:53 Remember that components follow the singleton pattern, but component managers do not. There is only one active instance of any component per component manager. The component constructor checks with the component manager whether there is already an active instance before allocating a new instance. If the component was already instantiated, the existing instance is returned: 54 54 55 55 {{{ … … 60 60 }}} 61 61 62 If a component needs to initialize data members, it can override the `__init__` method. But because the component manager needs to be able to instantiate the component on demand, `__init__` must '' not'' require any extra parameters, ''including''the reference to the component manager passed into the constructor:62 If a component needs to initialize data members, it can override the `__init__` method. But because the component manager needs to be able to instantiate the component on demand, `__init__` must '''not''' require any extra parameters, including the reference to the component manager passed into the constructor: 63 63 64 64 {{{ … … 74 74 }}} 75 75 76 Direct `Component` sub-classes also do not need to worry about invoking the base class' (i.e. `Component`'s) `__init__` method, as it's empty. 77 78 Notes: 79 * You can't pass data to the constructor of a component. 80 81 === Components instantiating other Components === 76 Direct `Component` sub-classes also do not need to worry about invoking the base class' (i.e. `Component`'s) `__init__` method, as it is empty. 77 78 '''Note''': You can't pass data to the constructor of a component. 79 80 === Components instantiating other Components 82 81 83 82 If one `Component` instantiates another, it typically will use the same `ComponentManager`, instead of creating a new `ComponentManager`. … … 90 89 }}} 91 90 92 Note that within trac, the component manager is more commonly referenced as `self.env`.93 94 == Declaring an extension point ==91 Note that within Trac, the component manager is more commonly referenced as `self.env`. 92 93 == Declaring an extension point 95 94 96 95 The component above doesn't actually do anything. Making an object a component only makes it act as a singleton in the scope of a component manager, which isn't that exciting in itself. 97 96 98 The real value of components becomes clearer when the facilities for extensions are used. As a simple example, the following component provides an extension point that lets other components listen to changes to the data it manages (in this case a list of to-do items) –following the widely known observable pattern:97 The real value of components becomes clearer when the facilities for extensions are used. As a simple example, the following component provides an extension point that lets other components listen to changes to the data it manages (in this case a list of to-do items), following the widely known observable pattern: 99 98 100 99 {{{ … … 121 120 Here, the `TodoList` class declares an extension point called `observers` with the interface `ITodoObserver`. The interface defines the ''contract'' that extending components need to conform to. 122 121 123 The `TodoList` component notifies the observers inside the `add()` method by iterating over `self.observers` and calling the `todo_added()` method for each. This works because the `observers` attribute is a [http://users.rcn.com/python/download/Descriptor.htm descriptor]: When it is accessed, it finds all '' enabled'' components (see [#component_lifecycle below]) that declare to extend the extension point. For each of those components, it gets the instance from the component manager, potentially activating it if it is getting accessed for the first time.124 125 Note that there are actually three ways to define an extension point:122 The `TodoList` component notifies the observers inside the `add()` method by iterating over `self.observers` and calling the `todo_added()` method for each. This works because the `observers` attribute is a [http://users.rcn.com/python/download/Descriptor.htm descriptor]: When it is accessed, it finds all '''enabled''' components (see [#component_lifecycle below]) that declare to extend the extension point. For each of those components, it gets the instance from the component manager, potentially activating it if it is getting accessed for the first time. 123 124 Note that there are multiple ways to define an extension point: 126 125 127 126 * `trac.core.ExtensionPoint`: This is an ''unordered list of all'' enabled components implementing a specific extension point interface. 128 127 * `trac.config.ExtensionOption`: An option for [wiki:TracIni trac.ini] that describes ''exactly one'' enabled component implementing a specific extension point interface. 129 * `trac.config.OrderedExtensionsOption`: An option for [wiki:TracIni trac.ini] that describes an ''ordered list'' of enabled components implementing a specific extension point interface. (Components that also implement the same interface but are not listed in the option can automatically be appended to the list.)130 131 == Plugging in to an extension point ==128 * `trac.config.OrderedExtensionsOption`: An option for [wiki:TracIni trac.ini] that describes an ''ordered list'' of enabled components implementing a specific extension point interface. Components that also implement the same interface but are not listed in the option can automatically be appended to the list. 129 130 == Plugging in to an extension point 132 131 133 132 Now that we have an extendable component, let's add another component that extends it: … … 149 148 You can specify multiple extension point interfaces to extend with the `implements` method by simply passing them as additional arguments. 150 149 151 == Putting it together ==150 == Putting it together 152 151 153 152 Now that we've declared both a component exposing an extension point, and another component extending that extension point, let's use the to-do list example to see what happens: … … 173 172 }}} 174 173 175 This output obviously comes from the `TodoPrinter`. Note however that the code snippet above doesn't even mention that class. All that is needed to have it participating in the action is to declare the class. ''(That implies that an extending class needs to be imported by a python script to be registered. The aspect of loading components is however separate from the extension mechanism itself.)''174 This output obviously comes from the `TodoPrinter`. Note however that the code snippet above doesn't even mention that class. All that is needed to have it participating in the action is to declare the class. That implies that an extending class needs to be imported by a python script to be registered. The aspect of loading components is however separate from the extension mechanism itself. 176 175 177 176 == Component Lifecycle and State == #component_lifecycle 177 178 178 This section will shed some light on how components come to be. It describes some inner workings and terminology that you may come across when trying to understand components, and provides information about where the specific parts are implemented. 179 179 … … 191 191 * by listing the file in `entry_points` section of a plugin (see [wiki:TracDev/PluginDevelopment#Packagingplugins Packaging Plugins]) 192 192 193 The registration is handled by `trac.core.ComponentMeta` using [http://www.ibm.com/developerworks/linux/library/l-pymeta .html metaclass programming].194 2. ''Activation:'' A component gets activated when it's first used. So, "activation" is basically just another word for "instantiation", though since a component is a singleton it's only activate/instantiated once. When a component gets activated, Trac's component manager adds some useful fields to the component (specifically: `env`, `log`, and `config`).[[BR]]193 The registration is handled by `trac.core.ComponentMeta` using [http://www.ibm.com/developerworks/linux/library/l-pymeta3/index.html metaclass programming]. 194 2. ''Activation:'' A component gets activated when it's first used. So, "activation" is basically another word for "instantiation", though since a component is a singleton it's only activated/instantiated once. When a component gets activated, Trac's component manager adds some useful fields to the component, specifically: `env`, `log`, and `config`.[[BR]] 195 195 A component can be activated using one of the following methods: 196 * when it's manually constructed for the first time (using `Component(compmngr)`)196 * when it's manually constructed for the first time, using `Component(compmngr)` 197 197 * when one of the extension points the component implements is used for the first time. Note that the component must be enabled for this way to work (see below). 198 198 … … 207 207 Enabling a component is done in the `[components]` section of [wiki:TracIni trac.ini]. This is implemented in `trac.env.Environment.is_component_enabled()`. Whether a component is enabled or disabled is checked ''only once'' when an extension point that component implements is first used. 208 208 209 '' Miscellaneous notes:''209 '''Miscellaneous notes:''' 210 210 * Components can be marked "abstract". This is done simply by adding a member field `abstract = True` to the component class. 211 211 {{{ … … 219 219 * Components should be listed in the `entry_points` section, if they define any options (from `trac.config`). This way `trac.ini` editors can find this option even if it still has its default value. Options are registered when the component is registered. The component that defines the option doesn't need to be enabled for the option to be registered and can even be abstract. 220 220 221 222 == How components are used in Trac's code == 223 224 The typical use of components in Trac starts with a top-level “service provider” that we'll pick up and use directly. Take, for example, `trac.perm.PermissionSystem`: 221 == How components are used in Trac's code 222 223 The typical use of components in Trac starts with a top-level '''service provider''' that we'll pick up and use directly. Take, for example, `trac.perm.PermissionSystem`: 225 224 {{{ 226 225 #!python … … 270 269 271 270 }}} 272 The above adds an option called `permission_store` to `trac.ini`, declares that the component named by the option implements `IPermissionStore`, and sets its default to `DefaultPermissionStore`. See [browser:trunk/trac/config.py trac.config] for `ExtensionOption` and friends. Methods of service providers such as `PermissionSystem` are commonly a thin forwarding layer over such an `ExtensionOption`. For example: 271 272 The above adds an option called `permission_store` to `trac.ini`, declares that the component named by the option implements `IPermissionStore`, and sets its default to `DefaultPermissionStore`. See [browser:trunk/trac/config.py trac.config] for `ExtensionOption` and friends. Methods of service providers such as `PermissionSystem` are commonly a thin forwarding layer over such an `ExtensionOption`. For example: 273 273 274 {{{ 274 275 #!python … … 280 281 return self.store.get_all_permissions() 281 282 }}} 283 282 284 Thus, service providers are directly manipulated from Python, and are customized through the automatic aggregation of components implementing `ExtensionPoint`s and through configuration of `ExtensionOption`s by Trac administrators. 283 285