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
= open(container
['path'] + path
[len(name
) + 1:], 'rb')
52 shutil
.copyfileobj(f
, handler
.wfile
)
55 def get_local_base_path(self
, handler
, query
):
57 subcname
= query
['Container'][0]
58 container
= handler
.server
.containers
[subcname
.split('/')[0]]
60 return os
.path
.normpath(container
['path'])
62 def get_local_path(self
, handler
, query
):
64 subcname
= query
['Container'][0]
65 container
= handler
.server
.containers
[subcname
.split('/')[0]]
67 path
= os
.path
.normpath(container
['path'])
68 for folder
in subcname
.split('/')[1:]:
71 path
= os
.path
.join(path
, folder
)
74 def item_count(self
, handler
, query
, cname
, files
, last_start
=0):
75 """Return only the desired portion of the list, as specified by
76 ItemCount, AnchorItem and AnchorOffset. 'files' is either a
77 list of strings, OR a list of objects with a 'name' attribute.
79 totalFiles
= len(files
)
82 if totalFiles
and 'ItemCount' in query
:
83 count
= int(query
['ItemCount'][0])
85 if 'AnchorItem' in query
:
86 bs
= '/TiVoConnect?Command=QueryContainer&Container='
87 local_base_path
= self
.get_local_base_path(handler
, query
)
89 anchor
= query
['AnchorItem'][0]
90 if anchor
.startswith(bs
):
91 anchor
= anchor
.replace(bs
, '/', 1)
92 anchor
= unquote(anchor
)
93 anchor
= anchor
.replace(os
.path
.sep
+ cname
, local_base_path
, 1)
94 if not '://' in anchor
:
95 anchor
= os
.path
.normpath(anchor
)
97 if type(files
[0]) == str:
100 filenames
= [x
.name
for x
in files
]
102 index
= filenames
.index(anchor
, last_start
)
106 index
= filenames
.index(anchor
, 0, last_start
)
108 print 'Anchor not found:', anchor
110 print 'Anchor not found:', anchor
# just use index = 0
115 if 'AnchorOffset' in query
:
116 index
+= int(query
['AnchorOffset'][0])
120 files
= files
[index
:index
+ count
]
123 if index
+ count
< 0:
125 files
= files
[index
+ count
:index
]
128 else: # No AnchorItem
131 files
= files
[:count
]
133 index
= count
% len(files
)
134 files
= files
[count
:]
136 return files
, totalFiles
, index
138 def get_files(self
, handler
, query
, filterFunction
=None):
140 def build_recursive_list(path
, recurse
=True):
143 for f
in os
.listdir(path
):
144 if f
.startswith('.'):
146 f
= os
.path
.join(path
, f
)
147 if recurse
and os
.path
.isdir(f
):
148 files
.extend(build_recursive_list(f
))
150 if not filterFunction
or filterFunction(f
, file_type
):
156 subcname
= query
['Container'][0]
157 cname
= subcname
.split('/')[0]
158 path
= self
.get_local_path(handler
, query
)
160 file_type
= query
.get('Filter', [''])[0]
162 recurse
= query
.get('Recurse', ['No'])[0] == 'Yes'
163 files
= build_recursive_list(path
, recurse
)
165 totalFiles
= len(files
)
168 xdir
= os
.path
.isdir(os
.path
.join(path
, x
))
169 ydir
= os
.path
.isdir(os
.path
.join(path
, y
))
172 return name_sort(x
, y
)
180 return cmp(os
.stat(y
).st_mtime
, os
.stat(x
).st_mtime
)
182 if query
.get('SortOrder', ['Normal'])[0] == 'Random':
183 seed
= query
.get('RandomSeed', ['1'])[0]
184 self
.random_lock
.acquire()
186 random
.shuffle(files
)
187 self
.random_lock
.release()
188 elif query
.get('SortOrder', ['Normal'])[0] == '!CaptureDate':
189 files
.sort(date_sort
)
194 return self
.item_count(handler
, query
, cname
, files
)