# HG changeset patch # User Paul Boddie # Date 1333151682 -7200 # Node ID 3107768d9a3bee8a7bec317b4721a7aebb60e05a # Parent 584da002fc01e1170505b67490f62a6921da1f3b Removed symbolic link creation since the import manifests indicate the actual locations of the revisions. Added some docstrings and comments. diff -r 584da002fc01 -r 3107768d9a3b convert.py --- a/convert.py Sat Mar 31 01:35:36 2012 +0200 +++ b/convert.py Sat Mar 31 01:54:42 2012 +0200 @@ -1,6 +1,6 @@ #!/usr/bin/env python -from os import mkdir, makedirs, symlink +from os import mkdir, makedirs from os.path import exists, extsep, join, splitext from zipfile import ZipFile from cStringIO import StringIO @@ -31,30 +31,6 @@ if objecttype == "Page": - # Handle pages. - - if content.has_key("historicalVersions"): - versions = content["historicalVersions"] - - # Make a page directory and links from the versions to separate - # files. - - page_dir = join(pages_dir, identifier) - revisions_dir = join(page_dir, "revisions") - - mkdir(page_dir) - mkdir(revisions_dir) - - # Historical versions are stored separately. - - for n, (cls, version) in enumerate(versions): - if cls == "Page": - symlink(join("..", "..", "..", "..", versions_dir, version), join(revisions_dir, str(n+1))) - - # The page always seems to hold the current version itself. - - symlink(join("..", "..", "..", "..", versions_dir, identifier), join(revisions_dir, str(len(versions)+1))) - # Handle pages and revisions, adding revisions to the page manifest. if content.has_key("originalVersion"): @@ -114,16 +90,28 @@ self.elements.append((attributes[-1]["class"], text.strip())) def mkdirs(name): + + "Make the directory with the given 'name' at any depth." + try: makedirs(name) except OSError: pass def append(filename, s): + + "Append to the file with the given 'filename' the string 's'." + write(filename, s, True) def write(filename, s, append=False): - f = codecs.open(filename, append and "ab" or "wb", encoding="utf-8") + + """ + Write to the file with the given 'filename' the string 's'. If the optional + 'append' parameter is set to a true value, 's' will be appended to the file. + """ + + f = codecs.open(filename, append and "a" or "w", encoding="utf-8") try: f.write(s) finally: @@ -154,6 +142,8 @@ p["collection"] = handler.handle_collection p["element"] = handler.handle_element + # Open the XML dump. + f = open(filename) if is_zipfile: @@ -162,9 +152,14 @@ else: ff = f + # Parse the data. + try: p.parse(ff) finally: f.close() + # Tidy up the import manifests, sorting each of them by revision and + # finalising them. + # vim: tabstop=4 expandtab shiftwidth=4