6 from urlparse
import urlparse
10 unquote
= urllib
.unquote_plus
12 quote
= lambda x
: urllib
.quote(x
.replace(os
.path
.sep
, '/'))
13 unquote
= lambda x
: urllib
.unquote_plus(x
).replace('/', os
.path
.sep
)
16 CONTENT_TYPE
= 'text/html'
20 module_name
= '.'.join(['plugins', name
, name
])
21 module
= __import__(module_name
, globals(), locals(), name
)
22 plugin
= getattr(module
, module
.CLASS_NAME
)()
25 print 'Error no', name
, 'plugin exists. Check the type ' \
26 'setting for your share.'
31 random_lock
= threading
.Lock()
35 def __new__(cls
, *args
, **kwds
):
36 it
= cls
.__dict
__.get('__it__')
39 cls
.__it
__ = it
= object.__new
__(cls
)
40 it
.init(*args
, **kwds
)
46 def send_file(self
, handler
, container
, name
):
47 o
= urlparse("http://fake.host" + handler
.path
)
49 handler
.send_response(200)
51 f
= file(container
['path'] + path
[len(name
) + 1:], 'rb')
52 shutil
.copyfileobj(f
, handler
.wfile
)
54 def get_local_base_path(self
, handler
, query
):
56 subcname
= query
['Container'][0]
57 container
= handler
.server
.containers
[subcname
.split('/')[0]]
59 return os
.path
.normpath(container
['path'])
61 def get_local_path(self
, handler
, query
):
63 subcname
= query
['Container'][0]
64 container
= handler
.server
.containers
[subcname
.split('/')[0]]
66 path
= os
.path
.normpath(container
['path'])
67 for folder
in subcname
.split('/')[1:]:
70 path
= os
.path
.join(path
, folder
)
73 def item_count(self
, handler
, query
, cname
, files
, last_start
=0):
74 """Return only the desired portion of the list, as specified by
75 ItemCount, AnchorItem and AnchorOffset. 'files' is either a
76 list of strings, OR a list of objects with a 'name' attribute.
78 totalFiles
= len(files
)
81 if totalFiles
and 'ItemCount' in query
:
82 count
= int(query
['ItemCount'][0])
84 if 'AnchorItem' in query
:
85 bs
= '/TiVoConnect?Command=QueryContainer&Container='
86 local_base_path
= self
.get_local_base_path(handler
, query
)
88 anchor
= query
['AnchorItem'][0]
89 if anchor
.startswith(bs
):
90 anchor
= anchor
.replace(bs
, '/', 1)
91 anchor
= unquote(anchor
)
92 anchor
= anchor
.replace(os
.path
.sep
+ cname
, local_base_path
, 1)
93 if not '://' in anchor
:
94 anchor
= os
.path
.normpath(anchor
)
96 if type(files
[0]) == str:
99 filenames
= [x
.name
for x
in files
]
101 index
= filenames
.index(anchor
, last_start
)
105 index
= filenames
.index(anchor
, 0, last_start
)
107 print 'Anchor not found:', anchor
109 print 'Anchor not found:', anchor
# just use index = 0
114 if 'AnchorOffset' in query
:
115 index
+= int(query
['AnchorOffset'][0])
119 files
= files
[index
:index
+ count
]
122 if index
+ count
< 0:
124 files
= files
[index
+ count
:index
]
127 else: # No AnchorItem
130 files
= files
[:count
]
132 index
= count
% len(files
)
133 files
= files
[count
:]
135 return files
, totalFiles
, index
137 def get_files(self
, handler
, query
, filterFunction
=None):
139 def build_recursive_list(path
, recurse
=True):
142 for f
in os
.listdir(path
):
143 if f
.startswith('.'):
145 f
= os
.path
.join(path
, f
)
146 if recurse
and os
.path
.isdir(f
):
147 files
.extend(build_recursive_list(f
))
149 if not filterFunction
or filterFunction(f
, file_type
):
155 subcname
= query
['Container'][0]
156 cname
= subcname
.split('/')[0]
157 path
= self
.get_local_path(handler
, query
)
159 file_type
= query
.get('Filter', [''])[0]
161 recurse
= query
.get('Recurse', ['No'])[0] == 'Yes'
162 files
= build_recursive_list(path
, recurse
)
164 totalFiles
= len(files
)
167 xdir
= os
.path
.isdir(os
.path
.join(path
, x
))
168 ydir
= os
.path
.isdir(os
.path
.join(path
, y
))
171 return name_sort(x
, y
)
179 return cmp(os
.stat(y
).st_ctime
, os
.stat(x
).st_ctime
)
181 if query
.get('SortOrder', ['Normal'])[0] == 'Random':
182 seed
= query
.get('RandomSeed', ['1'])[0]
183 self
.random_lock
.acquire()
185 random
.shuffle(files
)
186 self
.random_lock
.release()
187 elif query
.get('SortOrder', ['Normal'])[0] == '!CaptureDate':
188 files
.sort(date_sort
)
193 return self
.item_count(handler
, query
, cname
, files
)