Browse Source

fix up elementtree import, add some minor tests and fix updating..

main
John-Mark Gurney 3 years ago
parent
commit
8e020729e0
2 changed files with 55 additions and 7 deletions
  1. +54
    -6
      DIDLLite.py
  2. +1
    -1
      et.py

+ 54
- 6
DIDLLite.py View File

@@ -4,10 +4,15 @@
# Copyright 2005, Tim Potter <tpot@samba.org>
# Copyright 2006-2009 John-Mark Gurney <jmg@funkthat.com>

__version__ = '$Change$'
# $Id$
__version__ = '$Change: 1665 $'
# $Id: //depot/python/pymeds/main/DIDLLite.py#32 $

from elementtree.ElementTree import Element, SubElement, tostring, _ElementInterface
import itertools
import unittest

import et
for i in [ 'Element', 'SubElement', 'tostring', '_ElementInterface' ]:
locals()[i] = getattr(et.ET, i)

class Resource(object):
"""An object representing a resource."""
@@ -356,8 +361,8 @@ class Container(Object, list):
return ((x.id, x.title) for x in self)

def genChildren(self):
'''This function returns a list or dict of names for new
children.'''
'''This function returns a list or dict of names of all
the current children.'''

raise NotImplementedError

@@ -378,6 +383,7 @@ class Container(Object, list):
self.doingUpdate = True
self.needcontupdate = False

# Get the current children
children = self.genChildren()
if isinstance(children, dict):
oldchildren = self.oldchildren
@@ -387,22 +393,33 @@ class Container(Object, list):
children = set(children)
isdict = False

# Delete the old object that no longer exists.
# Make a mapping of current names to ids.
names = {}
#print 'i:', `self`, `self.genCurrent`, `self.__class__`
print 'i:', `self`, `self.genCurrent`, `self.__class__`
for id, i in tuple(self.genCurrent()):
if i not in children:
didupdate = True
# delete
print 'del:', `id`, `i`
self.cd.delItem(id)
self.needcontupdate = True
else:
names[i] = id

# Make sure that the existing objects don't need to be
# updated.
# Create any new objects that don't currently exist.
for i in children:
if i in names:
if isdict:
print 'oc:', `oldchildren[i]`, `children[i]`
if oldchildren[i] == children[i]:
continue

# Delete the old and recreate
self.cd.delItem(names[i])
self.needcontupdate = True
else:
# XXX - some sort of comparision?
continue
@@ -473,6 +490,37 @@ class Container(Object, list):
(cls.__module__, cls.__name__, self.id, self.parentID,
self.title, len(self))

class TestContainerObj(Container):
def genChildren(self):
return self._genchildren

def createObject(self, name):
return Object, name, (), {}

class MockContainer(object):
def __init__(self):
self.itemiter = itertools.count(1)

def addItem(self, *args, **kwargs):
return self.itemiter.next()

def __getitem__(self, id):
return Container(None, '0', None, None)

class TestContainer(unittest.TestCase):
def xtest_container(self):
cont = MockContainer()

c = TestContainerObj(cont, None, None, None)

self.assertEqual(len(tuple(c.genCurrent())), 0)

c._genchildren = [ 'objb', 'obja' ]

c.doUpdate()

self.assertEqual(tuple(c.genCurrent()), ((1, 'obja'), (2, 'objb')))

class Person(Container):
klass = Container.klass + '.person'



+ 1
- 1
et.py View File

@@ -21,7 +21,7 @@ except ImportError:
except ImportError:
""" this seems to be necessary with the python2.5 on the Maemo platform """
try:
from xml.etree import cElementTree as ET
from xml.etree import ElementTree as ET
from xml import etree as elementtree
except ImportError:
try:


Loading…
Cancel
Save