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 CONTENT_TYPE
= 'text/html'
15 module_name
= '.'.join(['plugins', name
, name
])
17 module
= __import__(module_name
, globals(), locals(), name
)
18 plugin
= getattr(module
, module
.CLASS_NAME
)()
21 print 'Error no', name
, 'plugin exists. Check the type ' \
22 'setting for your share.'
27 random_lock
= threading
.Lock()
31 def __new__(cls
, *args
, **kwds
):
32 it
= cls
.__dict
__.get('__it__')
35 cls
.__it
__ = it
= object.__new
__(cls
)
36 it
.init(*args
, **kwds
)
42 def send_file(self
, handler
, container
, name
):
43 o
= urlparse("http://fake.host" + handler
.path
)
45 handler
.send_response(200)
47 f
= file(container
['path'] + path
[len(name
) + 1:], 'rb')
48 shutil
.copyfileobj(f
, handler
.wfile
)
50 def get_local_base_path(self
, handler
, query
):
52 subcname
= query
['Container'][0]
53 container
= handler
.server
.containers
[subcname
.split('/')[0]]
55 return os
.path
.normpath(container
['path'])
57 def get_local_path(self
, handler
, query
):
59 subcname
= query
['Container'][0]
60 container
= handler
.server
.containers
[subcname
.split('/')[0]]
62 path
= os
.path
.normpath(container
['path'])
63 for folder
in subcname
.split('/')[1:]:
66 path
= os
.path
.join(path
, folder
)
69 def item_count(self
, handler
, query
, cname
, files
, last_start
=0):
70 """Return only the desired portion of the list, as specified by
71 ItemCount, AnchorItem and AnchorOffset. 'files' is either a
72 list of strings, OR a list of objects with a 'name' attribute.
74 totalFiles
= len(files
)
77 if totalFiles
and query
.has_key('ItemCount'):
78 count
= int(query
['ItemCount'][0])
80 if query
.has_key('AnchorItem'):
81 bs
= '/TiVoConnect?Command=QueryContainer&Container='
82 local_base_path
= self
.get_local_base_path(handler
, query
)
84 anchor
= query
['AnchorItem'][0]
85 if anchor
.startswith(bs
):
86 anchor
= anchor
.replace(bs
, '/', 1)
87 anchor
= unquote(anchor
)
88 anchor
= anchor
.replace(os
.path
.sep
+ cname
, local_base_path
, 1)
89 if not '://' in anchor
:
90 anchor
= os
.path
.normpath(anchor
)
92 if type(files
[0]) == str:
95 filenames
= [x
.name
for x
in files
]
97 index
= filenames
.index(anchor
, last_start
)
101 index
= filenames
.index(anchor
, 0, last_start
)
103 print 'Anchor not found:', anchor
105 print 'Anchor not found:', anchor
# just use index = 0
110 if query
.has_key('AnchorOffset'):
111 index
+= int(query
['AnchorOffset'][0])
115 files
= files
[index
:index
+ count
]
118 if index
+ count
< 0:
120 files
= files
[index
+ count
:index
]
123 else: # No AnchorItem
126 files
= files
[:count
]
128 index
= count
% len(files
)
129 files
= files
[count
:]
131 return files
, totalFiles
, index
133 def get_files(self
, handler
, query
, filterFunction
=None):
135 def build_recursive_list(path
, recurse
=True):
137 for file in os
.listdir(path
):
138 if file.startswith('.'):
140 file = os
.path
.join(path
, file)
141 if recurse
and os
.path
.isdir(file):
142 files
.extend(build_recursive_list(file))
144 if not filterFunction
or filterFunction(file, file_type
):
148 subcname
= query
['Container'][0]
149 cname
= subcname
.split('/')[0]
150 path
= self
.get_local_path(handler
, query
)
152 file_type
= query
.get('Filter', [''])[0]
154 recurse
= query
.get('Recurse',['No'])[0] == 'Yes'
155 files
= build_recursive_list(path
, recurse
)
157 totalFiles
= len(files
)
160 xdir
= os
.path
.isdir(os
.path
.join(path
, x
))
161 ydir
= os
.path
.isdir(os
.path
.join(path
, y
))
164 return name_sort(x
, y
)
171 if query
.get('SortOrder',['Normal'])[0] == 'Random':
172 seed
= query
.get('RandomSeed', ['1'])[0]
173 self
.random_lock
.acquire()
175 random
.shuffle(files
)
176 self
.random_lock
.release()
181 return self
.item_count(handler
, query
, cname
, files
)