Remove six as a dependency

This commit is contained in:
Marco Bonetti 2021-07-17 18:19:59 +02:00
parent cc12a30761
commit e095402daa
6 changed files with 40 additions and 38 deletions

View file

@ -7,6 +7,7 @@ Version 0.9.7 (unreleased)
* Arabic translation. (#257, thanks @Bashar)
* Translations via the DeepL API (#258, thanks @halitcelik)
* Fixed unicode handling in gettext headers (#259, thanks @NotSqrt)
* Remove six as a dependency
Version 0.9.6

View file

@ -6,11 +6,8 @@ from django.conf import settings
from django.core.cache import caches
from django.core.exceptions import ImproperlyConfigured
import six
from .conf import settings as rosetta_settings
cache = caches[rosetta_settings.ROSETTA_CACHE_NAME]
@ -49,8 +46,13 @@ class SessionRosettaStorage(BaseRosettaStorage):
def __init__(self, request):
super(SessionRosettaStorage, self).__init__(request)
if 'signed_cookies' in settings.SESSION_ENGINE and 'pickle' not in settings.SESSION_SERIALIZER.lower():
raise ImproperlyConfigured("Sorry, but django-rosetta doesn't support the `signed_cookies` SESSION_ENGINE, because rosetta specific session files cannot be serialized.")
if (
'signed_cookies' in settings.SESSION_ENGINE
and 'pickle' not in settings.SESSION_SERIALIZER.lower()
):
raise ImproperlyConfigured(
"Sorry, but django-rosetta doesn't support the `signed_cookies` SESSION_ENGINE, because rosetta specific session files cannot be serialized."
)
def get(self, key, default=None):
if key in self.request.session:
@ -64,7 +66,7 @@ class SessionRosettaStorage(BaseRosettaStorage):
return key in self.request.session
def delete(self, key):
del(self.request.session[key])
del self.request.session[key]
class CacheRosettaStorage(BaseRosettaStorage):
@ -76,21 +78,32 @@ class CacheRosettaStorage(BaseRosettaStorage):
if 'rosetta_cache_storage_key_prefix' in self.request.session:
self._key_prefix = self.request.session['rosetta_cache_storage_key_prefix']
else:
self._key_prefix = hashlib.new('sha1', six.text_type(time.time()).encode('utf8')).hexdigest()
self._key_prefix = hashlib.new(
'sha1', str(time.time()).encode('utf8')
).hexdigest()
self.request.session['rosetta_cache_storage_key_prefix'] = self._key_prefix
if self.request.session['rosetta_cache_storage_key_prefix'] != self._key_prefix:
raise ImproperlyConfigured("You can't use the CacheRosettaStorage because your Django Session storage doesn't seem to be working. The CacheRosettaStorage relies on the Django Session storage to avoid conflicts.")
raise ImproperlyConfigured(
"You can't use the CacheRosettaStorage because your Django Session storage doesn't seem to be working. The CacheRosettaStorage relies on the Django Session storage to avoid conflicts."
)
# Make sure we're not using DummyCache
if 'dummycache' in settings.CACHES[rosetta_settings.ROSETTA_CACHE_NAME]['BACKEND'].lower():
raise ImproperlyConfigured("You can't use the CacheRosettaStorage if your cache isn't correctly set up (you are using the DummyCache cache backend).")
if (
'dummycache'
in settings.CACHES[rosetta_settings.ROSETTA_CACHE_NAME]['BACKEND'].lower()
):
raise ImproperlyConfigured(
"You can't use the CacheRosettaStorage if your cache isn't correctly set up (you are using the DummyCache cache backend)."
)
# Make sure the cache actually works
try:
self.set('rosetta_cache_test', 'rosetta')
if not self.get('rosetta_cache_test') == 'rosetta':
raise ImproperlyConfigured("You can't use the CacheRosettaStorage if your cache isn't correctly set up, please double check your Django DATABASES setting and that the cache server is responding.")
raise ImproperlyConfigured(
"You can't use the CacheRosettaStorage if your cache isn't correctly set up, please double check your Django DATABASES setting and that the cache server is responding."
)
finally:
self.delete('rosetta_cache_test')
@ -113,6 +126,7 @@ class CacheRosettaStorage(BaseRosettaStorage):
def get_storage(request):
from rosetta.conf import settings
storage_module, storage_class = settings.STORAGE_CLASS.rsplit('.', 1)
storage_module = importlib.import_module(storage_module)
return getattr(storage_module, storage_class)(request)

View file

@ -1,10 +1,8 @@
import re
import six
from django import template
from django.utils.html import escape
from django.utils.safestring import mark_safe
from rosetta.access import can_translate
register = template.Library()
@ -77,7 +75,7 @@ class IncrNode(template.Node):
def render(self, context):
self.val += 1
return six.text_type(self.val)
return str(self.val)
def is_fuzzy(message):

View file

@ -3,9 +3,9 @@ import os
import os.path
import re
import zipfile
from io import BytesIO
from urllib.parse import urlencode
import six
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import user_passes_test
@ -153,9 +153,7 @@ class RosettaFileLevelMixin(RosettaBaseMixin):
# value of the meat of each entry on its side in an attribute
# called "md5hash".
str_to_hash = (
six.text_type(entry.msgid)
+ six.text_type(entry.msgstr)
+ six.text_type(entry.msgctxt or '')
str(entry.msgid) + str(entry.msgstr) + str(entry.msgctxt or '')
).encode('utf8')
entry.md5hash = hashlib.md5(str_to_hash).hexdigest()
else:
@ -169,9 +167,7 @@ class RosettaFileLevelMixin(RosettaBaseMixin):
# a hashed value of the meat of each entry on its side in
# an attribute called "md5hash".
str_to_hash = (
six.text_type(entry.msgid)
+ six.text_type(entry.msgstr)
+ six.text_type(entry.msgctxt or '')
str(entry.msgid) + str(entry.msgstr) + str(entry.msgctxt or '')
).encode('utf8')
entry.md5hash = hashlib.new('md5', str_to_hash).hexdigest()
storage.set(self.po_file_cache_key, po_file)
@ -308,7 +304,7 @@ class TranslationFormView(RosettaFileLevelMixin, TemplateView):
# polib parses .po files into unicode strings, but
# doesn't bother to convert plural indexes to int,
# so we need unicode here.
plural_id = six.text_type(plural_id)
plural_id = str(plural_id)
# Above no longer true as of Polib 1.0.4
if plural_id and plural_id.isdigit():
@ -502,7 +498,7 @@ class TranslationFormView(RosettaFileLevelMixin, TemplateView):
message.main_lang = main_lang_po.find(message.msgid).msgstr
# Collect some constants for the template
rosetta_i18n_lang_name = six.text_type(
rosetta_i18n_lang_name = str(
dict(rosetta_settings.ROSETTA_LANGUAGES).get(self.language_id)
)
# "bidi" as in "bi-directional"
@ -626,12 +622,12 @@ class TranslationFormView(RosettaFileLevelMixin, TemplateView):
def concat_entry(e):
return (
six.text_type(e.msgstr)
+ six.text_type(e.msgid)
+ six.text_type(e.msgctxt)
+ six.text_type(e.comment)
str(e.msgstr)
+ str(e.msgid)
+ str(e.msgctxt)
+ str(e.comment)
+ u''.join([o[0] for o in e.occurrences])
+ six.text_type(e.msgid_plural)
+ str(e.msgid_plural)
+ u''.join(e.msgstr_plural.values())
)
@ -670,9 +666,9 @@ class TranslationFileDownload(RosettaFileLevelMixin, View):
offered_fn = self.po_file_path.split('/')[-1]
po_fn = str(self.po_file_path.split('/')[-1])
mo_fn = str(po_fn.replace('.po', '.mo')) # not so smart, huh
zipdata = six.BytesIO()
zipdata = BytesIO()
with zipfile.ZipFile(zipdata, mode="w") as zipf:
zipf.writestr(po_fn, six.text_type(self.po_file).encode("utf8"))
zipf.writestr(po_fn, str(self.po_file).encode("utf8"))
zipf.writestr(mo_fn, self.po_file.to_binary())
zipdata.seek(0)

View file

@ -70,12 +70,7 @@ setup(
],
include_package_data=True,
zip_safe=False,
install_requires=[
'six >=1.2.0',
'Django >= 2.0',
'requests >= 2.1.0',
'polib >= 1.1.0',
],
install_requires=['Django >= 2.0', 'requests >= 2.1.0', 'polib >= 1.1.0'],
tests_require=['tox', 'vcrpy'],
cmdclass={'test': Tox},
)

View file

@ -38,11 +38,9 @@ deps =
django32: pymemcache
requests
polib>=1.1.0
six
goslate
vcrpy
coverage
pudb
[testenv:gettext]
basepython = python3