view tools/post_sub.py @ 693:ad69236e8501

For issue #52, update many 3rd party Javascript libraries. Updated to jquery 1.10.2, jquery ui 1.10.3. This broke a lot of stuff. - Found a newer version of the jquery cycle all plugin (3.0.3). - Updated JPlayer to 2.4.0. - Updated to MarkItUp 1.1.14. This also required me to add multiline attributes set to true on various buttons in the markdown set. - As per a stackoverflow post, added some code to get multiline titles in a jQuery UI dialog. They removed that functionality but allow you to put it back. Tweaked the MarkItUp preview CSS to show blockquotes in italic. Did not update TinyMCE at this time. I'm not using the JQuery version and this version appears to work ok for now. What I should do is make a repo for MarkItUp and do a vendor branch thing so I don't have to futz around diffing directories to figure out if I'll lose changes when I update.
author Brian Neal <bgneal@gmail.com>
date Wed, 04 Sep 2013 19:55:20 -0500
parents e877b9c05740
children
line wrap: on
line source
"""
This script reads a .csv dump of the forums post table. It writes a new file,
performing a search and replace over a given field.

The output file can be imported into MySQL with:

LOAD DATA LOCAL INFILE 'forums_post.csv' REPLACE INTO TABLE forums_post
CHARACTER SET utf8
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY ''
LINES TERMINATED BY '\r\n';
SHOW WARNINGS;

"""
from __future__ import with_statement
import csv
import re
import optparse
import sys


USAGE = "usage: %prog [options] infile outfile"
DESCRIPTION = """\
Performs a search and replace on a field in a forums post .csv file.
"""

POST_FIELDS = ('id', 'topic_id', 'user_id', 'creation_date', 'update_date',
        'body', 'html', 'user_ip')


def main(argv=None):
    parser = optparse.OptionParser(usage=USAGE, description=DESCRIPTION)
    parser.set_defaults(
        progress=False,
        field='body',
    )
    parser.add_option("-p", "--progress", action="store_true",
        help="Output a . after every 100 posts to show progress [default: %default]")
    parser.add_option("-f", "--field",
        help="Name of the field to search [default: %default]")
    parser.add_option("-s", "--search", help="The search pattern")
    parser.add_option("-r", "--replace", help="The replacement text")

    opts, args = parser.parse_args(args=argv)

    if len(args) != 2:
        sys.exit("Please supply input and output file arguments.")

    if opts.search is None:
        sys.exit("Please specify a search pattern.")
    search_re = re.compile(opts.search)

    if opts.replace is None:
        sys.exit("Please specify replacement text.")

    with open(args[0], "rb") as infile:
        reader = csv.DictReader(infile)
        if opts.field not in reader.fieldnames:
            sys.exit("Error, invalid field option: %s" % opts.field)

        with open(args[1], "wb") as outfile:
            writer = csv.DictWriter(outfile, POST_FIELDS)

            n = 0
            for row in reader:
                row[opts.field] = search_re.sub(opts.replace, row[opts.field])
                writer.writerow(row)

                if n % 100 == 0:
                    sys.stdout.write('.')
                    sys.stdout.flush()

            print

if __name__ == '__main__':
    try:
        main()
    except IOError, ex:
        sys.exit("IO Error: %s" % ex)
    except KeyboardInterrupt:
        sys.exit("Control-C interrupt")