Ticket #5530: issue_5530.patch

File issue_5530.patch, 2.1 KB (added by simenheg, 7 years ago)
  • mediagoblin/gmg_commands/batchaddmedia.py

    From 2ea9438729c3c28fc98ff08bf4b50fa64638400b Mon Sep 17 00:00:00 2001
    From: =?UTF-8?q?Simen=20Heggest=C3=B8yl?= <simenheg@gmail.com>
    Date: Sun, 8 Oct 2017 11:04:10 +0200
    Subject: [PATCH] Port batchaddmedia command to Python 3
    
    ---
     mediagoblin/gmg_commands/batchaddmedia.py | 12 +++++++++---
     1 file changed, 9 insertions(+), 3 deletions(-)
    
    diff --git a/mediagoblin/gmg_commands/batchaddmedia.py b/mediagoblin/gmg_commands/batchaddmedia.py
    index 274d72bc..e80a8601 100644
    a b from __future__ import print_function  
    1919import codecs
    2020import csv
    2121import os
     22import sys
    2223
    2324import requests
    2425import six
    def batchaddmedia(args):  
    99100        contents = all_metadata.read()
    100101        media_metadata = parse_csv_file(contents)
    101102
    102     for media_id, file_metadata in media_metadata.iteritems():
     103    for media_id, file_metadata in media_metadata.items():
    103104        files_attempted += 1
    104105        # In case the metadata was not uploaded initialize an empty dictionary.
    105106        json_ld_metadata = compact_and_validate({})
    Metadata was not uploaded.""".format(  
    141142                file_path = os.path.join(abs_metadata_dir, path)
    142143                file_abs_path = os.path.abspath(file_path)
    143144            try:
    144                 media_file = file(file_abs_path, 'r')
     145                media_file = open(file_abs_path, 'rb')
    145146            except IOError:
    146147                print(_(u"""\
    147148FAIL: Local file {filename} could not be accessed.
    def parse_csv_file(file_contents):  
    203204    # Build a dictionary
    204205    for index, line in enumerate(lines):
    205206        if line.isspace() or line == u'': continue
    206         values = unicode_csv_reader([line]).next()
     207        if (sys.version_info[0] == 3):
     208            # Python 3's csv.py supports Unicode out of the box.
     209            reader = csv.reader([line])
     210        else:
     211            reader = unicode_csv_reader([line])
     212        values = next(reader)
    207213        line_dict = dict([(key[i], val)
    208214            for i, val in enumerate(values)])
    209215        media_id = line_dict.get('id') or index