Changed line endings
[pyTivo/krkeegan.git] / plugin.py
bloba26d46c0fcb63b13824651c888fc1213c391f65c
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, name)()
9 return plugin
11 class Plugin(object):
13 def __new__(cls, *args, **kwds):
14 it = cls.__dict__.get('__it__')
15 if it is not None:
16 return it
17 cls.__it__ = it = object.__new__(cls)
18 it.init(*args, **kwds)
19 return it
21 def init(self):
22 pass
24 content_type = ''
26 def SendFile(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