xmllib default value question

Sjoerd Mullender sjoerd at oratrix.nl
Fri Mar 24 07:03:15 EST 2000


Which version of xmllib are you using?  The current CVS version
doesn't use this method of specifying default values anymore.  The
change was made because XML identifiers are not Python identifiers.

The way to do this using the current version (and also the one from at
least 1.5.2) is the following:

class ConfigParser(xmllib.XMLParser):

   attributes = {
	   'network': {'default': 'no'},
	   'access': {'order': 'allow deny'},
	   'allow': {'host': '*', 'port': '*'},
	   'deny': {'host': '*', 'port': '*'},
	   }

   def start_allow(self, attr):
	self.value = (attr['host'], attr['port'])

You can also use the instance variable "elements" to specify the
methods to handle the element start and end tags.  You'd probably have
to specify those in an __init__ method:

   def __init__(self):
	xmllib.XMLParser.__init__(self)
	self.elements = {
	     'allow': (self.start_allow, None),
	     }

The tuple contains start and end tag methods (None if no special
handling is required).

On Thu, Mar 23 2000 Jp Calderone wrote:

> I'm using the xmllib toread in configuration files, but I don't 
> quite understand how default values are supposed to be accessed.
> I've created the tag_<attribute> dictionaries, and I'm pretty
> sure the XML is correct, but I keep getting KeyErrors when I
> try to look up the name of the value in the attribute dictionary.
> Here's the basic code (and xml):
> 
> 
> 
> class ConfigParser(xmllib.XMLParser):
> 
>    tag_network = {'default': 'no'}
>    tag_access = {'order': 'allow deny'}
>    tag_allow = {'host': '*', 'port': '*'}
>    tag_deny = {'host': '*', 'port': '*'}
> 
>    def start_allow(self, attr):
> 	self.value = (attr['host'], attr['port'])
> 
> 
> <!DOCTYPE interface [
> 	...
>    <!ELEMENT allow #EMPTY>
>    <!ATTLIST allow
>                host CDATA "*"
>                port CDATA "*"  
>    >
> 	...
> ]>
> 
> <interface>
> 	<allow host="somehost.com"/>
> </interface>
> 
>  As far as I can tell, this should invoke the start_allow()
> method with a dictionary something like 
> {'host': 'somehost.com', 'port': '*'} but the single line in
> start_allow raises a KeyError on port.  Anyone know why/how
> to fix this?
> 
>  Thanks in advance, jp
> 
> -- 
> Where a calculator on the ENIAC is equipped with 18,000 vacuum tubes and
> weighs 30 tons, computers in the future may have only 1,000 vacuum tubes
> and
> weigh only 1.5 tons.    -- Popular Mechanics, March 1949
> --
>  5:26pm up 16 days, 23:55, 3 users, load average: 0.14, 0.06, 0.01

-- Sjoerd Mullender <sjoerd.mullender at oratrix.com>




More information about the Python-list mailing list