Kaydet (Commit) 10263d6e authored tarafından Just van Rossum's avatar Just van Rossum

- cleaned up example/test code

- don't encode/escape elements
- fixed typo in doc string
- provide our own copy function for the Dict class
üst 2db92a6e
...@@ -34,19 +34,19 @@ The <date> plist data has (limited) support through the Date class. ...@@ -34,19 +34,19 @@ The <date> plist data has (limited) support through the Date class.
Generate Plist example: Generate Plist example:
pl = Plist( pl = Plist(
Foo="Doodah", aString="Doodah",
aList=["A", "B", 12, 32.1, [1, 2, 3]], aList=["A", "B", 12, 32.1, [1, 2, 3]],
aFloat = 0.1, aFloat = 0.1,
anInt = 728, anInt = 728,
aDict=Dict( aDict=Dict(
aString="<hello & hi there!>", anotherString="<hello & hi there!>",
SomeUnicodeValue=u'M\xe4ssig, Ma\xdf', aUnicodeValue=u'M\xe4ssig, Ma\xdf',
someTrueValue=True, aTrueValue=True,
someFalseValue=False, aFalseValue=False,
), ),
someData = Data("hello there!"), someData = Data("<binary gunk>"),
someMoreData = Data("hello there! " * 10), someMoreData = Data("<lots of binary gunk>" * 10),
aDate = Date(time.mktime(time.gmtime())), aDate = Date(time.mktime(time.gmtime())),
) )
# unicode keys are possible, but a little awkward to use: # unicode keys are possible, but a little awkward to use:
pl[u'\xc5benraa'] = "That was a unicode key." pl[u'\xc5benraa'] = "That was a unicode key."
...@@ -78,7 +78,6 @@ class DumbXMLWriter: ...@@ -78,7 +78,6 @@ class DumbXMLWriter:
def beginElement(self, element): def beginElement(self, element):
self.stack.append(element) self.stack.append(element)
element = _encode(element)
self.writeln("<%s>" % element) self.writeln("<%s>" % element)
self.indentLevel += 1 self.indentLevel += 1
...@@ -90,7 +89,6 @@ class DumbXMLWriter: ...@@ -90,7 +89,6 @@ class DumbXMLWriter:
def simpleElement(self, element, value=None): def simpleElement(self, element, value=None):
if value: if value:
element = _encode(element)
value = _encode(value) value = _encode(value)
self.writeln("<%s>%s</%s>" % (element, value, element)) self.writeln("<%s>%s</%s>" % (element, value, element))
else: else:
...@@ -172,7 +170,7 @@ class PlistWriter(DumbXMLWriter): ...@@ -172,7 +170,7 @@ class PlistWriter(DumbXMLWriter):
class Dict: class Dict:
"""Dict wrapper for convenient acces of values through attributes.""" """Dict wrapper for convenient access of values through attributes."""
def __init__(self, **args): def __init__(self, **args):
self.__dict__.update(args) self.__dict__.update(args)
...@@ -189,6 +187,9 @@ class Dict: ...@@ -189,6 +187,9 @@ class Dict:
return "%s(**%s)" % (self.__class__.__name__, self.__dict__) return "%s(**%s)" % (self.__class__.__name__, self.__dict__)
__repr__ = __str__ __repr__ = __str__
def copy(self):
return self.__class__(**self.__dict__)
def __getattr__(self, attr): def __getattr__(self, attr):
"""Delegate everything else to the dict object.""" """Delegate everything else to the dict object."""
return getattr(self.__dict__, attr) return getattr(self.__dict__, attr)
...@@ -400,19 +401,19 @@ if __name__ == "__main__": ...@@ -400,19 +401,19 @@ if __name__ == "__main__":
import time import time
if len(sys.argv) == 1: if len(sys.argv) == 1:
pl = Plist( pl = Plist(
Foo="Doodah", aString="Doodah",
aList=["A", "B", 12, 32.1, [1, 2, 3]], aList=["A", "B", 12, 32.1, [1, 2, 3]],
aFloat = 0.1, aFloat = 0.1,
anInt = 728, anInt = 728,
aDict=Dict( aDict=Dict(
aString="<hello & hi there!>", anotherString="<hello & hi there!>",
SomeUnicodeValue=u'M\xe4ssig, Ma\xdf', aUnicodeValue=u'M\xe4ssig, Ma\xdf',
someTrueValue=True, aTrueValue=True,
someFalseValue=False, aFalseValue=False,
), ),
someData = Data("hello there!"), someData = Data("<binary gunk>"),
someMoreData = Data("hello there! " * 10), someMoreData = Data("<lots of binary gunk>" * 10),
aDate = Date(time.mktime(time.gmtime())), aDate = Date(time.mktime(time.gmtime())),
) )
elif len(sys.argv) == 2: elif len(sys.argv) == 2:
pl = Plist.fromFile(sys.argv[1]) pl = Plist.fromFile(sys.argv[1])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment