1 import os
, shutil
, re
, random
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
)()
15 def __new__(cls
, *args
, **kwds
):
16 it
= cls
.__dict
__.get('__it__')
19 cls
.__it
__ = it
= object.__new
__(cls
)
20 it
.init(*args
, **kwds
)
26 def send_file(self
, handler
, container
, name
):
27 o
= urlparse("http://fake.host" + handler
.path
)
28 path
= unquote_plus(o
[2])
29 handler
.send_response(200)
31 f
= file(container
['path'] + path
[len(name
)+1:], 'rb')
32 shutil
.copyfileobj(f
, handler
.wfile
)
34 def get_local_base_path(self
, handler
, query
):
36 subcname
= query
['Container'][0]
37 container
= handler
.server
.containers
[subcname
.split('/')[0]]
39 return container
['path']
41 def get_local_path(self
, handler
, query
):
43 subcname
= query
['Container'][0]
44 container
= handler
.server
.containers
[subcname
.split('/')[0]]
46 path
= container
['path']
47 for folder
in subcname
.split('/')[1:]:
50 path
= os
.path
.join(path
, folder
)
53 def get_files(self
, handler
, query
, filterFunction
=None):
54 subcname
= query
['Container'][0]
55 cname
= subcname
.split('/')[0]
56 path
= self
.get_local_path(handler
, query
)
58 files
= [ os
.path
.join(path
, file) for file in os
.listdir(path
)]
59 if query
.get('Recurse',['No'])[0] == 'Yes':
61 if os
.path
.isdir(file):
62 for new_file
in os
.listdir(file):
63 files
.append( os
.path
.join(file, new_file
) )
65 file_type
= query
['Filter'][0]
67 files
= [file for file in files
if filterFunction(file, file_type
)]
69 totalFiles
= len(files
)
72 xdir
= os
.path
.isdir(os
.path
.join(path
, x
))
73 ydir
= os
.path
.isdir(os
.path
.join(path
, y
))
76 return name_sort(x
, y
)
82 return name_sort(x
, y
)
85 numbername
= re
.compile(r
'(\d*)(.*)')
86 m
= numbername
.match(x
)
89 m
= numbername
.match(y
)
93 if xNumber
and yNumber
:
94 xNumber
, yNumber
= int(xNumber
), int(yNumber
)
95 if xNumber
== yNumber
:
96 return cmp(xStr
, yStr
)
98 return cmp(xNumber
, yNumber
)
104 return cmp(xStr
, yStr
)
106 if query
.get('SortOrder',['Normal'])[0] == 'Random':
107 seed
= query
.get('RandomSeed', ['1'])[0]
109 random
.shuffle(files
)
113 local_base_path
= self
.get_local_base_path(handler
, query
)
117 if query
.has_key('ItemCount'):
118 count
= int(query
['ItemCount'] [0])
120 if query
.has_key('AnchorItem'):
121 anchor
= unquote(query
['AnchorItem'][0])
122 for file, i
in zip(files
, range(len(files
))):
123 file_name
= file.replace(local_base_path
, '')
125 if os
.path
.isdir(os
.path
.join(file)):
126 file_url
= '/TiVoConnect?Command=QueryContainer&Container=' + cname
+ file_name
128 file_url
= '/' + cname
+ file_name
130 if file_url
== anchor
:
139 if query
.has_key('AnchorOffset'):
140 index
= index
+ int(query
['AnchorOffset'][0])
143 if index
< index
+ count
:
144 files
= files
[index
:index
+ count
]
145 return files
, totalFiles
, index
148 #off the start of the list
149 if index
+ count
< 0:
150 index
+= 0 - (index
+ count
)
151 files
= files
[index
+ count
:index
]
152 return files
, totalFiles
, index
+ count