From 30ae4d54fd14911677da3381099916a7f92c7183 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Sat, 29 Dec 2007 23:37:54 -0500 Subject: [PATCH] Better handling of ItemCount/AnchorItem --- plugin.py | 95 ++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 55 insertions(+), 40 deletions(-) diff --git a/plugin.py b/plugin.py index 46c941f..127834d 100644 --- a/plugin.py +++ b/plugin.py @@ -52,6 +52,58 @@ class Plugin(object): path = os.path.join(path, folder) return path + def item_count(self, handler, query, cname, files): + """Return only the desired portion of the list, as specified by + ItemCount, AnchorItem and AnchorOffset + """ + totalFiles = len(files) + index = 0 + + if query.has_key('ItemCount'): + count = int(query['ItemCount'][0]) + + if query.has_key('AnchorItem'): + bs = '/TiVoConnect?Command=QueryContainer&Container=' + local_base_path = self.get_local_base_path(handler, query) + + anchor = query['AnchorItem'][0] + if anchor.startswith(bs): + anchor = anchor.replace(bs, '/') + anchor = unquote(anchor) + anchor = anchor.replace(os.path.sep + cname, local_base_path) + anchor = os.path.normpath(anchor) + + try: + index = files.index(anchor) + except ValueError: + print 'Anchor not found:', anchor # just use index = 0 + + if count > 0: + index += 1 + + if query.has_key('AnchorOffset'): + index += int(query['AnchorOffset'][0]) + + #foward count + if count > 0: + files = files[index:index + count] + #backwards count + elif count < 0: + if index + count < 0: + count = -index + files = files[index + count:index] + index += count + + else: # No AnchorItem + + if count >= 0: + files = files[:count] + else: + index = count % len(files) + files = files[count:] + + return files, totalFiles, index + def get_files(self, handler, query, filterFunction=None): def build_recursive_list(path, recurse=True): @@ -68,7 +120,7 @@ class Plugin(object): subcname = query['Container'][0] cname = subcname.split('/')[0] path = self.get_local_path(handler, query) - + file_type = query.get('Filter', [''])[0] recurse = query.get('Recurse',['No'])[0] == 'Yes' @@ -115,43 +167,6 @@ class Plugin(object): self.random_lock.release() else: files.sort(dir_sort) - - local_base_path = self.get_local_base_path(handler, query) - index = 0 - count = 10 - if query.has_key('ItemCount'): - count = int(query['ItemCount'] [0]) - - if query.has_key('AnchorItem'): - anchor = unquote(query['AnchorItem'][0]) - for file, i in zip(files, range(len(files))): - file_name = file.replace(local_base_path, '') - - if os.path.isdir(os.path.join(file)): - file_url = '/TiVoConnect?Command=QueryContainer&Container=' + cname + file_name - else: - file_url = '/' + cname + file_name - file_url = file_url.replace('\\', '/') - - if file_url == anchor: - if count > 0: - index = i + 1 - else: - index = i - break - - if query.has_key('AnchorOffset'): - index = index + int(query['AnchorOffset'][0]) - - #foward count - if index < index + count: - files = files[index:index + count ] - return files, totalFiles, index - #backwards count - else: - #off the start of the list - if index + count < 0: - index += 0 - (index + count) - files = files[index + count:index] - return files, totalFiles, index + count + # Trim the list + return self.item_count(handler, query, cname, files) -- 2.11.4.GIT