1 import os
, shutil
, re
, random
, threading
2 from urllib
import unquote
, unquote_plus
3 from urlparse
import urlparse
6 module_name
= '.'.join(['plugins', name
, name
])
7 module
= __import__(module_name
, globals(), locals(), name
)
8 plugin
= getattr(module
, module
.CLASS_NAME
)()
13 random_lock
= threading
.Lock()
17 def __new__(cls
, *args
, **kwds
):
18 it
= cls
.__dict
__.get('__it__')
21 cls
.__it
__ = it
= object.__new
__(cls
)
22 it
.init(*args
, **kwds
)
28 def send_file(self
, handler
, container
, name
):
29 o
= urlparse("http://fake.host" + handler
.path
)
30 path
= unquote_plus(o
[2])
31 handler
.send_response(200)
33 f
= file(container
['path'] + path
[len(name
)+1:], 'rb')
34 shutil
.copyfileobj(f
, handler
.wfile
)
36 def get_local_base_path(self
, handler
, query
):
38 subcname
= query
['Container'][0]
39 container
= handler
.server
.containers
[subcname
.split('/')[0]]
41 return container
['path']
43 def get_local_path(self
, handler
, query
):
45 subcname
= query
['Container'][0]
46 container
= handler
.server
.containers
[subcname
.split('/')[0]]
48 path
= container
['path']
49 for folder
in subcname
.split('/')[1:]:
52 path
= os
.path
.join(path
, folder
)
55 def get_files(self
, handler
, query
, filterFunction
=None):
56 subcname
= query
['Container'][0]
57 cname
= subcname
.split('/')[0]
58 path
= self
.get_local_path(handler
, query
)
60 files
= [ os
.path
.join(path
, file) for file in os
.listdir(path
)]
61 if query
.get('Recurse',['No'])[0] == 'Yes':
63 if os
.path
.isdir(file):
64 for new_file
in os
.listdir(file):
65 files
.append( os
.path
.join(file, new_file
) )
67 file_type
= query
.get('Filter', [''])[0]
69 files
= [file for file in files
if filterFunction(file, file_type
)]
71 totalFiles
= len(files
)
74 xdir
= os
.path
.isdir(os
.path
.join(path
, x
))
75 ydir
= os
.path
.isdir(os
.path
.join(path
, y
))
78 return name_sort(x
, y
)
84 return name_sort(x
, y
)
87 numbername
= re
.compile(r
'(\d*)(.*)')
88 m
= numbername
.match(x
)
91 m
= numbername
.match(y
)
95 if xNumber
and yNumber
:
96 xNumber
, yNumber
= int(xNumber
), int(yNumber
)
97 if xNumber
== yNumber
:
98 return cmp(xStr
, yStr
)
100 return cmp(xNumber
, yNumber
)
106 return cmp(xStr
, yStr
)
108 if query
.get('SortOrder',['Normal'])[0] == 'Random':
109 seed
= query
.get('RandomSeed', ['1'])[0]
110 self
.random_lock
.acquire()
112 random
.shuffle(files
)
113 self
.random_lock
.release()
117 local_base_path
= self
.get_local_base_path(handler
, query
)
121 if query
.has_key('ItemCount'):
122 count
= int(query
['ItemCount'] [0])
124 if query
.has_key('AnchorItem'):
125 anchor
= unquote(query
['AnchorItem'][0])
126 for file, i
in zip(files
, range(len(files
))):
127 file_name
= file.replace(local_base_path
, '')
129 if os
.path
.isdir(os
.path
.join(file)):
130 file_url
= '/TiVoConnect?Command=QueryContainer&Container=' + cname
+ file_name
132 file_url
= '/' + cname
+ file_name
134 if file_url
== anchor
:
143 if query
.has_key('AnchorOffset'):
144 index
= index
+ int(query
['AnchorOffset'][0])
147 if index
< index
+ count
:
148 files
= files
[index
:index
+ count
]
149 return files
, totalFiles
, index
152 #off the start of the list
153 if index
+ count
< 0:
154 index
+= 0 - (index
+ count
)
155 files
= files
[index
+ count
:index
]
156 return files
, totalFiles
, index
+ count