Пример #1
0
class Browser:
	'Class for traversing the browser structure and controlling local media playback'

	def __init__(self):
		self.dbh = DbHandler()
		self.dbh.initmetadata()
		self.player = Player()
		self.sh = SpotifyHandler()
		self.rootnode = self.initbrowsernodes()
		self.curnode = self.rootnode
		self.mostrecentplayer = ''

	def initbrowsernodes(self):
		root = BrowserNode('root', self.dbh, self.sh)

		localmedia = BrowserNode('Local media', self.dbh, self.sh)
		spotify = BrowserNode('Spotify', self.dbh, self.sh, 'spotify playlists')
		restart = BrowserNode('Restart', self.dbh, self.sh, 'restart')

		albumartists = BrowserNode('Album artists', self.dbh, self.sh, 'tags', 'albumartist')
		artists = BrowserNode('Artists', self.dbh, self.sh, 'tags', 'artist')
		composers = BrowserNode('Composers', self.dbh, self.sh, 'tags', 'composer')
		genres = BrowserNode('Genres', self.dbh, self.sh, 'tags', 'genre')

		localmedia.addchild(albumartists)
		localmedia.addchild(artists)
		localmedia.addchild(composers)
		localmedia.addchild(genres)

		root.addchild(localmedia)
		root.addchild(spotify)
		root.addchild(restart)

		return root

	def curlist(self):
		children = []
		for child in self.curnode.getchildren():
			children.append(str(child))
		return children

	def select(self, index):
		selection = self.curnode.getchild(index)
		if selection.querytarget == 'play local':
			self.mostrecentplayer = 'local'
			self.sh.stop()
			filenames = []
			for child in self.curnode.getchildren():
				filenames.append(child.querysearch)
			self.player.play(filenames, index)
		elif selection.querytarget == 'play spotify':
			self.mostrecentplayer = 'spotify'
			self.player.stop()
			self.sh.selecttrack(selection.querysearch)
			self.sh.play()
		elif selection.querytarget == 'restart':
			self.restart_program()
		else:
			self.curnode = self.curnode.getchild(index)
		return self.curlist()

	def back(self):
		parent = self.curnode.getparent()
		if parent:
			self.curnode = parent
		return self.curlist()

	def togglepause(self):
		if self.mostrecentplayer == 'local':
			self.player.pause()
		elif self.mostrecentplayer == 'spotify':
			self.sh.togglepause()

	def next(self):
		if self.mostrecentplayer == 'local':
			self.player.next()
		elif self.mostrecentplayer == 'spotify':
			self.sh.next()

	def prev(self):
		if self.mostrecentplayer == 'local':
			self.player.prev()
		elif self.mostrecentplayer == 'spotify':
			self.sh.prev()

	def restart_program(self):
		python = sys.executable
		os.execl(python, python, *sys.argv)