Changed the plugin class names and added an attribute on the module
[pyTivo.git] / plugin.py
blob9e275214f1bd88dbafc84af3b286413ed35b72f7
1 import os, shutil, re
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 content_type = ''
15 def __new__(cls, *args, **kwds):
16 it = cls.__dict__.get('__it__')
17 if it is not None:
18 return it
19 cls.__it__ = it = object.__new__(cls)
20 it.init(*args, **kwds)
21 return it
23 def init(self):
24 pass
26 def send_file(self, handler, container, name):
27 o = urlparse("http://fake.host" + handler.path)
28 path = unquote_plus(o[2])
29 handler.send_response(200)
30 handler.end_headers()
31 f = file(container['path'] + path[len(name)+1:], 'rb')
32 shutil.copyfileobj(f, handler.wfile)
34 def get_local_path(self, handler, query):
36 subcname = query['Container'][0]
37 container = handler.server.containers[subcname.split('/')[0]]
39 path = container['path']
40 for folder in subcname.split('/')[1:]:
41 if folder == '..':
42 return False
43 path = os.path.join(path, folder)
44 return path
46 def get_files(self, handler, query, filterFunction=None):
47 subcname = query['Container'][0]
48 path = self.get_local_path(handler, query)
50 files = os.listdir(path)
51 if filterFunction:
52 files = filter(filterFunction, files)
53 totalFiles = len(files)
55 def dir_sort(x, y):
56 xdir = os.path.isdir(os.path.join(path, x))
57 ydir = os.path.isdir(os.path.join(path, y))
59 if xdir and ydir:
60 return name_sort(x, y)
61 elif xdir:
62 return -1
63 elif ydir:
64 return 1
65 else:
66 return name_sort(x, y)
68 def name_sort(x, y):
69 numbername = re.compile(r'(\d*)(.*)')
70 m = numbername.match(x)
71 xNumber = m.group(1)
72 xStr = m.group(2)
73 m = numbername.match(y)
74 yNumber = m.group(1)
75 yStr = m.group(2)
77 if xNumber and yNumber:
78 xNumber, yNumber = int(xNumber), int(yNumber)
79 if xNumber == yNumber:
80 return cmp(xStr, yStr)
81 else:
82 return cmp(xNumber, yNumber)
83 elif xNumber:
84 return -1
85 elif yNumber:
86 return 1
87 else:
88 return cmp(xStr, yStr)
90 files.sort(dir_sort)
92 index = 0
93 count = 10
94 if query.has_key('ItemCount'):
95 count = int(query['ItemCount'] [0])
97 if query.has_key('AnchorItem'):
98 anchor = unquote(query['AnchorItem'][0])
99 for i in range(len(files)):
100 if os.path.isdir(os.path.join(path,files[i])):
101 file_url = '/TiVoConnect?Command=QueryContainer&Container=' + subcname + '/' + files[i]
102 else:
103 file_url = '/' + subcname + '/' + files[i]
104 if file_url == anchor:
105 if count > 0:
106 index = i + 1
107 elif count < 0:
108 index = i - 1
109 else:
110 index = i
111 break
112 if query.has_key('AnchorOffset'):
113 index = index + int(query['AnchorOffset'][0])
115 #foward count
116 if index < index + count:
117 files = files[index:index + count ]
118 return files, totalFiles, index
119 #backwards count
120 else:
121 print 'index, count', index, count
122 print index + count
123 #off the start of the list
124 if index + count < 0:
125 print 0 - (index + count)
126 index += 0 - (index + count)
127 print index + count
128 files = files[index + count:index]
129 return files, totalFiles, index + count