diff -r 0daabcdc0340 -r 03c7068bdbb8 quoted.py --- a/quoted.py Tue Nov 10 10:39:47 2009 -0800 +++ b/quoted.py Sun May 23 09:56:53 2010 -0700 @@ -40,6 +40,9 @@ For values that are used as arguments to the % operator, this allows str(value) and repr(value), if called as part of the formatting, to produce quoted results. + + Supports positional and named arguments, including attribute access, + provided by the string format() method introduced in Python 3.0 and 2.6. """ __slots__ = ['value'] @@ -55,6 +58,12 @@ def __getitem__(self, key): return _quote_wrap(self.value[key]) + def __getattr__(self, name): + return _quote_wrap(getattr(self.value, name)) + + def keys(self): + return self.value.keys() + def _quote_wrap(x): """(x) -> _xml_quote_wrapper | x Not for outside code. @@ -73,7 +82,7 @@ """ __slots__ = [] - def __new__(klass, string=None, encoding=sys.getdefaultencoding(), + def __new__(klass, string=None, encoding=sys.getdefaultencoding(), errors='strict'): if string is None: return xml('') @@ -119,6 +128,19 @@ """ return xml(unicode_str.join(self, (xml_quote(item) for item in items))) + def __html__(self): + """ + Returns self, the xml instance. Included for compatibility with + other templating systems and frameworks which have adopted the __html__ + method marker as a sign to quote no more. + """ + return self + + def format(self, *args, **kwargs): + quoted_args = tuple(_quote_wrap(arg) for arg in args) + quoted_kwargs = _xml_quote_wrapper(kwargs) + return xml(unicode_str.format(self, *quoted_args, **quoted_kwargs)) + join_xml = xml().join xml.quote = staticmethod(xml_quote)