Debian Codegraph @

doc/source/contributing/schema_extensions.rst

raw
1 Extending {kiwi} with Custom Operations
2 ========================================
3
4 .. note:: **Abstract**
5
6 Extension plugins in {kiwi} offer a mechanism for adding information outside the standard {kiwi} schema.
7
8 This document describes how to create an extension plugin for the {kiwi}
9 schema as well as how to add and validate additional information in the
10 {kiwi} image description.
11
12 The described schema extension can be used in an additional {kiwi} task
13 plugin to provide a new subcommand for {kiwi}. At the present moment, there
14 is no other plugin interface except for providing additional {kiwi} commands.
15
16 Depending on the demand for custom plugins, the interface
17 to hook into code in other parts of the {kiwi} processing
18 needs to be extended.
19
20 This description applies to version |version|.
21
22 The <extension> Section
23 -----------------------
24
25 The main {kiwi} schema supports an extension section that allows you
26 to specify any XML structure and attributes, as long as they are
27 attached to a namespace. This means that any custom XML
28 structure can be implemented similar to the example below:
29
30 .. code:: bash
31
32 <image>
33 ...
34 <extension xmlns:my_plugin="http://www.my_plugin.com">
35 <my_plugin:my_feature>
36 <my_plugin:title name="cool stuff"/>
37 </my_plugin:my_feature>
38 </extension>
39 </image>
40
41 * Any top-level namespace must be unique.
42 * Multiple different top-level namespaces are allowed,
43 for example, `my_plugin_a`, `my_plugin_b`.
44
45 RELAX NG Schema for the Extension
46 ---------------------------------
47
48 If an extension section is found, {kiwi} looks up its namespace and uses
49 the main XML catalog for the schema file to validate the extension data.
50 The schema file must be a RELAX NG schema in the `.rng` format. We recommend
51 saving the schema as :file:`/usr/share/xml/kiwi/my_plugin.rng`.
52
53 For the example above, the RELAX NG Schema in the compressed format,
54 :file:`my_plugin.rnc`, looks as follows:
55
56 .. there is no rnc syntax highlighting, try cpp
57 .. code:: cpp
58
59 namespace my_plugin = "http://www.my_plugin.com"
60
61 start =
62 k.my_feature
63
64 div {
65 k.my_feature.attlist = empty
66 k.my_feature =
67 element my_plugin:my_feature {
68 k.my_feature.attlist &
69 k.title
70 }
71 }
72
73 div {
74 k.title.name.attribute =
75 attribute name { text }
76 k.title.attlist = k.title.name.attribute
77 k.title =
78 element my_plugin:title {
79 k.title.attlist
80 }
81 }
82
83 To convert this schema to the `.rng` format, just call:
84
85 .. code:: bash
86
87 $ trang -I rnc -O rng my_plugin.rnc /usr/share/xml/kiwi/my_plugin.rng
88
89 Extension schema in XML catalog
90 -------------------------------
91
92 As mentioned above, the mapping from the extension namespace to the
93 correct RELAX NG schema file is handled by an XML catalog file. The
94 XML catalog for the example is as follows:
95
96 .. code:: bash
97
98 <?xml version="1.0"?>
99 <catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
100 <system
101 systemId="http://www.my_plugin.com"
102 uri="file:////usr/share/xml/kiwi/my_plugin.rng"/>
103 </catalog>
104
105 For resolving the catalog, {kiwi} uses the `xmlcatalog` command
106 and the main XML catalog from the system, :file:`/etc/xml/catalog`.
107
108 .. note::
109
110 How the main catalog is informed about the existence of the {kiwi} extension
111 catalog file depends on the distribution and its version. Refer to the
112 distribution documentation for information on adding XML catalogs.
113
114 If the following command provides the information for the correct
115 RELAX NG schema file, you are ready for a first test:
116
117 .. code:: bash
118
119 $ xmlcatalog /etc/xml/catalog http://www.my_plugin.com
120
121 Using the extension
122 -------------------
123
124 To test the extension, insert the example extension into one of your
125 image description's :file:`config.xml` file.
126
127 The following example reads the name attribute from the title
128 section of the `my_feature` root element and prints it:
129
130 .. code:: python
131
132 import logging
133
134 from kiwi.xml_description import XMLDescription
135
136 description = XMLDescription('path/to/kiwi/XML/config.xml')
137 description.load()
138
139 my_plugin = description.get_extension_xml_data('my_plugin')
140
141 print(my_plugin.getroot()[0].get('name'))