CoCalc Shared Fileswww / cgi-bin / mfd / XML.pyOpen in CoCalc with one click!
Author: William A. Stein
1
__version__ = "$Revision: 1.2 $"[11:-4]
2
3
from SGML import Markup, Comment
4
from string import lower, join, replace
5
6
class XMLPI(Markup):
7
8
attlist = {}
9
defaults = {'version': '1.0'}
10
attr_translations = {'id': 'ID',
11
'klass': 'class',
12
'label_for': 'for',
13
'http_equiv': 'http-equiv',
14
'accept_charset': 'accept-charset'}
15
16
def __init__(self, **attr):
17
self.dict = {}
18
self.content = ()
19
self.name = 'xml'
20
self.dict.update(self.defaults)
21
self.dict.update(attr)
22
23
def __getitem__(self, k): return self.dict[k]
24
25
def str_attribute(self, k):
26
k2 = self.attr_translations.get(k, k)
27
return '%s="%s"' % \
28
(k2, self.attlist.get(k, 1) and str(self[k]) or k2) \
29
30
def str_attribute_list(self):
31
return join(map(self.str_attribute, self.dict.keys()))
32
33
start_tag_string = "<?%s %s"
34
end_tag_string = " ?>\n"
35
36
def start_tag(self):
37
a = self.str_attribute_list()
38
return self.start_tag_string % (self.name, a)
39
40
def end_tag(self):
41
return self.end_tag_string
42
43