1 import os
, shutil
, random
, threading
, urllib
2 from urlparse
import urlparse
6 unquote
= urllib
.unquote_plus
8 quote
= lambda x
: urllib
.quote(x
.replace(os
.path
.sep
, '/'))
9 unquote
= lambda x
: urllib
.unquote_plus(x
).replace('/', os
.path
.sep
)
12 module_name
= '.'.join(['plugins', name
, name
])
13 module
= __import__(module_name
, globals(), locals(), name
)
14 plugin
= getattr(module
, module
.CLASS_NAME
)()
19 random_lock
= threading
.Lock()
23 def __new__(cls
, *args
, **kwds
):
24 it
= cls
.__dict
__.get('__it__')
27 cls
.__it
__ = it
= object.__new
__(cls
)
28 it
.init(*args
, **kwds
)
34 def send_file(self
, handler
, container
, name
):
35 o
= urlparse("http://fake.host" + handler
.path
)
37 handler
.send_response(200)
39 f
= file(container
['path'] + path
[len(name
) + 1:], 'rb')
40 shutil
.copyfileobj(f
, handler
.wfile
)
42 def get_local_base_path(self
, handler
, query
):
44 subcname
= query
['Container'][0]
45 container
= handler
.server
.containers
[subcname
.split('/')[0]]
47 return os
.path
.normpath(container
['path'])
49 def get_local_path(self
, handler
, query
):
51 subcname
= query
['Container'][0]
52 container
= handler
.server
.containers
[subcname
.split('/')[0]]
54 path
= os
.path
.normpath(container
['path'])
55 for folder
in subcname
.split('/')[1:]:
58 path
= os
.path
.join(path
, folder
)
61 def item_count(self
, handler
, query
, cname
, files
, last_start
=0):
62 """Return only the desired portion of the list, as specified by
63 ItemCount, AnchorItem and AnchorOffset. 'files' is either a
64 list of strings, OR a list of objects with a 'name' attribute.
66 totalFiles
= len(files
)
69 if totalFiles
and query
.has_key('ItemCount'):
70 count
= int(query
['ItemCount'][0])
72 if query
.has_key('AnchorItem'):
73 bs
= '/TiVoConnect?Command=QueryContainer&Container='
74 local_base_path
= self
.get_local_base_path(handler
, query
)
76 anchor
= query
['AnchorItem'][0]
77 if anchor
.startswith(bs
):
78 anchor
= anchor
.replace(bs
, '/', 1)
79 anchor
= unquote(anchor
)
80 anchor
= anchor
.replace(os
.path
.sep
+ cname
, local_base_path
, 1)
81 if not '://' in anchor
:
82 anchor
= os
.path
.normpath(anchor
)
84 if type(files
[0]) == str:
87 filenames
= [x
.name
for x
in files
]
89 index
= filenames
.index(anchor
, last_start
)
93 index
= filenames
.index(anchor
, 0, last_start
)
95 print 'Anchor not found:', anchor
97 print 'Anchor not found:', anchor
# just use index = 0
102 if query
.has_key('AnchorOffset'):
103 index
+= int(query
['AnchorOffset'][0])
107 files
= files
[index
:index
+ count
]
110 if index
+ count
< 0:
112 files
= files
[index
+ count
:index
]
115 else: # No AnchorItem
118 files
= files
[:count
]
120 index
= count
% len(files
)
121 files
= files
[count
:]
123 return files
, totalFiles
, index
125 def get_files(self
, handler
, query
, filterFunction
=None):
127 def build_recursive_list(path
, recurse
=True):
129 for file in os
.listdir(path
):
130 file = os
.path
.join(path
, file)
131 if recurse
and os
.path
.isdir(file):
132 files
.extend(build_recursive_list(file))
134 if not filterFunction
or filterFunction(file, file_type
):
138 subcname
= query
['Container'][0]
139 cname
= subcname
.split('/')[0]
140 path
= self
.get_local_path(handler
, query
)
142 file_type
= query
.get('Filter', [''])[0]
144 recurse
= query
.get('Recurse',['No'])[0] == 'Yes'
145 files
= build_recursive_list(path
, recurse
)
147 totalFiles
= len(files
)
150 xdir
= os
.path
.isdir(os
.path
.join(path
, x
))
151 ydir
= os
.path
.isdir(os
.path
.join(path
, y
))
154 return name_sort(x
, y
)
161 if query
.get('SortOrder',['Normal'])[0] == 'Random':
162 seed
= query
.get('RandomSeed', ['1'])[0]
163 self
.random_lock
.acquire()
165 random
.shuffle(files
)
166 self
.random_lock
.release()
171 return self
.item_count(handler
, query
, cname
, files
)