aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/tools
diff options
context:
space:
mode:
authorChristopher Allan Webber <cwebber@dustycloud.org>2014-09-22 13:58:13 -0500
committerChristopher Allan Webber <cwebber@dustycloud.org>2014-09-22 13:58:13 -0500
commita6252cbf211691aa9d81fd535f5af7bcdbbd6620 (patch)
treeca1131ba7a8ff0437d392c20a70f17ee7470e3dd /mediagoblin/tools
parentc47f402d7ee42e88ee1086351010dd598da6c0a5 (diff)
parent6430ae97eca57f4db4bcef54436df6c2abcd21ad (diff)
downloadmediagoblin-a6252cbf211691aa9d81fd535f5af7bcdbbd6620.tar.lz
mediagoblin-a6252cbf211691aa9d81fd535f5af7bcdbbd6620.tar.xz
mediagoblin-a6252cbf211691aa9d81fd535f5af7bcdbbd6620.zip
Merge branch 'merge-python3-port'
Conflicts: setup.py
Diffstat (limited to 'mediagoblin/tools')
-rw-r--r--mediagoblin/tools/crypto.py6
-rw-r--r--mediagoblin/tools/exif.py15
-rw-r--r--mediagoblin/tools/mail.py20
-rw-r--r--mediagoblin/tools/metadata.py3
-rw-r--r--mediagoblin/tools/pagination.py6
-rw-r--r--mediagoblin/tools/processing.py11
-rw-r--r--mediagoblin/tools/response.py3
-rw-r--r--mediagoblin/tools/staticdirect.py4
-rw-r--r--mediagoblin/tools/template.py11
-rw-r--r--mediagoblin/tools/translate.py17
-rw-r--r--mediagoblin/tools/url.py4
-rw-r--r--mediagoblin/tools/workbench.py10
12 files changed, 65 insertions, 45 deletions
diff --git a/mediagoblin/tools/crypto.py b/mediagoblin/tools/crypto.py
index b219a484..c85ecd4a 100644
--- a/mediagoblin/tools/crypto.py
+++ b/mediagoblin/tools/crypto.py
@@ -51,7 +51,7 @@ def load_key(filename):
def create_key(key_dir, key_filepath):
global __itsda_secret
- old_umask = os.umask(077)
+ old_umask = os.umask(0o77)
key_file = None
try:
if not os.path.isdir(key_dir):
@@ -60,7 +60,7 @@ def create_key(key_dir, key_filepath):
key = str(getrandbits(192))
key_file = tempfile.NamedTemporaryFile(dir=key_dir, suffix='.bin',
delete=False)
- key_file.write(key)
+ key_file.write(key.encode('ascii'))
key_file.flush()
os.rename(key_file.name, key_filepath)
key_file.close()
@@ -79,7 +79,7 @@ def setup_crypto():
key_filepath = os.path.join(key_dir, 'itsdangeroussecret.bin')
try:
load_key(key_filepath)
- except IOError, error:
+ except IOError as error:
if error.errno != errno.ENOENT:
raise
create_key(key_dir, key_filepath)
diff --git a/mediagoblin/tools/exif.py b/mediagoblin/tools/exif.py
index 50f1aabf..ec83f43c 100644
--- a/mediagoblin/tools/exif.py
+++ b/mediagoblin/tools/exif.py
@@ -14,6 +14,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+import six
+
from exifread import process_file
from exifread.utils import Ratio
@@ -75,7 +77,7 @@ def extract_exif(filename):
Returns EXIF tags found in file at ``filename``
"""
try:
- with file(filename) as image:
+ with open(filename, 'rb') as image:
return process_file(image, details=False)
except IOError:
raise BadMediaFail(_('Could not read the image file.'))
@@ -94,7 +96,7 @@ def clean_exif(exif):
'Thumbnail JPEGInterchangeFormat']
return dict((key, _ifd_tag_to_dict(value)) for (key, value)
- in exif.iteritems() if key not in disabled_tags)
+ in six.iteritems(exif) if key not in disabled_tags)
def _ifd_tag_to_dict(tag):
@@ -110,7 +112,7 @@ def _ifd_tag_to_dict(tag):
'field_length': tag.field_length,
'values': None}
- if isinstance(tag.printable, str):
+ if isinstance(tag.printable, six.binary_type):
# Force it to be decoded as UTF-8 so that it'll fit into the DB
data['printable'] = tag.printable.decode('utf8', 'replace')
@@ -118,7 +120,7 @@ def _ifd_tag_to_dict(tag):
data['values'] = [_ratio_to_list(val) if isinstance(val, Ratio) else val
for val in tag.values]
else:
- if isinstance(tag.values, str):
+ if isinstance(tag.values, six.binary_type):
# Force UTF-8, so that it fits into the DB
data['values'] = tag.values.decode('utf8', 'replace')
else:
@@ -132,7 +134,8 @@ def _ratio_to_list(ratio):
def get_useful(tags):
- return dict((key, tag) for (key, tag) in tags.iteritems())
+ from collections import OrderedDict
+ return OrderedDict((key, tag) for (key, tag) in six.iteritems(tags))
def get_gps_data(tags):
@@ -149,7 +152,7 @@ def get_gps_data(tags):
'latitude': tags['GPS GPSLatitude'],
'longitude': tags['GPS GPSLongitude']}
- for key, dat in dms_data.iteritems():
+ for key, dat in six.iteritems(dms_data):
gps_data[key] = (
lambda v:
float(v[0].num) / float(v[0].den) \
diff --git a/mediagoblin/tools/mail.py b/mediagoblin/tools/mail.py
index ab355835..ab3e0eaa 100644
--- a/mediagoblin/tools/mail.py
+++ b/mediagoblin/tools/mail.py
@@ -14,11 +14,13 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+from __future__ import print_function, unicode_literals
+
import six
import smtplib
import sys
-from email.MIMEText import MIMEText
from mediagoblin import mg_globals, messages
+from mediagoblin._compat import MIMEText
from mediagoblin.tools import common
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -130,12 +132,12 @@ def send_email(from_addr, to_addrs, subject, message_body):
EMAIL_TEST_INBOX.append(message)
elif mg_globals.app_config['email_debug_mode']:
- print u"===== Email ====="
- print u"From address: %s" % message['From']
- print u"To addresses: %s" % message['To']
- print u"Subject: %s" % message['Subject']
- print u"-- Body: --"
- print message.get_payload(decode=True)
+ print("===== Email =====")
+ print("From address: %s" % message['From'])
+ print("To addresses: %s" % message['To'])
+ print("Subject: %s" % message['Subject'])
+ print("-- Body: --")
+ print(message.get_payload(decode=True))
return mhost.sendmail(from_addr, to_addrs, message.as_string())
@@ -162,5 +164,5 @@ def email_debug_message(request):
if mg_globals.app_config['email_debug_mode']:
# DEBUG message, no need to translate
messages.add_message(request, messages.DEBUG,
- u"This instance is running in email debug mode. "
- u"The email will be on the console of the server process.")
+ "This instance is running in email debug mode. "
+ "The email will be on the console of the server process.")
diff --git a/mediagoblin/tools/metadata.py b/mediagoblin/tools/metadata.py
index bfefcac9..aeb4f829 100644
--- a/mediagoblin/tools/metadata.py
+++ b/mediagoblin/tools/metadata.py
@@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+from io import open
import os
import copy
import json
@@ -102,7 +103,7 @@ def load_resource(package, resource_path):
os.path.sep.
"""
filename = resource_filename(package, os.path.sep.join(resource_path))
- return file(filename).read()
+ return open(filename, encoding="utf-8").read()
def load_resource_json(package, resource_path):
"""
diff --git a/mediagoblin/tools/pagination.py b/mediagoblin/tools/pagination.py
index 855878e0..a525caf7 100644
--- a/mediagoblin/tools/pagination.py
+++ b/mediagoblin/tools/pagination.py
@@ -17,9 +17,11 @@
import urllib
import copy
from math import ceil, floor
-from itertools import izip, count
+from itertools import count
from werkzeug.datastructures import MultiDict
+from six.moves import zip
+
PAGINATION_DEFAULT_PER_PAGE = 30
@@ -52,7 +54,7 @@ class Pagination(object):
if jump_to_id:
cursor = copy.copy(self.cursor)
- for (doc, increment) in izip(cursor, count(0)):
+ for (doc, increment) in list(zip(cursor, count(0))):
if doc.id == jump_to_id:
self.page = 1 + int(floor(increment / self.per_page))
diff --git a/mediagoblin/tools/processing.py b/mediagoblin/tools/processing.py
index 2abe6452..39a329c5 100644
--- a/mediagoblin/tools/processing.py
+++ b/mediagoblin/tools/processing.py
@@ -18,8 +18,7 @@ import logging
import json
import traceback
-from urllib2 import urlopen, Request, HTTPError
-from urllib import urlencode
+from six.moves.urllib import request, parse
_log = logging.getLogger(__name__)
@@ -37,10 +36,10 @@ def create_post_request(url, data, **kw):
data_parser: The parser function that is used to parse the `data`
argument
'''
- data_parser = kw.get('data_parser', urlencode)
+ data_parser = kw.get('data_parser', parse.urlencode)
headers = kw.get('headers', {})
- return Request(url, data_parser(data), headers=headers)
+ return request.Request(url, data_parser(data), headers=headers)
def json_processing_callback(entry):
@@ -76,11 +75,11 @@ def json_processing_callback(entry):
data_parser=json.dumps)
try:
- urlopen(request)
+ request.urlopen(request)
_log.debug('Processing callback for {0} sent'.format(entry))
return True
- except HTTPError:
+ except request.HTTPError:
_log.error('Failed to send callback: {0}'.format(
traceback.format_exc()))
diff --git a/mediagoblin/tools/response.py b/mediagoblin/tools/response.py
index 88270265..889938a8 100644
--- a/mediagoblin/tools/response.py
+++ b/mediagoblin/tools/response.py
@@ -16,6 +16,7 @@
import json
+import six
import werkzeug.utils
from werkzeug.wrappers import Response as wz_Response
from mediagoblin.tools.template import render_template
@@ -153,7 +154,7 @@ def json_response(serializable, _disable_cors=False, *args, **kw):
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With'}
- for key, value in cors_headers.iteritems():
+ for key, value in six.iteritems(cors_headers):
response.headers.set(key, value)
return response
diff --git a/mediagoblin/tools/staticdirect.py b/mediagoblin/tools/staticdirect.py
index 8381b8b6..881dd20e 100644
--- a/mediagoblin/tools/staticdirect.py
+++ b/mediagoblin/tools/staticdirect.py
@@ -24,6 +24,8 @@
import logging
+import six
+
_log = logging.getLogger(__name__)
@@ -48,7 +50,7 @@ class StaticDirect(object):
def __init__(self, domains):
self.domains = dict(
[(key, value.rstrip('/'))
- for key, value in domains.iteritems()])
+ for key, value in six.iteritems(domains)])
self.cache = {}
def __call__(self, filepath, domain=None):
diff --git a/mediagoblin/tools/template.py b/mediagoblin/tools/template.py
index e5acdf45..b01196fd 100644
--- a/mediagoblin/tools/template.py
+++ b/mediagoblin/tools/template.py
@@ -14,6 +14,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+import six
import jinja2
from jinja2.ext import Extension
@@ -33,7 +34,6 @@ from mediagoblin.tools.pluginapi import get_hook_templates, hook_transform
from mediagoblin.tools.timesince import timesince
from mediagoblin.meddleware.csrf import render_csrf_form_token
-
SETUP_JINJA_ENVS = {}
@@ -66,9 +66,12 @@ def get_jinja_env(template_loader, locale):
'jinja2.ext.i18n', 'jinja2.ext.autoescape',
TemplateHookExtension] + local_exts)
- template_env.install_gettext_callables(
- mg_globals.thread_scope.translations.ugettext,
- mg_globals.thread_scope.translations.ungettext)
+ if six.PY2:
+ template_env.install_gettext_callables(mg_globals.thread_scope.translations.ugettext,
+ mg_globals.thread_scope.translations.ungettext)
+ else:
+ template_env.install_gettext_callables(mg_globals.thread_scope.translations.gettext,
+ mg_globals.thread_scope.translations.ngettext)
# All templates will know how to ...
# ... fetch all waiting messages and remove them from the queue
diff --git a/mediagoblin/tools/translate.py b/mediagoblin/tools/translate.py
index f77351b5..a5e56cfe 100644
--- a/mediagoblin/tools/translate.py
+++ b/mediagoblin/tools/translate.py
@@ -17,6 +17,7 @@
import gettext
import pkg_resources
+import six
from babel import localedata
from babel.support import LazyProxy
@@ -52,9 +53,9 @@ class ReallyLazyProxy(LazyProxy):
"""
Like LazyProxy, except that it doesn't cache the value ;)
"""
- @property
- def value(self):
- return self._func(*self._args, **self._kwargs)
+ def __init__(self, func, *args, **kwargs):
+ super(ReallyLazyProxy, self).__init__(func, *args, **kwargs)
+ object.__setattr__(self, '_is_cache_enabled', False)
def __repr__(self):
return "<%s for %s(%r, %r)>" % (
@@ -146,8 +147,9 @@ def pass_to_ugettext(*args, **kwargs):
The reason we can't have a global ugettext method is because
mg_globals gets swapped out by the application per-request.
"""
- return mg_globals.thread_scope.translations.ugettext(
- *args, **kwargs)
+ if six.PY2:
+ return mg_globals.thread_scope.translations.ugettext(*args, **kwargs)
+ return mg_globals.thread_scope.translations.gettext(*args, **kwargs)
def pass_to_ungettext(*args, **kwargs):
"""
@@ -156,8 +158,9 @@ def pass_to_ungettext(*args, **kwargs):
The reason we can't have a global ugettext method is because
mg_globals gets swapped out by the application per-request.
"""
- return mg_globals.thread_scope.translations.ungettext(
- *args, **kwargs)
+ if six.PY2:
+ return mg_globals.thread_scope.translations.ungettext(*args, **kwargs)
+ return mg_globals.thread_scope.translations.ngettext(*args, **kwargs)
def lazy_pass_to_ugettext(*args, **kwargs):
diff --git a/mediagoblin/tools/url.py b/mediagoblin/tools/url.py
index 657c0373..4d97247a 100644
--- a/mediagoblin/tools/url.py
+++ b/mediagoblin/tools/url.py
@@ -17,6 +17,8 @@
import re
from unidecode import unidecode
+import six
+
_punct_re = re.compile(r'[\t !"#:$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
@@ -27,4 +29,4 @@ def slugify(text, delim=u'-'):
result = []
for word in _punct_re.split(text.lower()):
result.extend(unidecode(word).split())
- return unicode(delim.join(result))
+ return six.text_type(delim.join(result))
diff --git a/mediagoblin/tools/workbench.py b/mediagoblin/tools/workbench.py
index 0bd4096b..f1ad6414 100644
--- a/mediagoblin/tools/workbench.py
+++ b/mediagoblin/tools/workbench.py
@@ -18,10 +18,15 @@ import os
import shutil
import tempfile
+import six
+
+from mediagoblin._compat import py2_unicode
# Actual workbench stuff
# ----------------------
+
+@py2_unicode
class Workbench(object):
"""
Represent the directory for the workbench
@@ -36,11 +41,8 @@ class Workbench(object):
"""
self.dir = dir
- def __unicode__(self):
- return unicode(self.dir)
-
def __str__(self):
- return str(self.dir)
+ return six.text_type(self.dir)
def __repr__(self):
try: