[XML-SIG] Interested in feedback

Benjamin Saller case@appliedtheory.com
Mon, 26 Jun 2000 23:30:58 -0400 (EDT)


  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.
  Send mail to mime@docserver.cac.washington.edu for more info.

--2144283543-236838159-962076658=:25151
Content-Type: TEXT/PLAIN; charset=US-ASCII


I am not really sure how to prefix this. I have something that is while
not general purpose does seem simple to use in the 80% case. If people
disagree, or can think of things that might make this better I would like
some feedback.

This module has a simple interface and is just over 200 lines.

import xmlObjects

p   = xmlObjects.Parser()
xml = p.parse("filename")  # where maybe this should be a string or fp

xml.getValue("container1.allow.host")     # == 'loki'
xml.getValue("container2.allow.host[1]")  # == 'foo.bar.com'
xml.getValue("listen[port]", convert=int) # == 9000

xml.getValues("container2.allow.host") # == ['loki...', 'foo...' 'baz...']

xml.getXML() # does a decent job of reproducing the source

for the data set

<container>
	<listen port="9000"/>
         <container1>
		<allow>
			<host>loki.appliedtheory.com</host>
		</allow>
	</container1>
	<container2>
		<allow>
			<host>127.0.0.1</host>
			<host>foo.bar.com</host>
			<host>baz.bar.com</host>
		</allow>
	</container2>
	<container3>
		<float>100.4123</float>
		<numbers>
			<int>8</int>
			<int>16</int>
			<int>32</int>
		</numbers>
	</container3>
</container>

-- 
Benjamin Saller                                     <case@appliedtheory.com>
Technical Strategist                                AppliedTheory
	Where tire hits pavement on the Information super-highway,	
			 that's where my head is...

--2144283543-236838159-962076658=:25151
Content-Type: TEXT/PLAIN; charset=US-ASCII; name="xmlObjects.py"
Content-Transfer-Encoding: BASE64
Content-ID: <Pine.LNX.4.21.0006262330580.25151@loki.appliedtheory.com>
Content-Description: 
Content-Disposition: attachment; filename="xmlObjects.py"

IyEvdXNyL2Jpbi9weXRob24NCiMNCiMgIENvcHlyaWdodCAoQykgMTk5OS0y
MDAwICBCZW5qYW1pbiBTYWxsZXIgYW5kIEFwcGxpZWRUaGVvcnkNCiMgICAg
ICAgICAgICAgICAgICAgICAgICAgICBhbmQ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KSkNCiAgICAgICAgaWYg
bGVuKGF0dHJzKToNCiAgICAgICAgICAgIHJldHVybiAiICVzIiAlIChzdHJp
bmcuam9pbihhdHRycywgJyAnKSkNCiAgICAgICAgZWxzZToNCiAgICAgICAg
ICAgIHJldHVybiAnJw0KDQogICAgZGVmIF93cml0ZUVuZChzZWxmKToNCiAg
ICAgICAgcmV0dXJuICI8LyVzPiIgJSBzZWxmLl9uYW1lDQoNCiAgICBkZWYg
X3dyaXRlVGV4dChzZWxmKToNCiAgICAgICAgcmV0dXJuIHNlbGYuX2dldERh
dGEoKQ0KDQogICAgZGVmIHhtbChzZWxmLCBpbmRlbnQ9MCk6DQogICAgICAg
IHRhYnMgPSAnICAgICcgKiBpbmRlbnQNCiAgICAgICAgDQogICAgICAgIGlm
IGxlbihzZWxmLl9jaGlsZHJlbik6DQogICAgICAgICAgICByZXN1bHRzID0g
WyIlcyVzJXMiICUgKHRhYnMsIHNlbGYuX3dyaXRlU3RhcnQoKSwNCiAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgc2VsZi5fd3JpdGVUZXh0
KCkpXQ0KICAgICAgICAgICAgZm9yIGssdiBpbiBzZWxmLl9jaGlsZHJlbi5p
dGVtcygpOg0KICAgICAgICAgICAgICAgIGZvciBkYXR1bSBpbiB2Og0KICAg
ICAgICAgICAgICAgICAgICByZXN1bHRzLmFwcGVuZChkYXR1bS54bWwoaW5k
ZW50ICsgMSkpDQogICAgICAgICAgICAgICAgICAgIA0KICAgICAgICAgICAg
cmVzdWx0cy5hcHBlbmQoIiVzJXMiICUgKHRhYnMsIHNlbGYuX3dyaXRlRW5k
KCkpKQ0KICAgICAgICAgICAgICAgICAgICAgICAgICAgDQogICAgICAgIGVs
c2U6DQogICAgICAgICAgICAjIE5vIGNoaWxkcmVuDQogICAgICAgICAgICB0
ZXh0ID0gc2VsZi5fd3JpdGVUZXh0KCkNCiAgICAgICAgICAgIGlmIGxlbih0
ZXh0KToNCiAgICAgICAgICAgICAgICByZXN1bHRzID0gWyIlcyVzJXMiICUg
KHRhYnMsIHNlbGYuX3dyaXRlU3RhcnQoKSwNCiAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgIHNlbGYuX3dyaXRlVGV4dCgpKV0NCiAg
ICAgICAgICAgIGVsc2U6DQogICAgICAgICAgICAgICAgcmVzdWx0cyA9IFsi
JXMlcyIgICAlICh0YWJzLCBzZWxmLl93cml0ZVN0YXJ0KDEpKV0NCg0KICAg
ICAgICByZXR1cm4gc3RyaW5nLmpvaW4ocmVzdWx0cywgJ1xuJykNCiAgICAg
ICAgICAgICAgICAgICAgICAgDQogICAgZGVmIGdldFhNTChzZWxmKToNCiAg
ICAgICAgcmV0dXJuIHNlbGYueG1sKCkNCiAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgDQogICAgICAgIA0KICAgICAgICANCmNsYXNzIHht
bE9iamVjdCh4bWxPYmplY3RXcml0ZXJNaXhJbik6DQogICAgZGVmIF9faW5p
dF9fKHNlbGYsIG5hbWUsIGF0dHJzPU5vbmUpOg0KICAgICAgICBzZWxmLl9j
aGlsZHJlbiA9IHt9DQogICAgICAgIHNlbGYuX2RhdGEgICAgID0gW10NCiAg
ICAgICAgc2VsZi5fbmFtZSAgICAgPSBuYW1lDQogICAgICAgIA0KICAgICAg
ICBzZWxmLl9hdHRycyAgICA9IHt9DQogICAgICAgIGlmIGF0dHJzOiAgICAg
ICAgc2VsZi5fYXR0cnMudXBkYXRlKGF0dHJzKQ0KDQogICAgZGVmIF9hZGRD
aGlsZChzZWxmLCBvYmplY3QpOg0KICAgICAgICB0cnk6DQogICAgICAgICAg
ICBzZWxmLl9jaGlsZHJlbltvYmplY3QuX25hbWVdLmFwcGVuZChvYmplY3Qp
DQogICAgICAgIGV4Y2VwdCBLZXlFcnJvcjoNCiAgICAgICAgICAgIHNlbGYu
X2NoaWxkcmVuW29iamVjdC5fbmFtZV0gPSBbb2JqZWN0XQ0KDQogICAgZGVm
IF9hZGR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wYXRoLCAnLicpDQogICAgICAgIG9iamVjdCA9
IHNlbGYNCg0KICAgICAgICBsYXN0ICAgPSBsZW4ocGFydHMpDQogICAgICAg
IGkgICAgICA9IDANCg0KICAgICAgICBmb3IgcGFydCBpbiBwYXJ0czoNCiAg
ICAgICAgICAgIGkgPSBpICsgMQ0KICAgICAgICAgICAgaXNMYXN0ID0gaSA9
PSBsYXN0DQogICAgICAgICAgICANCiAgICAgICAgICAgIG9iamVjdCwgYXR0
ciA9IG9iamVjdC5fZ2V0UGFydChwYXJ0KQ0KICAgICAgICAgICAgaWYgbm90
IG9iamVjdDoNCiAgICAgICAgICAgICAgICByZXR1cm4gTm9uZQ0KDQogICAg
ICAgICAgICBpZiBub3QgaXNMYXN0IGFuZCB0eXBlKG9iamVjdCkgPT0gTGlz
dFR5cGU6DQogICAgICAgICAgICAgICAgb2JqZWN0ID0gb2JqZWN0WzBdDQoN
CiAgICAgICAgaWYgdHlwZShvYmplY3QpICE9IExpc3RUeXBlOg0KICAgICAg
ICAgICAgb2JqZWN0ID0gW29iamVjdF0NCiAgICAgICAgDQogICAgICAgIHJl
dHVybiBvYmplY3QsIGF0dHINCiAgICANCiAgICAgICAgICAgIA0KICAgIGRl
ZiBnZXRPYmplY3RzKHNlbGYsIHBhdGgpOg0KICAgICAgICBvYmplY3RzLCBh
dHRyID0gc2VsZi5fZ2V0UGF0aChwYXRoKQ0KICAgICAgICAjcHJpbnQgImdl
dE9iamVjdHMiLCBwYXRoLCBvYmplY3RzLCBhdHRyDQogICAgICAgIHJldHVy
biBvYmplY3RzLCBhdHRyDQoNCg0KICAgIGRlZiBnZXRPYmplY3Qoc2VsZiwg
cGF0aCk6DQogICAgICAgIG9iamVjdHMsIGF0dHIgPSBzZWxmLmdldE9iamVj
dHMocGF0aCkNCiAgICAgICAgcmV0dXJuIG9iamVjdHNbMF0sIGF0dHINCiAg
ICANCiAgICBkZWYgZ2V0VmFsdWUoc2VsZiwgcGF0aCwgZmFpbD1Ob25lLCBj
b252ZXJ0PU5vbmUpOg0KICAgICAgICBvYmplY3QgPSBzZWxmLmdldFZhbHVl
cyhwYXRoLCBmYWlsLCBjb252ZXJ0KQ0KICAgICAgICBpZiB0eXBlKG9iamVj
dCkgPT0gTGlzdFR5cGU6DQogICAgICAgICAgICByZXR1cm4gb2JqZWN0WzBd
DQogICAgICAgIGVsc2U6DQogICAgICAgICAgICByZXR1cm4gb2JqZWN0ICNm
YWlsdmFsDQogICAgICAgIA0KICAgIGRlZiBnZXRWYWx1ZXMoc2VsZiwgcGF0
aCwgZmFpbD1Ob25lLCBjb252ZXJ0PU5vbmUpOg0KICAgICAgICByZXN1bHRz
ID0gW10NCiAgICAgICAgb2JqZWN0LCBhdHRyID0gc2VsZi5nZXRPYmplY3Rz
KHBhdGgpDQoNCiAgICAgICAgaWYgbm90IG9iamVjdDoNCiAgICAgICAgICAg
IHJldHVybiBmYWlsDQoNCiAgICAgICAgaWYgYXR0cjoNCiAgICAgICAgICAg
IGZvciBvIGluIG9iamVjdDoNCiAgICAgICAgICAgICAgICByZXN1bHRzLmFw
cGVuZChvLl9nZXRBdHRyKGF0dHIpKQ0KICAgICAgICBlbHNlOg0KICAgICAg
ICAgICAgI3ByaW50ICJBYm91dCB0byBpdG9yIG9uIiwgb2JqZWN0LCAiZm9y
IiwgcGF0aA0KICAgICAgICAgICAgZm9yIG8gaW4gb2JqZWN0Og0KICAgICAg
ICAgICAgICAgIHJlc3VsdHMuYXBwZW5kKG8uX2dldERhdGEoKSkNCg0KICAg
ICAgICBpZiBjb252ZXJ0Og0KICAgICAgICAgICAgcmVzdWx0cyA9IG1hcChj
b252ZXJ0LCByZXN1bHRzKQ0KDQogICAgICAgIHJldHVybiByZXN1bHRzDQog
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgDQogICAgDQpjbGFzcyB4
bWxIYW5kbGVyKHNheGxpYi5Eb2N1bWVudEhhbmRsZXIpOg0KICAgIGRlZiBf
X2luaXRfXyhzZWxmKToNCiAgICAgICAgc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