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
, name
)()
13 def __new__(cls
, *args
, **kwds
):
14 it
= cls
.__dict
__.get('__it__')
17 cls
.__it
__ = it
= object.__new
__(cls
)
18 it
.init(*args
, **kwds
)
26 def SendFile(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_path(self
, handler
, query
):
36 subcname
= query
['Container'][0]
37 container
= handler
.server
.containers
[subcname
.split('/')[0]]
39 path
= container
['path']
40 for folder
in subcname
.split('/')[1:]:
43 path
= os
.path
.join(path
, folder
)
46 def get_files(self
, handler
, query
, filterFunction
=None):
47 subcname
= query
['Container'][0]
48 path
= self
.get_local_path(handler
, query
)
50 files
= os
.listdir(path
)
52 files
= filter(filterFunction
, files
)
53 totalFiles
= len(files
)
56 xdir
= os
.path
.isdir(os
.path
.join(path
, x
))
57 ydir
= os
.path
.isdir(os
.path
.join(path
, y
))
60 return name_sort(x
, y
)
66 return name_sort(x
, y
)
69 numbername
= re
.compile(r
'(\d*)(.*)')
70 m
= numbername
.match(x
)
73 m
= numbername
.match(y
)
77 if xNumber
and yNumber
:
78 xNumber
, yNumber
= int(xNumber
), int(yNumber
)
79 if xNumber
== yNumber
:
80 return cmp(xStr
, yStr
)
82 return cmp(xNumber
, yNumber
)
88 return cmp(xStr
, yStr
)
94 if query
.has_key('ItemCount'):
95 count
= int(query
['ItemCount'] [0])
97 if query
.has_key('AnchorItem'):
98 anchor
= unquote(query
['AnchorItem'][0])
99 for i
in range(len(files
)):
100 if os
.path
.isdir(os
.path
.join(path
,files
[i
])):
101 file_url
= '/TiVoConnect?Command=QueryContainer&Container=' + subcname
+ '/' + files
[i
]
103 file_url
= '/' + subcname
+ '/' + files
[i
]
104 if file_url
== anchor
:
112 if query
.has_key('AnchorOffset'):
113 index
= index
+ int(query
['AnchorOffset'][0])
116 if index
< index
+ count
:
117 files
= files
[index
:index
+ count
]
118 return files
, totalFiles
, index
121 print 'index, count', index
, count
123 #off the start of the list
124 if index
+ count
< 0:
125 print 0 - (index
+ count
)
126 index
+= 0 - (index
+ count
)
128 files
= files
[index
+ count
:index
]
129 return files
, totalFiles
, index
+ count