#!/usr/bin/env python
import argparse
from configparser import ConfigParser
from contextlib import closing
from os.path import dirname, join, normpath, realpath
import sys
from urllib2 import urlopen

encodings = ("utf-8", "iso-8859-1")

def read_pls(path):
    try:
        with closing(urlopen(path)) as resp:
            return resp.read(), None
    except ValueError:
        the_path = normpath(realpath(path))
        with open(the_path, "r") as fh:
            return fh.read(), dirname(the_path)

def decode(text):
    result = None
    for enc in encodings:
        try:
            result = text.decode(enc)
            break
        except UnicodeDecodeError:
            pass
    return result

def playlist_files(config):
    n = config.getint("playlist", "NumberOfEntries")
    for i in xrange(1, n+1):
        yield config.get("playlist", "File%d" % i)

parser = argparse.ArgumentParser()
parser.add_argument("--mpc", dest = "mpc", action = "store_const", const = True, default = False)
parser.add_argument("--encoding", dest = "encodings", metavar = "encoding1,encoding2,...", action = "store")
parser.add_argument("paths", metavar = "path-or-URL", nargs = "+")
args = parser.parse_args()

if args.encodings != None:
    encodings = args.encodings.split(",")

config = ConfigParser()
for path in args.paths:
    try:
        pls_bytes, directory = read_pls(path)
        pls = decode(pls_bytes)
        if pls != None:
            config.read_string(pls)
            for raw_fn in playlist_files(config):
                fn = join(directory, raw_fn) if directory != None else raw_fn
                if args.mpc and directory != None:
                    fn = "file://" + fn
                print fn
        else:
            print >> sys.stderr, "%s\n    Failed to decode file (tried %s)" % (path, ", ".join(encodings))
    except Exception, e:
        print >> sys.stderr, "%s\n    [%s] %s" % (path, type(e).__name__, e)

@@ -198,31 +198,35 @@
 en%0A%0A
-encodings
+DEFAULT_ENCODINGS
  = 
-(
 %22utf-8
-%22, %22
+,
 iso-
@@ -232,17 +232,16 @@
 -8859-1%22
-)
 %0A%0Adef re
@@ -898,16 +898,71 @@
 tParser(
+description = %22Print the playlist items in .pls files.%22
 )%0Aparser
@@ -983,16 +983,20 @@
 %22--mpc%22,
+%0A   
  dest = 
@@ -1001,16 +1001,20 @@
 = %22mpc%22,
+%0A   
  action 
@@ -1029,16 +1029,20 @@
 _const%22,
+%0A   
  const =
@@ -1047,16 +1047,20 @@
  = True,
+%0A   
  default
@@ -1067,16 +1067,80 @@
  = False
+,%0A    help = %22print items in a format compatible with %60mpc add%60%22
 )%0Aparser
@@ -1166,16 +1166,20 @@
 coding%22,
+%0A   
  dest = 
@@ -1190,16 +1190,20 @@
 odings%22,
+%0A   
  metavar
@@ -1231,16 +1231,20 @@
 g2,...%22,
+%0A   
  action 
@@ -1252,16 +1252,124 @@
  %22store%22
+,%0A    default = DEFAULT_ENCODINGS,%0A    help = %22character encodings to try (default: %25s)%22 %25 DEFAULT_ENCODINGS
 )%0Aparser
@@ -1390,16 +1390,20 @@
 %22paths%22,
+%0A   
  metavar
@@ -1419,16 +1419,20 @@
 or-URL%22,
+%0A   
  nargs =
@@ -1439,69 +1439,91 @@
  %22+%22
-)%0Aargs = parser.parse_args()%0A%0Aif args.encodings != None:%0A    
+,%0A    help = %22file system path or URL to a .pls file%22)%0Aargs = parser.parse_args()%0A%0A
 enco

add description of program and parameters