From cbf589ccbaa8a96f596f956cd103e754f0936e28 Mon Sep 17 00:00:00 2001 From: kaliko Date: Wed, 4 Apr 2012 14:40:46 +0000 Subject: [PATCH] * add Goodie class (wraps mpdclass and startop together) --- src/crop | 10 ++-- src/lib/goodies.py | 50 +++++++++++++++++++ src/lib/mpdclass.py | 22 +++++++-- src/lib/mpdutils.py | 118 -------------------------------------------- src/lib/startop.py | 20 ++++---- src/mfade | 11 ++--- src/mtopls | 13 +++-- src/nalbum | 13 +++-- src/wakeup | 13 +++-- 9 files changed, 104 insertions(+), 166 deletions(-) create mode 100755 src/lib/goodies.py delete mode 100644 src/lib/mpdutils.py diff --git a/src/crop b/src/crop index 5f7dde5..4829a65 100755 --- a/src/crop +++ b/src/crop @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (c) 2009, 2010 Efrim +# Copyright (c) 2009, 2010, 2012 Kaliko Jack # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -19,15 +19,14 @@ import sys -from lib.mpdclass import MPDClass -from lib.startop import StartOpt +from lib.goodies import Goodie NAME = 'crop' VERSION = '0.1' USAGE = 'USAGE: %prog [--help] | [ ]' -class Crop(StartOpt, MPDClass): +class Crop(Goodie): """ """ script_info = dict({ @@ -39,8 +38,7 @@ class Crop(StartOpt, MPDClass): def __init__(self): """""" - StartOpt.__init__(self, self.__class__.script_info, []) - MPDClass.__init__(self) + Goodie.__init__(self, self.__class__.script_info) self.nb_tracks = 6 self._get_arg() self._run() diff --git a/src/lib/goodies.py b/src/lib/goodies.py new file mode 100755 index 0000000..3cda566 --- /dev/null +++ b/src/lib/goodies.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2012 Kaliko Jack +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +""" +DOC: + All goodies inherit from Goodie which in turn inherits from StartOpt and + MPDClass. + + StartOpt deals with command line argument and options, it will look for env. + var. $MPD_HOST and $MPD_PORT as well + + MPDClass is a plain MPD client +""" + +from lib.mpdclass import MPDClass +from lib.startop import StartOpt + + +class Goodie(StartOpt, MPDClass): + """""" + + def __init__(self, script_info, extra_options=[]): + """""" + StartOpt.__init__(self, script_info, extra_options) + self.con_id = dict({'host': self.cli_options.host, + 'port': self.cli_options.port}) + MPDClass.__init__(self) + + +# Script starts here +if __name__ == '__main__': + pass + +# VIM MODLINE +# vim: ai ts=4 sw=4 sts=4 expandtab diff --git a/src/lib/mpdclass.py b/src/lib/mpdclass.py index 8ea0191..d20b022 100755 --- a/src/lib/mpdclass.py +++ b/src/lib/mpdclass.py @@ -1,6 +1,22 @@ -#!/usr/bin/env python # -*- coding: utf-8 -*- +# Copyright (c) 2009, 2010, 2012 Kaliko Jack +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + + import sys from socket import error as SocketError @@ -22,10 +38,8 @@ class MPDClass(object): """ Simple wrapper to connect MPD. """ - con_id = dict({'host': self.cli_options.host, - 'port': self.cli_options.port}) try: - self.client.connect(**con_id) + self.client.connect(**self.con_id) except SocketError: return False return True diff --git a/src/lib/mpdutils.py b/src/lib/mpdutils.py deleted file mode 100644 index ce1b38d..0000000 --- a/src/lib/mpdutils.py +++ /dev/null @@ -1,118 +0,0 @@ -# -*- coding: utf-8 -*- - -# Copyright (c) 2009 Efrim {{{ -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# -# }}} - -import sys - -from os import environ -from socket import error as SocketError - -try: - from mpd import (MPDClient, CommandError) -except ImportError, err: - print 'ERROR: "%s"\n\nPlease install python-mpd module.\n' % err - sys.exit(1) - - -def get_mpd_environ():#{{{ - """ - Retrieve MPD env. var. - """ - con_id = dict({'host': 'localhost', - 'port': int(6600)}) - passwd = None - mpd_host_env = environ.get('MPD_HOST') - if mpd_host_env: - # If password is set: - # mpd_host_env = ['pass', 'host'] because MPD_HOST=pass@host - mpd_host_env = mpd_host_env.split('@') - mpd_host_env.reverse() - con_id.update({'host': mpd_host_env[0]}) - if len(mpd_host_env) > 1: - print 'passwd set in MPD_HOST' - passwd = mpd_host_env[1] - - con_id.update({'port': int(environ.get('MPD_PORT', con_id.get('port')))}) - return (con_id, passwd)#}}} - - -def mpdConnect(client, con_id):#{{{ - """ - Simple wrapper to connect MPD. - """ - try: - client.connect(**con_id) - except SocketError: - return False - return True#}}} - - -def mpdAuth(client, secret):#{{{ - """ - Authenticate - """ - try: - client.password(secret) - except CommandError: - return False - return True#}}} - - -def mconnect(host=None, port=None):#{{{ - """""" - ## get connection id from ENV VAR - con_id, passwd = get_mpd_environ() - if host: - con_id.update({'host': host}) - if port: - con_id.update({'port': port}) - ## MPD object instance - client = MPDClient() - if mpdConnect(client, con_id): - #print 'Got connected!' - True - else: - print 'ERROR: fail to connect MPD server.' - sys.exit(1) - - ## Auth if password is set non False - if passwd: - if mpdAuth(client, passwd): - #print 'Pass auth!' - True - else: - print 'ERROR: fail trying to pass auth. Check password?' - client.disconnect() - sys.exit(1) - return client#}}} - -def collapse_tags(value): - if isinstance(value, list): - #self.__dict__[tag] = ", ".join(set(value)) - self.collapse_tags_bool = True - return ', '.join(set(value)) - return value#}}} - -# Script starts here -if __name__ == '__main__': - cli = mconnect() - cli.disconnect() - -# VIM MODLINE -# vim: ai ts=4 sw=4 sts=4 expandtab - diff --git a/src/lib/startop.py b/src/lib/startop.py index b73160b..c44159f 100644 --- a/src/lib/startop.py +++ b/src/lib/startop.py @@ -6,7 +6,6 @@ from os import environ # Options list # pop out 'sw' value before creating OptionParser object. -##{{{ OPTS = list([ { 'sw': ['-S', '--hostname'], @@ -19,12 +18,11 @@ OPTS = list([ 'dest': 'port', 'help': 'Port MPD in listening on (default: 6600 or MPD_PORT if set)'}, ]) -#}}} USAGE = u"""USAGE: %prog [--help] [options]""" -def get_mpd_environ():#{{{ +def get_mpd_environ(): """ Retrieve MPD env. var. """ @@ -43,16 +41,17 @@ def get_mpd_environ():#{{{ if len(mpd_host_env) > 1: print 'passwd set in MPD_HOST' passwd = mpd_host_env[1] + con_id.update({'passwd': passwd}) if mpd_port: con_id.update({'port': int(mpd_port)}) - return (con_id, passwd)#}}} + return (con_id, passwd) class StartOpt(object): """ """ - def __init__(self, script_info, child_options):#{{{ + def __init__(self, script_info, child_options): # Strong assumption? self.localencoding = 'utf8' self.parser = None @@ -63,17 +62,17 @@ class StartOpt(object): # options allows to add new cli options within child objects calling # parent __init__() self.options = list(child_options + OPTS) - self.main()#}}} + self.main() - def _get_encoding(self):#{{{ + def _get_encoding(self): """Get local encoding""" - self.localencoding = getpreferredencoding()#}}} + self.localencoding = getpreferredencoding() def _u8_convert(self, string): """Convert CLI input string to UTF8 (mpd standart)""" return unicode(string, self.localencoding).encode('UTF-8') - def declare_opts(self):#{{{ + def declare_opts(self): """ Declare options in OptionParser object. """ @@ -98,7 +97,7 @@ class StartOpt(object): if dest in ['host']: self.parser.set_defaults(host=con_id.get('host')) if dest in ['port']: - self.parser.set_defaults(port=con_id.get('port'))#}}} + self.parser.set_defaults(port=con_id.get('port')) def main(self): """declare options, parse command line""" @@ -116,4 +115,3 @@ if __name__ == '__main__': # VIM MODLINE # vim: ai ts=4 sw=4 sts=4 expandtab - diff --git a/src/mfade b/src/mfade index 0ffd9df..2a791ca 100755 --- a/src/mfade +++ b/src/mfade @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (c) 2009, 2010 Efrim +# Copyright (c) 2009, 2010, 2012 Kaliko Jack # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -28,8 +28,8 @@ import sys from time import sleep -from lib.mpdclass import MPDClass -from lib.startop import StartOpt +from lib.goodies import Goodie + NAME = 'mfade' VERSION = '0.1' @@ -39,7 +39,7 @@ to 50% when paused or stopped and from current volume to 10th of it if playing, both over 10 minutes.""" -class Sleep(StartOpt, MPDClass): +class Sleep(Goodie): """""" script_info = dict({ 'version': VERSION, @@ -50,8 +50,7 @@ class Sleep(StartOpt, MPDClass): def __init__(self): """""" - StartOpt.__init__(self, self.__class__.script_info, []) - MPDClass.__init__(self) + Goodie.__init__(self, self.__class__.script_info) self.tempo = int(10) self.volum = None self._consume_args() diff --git a/src/mtopls b/src/mtopls index 1210fd6..6a845ad 100755 --- a/src/mtopls +++ b/src/mtopls @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (c) 2009, 2010 Efrim +# Copyright (c) 2009, 2010, 2012 Kaliko Jack # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -21,17 +21,17 @@ import sys from os import(access, W_OK) -from os.path import (dirname, isfile, join, basename, abspath) +from os.path import (dirname, isfile, basename) + +from lib.goodies import Goodie -from lib.mpdclass import MPDClass -from lib.startop import StartOpt NAME = 'mtopls' VERSION = '0.1' USAGE = 'USAGE: %prog [--help] | /path/to/the/playlist/file/' -class MtoPls(StartOpt, MPDClass): +class MtoPls(Goodie): """ """ script_info = dict({ @@ -43,8 +43,7 @@ class MtoPls(StartOpt, MPDClass): def __init__(self): """""" - StartOpt.__init__(self, self.__class__.script_info, []) - MPDClass.__init__(self) + Goodie.__init__(self, self.__class__.script_info) self.playlist = None self._run() diff --git a/src/nalbum b/src/nalbum index e48ad68..ae53c79 100755 --- a/src/nalbum +++ b/src/nalbum @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (c) 2009, 2010 Efrim {{{ +# Copyright (c) 2009, 2010, 2012 Kaliko Jack # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -16,15 +16,14 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . # -# }}} import sys from time import sleep -from lib.mpdclass import MPDClass -from lib.startop import StartOpt +from lib.goodies import Goodie + NAME = 'nalbum' VERSION = '0.1' @@ -39,7 +38,7 @@ NALBUM_OPTS = list([ ]) -class Nalbum(StartOpt, MPDClass): +class Nalbum(Goodie): """ """ script_info = dict({ @@ -50,8 +49,8 @@ class Nalbum(StartOpt, MPDClass): def __init__(self): """""" - StartOpt.__init__(self, self.__class__.script_info, NALBUM_OPTS) - MPDClass.__init__(self) + Goodie.__init__(self, self.__class__.script_info, + extra_options=NALBUM_OPTS) self._run() def _fade(self, io='out'): diff --git a/src/wakeup b/src/wakeup index 7009081..79013f0 100755 --- a/src/wakeup +++ b/src/wakeup @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (c) 2009, 2010 Efrim {{{ +# Copyright (c) 2009, 2010, 2012 Kaliko Jack # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -16,15 +16,14 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . # -# }}} import sys from random import choice -from lib.mpdclass import MPDClass -from lib.startop import StartOpt +from lib.goodies import Goodie + NAME = 'wakeup' VERSION = '0.1' @@ -40,7 +39,7 @@ WAKEUP_OPTS = list([ ]) -class WakeUp(StartOpt, MPDClass): +class WakeUp(Goodie): """ """ script_info = dict({ @@ -52,8 +51,8 @@ class WakeUp(StartOpt, MPDClass): def __init__(self): """""" - StartOpt.__init__(self, self.__class__.script_info, WAKEUP_OPTS) - MPDClass.__init__(self) + Goodie.__init__(self, self.__class__.script_info, + extra_options=WAKEUP_OPTS) self._get_arg() self._run() -- 2.39.2