11Adding new tags
22===============
33
4- Are there XML tags you want to use that aren't supported by PodGen? If so, you
5- should be able to add them in using inheritance.
4+ Are there XML elements you want to use that aren't supported by PodGen? If so,
5+ you should be able to add them in using inheritance.
6+
7+ .. warning ::
8+
9+ This is an advanced topic.
610
711.. note ::
812
@@ -11,47 +15,67 @@ should be able to add them in using inheritance.
1115
1216.. note ::
1317
14- Feel free to add a feature request to GitHub Issues if you think PodGen
15- should support a certain tag out of the box.
18+ Feel free to add a feature request to `GitHub Issues `_ if you think PodGen
19+ should support a certain element out of the box.
20+
21+ .. _GitHub Issues : https://github.com/tobinus/python-podgen/issues
1622
1723
1824Quick How-to
1925------------
2026
21- #. Create new class that extends Podcast.
27+ #. Create new class that extends :class: ` . Podcast` .
2228#. Add the new attribute.
23- #. Override :meth: `.Podcast._create_rss `, call super()._create_rss(),
24- add the new tag to its result and return the new tree.
29+ #. Override :meth: `~.Podcast._create_rss `, call ``super()._create_rss() ``,
30+ add the new element to its result and return the new tree.
31+
32+ You can do the same with :class: `.Episode `, if you replace
33+ :meth: `~.Podcast._create_rss ` with :meth: `~Episode.rss_entry ` above.
34+
35+ There are plenty of small quirks you have to keep in mind. You are strongly
36+ encouraged to read the example below.
37+
38+ Using namespaces
39+ ^^^^^^^^^^^^^^^^
2540
2641If you'll use RSS elements from another namespace, you must make sure you
27- update the _nsmap attribute of Podcast (you cannot define new namespaces from
28- an episode!). It is a dictionary with the prefix as key and the
29- URI for that namespace as value. To use a namespace, you must put the URI inside
30- curly braces, with the tag name following right after (outside the braces).
31- For example::
42+ update the :attr: ` ~.Podcast. _nsmap` attribute of :class: ` . Podcast`
43+ (you cannot define new namespaces from an episode!). It is a dictionary with the
44+ prefix as key and the URI for that namespace as value. To use a namespace, you
45+ must put the URI inside curly braces, with the tag name following right after
46+ (outside the braces). For example::
3247
3348 "{%s}link" % self._nsmap['atom'] # This will render as atom:link
3449
35- The `lxml API documentation `_ is a pain to read, so just look at the source code
36- for PodGen to figure out how to do things. The example below may help, too .
50+ The `lxml API documentation `_ is a pain to read, so just look at the ` source code
51+ for PodGen `_ and the example below.
3752
3853.. _lxml API documentation : http://lxml.de/api/index.html
54+ .. _source code for PodGen : https://github.com/tobinus/python-podgen/blob/master/podgen/podcast.py
3955
40- You can do the same with Episode, if you replace _create_rss() with
41- rss_entry() above.
42-
43- Example: Adding a ttl field
44- ---------------------------
56+ Example: Adding a ttl element
57+ -----------------------------
4558
4659The examples here assume version 3 of Python is used.
4760
61+ ``ttl `` is an RSS element and stands for "time to live", and can only be an
62+ integer which indicates how many minutes the podcatcher can rely on its copy of
63+ the feed before refreshing (or something like that). There is confusion as to
64+ what it is supposed to mean (max refresh frequency? min refresh frequency?),
65+ which is why it is not included in PodGen. If you use it, you should treat it as
66+ the **recommended ** update period (source: `RSS Best Practices `_).
67+
68+ .. _RSS Best Practices : http://www.rssboard.org/rss-profile#element-channel-ttl
69+
4870Using traditional inheritance
4971^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5072
5173::
5274
75+ # The module used to create the XML tree and generate the XML
5376 from lxml import etree
5477
78+ # The class we will extend
5579 from podgen import Podcast
5680
5781
@@ -73,6 +97,9 @@ Using traditional inheritance
7397 # Call Podcast's constructor
7498 super().__init__(*args, **kwargs)
7599
100+ # If we were to use another namespace, we would add this here:
101+ # self._nsmap['prefix'] = "URI"
102+
76103 @property
77104 def ttl(self):
78105 """Your suggestion for how many minutes podcatchers should wait
@@ -83,42 +110,59 @@ Using traditional inheritance
83110 :type: :obj:`int`
84111 :RSS: ttl
85112 """
113+ # By using @property and @ttl.setter, we encapsulate the ttl field
114+ # so that we can check the value that is assigned to it.
115+ # If you don't need this, you could just rename self.__ttl to
116+ # self.ttl and remove those two methods.
86117 return self.__ttl
87118
88119 @ttl.setter
89120 def ttl(self, ttl):
121+ # Try to convert to int
90122 try:
91123 ttl_int = int(ttl)
92124 except ValueError:
93125 raise TypeError("ttl expects an integer, got %s" % ttl)
94-
126+ # Is this negative?
95127 if ttl_int < 0:
96128 raise ValueError("Negative ttl values aren't accepted, got %s"
97129 % ttl_int)
130+ # All checks passed
98131 self.__ttl = ttl_int
99132
100133 def _create_rss(self):
134+ # Let Podcast generate the lxml etree (adding the standard elements)
101135 rss = super()._create_rss()
136+ # We must get the channel element, since we want to add subelements
137+ # to it.
102138 channel = rss.find("channel")
139+ # Only add the ttl element if it has been populated.
103140 if self.__ttl is not None:
141+ # First create our new subelement of channel.
104142 ttl = etree.SubElement(channel, 'ttl')
143+ # If we were to use another namespace, we would instead do this:
144+ # ttl = etree.SubElement(channel,
145+ # '{%s}ttl' % self._nsmap['prefix'])
146+
147+ # Then, fill it with the ttl value
105148 ttl.text = str(self.__ttl)
106149
150+ # Return the new etree, now with ttl
107151 return rss
108152
109153 # How to use the new class (normally, you would put this somewhere else)
110154 myPodcast = PodcastWithTtl(name="Test", website="http://example.org",
111155 explicit=False, description="Testing ttl")
112- myPodcast.ttl = 90
156+ myPodcast.ttl = 90 # or set ttl=90 in the constructor
113157 print(myPodcast)
114158
115159
116160Using mixins
117161^^^^^^^^^^^^
118162
119- To use mixins, you cannot make the class with the ttl functionality inherit
120- Podcast. Instead, it must inherit nothing. Other than that, the code will be
121- the same, so it doesn't make sense to repeat it here.
163+ To use mixins, you cannot make the class with the `` ttl `` functionality inherit
164+ :class: ` . Podcast` . Instead, it must inherit nothing. Other than that, the code
165+ will be the same, so it doesn't make sense to repeat it here.
122166
123167::
124168
@@ -137,28 +181,29 @@ the same, so it doesn't make sense to repeat it here.
137181
138182Note the order of the mixins in the class declaration. You should read it as
139183the path Python takes when looking for a method. First Python checks
140- PodcastWithTtl, then TtlMixin, finally Podcast. This is also the order the
141- methods are called when chained together using super(). If you had Podcast
142- first, Podcast's _create_rss() method would be run first, and since it never
143- calls super()._create_rss(), the TtlMixin's _create_rss would never be run.
144- Therefore, you should always have Podcast last in that list.
184+ ``PodcastWithTtl ``, then ``TtlMixin `` and finally :class: `.Podcast `. This is
185+ also the order the methods are called when chained together using :func: `super `.
186+ If you had Podcast first, :meth: `.Podcast._create_rss ` method would be run
187+ first, and since it never calls ``super()._create_rss() ``, the ``TtlMixin ``'s
188+ ``_create_rss `` would never be run. Therefore, you should always have
189+ :class: `.Podcast ` last in that list.
145190
146191Which approach is best?
147192^^^^^^^^^^^^^^^^^^^^^^^
148193
149194The advantage of mixins isn't really displayed here, but it will become
150195apparent as you add more and more extensions. Say you define 5 different mixins,
151- which all add exactly one more attribute to Podcast. If you used traditional
196+ which all add exactly one more element to :class: ` . Podcast` . If you used traditional
152197inheritance, you would have to make sure each of those 5 subclasses made up a
153- tree. That is, class 1 would inherit Podcast. Class 2 would have to inherit
198+ tree. That is, class 1 would inherit :class: ` . Podcast` . Class 2 would have to inherit
154199class 1, class 3 would have to inherit class 2 and so on. If two of the classes
155- had the same superclass, you would be screwed.
200+ had the same superclass, you could get screwed.
156201
157202By using mixins, you can put them together however you want. Perhaps for one
158- podcast you only need ttl, while for another podcast you want to use the
159- textInput element in addition to ttl, and another podcast requires the
160- textInput element together with the comments element. Using traditional
161- inheritance, you would have to duplicate code for textInput in two classes. Not
203+ podcast you only need `` ttl `` , while for another podcast you want to use the
204+ `` textInput `` element in addition to `` ttl `` , and another podcast requires the
205+ `` textInput `` element together with the `` comments `` element. Using traditional
206+ inheritance, you would have to duplicate code for `` textInput `` in two classes. Not
162207so with mixins::
163208
164209 class PodcastWithTtl(TtlMixin, Podcast):
@@ -174,6 +219,6 @@ so with mixins::
174219 def __init__(*args, **kwargs):
175220 super().__init__(*args, **kwargs)
176221
177- If the list of attributes you want to use varies between different podcasts,
222+ If the list of elements you want to use varies between different podcasts,
178223mixins are the way to go. On the other hand, mixins are overkill if you are okay
179- with one giant class with all the attributes you need.
224+ with one giant class with all the elements you need.
0 commit comments