Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Z
ZODB
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Nicolas Wavrant
ZODB
Commits
5667d13c
Commit
5667d13c
authored
Feb 27, 2004
by
Fred Drake
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
try explaining how to use schema components to extend existing schema
parent
3887b06c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
155 additions
and
0 deletions
+155
-0
doc/ZConfig/zconfig.tex
doc/ZConfig/zconfig.tex
+155
-0
No files found.
doc/ZConfig/zconfig.tex
View file @
5667d13c
...
...
@@ -1061,6 +1061,161 @@ The types defined in this component implement the
\end
{
seealso
}
\section
{
Using Components to Extend Schema
}
% XXX This section needs a lot of work, but should get people started
% who really want to add new pieces ZConfig-configured applications.
It is possible to use schema components and the
\keyword
{
\%
import
}
construct to extend the set of section types available for a specific
configuration file, and allow the new components to be used in place
of standard components.
The key to making this work is the use of abstract section types.
Wherever the original schema accepts an abstract type, it is possible
to load new implementations of the abstract type and use those instead
of, or in addition to, the implementations loaded by the original
schema.
Abstract types are generally used to represent interfaces. Sometimes
these are interfaces for factory objects, and sometimes not, but
there's an interface that the new component needs to implement. What
interface is required should be documented in the
\element
{
description
}
element in the
\element
{
abstracttype
}
element;
this may be by reference to an interface specified in a Python module
or described in some other bit of documentation.
The following things need to be created to make the new component
usable from the configuration file:
\begin
{
enumerate
}
\item
An implementation of the required interface.
\item
A schema component that defines a section type that contains
the information needed to construct the component.
\item
A ``datatype'' function that converts configuration data to an
instance of the component.
\end
{
enumerate
}
For simplicity, let's assume that the implementation is defined by a
Python class.
The example component we build here will be in the
\module
{
noise
}
package, but any package will do. Components loadable using
\keyword
{
\%
import
}
must be contained in the
\file
{
component.xml
}
file;
alternate filenames may not be selected by the
\keyword
{
\%
import
}
construct.
Create a ZConfig component that provides a section type to support
your component. The new section type must declare that it implements
the appropriate abstract type; it should probably look something like
this:
\begin
{
verbatim
}
<component prefix
=
"noise.server">
<import package
=
"ZServer"
/
>
<sectiontype name
=
"noise
-
generator"
implements
=
"ZServer.server"
datatype
=
".NoiseServerFactory">
<
!--
specific configuration data should be described here
--
>
<key name
=
"port"
datatype
=
"port
-
number"
required
=
"yes">
<description>
Port number to listen on.
<
/
description>
<
/
key>
<key name
=
"color"
datatype
=
".noise
_
color"
default
=
"white">
<description>
Silly way to specify a noise generation algorithm.
<
/
description>
<
/
key>
<
/
sectiontype>
<
/
component>
\end
{
verbatim
}
This example uses one of the standard ZConfig datatypes,
\datatype
{
port
-
number
}
, and requires two additional types to be
provided by the
\module
{
noise.server
}
module:
\class
{
NoiseServerFactory
}
and
\function
{
noise
_
color
()
}
.
The
\function
{
noise
_
color
()
}
function is a datatype conversion for a
key, so it accepts a string and returns the value that should be used:
\begin
{
verbatim
}
_
noise
_
colors
=
{
# color
-
> r,g,b
'white':
(
255
,
255
,
255
)
,
'pink':
(
255
,
182
,
193
)
,
}
def noise
_
color
(
string
)
:
if string in
_
noise
_
colors:
return
_
noise
_
colors
[
string
]
else:
raise ValueError
(
'unknown noise color:
%r' % string)
\end
{
verbatim
}
\class
{
NoiseServerFactory
}
is a little different, as it's the datatype
function for a section rather than a key. The parameter isn't a
string, but a section value object with two attributes,
\member
{
port
}
and
\member
{
color
}
.
Since the
\datatype
{
ZServer.server
}
abstract type requires that the
component returned is a factory object, the datatype function can be
implemented at the constructor for the class of the factory object.
(
If the datatype function could select different implementation
classes based on the configuration values, it makes more sense to use
a simple function that returns the appropriate implementation.
)
A class that implements this datatype might look like this:
\begin
{
verbatim
}
from ZServer.datatypes import ServerFactory
from noise.generator import WhiteNoiseGenerator, PinkNoiseGenerator
class NoiseServerFactory
(
ServerFactory
)
:
def
__
init
__
(
self, section
)
:
# host and ip will be initialized by ServerFactory.prepare
()
self.host
=
None
self.ip
=
None
self.port
=
section.port
self.color
=
section.color
def create
(
self
)
:
if self.color
==
'white':
generator
=
WhiteNoiseGenerator
()
else:
generator
=
PinkNoiseGenerator
()
return NoiseServer
(
self.ip, self.port, generator
)
\end
{
verbatim
}
You'll need to arrange for the package containing this component is
available on Python's
\code
{
sys.path
}
before the configuration file is
loaded; this is mostly easily done by manipulating the
\envvar
{
PYTHONPATH
}
environment variable.
Your configuration file can now include the following to load and use
your new component:
\begin
{
verbatim
}
%import noise
<noise
-
generator>
port
1234
color white
<
/
noise
-
generator>
\end
{
verbatim
}
\section
{
\module
{
ZConfig
}
---
Basic configuration support
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment