moved from screen to tmux
[tvweb.git] / filechooser.py
blobc910dca19d8943f1b5bcfc0d1e5deca26b649e83
2 import os
3 import re
4 import string
6 class FileChooser():
8 def __init__(self):
9 self.allowed_prefix = [
10 '/mnt/data',
11 '/home/ftp/serialy',
12 '/mnt/kesunka/serialy',
13 '/mnt/kesunka/video',
15 self.back_item = '__back__'
16 self.cpath = self.allowed_prefix[0]
18 def getAllowed(self):
19 return self.allowed_prefix
21 def getCurrentPath(self):
22 return self.cpath
24 def getFile(self, filename):
25 out = self.cpath + "/" + filename
26 if self.checkPath(out):
27 return out
28 return None
30 def setPath(self, dir):
31 if self.checkPath(dir):
32 self.cpath = dir
34 def checkPath(self, file):
35 if re.match("("+string.join(self.allowed_prefix, "|")+")", file) and not re.search("(/\.\.|\.\./)", file):
36 return True
37 else:
38 return False
40 def goBack(self, item=None):
41 print "go back"
42 if item.split('/')[-1] == self.back_item:
43 print "matches ", self.back_item
44 out = self.cpath[:self.cpath.rfind('/')]
45 if re.match("("+string.join(self.allowed_prefix, "|")+")", out):
46 self.cpath = out
48 def getList(self):
49 out = self.transformList(os.listdir(self.cpath))
50 return out
52 def transformList(self, list):
53 out = []
54 list.sort()
55 for d in list:
56 if all(ord(c) < 128 for c in d):
57 out.append(d)
58 out.insert(0, self.back_item)
59 return out
62 if __name__ == "__main__":
63 fc = FileChooser()
64 print fc.getFile("na/..me")
65 print fc.getList()