Simplified
[pyTivo.git] / plugin.py
blob6600ac95dde7a52a09c6851c2c2b038402f031b6
1 import os, shutil, re, random, threading
2 from urllib import unquote, unquote_plus
3 from urlparse import urlparse
5 def GetPlugin(name):
6 module_name = '.'.join(['plugins', name, name])
7 module = __import__(module_name, globals(), locals(), name)
8 plugin = getattr(module, module.CLASS_NAME)()
9 return plugin
11 class Plugin(object):
13 random_lock = threading.Lock()
15 CONTENT_TYPE = ''
17 def __new__(cls, *args, **kwds):
18 it = cls.__dict__.get('__it__')
19 if it is not None:
20 return it
21 cls.__it__ = it = object.__new__(cls)
22 it.init(*args, **kwds)
23 return it
25 def init(self):
26 pass
28 def send_file(self, handler, container, name):
29 o = urlparse("http://fake.host" + handler.path)
30 path = unquote_plus(o[2])
31 handler.send_response(200)
32 handler.end_headers()
33 f = file(container['path'] + path[len(name)+1:], 'rb')
34 shutil.copyfileobj(f, handler.wfile)
36 def get_local_base_path(self, handler, query):
38 subcname = query['Container'][0]
39 container = handler.server.containers[subcname.split('/')[0]]
41 return container['path']
43 def get_local_path(self, handler, query):
45 subcname = query['Container'][0]
46 container = handler.server.containers[subcname.split('/')[0]]
48 path = container['path']
49 for folder in subcname.split('/')[1:]:
50 if folder == '..':
51 return False
52 path = os.path.join(path, folder)
53 return path
55 def get_files(self, handler, query, filterFunction=None):
56 subcname = query['Container'][0]
57 cname = subcname.split('/')[0]
58 path = self.get_local_path(handler, query)
60 files = [ os.path.join(path, file) for file in os.listdir(path)]
61 if query.get('Recurse',['No'])[0] == 'Yes':
62 for file in files:
63 if os.path.isdir(file):
64 for new_file in os.listdir(file):
65 files.append( os.path.join(file, new_file) )
67 file_type = query.get('Filter', [''])[0]
68 if filterFunction:
69 files = [file for file in files if filterFunction(file, file_type)]
71 totalFiles = len(files)
73 def dir_sort(x, y):
74 xdir = os.path.isdir(os.path.join(path, x))
75 ydir = os.path.isdir(os.path.join(path, y))
77 if xdir == ydir:
78 return name_sort(x, y)
79 else:
80 return ydir - xdir
82 def name_sort(x, y):
83 numbername = re.compile(r'(\d*)(.*)')
84 m = numbername.match(x)
85 xNumber = m.group(1)
86 xStr = m.group(2)
87 m = numbername.match(y)
88 yNumber = m.group(1)
89 yStr = m.group(2)
91 if xNumber and yNumber:
92 xNumber, yNumber = int(xNumber), int(yNumber)
93 if xNumber == yNumber:
94 return cmp(xStr, yStr)
95 else:
96 return cmp(xNumber, yNumber)
97 elif xNumber:
98 return -1
99 elif yNumber:
100 return 1
101 else:
102 return cmp(xStr, yStr)
104 if query.get('SortOrder',['Normal'])[0] == 'Random':
105 seed = query.get('RandomSeed', ['1'])[0]
106 self.random_lock.acquire()
107 random.seed(seed)
108 random.shuffle(files)
109 self.random_lock.release()
110 else:
111 files.sort(dir_sort)
113 local_base_path = self.get_local_base_path(handler, query)
115 index = 0
116 count = 10
117 if query.has_key('ItemCount'):
118 count = int(query['ItemCount'] [0])
120 if query.has_key('AnchorItem'):
121 anchor = unquote(query['AnchorItem'][0])
122 for file, i in zip(files, range(len(files))):
123 file_name = file.replace(local_base_path, '')
125 if os.path.isdir(os.path.join(file)):
126 file_url = '/TiVoConnect?Command=QueryContainer&Container=' + cname + file_name
127 else:
128 file_url = '/' + cname + file_name
129 file_url = file_url.replace('\\', '/')
131 if file_url == anchor:
132 if count > 0:
133 index = i + 1
134 else:
135 index = i
136 break
138 if query.has_key('AnchorOffset'):
139 index = index + int(query['AnchorOffset'][0])
141 #foward count
142 if index < index + count:
143 files = files[index:index + count ]
144 return files, totalFiles, index
145 #backwards count
146 else:
147 #off the start of the list
148 if index + count < 0:
149 index += 0 - (index + count)
150 files = files[index + count:index]
151 return files, totalFiles, index + count