Skip to content

Commit

Permalink
Merge pull request #28 from bjarneo/feature/commander
Browse files Browse the repository at this point in the history
Commander
  • Loading branch information
bjarneo committed Dec 8, 2016
2 parents c661225 + f02a63d commit 40aa3f9
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 19 deletions.
8 changes: 7 additions & 1 deletion pytify/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pytify.strategy import get_pytify_class_by_platform
from pytify.song_list import SongList
from pytify.prompt import custom_prompt
from pytify.commander import Commander
import argparse
import sys
import pkg_resources
Expand All @@ -13,6 +14,8 @@ class App:
def __init__(self):
self.pytify = get_pytify_class_by_platform()()

self.command = Commander(self.pytify)

self.run()

def list_songs(self, list):
Expand Down Expand Up @@ -68,9 +71,12 @@ def interaction(self):
while 1:
search_input = custom_prompt()

if self.command.run(search_input):
continue

search = self.pytify.query(search_input)

if search is not False:
if search:
self.list_songs(list=self.pytify.list())


Expand Down
74 changes: 74 additions & 0 deletions pytify/commander.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from __future__ import absolute_import, unicode_literals


class Commander():
def __init__(self, Pytifylib):
self.pytify = Pytifylib

def parse(self, command):
if command and command[0] != '/':
return ''

command = command.replace('/', '')

return command

def commands(self):
return {
'help': 'list all commands',
'current': 'print current song (currently linux only)',
'next': 'play next song',
'prev': 'play previous song',
'pp': 'play or pause song',
'stop': 'stop',
'history': 'last five search results'
}

def validate(self, command):
for key in self.commands():
if command != key:
continue

return True

return False

def help(self):
print('\nCommands:')

for key, val in self.commands().items():
print(' {0:20} {1:75}'.format(key, val))

print('\n')

def run(self, command):
command = self.parse(command)

if not command:
return False

if not self.validate(command):
self.help()

if command == 'help':
self.help()

elif command == 'current':
print(self.pytify.get_current_playing())

elif command == 'next':
self.pytify.next()

elif command == 'prev':
self.pytify.prev()

elif command == 'pp':
self.pytify.play_pause()

elif command == 'stop':
self.pytify.stop()

elif command == 'history':
self.pytify.print_history()

return True
2 changes: 1 addition & 1 deletion pytify/dbus/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def get_metadata(self):
def get_current_playing(self):
playing = {}

for key, value in self.get_metadata().iteritems():
for key, value in self.get_metadata().items():
if key == 'xesam:album':
playing['album'] = value

Expand Down
16 changes: 8 additions & 8 deletions pytify/pytifylib.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ def list(self):
'-' * 30
))

for i in self.get_songs():
for key, song in self.get_songs().items():
list.append(space.format(
'%d.' % i,
'%s' % self.get_songs()[i]['artist'],
'%s' % self.get_songs()[i]['song'],
'%s' % self.get_songs()[i]['album']
'%d.' % key,
'%s' % song['artist'],
'%s' % song['song'],
'%s' % song['album']
))

return list
Expand All @@ -103,9 +103,6 @@ def _get_song_name_at_index(self, index):
self._songs[index]['song'])
)

def listen(self, index):
raise NotImplementedError()

def print_history(self):
if len(self._history) > 5:
self._history.pop(0)
Expand All @@ -115,6 +112,9 @@ def print_history(self):
for song in self._history:
print(song)

def listen(self, index):
raise NotImplementedError()

def next(self):
raise NotImplementedError()

Expand Down
16 changes: 8 additions & 8 deletions pytify/song_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, items):
curses.endwin()

# Display window
self.display()
curses.wrapper(self.display)

def shortcuts(self):
self.items.append(' ')
Expand All @@ -58,16 +58,16 @@ def navigate(self, n):
elif self.position > self.song_length:
self.position = self.song_length

def display(self):
def display(self, stdscr):
self.panel.top()
self.panel.show()
self.window.clear()
stdscr.clear()

# Play keys.
play = lambda c: c == ord('p') or c == curses.KEY_ENTER or c == 10 or c == 13

while True:
self.window.refresh()
stdscr.refresh()
curses.doupdate()

for index, item in enumerate(self.items):
Expand All @@ -76,9 +76,9 @@ def display(self):
else:
mode = curses.A_NORMAL

self.window.addstr(index, 1, str(item), mode)
stdscr.addstr(index, 1, str(item), mode)

key = self.window.getch()
key = stdscr.getch()

# Start song
if play(key):
Expand Down Expand Up @@ -108,13 +108,13 @@ def display(self):
elif key == ord('s'):
break

# Search
# Quit
elif key == ord('q'):
curses.endwin()
sys.exit()


self.window.clear()
stdscr.clear()
self.panel.hide()
panel.update_panels()
curses.doupdate()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup

__version__ = '3.1.5'
__version__ = '3.3.0'

setup(
name='pytify',
Expand Down

0 comments on commit 40aa3f9

Please sign in to comment.