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
|
| 19 | 19 | import codecs |
| 20 | 20 | import csv |
| 21 | 21 | import os |
| | 22 | import sys |
| 22 | 23 | |
| 23 | 24 | import requests |
| 24 | 25 | import six |
| … |
… |
def batchaddmedia(args):
|
| 99 | 100 | contents = all_metadata.read() |
| 100 | 101 | media_metadata = parse_csv_file(contents) |
| 101 | 102 | |
| 102 | | for media_id, file_metadata in media_metadata.iteritems(): |
| | 103 | for media_id, file_metadata in media_metadata.items(): |
| 103 | 104 | files_attempted += 1 |
| 104 | 105 | # In case the metadata was not uploaded initialize an empty dictionary. |
| 105 | 106 | json_ld_metadata = compact_and_validate({}) |
| … |
… |
Metadata was not uploaded.""".format(
|
| 141 | 142 | file_path = os.path.join(abs_metadata_dir, path) |
| 142 | 143 | file_abs_path = os.path.abspath(file_path) |
| 143 | 144 | try: |
| 144 | | media_file = file(file_abs_path, 'r') |
| | 145 | media_file = open(file_abs_path, 'rb') |
| 145 | 146 | except IOError: |
| 146 | 147 | print(_(u"""\ |
| 147 | 148 | FAIL: Local file {filename} could not be accessed. |
| … |
… |
def parse_csv_file(file_contents):
|
| 203 | 204 | # Build a dictionary |
| 204 | 205 | for index, line in enumerate(lines): |
| 205 | 206 | 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) |
| 207 | 213 | line_dict = dict([(key[i], val) |
| 208 | 214 | for i, val in enumerate(values)]) |
| 209 | 215 | media_id = line_dict.get('id') or index |