Browse Source

support formating attributes.. make sure they result in strings...

add method for formating the duration... convert from int/float to
the string..

make sorting a bit smarter...

[git-p4: depot-paths = "//depot/": change = 1377]
main
John-Mark Gurney 15 years ago
parent
commit
8487cd4765
1 changed files with 44 additions and 13 deletions
  1. +44
    -13
      DIDLLite.py

+ 44
- 13
DIDLLite.py View File

@@ -60,10 +60,36 @@ class Resource(object):
root.text = self.data

for i in self.attrs:
root.attrib[self.validattrs[i]] = str(self.attrs[i])
attr = self.validattrs[i]
value = self.attrs[i]
funname = 'format_%s' % attr
if hasattr(self, funname):
value = getattr(self, funname)(value)
else:
value = str(value)
assert isinstance(value, basestring), \
'value is not a string: %s' % `value`
root.attrib[attr] = value

return root

@staticmethod
def format_duration(s):
if isinstance(s, basestring):
return s

# assume it is a number
s = abs(s)
secs = int(s)
frac = s - secs
minutes, secs = divmod(secs, 60)
hours, minutes = divmod(minutes, 60)
if frac:
frac = ('%.2f' % frac)[1:]
else:
frac = ''
return '%d:%02d:%02d%s' % (hours, minutes, secs, frac)

class ResourceList(list):
'''Special class to not overwrite mimetypes that already exist.'''
def __init__(self, *args, **kwargs):
@@ -119,6 +145,8 @@ class Object(object):
self.title)

def checkUpdate(self):
# It's tempting to call doUpdate here, but each object has to
# decide that.
pass

def toElement(self):
@@ -137,10 +165,11 @@ class Object(object):

if self.res is not None:
try:
for res in iter(self.res):
root.append(res.toElement())
except TypeError:
root.append(self.res.toElement())
resiter = iter(self.res)
except TypeError, x:
resiter = [ self.res ]
for res in resiter:
root.append(res.toElement())

if self.writeStatus is not None:
SubElement(root, 'upnp:writeStatus').text = self.writeStatus
@@ -159,9 +188,10 @@ class Item(Object):
refID = None
needupdate = True

def doUpdate(self):
# Update parent container
def doUpdate(self, child=False):
Container.doUpdate(self.cd[self.parentID])
# do NOT update parent container per 2.2.9 for
# ContainerUpdateID changes

def toElement(self):

@@ -325,11 +355,7 @@ class Container(Object, list):

raise NotImplementedError

def sort(self, fun=None):
if fun is None:
return list.sort(self, lambda x, y: cmp(x.title,
y.title))

def sort(self, fun=lambda x, y: cmp(x.title, y.title)):
return list.sort(self, fun)

def doUpdate(self):
@@ -372,7 +398,12 @@ class Container(Object, list):
args = (children[i], )
else:
args = ()
#print 'i:', `i`, `isdict`, `args`, `self`
try:
#print 'i:', `i`, `isdict`, `args`, `self`
pass
except UnicodeEncodeError:
print 'i decode error'

klass, name, args, kwargs = self.createObject(i, *args)
if klass is not None:
self.cd.addItem(self.id, klass, name, *args,


Loading…
Cancel
Save