From 751ba78de56345e2747d94ba65e27516d9f4edfe Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Thu, 25 Apr 2013 19:10:08 +0200 Subject: [PATCH] Add option to suppress output --- jamendoparser.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/jamendoparser.py b/jamendoparser.py index a3060c1..4ff4adf 100644 --- a/jamendoparser.py +++ b/jamendoparser.py @@ -5,8 +5,9 @@ xml_url = "http://img.jamendo.com/data/dbdump_artistalbumtrack.xml.gz" def update_progress(count, blocksize, totalsize): percent = int(count * blocksize * 100 / totalsize) - sys.stdout.write("\rRetrieving Jamendo database... %2d%%" % percent) - sys.stdout.flush() + if options['no_output'] == False: + sys.stdout.write("\rRetrieving Jamendo database... %2d%%" % percent) + sys.stdout.flush() def get_attribute(element, tagname): val = element.find(tagname) @@ -24,6 +25,9 @@ parser = argparse.ArgumentParser(description='Downloads and parses the Jamendo X parser.add_argument('-D', dest='no_download', action='store_true', help='don\'t download the XML dump and use an existing XML dump instead') +parser.add_argument('-N', dest='no_output', action='store_true', + help='prevents the application from outputting anything') + parser.add_argument('-d', dest='database', action='store', default='jamendo.db', help='path of the database that should be used to store the data (will be created if it does not exist yet)') @@ -37,7 +41,8 @@ xml_file = options['xml_path'] if options['no_download'] == False: urllib.urlretrieve(xml_url, xml_file, reporthook=update_progress) - print "" + if options['no_output'] == False: + print "" database = sqlite3.connect(options['database']) cursor = database.cursor() @@ -118,13 +123,18 @@ for event, element in iterparse(xml, tag="artist"): tagweight = get_attribute(tag, 'weight') cursor.execute("INSERT INTO tags VALUES (?, ?, ?)", (trackid, tagid, tagweight)) - sys.stdout.write("\rInserting artists... %6d done" % (total + 1)) - sys.stdout.flush() + if options['no_output'] == False: + sys.stdout.write("\rInserting artists... %6d done" % (total + 1)) + sys.stdout.flush() total += 1 element.clear() -print "" -print "Parsed and inserted a total of %d artists." % total +if options['no_output'] == False: + print "" + print "Parsed and inserted a total of %d artists." % total + database.commit() -print "Changes committed to database." + +if options['no_output'] == False: + print "Changes committed to database."