From 68bad792dc2f14389362d9973f52a909b75690ee Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Fri, 12 Nov 2010 20:47:14 -0500 Subject: [PATCH] Library backup I can now backup the library to an xml file. This will be useful for library updates in the future. The plan is to eventually backport this feature to Ocarina 4.1 (To make the 4.1 -> 4.2 transition easier) --- libsaria/backup.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++ tests/backup.py | 10 +++++++++ 2 files changed, 64 insertions(+) create mode 100644 libsaria/backup.py create mode 100644 tests/backup.py diff --git a/libsaria/backup.py b/libsaria/backup.py new file mode 100644 index 00000000..516f5658 --- /dev/null +++ b/libsaria/backup.py @@ -0,0 +1,54 @@ +# Bryan Schumaker (11/12/2010) + +from libsaria import path +from libsaria.collection import library +import xm as xml + +add_child = xml.add_child +add_text = xml.add_text +get_attrs = library.get_attrs +lib_file = path.join(path.saria_dir(), "library.xml") + +def encode_attr(doc, node, attr, value): + child = add_child(doc, node, attr) + add_text(doc, child, value) + + +def encode_track(doc, node, id): + child = add_child(doc, node, "track") + (artist, album, title, filepath, score, count, genre, track, year, rate, + channel, seconds, sample, lenstr) = get_attrs( + id, "artist", "album", "title", "filepath", "score", + "count", "genre", "track", "year", "rate", "channel", + "seconds", "sample", "lenstr") + + encode_attr(doc, child, "id", id) + encode_attr(doc, child, "artist", artist) + encode_attr(doc, child, "album", album) + encode_attr(doc, child, "title", title) + encode_attr(doc, child, "filepath", filepath) + encode_attr(doc, child, "score", score) + encode_attr(doc, child, "count", count) + encode_attr(doc, child, "genre", genre) + encode_attr(doc, child, "track", track) + encode_attr(doc, child, "year", year) + encode_attr(doc, child, "rate", rate) + encode_attr(doc, child, "channel", channel) + encode_attr(doc, child, "seconds", seconds) + encode_attr(doc, child, "sample", sample) + encode_attr(doc, child, "lenstr", lenstr) + + +def backup(): + doc = xml.new() + child = add_child(doc, doc, "library") + for id in library.walk(): + encode_track(doc, child, id) + + fout = open(lib_file, 'w') + fout.write(doc.toprettyxml()) + fout.close() + print "Wrote library to: %s" % lib_file + + + diff --git a/tests/backup.py b/tests/backup.py new file mode 100644 index 00000000..0363c795 --- /dev/null +++ b/tests/backup.py @@ -0,0 +1,10 @@ +# Bryan Schumaker (11/12/2010) + +import libsaria +from libsaria import data +from libsaria import collection +from libsaria import backup +library = libsaria.collection.library + +library.load() +backup.backup()