Exemplo n.º 1
0
    def seek(self, timespec, position=None):
        status = self.mpc.status()

        if position is None:
            # Default is current song
            position = int(status["song"])
            elapsedtime, totaltime = parseTimeStatus(status["time"])
        else:
            stats = self.mpc.playlistinfo(position)
            # Check if the position refers to the current song.
            if status["songid"] == stats[0]["id"]:
                elapsedtime, totaltime = parseTimeStatus(status["time"])
            else:
                elapsedtime = 0
                totaltime = int(stats[0]["time"])

        seekto = parseTimeSpec(timespec, elapsedtime, totaltime)

        if self.output:
            printByName("seek", position=position, seekto=seekto)
        ret = self.mpc.seek(position, seekto)
        if self.after_status:
            self.status(after_command=True)

        return ret
Exemplo n.º 2
0
    def setvol(self, volumespec):
        relative = 0
        if volumespec.startswith("+"): relative = 1
        elif volumespec.startswith("-"): relative = -1

        if relative:
            status = self.mpc.status()

            volumechange = int(volumespec[1:])
            volume = int(status["volume"])
        else:
            volume = int(volumespec)
            if self.output:
                printByName("setvol", volume=volume)
            ret = self.mpc.setvol(volume)

            if self.after_status:
                self.status(after_command=True)
            return ret

        if relative == 1:
            newvolume = volume + volumechange
        elif relative == -1:
            newvolume = volume - volumechange

        if self.output:
            printByName("setvol", volume=newvolume)
        ret = self.mpc.setvol(newvolume)

        if self.after_status:
            self.status(after_command=True)
        return ret
Exemplo n.º 3
0
    def delete(self, position):
        """Delete song specified at position,
        Position can be a range like num-num
        If position is 0, remove the current playing song."""
        if "-" in position:
            split_position = position.split("-")
            if len(split_position) != 2:
                printError("delete_parse", position=position)
                return None
            elif not split_position[0]:
                # Negative number?
                try:
                    end = -1 * int(split_position[1])
                except ValueError:
                    printError("delete_parse", position=position)
                else:
                    printError("delete_negative", position=[end,])
                return None
            else:
                begin, end = split_position

            try:
                begin = int(begin) - 1
                end = int(end) - 1
            except ValueError:
                printError("delete_parse", position=position)
                return None

            if begin < 0 or end < 0:
                printError("delete_negative", position=[begin+1, end+1])
                return None

            if begin > end:
                begin, end = end, begin
            elif begin == end:
                end+=1

            printByName("delete", position=list(range(begin+1, end+1)))

            self.mpc.command_list_ok_begin()
            diff = 0
            for pos in range(begin, end):
                self.mpc.delete(pos - diff)
                diff += 1
            return self.mpc.command_list_end()

        if position == "0":
            # Remove current song
            pos = int(self.mpc.currentsong()["pos"])
        else:
            try:
                pos = int(position) - 1
            except ValueError:
                printError("delete_parse", position=position)
                return None

        printByName("delete", position=[pos+1,])

        return self.mpc.delete(pos)
Exemplo n.º 4
0
    def plchanges(self, version):
        ret = self.mpc.plchanges(version)

        if self.output:
            currentsong = self.mpc.currentsong()
            printByName("plchanges", currentsong=currentsong, results=ret)

        return ret
Exemplo n.º 5
0
    def enableoutput(self, outputid):
        if self.output:
            printByName("enableoutput", outputid=outputid)
        ret = self.mpc.enableoutput(outputid)

        if self.after_status:
            self.outputs()
        return ret
Exemplo n.º 6
0
 def volume(self, volumespec=None):
     if volumespec is None:
         status = self.mpc.status()
         volume = status["volume"]
         if self.output:
             printByName("volume", volume=volume)
     else:
         self.setvol(volumespec)
Exemplo n.º 7
0
    def addid(self, path, position=None):
        if position is None:
            ret = self.mpc.addid(path)
        else:
            ret = self.mpc.addid(path, position)

        if self.output:
            printByName("addid", playlistid=ret)
        return ret
Exemplo n.º 8
0
 def func(*args):
     if args:
         ret = getattr(self.mpc, attr)(*args)
     else:
         ret = getattr(self.mpc, attr)()
     kwargs = { self._common_commands[attr] : ret }
     if self.output:
         printByName(attr, **kwargs)
     return ret
Exemplo n.º 9
0
    def playlist(self, song=None):
        if song is None:
            ret = self.mpc.playlistinfo()
        else:
            ret = self.mpc.playlistinfo(song)

        if self.output:
            currentsong = self.mpc.currentsong()
            printByName("playlist", currentsong=currentsong, results=ret)

        return ret
Exemplo n.º 10
0
 def authenticate(self, funcname):
     """authenticate with the server if not allowed to execute funcname."""
     if funcname in self.mpd_notcommands:
         authfunc = self.__super.__getattr__("password")
         if config_data.has_option("mpd", "password"):
             authfunc(config_data.get("mpd", "password"))
         else:
             import getpass
             printByName("askpass", command=funcname)
             authfunc(getpass.getpass(_("Password: "******"notcommands")()
Exemplo n.º 11
0
    def lsplaylists(self, prefix=None):
        ret = self.mpc.lsinfo()
        playlists = []
        for value in ret:
            if "playlist" in value:
                if prefix is None or value["playlist"].startswith(prefix):
                    playlists.append(value)

        if self.output:
            printByName("lsplaylists", results=playlists)

        return playlists
Exemplo n.º 12
0
    def crossfade(self, seconds=None):
        if seconds is None:
            current_fade = self.mpc.status().get('xfade', 0)
            if self.output:
                printByName("crossfade", seconds=None, current=current_fade)
        else:
            if self.output:
                printByName("crossfade", seconds=seconds, current=None)
            ret = self.mpc.crossfade(seconds)

            if self.after_status:
                self.status()
            return ret
Exemplo n.º 13
0
    def update(self, *paths):
        if not paths:
            ret = self.mpc.update()
        else:
            self.mpc.authenticate("update")
            self.mpc.command_list_ok_begin()
            for path in paths:
                self.mpc.update(path)
            # When calling multiple update() commands only
            # one job id is returned.
            ret = self.mpc.command_list_end()[-1]

        if self.output:
            printByName("update", paths=list(paths), job=ret)
        return ret
Exemplo n.º 14
0
    def add(self, *paths):
        if not paths:
            paths = list(paths)
            # Read from standard input
            for line in sys.stdin.readlines():
                if line.endswith("\n"):
                    line = line[:-1]
                paths.append(line)

        self.mpc.authenticate("add")
        self.mpc.command_list_ok_begin()
        for path in paths:
            self.mpc.add(path)
            if self.output:
                printByName("add", path=path)
        return self.mpc.command_list_end()
Exemplo n.º 15
0
    def status(self, after_command=False):
        ret = self.mpc.status()

        if "songid" in ret:
            # We have a song playing/paused/stopped
            # Get information about it.
            song = self.mpc.playlistid(ret["songid"])
            if song:
                song = song[0]
            else:
                song = dict()
        else:
            song = dict()

        if self.output:
            printByName("status", status=ret, song=song)
        return ret
Exemplo n.º 16
0
    def crop(self):
        self.mpc.authenticate("currentsong")
        self.mpc.command_list_ok_begin()
        self.mpc.currentsong()
        self.mpc.playlistinfo()
        currentsong, songlist = self.mpc.command_list_end()

        if self.output:
            printByName("crop", currentsong=currentsong)

        self.mpc.authenticate("deleteid")
        self.mpc.command_list_ok_begin()
        for song in songlist:
            if currentsong and currentsong["id"] == song["id"]:
                continue
            self.mpc.deleteid(song["id"])
        return self.mpc.command_list_end()
Exemplo n.º 17
0
    def seekid(self, timespec, songid=None):
        status = self.mpc.status()

        if songid is None:
            # Default is current song
            songid = int(status["songid"])
            elapsedtime, totaltime = parseTimeStatus(status["time"])
        else:
            # Check if the position refers to the current song.
            if songid == int(status["songid"]):
                elapsedtime, totaltime = parseTimeStatus(status["time"])
            else:
                stats = self.mpc.playlistid(songid)
                elapsedtime = 0
                totaltime = int(stats[0]["time"])

        seekto = parseTimeSpec(timespec, elapsedtime, totaltime)

        if self.output: printByName("seekid", songid=songid, seekto=seekto)
        ret = self.mpc.seekid(songid, seekto)
        if self.after_status:
            self.status(after_command=True)

        return ret
Exemplo n.º 18
0
    def repeat(self, state=None):
        if state is None:
            # Toggle random mode
            current_state = self.mpc.status()["repeat"]
            if current_state == "0":
                new_state = 1
            elif current_state == "1":
                new_state = 0
        elif state in BOOLEAN_TRUE:
            new_state = 0
        elif state in BOOLEAN_FALSE:
            new_state = 1
        else:
            printError("not_boolean", state=state,
                    boolean_true=BOOLEAN_TRUE, boolean_false=BOOLEAN_FALSE)
            return None

        if self.output:
            printByName("repeat", state=new_state)
        ret = self.mpc.repeat(new_state)

        if self.after_status:
            self.status()
        return ret
Exemplo n.º 19
0
 def version(self):
     if self.output:
         printByName("version", version=self.mpc.mpd_version)
     return self.mpc.mpd_version
Exemplo n.º 20
0
 def func(*args):
     if self.output:
         printByName(attr, args=args)
     return getattr(self.mpc, attr)(*args)
Exemplo n.º 21
0
 def func(qtype, query):
     ret = getattr(self.mpc, attr)(qtype, query)
     if self.output:
         printByName(attr, qtype=qtype, query=query, results=ret)
     return ret