Test commit
[couchpytato.git] / mod_filebrowser.py
blobe5b11be74a3d632cb124f067cbecf3ff4f427438
1 import gui, pygame, os, time, mod, playlist, cfg, random
2 from pygame.locals import *
4 class Module(mod.Module):
5 def __init__(self, screen):
6 mod.Module.__init__(self, screen)
8 def filllist(self):
9 self.data = []
10 try:
11 self.gui['LIST'].liststart, self.gui['LIST'].selected = self.history[self.path]
12 except:
13 pass
14 if self.filetypes == '*':
15 self.data = os.listdir(self.path)
16 else:
17 for file in os.listdir(self.path):
18 if os.path.isdir(os.path.join(self.path, file)):
19 self.data.append(file)
20 else:
21 for ft in self.filetypes:
22 if file[-3:].lower() == ft:
23 self.data.append(file)
24 self.data.sort(key=str.lower)
25 if len(self.data) > 0:
26 for file in self.data:
27 self.gui['LIST'].add(file,os.path.join(self.path,file),self._action,self._prevtrig)
28 self.gui['LIST'].makelist()
29 else:
30 self.path, d = os.path.split(self.path)
31 self.depth -= 1
33 def init(self):
34 self.filetypes = cfg.modcf['filetypes']
35 self.path = cfg.modcf['path']
36 self.action = cfg.modcf['action']
37 self._path = cfg.modcf['path']
38 self.history = {}
39 self.filllist()
40 self.prevcnt = 0
41 self.history[self.path] = 0,0
42 self.addloop('previewimg', self._prevwait)
43 self.showgeneralgui()
44 self.depth = 0
45 cfg.lastmodname = cfg.modname
47 def keyhandler(self, event):
48 if event.type == KEYDOWN:
49 if event.key == K_DOWN: self.gui['LIST'].select_next()
50 if event.key == K_UP: self.gui['LIST'].select_last()
51 if event.key == K_PAGEDOWN: self.gui['LIST'].next_page()
52 if event.key == K_RIGHT:
53 if os.path.isdir(self.sel_path):
54 self._updprev()
55 self.history[self.path] = self.gui['LIST'].liststart, self.gui['LIST'].selected
56 self.gui['LIST'].clear()
57 self.path = self.sel_path
58 self.filllist()
59 self.depth += 1
60 if event.key == K_LEFT:
61 if self.depth > 0:
62 self._updprev()
63 self.gui['LIST'].clear()
64 self.path, d = os.path.split(self.path)
65 self.filllist()
66 self.depth -= 1
67 else:
68 cfg.modname = 'mainmenu'
69 cfg.modcf = cfg.cf['modules'][cfg.modname]
70 self.loadmodule = 'mainmenu'
71 if event.key == K_RETURN:
72 self.gui['LIST'].press()
73 if event.key == K_y: playlist.PLAYLIST.super_add(self._path, self.filetypes)
75 def joyhandler(self, joystick):
76 tol0 = 0.2
77 tol1 = tol0 - (tol0 * 2)
78 if joystick.get_axis(0) > tol0 or joystick.get_axis(0) < tol1:
79 if joystick.get_axis(0) > 0:
80 pass
81 elif joystick.get_axis(0) < 0:
82 pass
83 if joystick.get_axis(1) > tol0 or joystick.get_axis(1) < tol1:
84 if joystick.get_axis(1) > 0:
85 self.gui['LIST'].select_next()
86 elif joystick.get_axis(1) < 0:
87 self.gui['LIST'].select_last()
89 def _nut(self, value=None):
90 pass
92 def _action(self, value):
93 if self.action == 'PLAY_MUSIC':
94 if os.path.isdir(value):
95 path = value
96 playlist.PLAYLIST.add_dir(path, self.filetypes)
97 elif os.path.isfile(value):
98 path, fil = os.path.split(value)
99 playlist.PLAYLIST.add_dir(path, self.filetypes, self.gui['LIST'].liststart + self.gui['LIST'].selected)
100 elif self.action == 'SHOW_PICTURE':
101 print 'SHOW_PICTURE'
102 else:
103 # pygame stops. This was mainly necessary for ZSNES, which doesn't start with running
104 # pygame display, also it saves a lot cpu-time for the launched application
105 playlist.PLAYLIST.pause()
106 # quitting the display
107 # launching the application with filename parameters
108 os.system(self.action + ' "' + value + '"')
109 # bringing up the display again
110 playlist.PLAYLIST.playpause()
112 def _updprev(self):
113 if os.path.isdir(self.previmage):
114 inp = os.listdir(self.previmage)
115 image = cfg.thpath('blank.png')
116 imagepool = []
117 for line in inp:
118 if self._ispic(os.path.join(self.previmage, line)):
119 #image = os.path.join(self.previmage, line)
120 imagepool.append(os.path.join(self.previmage, line))
121 if len(imagepool) > 0:
122 num = random.randint(0, len(imagepool)-1)
123 image = imagepool[num]
124 self.changebykey('PREVIEW', image)
125 elif self._ispic(self.previmage):
126 self.changebykey('PREVIEW', self.previmage)
128 def _ispic(self, filename):
129 ispic = False
130 if os.path.isfile(filename):
131 ext = os.path.splitext(filename)[1].lower().lstrip('.')
132 if ext in self.filetypes
133 ispic = True
134 return ispic
136 def _prevwait(self):
137 self.prevcnt += 1
138 if self.prevcnt == 15:
139 self._updprev()
141 def _prevtrig(self, value):
142 self.sel_path = os.path.join(self.path, value)
143 self.previmage = value
144 self.prevcnt = 0