From a1372b5f667ca365d9bcbefc3ae3ee28716225b9 Mon Sep 17 00:00:00 2001
From: Tanner Prestegard <tanner.prestegard@ligo.org>
Date: Thu, 18 Jul 2019 13:48:23 -0500
Subject: [PATCH] Python 3: fix all StringIO usage

---
 gracedb/api/v1/events/views.py                       |  4 ++--
 gracedb/core/middleware/profiling.py                 |  4 ++--
 .../migrations/0036_populate_voevent_fields.py       |  7 +++++--
 gracedb/events/models.py                             | 12 ++++++------
 gracedb/events/reports.py                            |  4 ++--
 gracedb/events/translator.py                         |  4 ++--
 .../migrations/0004_populate_voevent_fields.py       |  7 +++++--
 gracedb/superevents/models.py                        |  6 +++---
 8 files changed, 27 insertions(+), 21 deletions(-)

diff --git a/gracedb/api/v1/events/views.py b/gracedb/api/v1/events/views.py
index 1321a43ef..eebfa6a28 100644
--- a/gracedb/api/v1/events/views.py
+++ b/gracedb/api/v1/events/views.py
@@ -4,9 +4,9 @@ import logging
 import os
 import shutil
 try:
-    from io import StringIO
-except ImportError:  # python < 3
     from StringIO import StringIO
+except ImportError:  # python >= 3
+    from io import StringIO
 
 from django.conf import settings
 from django.contrib.auth.models import User, Permission, Group as DjangoGroup
diff --git a/gracedb/core/middleware/profiling.py b/gracedb/core/middleware/profiling.py
index 6e6b0254d..b78257d8c 100644
--- a/gracedb/core/middleware/profiling.py
+++ b/gracedb/core/middleware/profiling.py
@@ -7,9 +7,9 @@ import os
 import re
 import tempfile
 try:
-    from io import StringIO
-except ImportError:  # python < 3
     from StringIO import StringIO
+except ImportError:  # python >= 3
+    from io import StringIO
 
 import hotshot, hotshot.stats
 
diff --git a/gracedb/events/migrations/0036_populate_voevent_fields.py b/gracedb/events/migrations/0036_populate_voevent_fields.py
index f5da68f22..32ac1bacb 100644
--- a/gracedb/events/migrations/0036_populate_voevent_fields.py
+++ b/gracedb/events/migrations/0036_populate_voevent_fields.py
@@ -1,7 +1,10 @@
 # -*- coding: utf-8 -*-
 # Generated by Django 1.11.20 on 2019-06-10 16:58
 from __future__ import unicode_literals
-from cStringIO import StringIO
+try:
+    from StringIO import StringIO
+except ImportError:  # python >= 3
+    from io import StringIO
 from hashlib import sha1
 import os
 from lxml import etree
@@ -66,7 +69,7 @@ def get_datadir(event_or_superevent):
     hash_input = str(event_or_superevent.id)
     if event_or_superevent.__class__.__name__.lower() == 'superevent':
         hash_input = 'superevent' + hash_input
-    hdf = StringIO(sha1(hash_input).hexdigest())
+    hdf = StringIO(sha1(hash_input.encode()).hexdigest())
 
     # Build up the nodes of the directory structure
     nodes = [hdf.read(i) for i in settings.GRACEDB_DIR_DIGITS]
diff --git a/gracedb/events/models.py b/gracedb/events/models.py
index c46b9992a..05f6ed93d 100644
--- a/gracedb/events/models.py
+++ b/gracedb/events/models.py
@@ -35,7 +35,10 @@ from django.conf import settings
 import pytz
 import calendar
 
-from io import StringIO
+try:
+    from StringIO import StringIO
+except ImportError:  # python >= 3
+    from io import StringIO
 
 from hashlib import sha1
 import shutil
@@ -241,11 +244,8 @@ class Event(models.Model):
     @property
     def datadir(self):
         # Create a file-like object which is the SHA-1 hexdigest of the Event's primary key
-        hid = sha1(str(self.id).encode("utf-8")).hexdigest()
-        try:
-            hdf = StringIO(hid.decode("utf-8"))
-        except AttributeError:  # python < 3
-            hdf = StringIO(hid)
+        hid = sha1(str(self.id).encode()).hexdigest()
+        hdf = StringIO(hid)
 
         # Build up the nodes of the directory structure
         nodes = [hdf.read(i) for i in settings.GRACEDB_DIR_DIGITS]
diff --git a/gracedb/events/reports.py b/gracedb/events/reports.py
index b6d70420e..01b824b41 100644
--- a/gracedb/events/reports.py
+++ b/gracedb/events/reports.py
@@ -30,9 +30,9 @@ from django.utils import timezone
 import pytz
 import json
 try:
-    from io import StringIO
-except ImportError:  # python < 3
     from StringIO import StringIO
+except ImportError:  # python >= 3
+    from io import StringIO
 
 @internal_user_required
 def histo(request):
diff --git a/gracedb/events/translator.py b/gracedb/events/translator.py
index 0877df9c2..93105fb74 100644
--- a/gracedb/events/translator.py
+++ b/gracedb/events/translator.py
@@ -19,9 +19,9 @@ from core.vfile import VersionedFile
 
 import json
 try:
-    from io import StringIO
-except ImportError:  # python < 3
     from StringIO import StringIO
+except ImportError:  # python >= 3
+    from io import StringIO
 
 from math import sqrt
 
diff --git a/gracedb/superevents/migrations/0004_populate_voevent_fields.py b/gracedb/superevents/migrations/0004_populate_voevent_fields.py
index a693ff835..2adc34c7f 100644
--- a/gracedb/superevents/migrations/0004_populate_voevent_fields.py
+++ b/gracedb/superevents/migrations/0004_populate_voevent_fields.py
@@ -1,7 +1,10 @@
 # -*- coding: utf-8 -*-
 # Generated by Django 1.11.20 on 2019-06-10 16:58
 from __future__ import unicode_literals
-from cStringIO import StringIO
+try:
+    from StringIO import StringIO
+except ImportError:  # python >= 3
+    from io import StringIO
 from hashlib import sha1
 import os
 from lxml import etree
@@ -62,7 +65,7 @@ def get_datadir(event_or_superevent):
     hash_input = str(event_or_superevent.id)
     if event_or_superevent.__class__.__name__.lower() == 'superevent':
         hash_input = 'superevent' + hash_input
-    hdf = StringIO(sha1(hash_input).hexdigest())
+    hdf = StringIO(sha1(hash_input.encode()).hexdigest())
 
     # Build up the nodes of the directory structure
     nodes = [hdf.read(i) for i in settings.GRACEDB_DIR_DIGITS]
diff --git a/gracedb/superevents/models.py b/gracedb/superevents/models.py
index 815603832..11b0c9125 100644
--- a/gracedb/superevents/models.py
+++ b/gracedb/superevents/models.py
@@ -1,7 +1,7 @@
 try:
-    from io import StringIO
-except ImportError:  # python < 3
     from StringIO import StringIO
+except ImportError:  # python >= 3
+    from io import StringIO
 import datetime
 from hashlib import sha1
 import logging
@@ -365,7 +365,7 @@ class Superevent(CleanSaveModel, AutoIncrementModel):
         # object's primary key. We prepend 'superevent' so as to not
         # have collisions with Event files
         hash_input = 'superevent' + str(self.id)
-        hdf = StringIO(sha1(hash_input).hexdigest().decode('utf-8'))
+        hdf = StringIO(sha1(hash_input.encode()).hexdigest())
 
         # Build up the nodes of the directory structure
         nodes = [hdf.read(i) for i in settings.GRACEDB_DIR_DIGITS]
-- 
GitLab