9 from Cheetah
.Filters
import Filter
10 from lrucache
import LRUCache
12 if os
.path
.sep
== '/':
14 unquote
= urllib
.unquote_plus
16 quote
= lambda x
: urllib
.quote(x
.replace(os
.path
.sep
, '/'))
17 unquote
= lambda x
: os
.path
.normpath(urllib
.unquote_plus(x
))
20 CONTENT_TYPE
= 'text/html'
24 module_name
= '.'.join(['plugins', name
, name
])
25 module
= __import__(module_name
, globals(), locals(), name
)
26 plugin
= getattr(module
, module
.CLASS_NAME
)()
29 print 'Error no', name
, 'plugin exists. Check the type ' \
30 'setting for your share.'
33 def GetPluginPath(name
):
35 module_name
= '.'.join(['plugins', name
, name
])
36 module
= __import__(module_name
, globals(), locals(), name
)
37 return getattr(module
, 'SCRIPTDIR')
41 class EncodeUnicode(Filter
):
42 def filter(self
, val
, **kw
):
43 """Encode Unicode strings, by default in UTF-8"""
45 encoding
= kw
.get('encoding', 'utf8')
49 val
= val
.decode('utf8')
51 if sys
.platform
== 'darwin':
52 val
= val
.decode('macroman')
54 val
= val
.decode('iso8859-1')
55 elif type(val
) != unicode:
57 return val
.encode(encoding
)
61 random_lock
= threading
.Lock()
65 recurse_cache
= LRUCache(5)
66 dir_cache
= LRUCache(10)
68 def __new__(cls
, *args
, **kwds
):
69 it
= cls
.__dict
__.get('__it__')
72 cls
.__it
__ = it
= object.__new
__(cls
)
73 it
.init(*args
, **kwds
)
79 def send_file(self
, handler
, path
, query
):
80 handler
.send_response(200)
83 shutil
.copyfileobj(f
, handler
.wfile
)
86 def get_local_base_path(self
, handler
, query
):
88 subcname
= query
['Container'][0]
89 container
= handler
.server
.containers
[subcname
.split('/')[0]]
91 return os
.path
.normpath(container
['path'])
93 def get_local_path(self
, handler
, query
):
95 subcname
= query
['Container'][0]
96 container
= handler
.server
.containers
[subcname
.split('/')[0]]
98 path
= os
.path
.normpath(container
['path'])
99 for folder
in subcname
.split('/')[1:]:
102 path
= os
.path
.join(path
, folder
)
105 def item_count(self
, handler
, query
, cname
, files
, last_start
=0):
106 """Return only the desired portion of the list, as specified by
107 ItemCount, AnchorItem and AnchorOffset. 'files' is either a
108 list of strings, OR a list of objects with a 'name' attribute.
110 def no_anchor(handler
, anchor
):
111 handler
.server
.logger
.warning('Anchor not found: ' + anchor
)
113 totalFiles
= len(files
)
116 if totalFiles
and 'ItemCount' in query
:
117 count
= int(query
['ItemCount'][0])
119 if 'AnchorItem' in query
:
120 bs
= '/TiVoConnect?Command=QueryContainer&Container='
121 local_base_path
= self
.get_local_base_path(handler
, query
)
123 anchor
= query
['AnchorItem'][0]
124 if anchor
.startswith(bs
):
125 anchor
= anchor
.replace(bs
, '/', 1)
126 anchor
= unquote(anchor
)
127 anchor
= anchor
.replace(os
.path
.sep
+ cname
, local_base_path
, 1)
128 if not '://' in anchor
:
129 anchor
= os
.path
.normpath(anchor
)
131 if type(files
[0]) == str:
134 filenames
= [x
.name
for x
in files
]
136 index
= filenames
.index(anchor
, last_start
)
140 index
= filenames
.index(anchor
, 0, last_start
)
142 no_anchor(handler
, anchor
)
144 no_anchor(handler
, anchor
) # just use index = 0
149 if 'AnchorOffset' in query
:
150 index
+= int(query
['AnchorOffset'][0])
153 index
= (index
+ count
) % len(files
)
155 files
= files
[index
:index
+ count
]
157 return files
, totalFiles
, index
159 def get_files(self
, handler
, query
, filterFunction
=None, force_alpha
=False):
162 def __init__(self
, name
, isdir
):
166 self
.mdate
= int(st
.st_mtime
)
169 def __init__(self
, files
):
175 def build_recursive_list(path
, recurse
=True):
178 for f
in os
.listdir(path
):
179 if f
.startswith('.'):
181 f
= os
.path
.join(path
, f
)
182 isdir
= os
.path
.isdir(f
)
183 if recurse
and isdir
:
184 files
.extend(build_recursive_list(f
))
186 if not filterFunction
or filterFunction(f
, file_type
):
187 files
.append(FileData(f
, isdir
))
192 subcname
= query
['Container'][0]
193 cname
= subcname
.split('/')[0]
194 path
= self
.get_local_path(handler
, query
)
196 file_type
= query
.get('Filter', [''])[0]
198 recurse
= query
.get('Recurse', ['No'])[0] == 'Yes'
201 rc
= self
.recurse_cache
204 if path
in rc
and rc
.mtime(path
) + 300 >= time
.time():
207 updated
= os
.stat(path
)[8]
208 if path
in dc
and dc
.mtime(path
) >= updated
:
211 if path
.startswith(p
) and rc
.mtime(p
) < updated
:
215 filelist
= SortList(build_recursive_list(path
, recurse
))
223 if x
.isdir
== y
.isdir
:
224 return name_sort(x
, y
)
226 return y
.isdir
- x
.isdir
229 return cmp(x
.name
, y
.name
)
232 return cmp(y
.mdate
, x
.mdate
)
234 sortby
= query
.get('SortOrder', ['Normal'])[0]
235 if filelist
.unsorted
or filelist
.sortby
!= sortby
:
237 filelist
.files
.sort(dir_sort
)
238 elif sortby
== '!CaptureDate':
239 filelist
.files
.sort(date_sort
)
241 filelist
.files
.sort(name_sort
)
243 filelist
.sortby
= sortby
244 filelist
.unsorted
= False
246 files
= filelist
.files
[:]
249 files
, total
, start
= self
.item_count(handler
, query
, cname
, files
,
252 filelist
.last_start
= start
253 return files
, total
, start