Changeset 30ae4d54fd14911677da3381099916a7f92c7183

Show
Ignore:
Timestamp:
12/29/07 22:37:54 (11 months ago)
Author:
William McBrine <wmcbrine@gmail.com>
git-committer:
William McBrine <wmcbrine@gmail.com> 1198989474 -0500
git-parent:

[28f9312f5a9ff8e40ff53d9fd6c1c1e15b29ebb7]

git-author:
William McBrine <wmcbrine@gmail.com> 1198989474 -0500
Message:

Better handling of ItemCount/AnchorItem?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugin.py

    r28f9312 r30ae4d5  
    5353        return path 
    5454 
     55    def item_count(self, handler, query, cname, files): 
     56        """Return only the desired portion of the list, as specified by  
     57           ItemCount, AnchorItem and AnchorOffset 
     58        """ 
     59        totalFiles = len(files) 
     60        index = 0 
     61 
     62        if query.has_key('ItemCount'): 
     63            count = int(query['ItemCount'][0]) 
     64 
     65            if query.has_key('AnchorItem'): 
     66                bs = '/TiVoConnect?Command=QueryContainer&Container=' 
     67                local_base_path = self.get_local_base_path(handler, query) 
     68 
     69                anchor = query['AnchorItem'][0] 
     70                if anchor.startswith(bs): 
     71                    anchor = anchor.replace(bs, '/') 
     72                anchor = unquote(anchor) 
     73                anchor = anchor.replace(os.path.sep + cname, local_base_path) 
     74                anchor = os.path.normpath(anchor) 
     75 
     76                try: 
     77                    index = files.index(anchor) 
     78                except ValueError: 
     79                    print 'Anchor not found:', anchor  # just use index = 0 
     80 
     81                if count > 0: 
     82                    index += 1 
     83 
     84                if query.has_key('AnchorOffset'): 
     85                    index += int(query['AnchorOffset'][0]) 
     86 
     87                #foward count 
     88                if count > 0: 
     89                    files = files[index:index + count] 
     90                #backwards count 
     91                elif count < 0: 
     92                    if index + count < 0: 
     93                        count = -index 
     94                    files = files[index + count:index] 
     95                    index += count 
     96 
     97            else:  # No AnchorItem 
     98 
     99                if count >= 0: 
     100                    files = files[:count] 
     101                else: 
     102                    index = count % len(files) 
     103                    files = files[count:] 
     104 
     105        return files, totalFiles, index 
     106 
    55107    def get_files(self, handler, query, filterFunction=None): 
    56108 
     
    69121        cname = subcname.split('/')[0] 
    70122        path = self.get_local_path(handler, query) 
    71          
     123 
    72124        file_type = query.get('Filter', [''])[0] 
    73125 
     
    116168        else: 
    117169            files.sort(dir_sort) 
    118          
    119         local_base_path = self.get_local_base_path(handler, query) 
    120170 
    121         index = 0 
    122         count = 10 
    123         if query.has_key('ItemCount'): 
    124             count = int(query['ItemCount'] [0]) 
    125              
    126         if query.has_key('AnchorItem'): 
    127             anchor = unquote(query['AnchorItem'][0]) 
    128             for file, i in zip(files, range(len(files))): 
    129                 file_name = file.replace(local_base_path, '') 
    130  
    131                 if os.path.isdir(os.path.join(file)): 
    132                     file_url = '/TiVoConnect?Command=QueryContainer&Container=' + cname + file_name 
    133                 else:                                 
    134                     file_url = '/' + cname + file_name 
    135                 file_url = file_url.replace('\\', '/') 
    136  
    137                 if file_url == anchor: 
    138                     if count > 0: 
    139                         index = i + 1 
    140                     else: 
    141                         index = i 
    142                     break 
    143  
    144             if query.has_key('AnchorOffset'): 
    145                 index = index +  int(query['AnchorOffset'][0]) 
    146  
    147         #foward count 
    148         if index < index + count: 
    149             files = files[index:index + count ] 
    150             return files, totalFiles, index 
    151         #backwards count 
    152         else: 
    153             #off the start of the list 
    154             if index + count < 0: 
    155                 index += 0 - (index + count) 
    156             files = files[index + count:index] 
    157             return files, totalFiles, index + count 
     171        # Trim the list 
     172        return self.item_count(handler, query, cname, files)