0001namespaces = {}
0002
0003class Namespace(object):
0004 def __init__(self, uri, prefix=None):
0005 self.uri = uri
0006 self.prefix = prefix
0007 if prefix:
0008 namespaces[uri] = prefix
0009
0010 def qname(self, name):
0011 if self.prefix:
0012 return self.prefix + ':' + name
0013 else:
0014 return name
0015
0016 def clarkname(self, name):
0017 if self.uri:
0018 return '{%s}%s' % (self.uri, name)
0019 else:
0020 return name
0021
0022 __getattr__ = clarkname
0023 __getitem__ = clarkname
0024
0025 def __str__(self):
0026 return self.uri
0027 def __unicode__(self):
0028 return unicode(self.uri)
0029 def __repr__(self):
0030 return 'Namespace(%r, %r)' % (self.uri, self.prefix)
0031 def __equals__(self, other):
0032 if isinstance(other, (str, unicode)):
0033 return self.uri == other
0034 elif isinstance(other, Namespace):
0035 return self.uri == other.uri
0036 else:
0037 return 0
0038
0039xml = Namespace('http://www.w3.org/XML/1998/namespace', 'xml')
0040xhtml = Namespace('http://www.w3.org/1999/xhtml', 'html')
0041atom = Namespace('http://purl.org/atom/ns#', 'atom')
0042rdf = Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdf')
0043rss = Namespace('http://purl.org/rss/1.0/', 'rss')
0044nons = Namespace(None, None)
0045
0046__all__ = ['xml', 'xhtml', 'atom', 'rdf', 'rss', 'nons', 'Namespace']