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
):
62 """Return only the desired portion of the list, as specified by
63 ItemCount, AnchorItem and AnchorOffset
65 totalFiles
= len(files
)
68 if totalFiles
and query
.has_key('ItemCount'):
69 count
= int(query
['ItemCount'][0])
71 if query
.has_key('AnchorItem'):
72 bs
= '/TiVoConnect?Command=QueryContainer&Container='
73 local_base_path
= self
.get_local_base_path(handler
, query
)
75 anchor
= query
['AnchorItem'][0]
76 if anchor
.startswith(bs
):
77 anchor
= anchor
.replace(bs
, '/', 1)
78 anchor
= unquote(anchor
)
79 anchor
= anchor
.replace(os
.path
.sep
+ cname
, local_base_path
, 1)
80 anchor
= os
.path
.normpath(anchor
)
83 index
= files
.index(anchor
)
85 print 'Anchor not found:', anchor
# just use index = 0
90 if query
.has_key('AnchorOffset'):
91 index
+= int(query
['AnchorOffset'][0])
95 files
= files
[index
:index
+ count
]
100 files
= files
[index
+ count
:index
]
103 else: # No AnchorItem
106 files
= files
[:count
]
108 index
= count
% len(files
)
109 files
= files
[count
:]
111 return files
, totalFiles
, index
113 def get_files(self
, handler
, query
, filterFunction
=None):
114 subcname
= query
['Container'][0]
115 cname
= subcname
.split('/')[0]
116 path
= self
.get_local_path(handler
, query
)
118 files
= [ os
.path
.join(path
, file) for file in os
.listdir(path
)]
119 if query
.get('Recurse',['No'])[0] == 'Yes':
121 if os
.path
.isdir(file):
122 for new_file
in os
.listdir(file):
123 files
.append( os
.path
.join(file, new_file
) )
125 file_type
= query
.get('Filter', [''])[0]
127 files
= [file for file in files
if filterFunction(file, file_type
)]
129 totalFiles
= len(files
)
132 xdir
= os
.path
.isdir(os
.path
.join(path
, x
))
133 ydir
= os
.path
.isdir(os
.path
.join(path
, y
))
136 return name_sort(x
, y
)
143 if query
.get('SortOrder',['Normal'])[0] == 'Random':
144 seed
= query
.get('RandomSeed', ['1'])[0]
145 self
.random_lock
.acquire()
147 random
.shuffle(files
)
148 self
.random_lock
.release()
153 return self
.item_count(handler
, query
, cname
, files
)