Skip to content

Commit 8a047c4

Browse files
authored
Merge pull request #559 from a-detiste/master
remove more Python2 compatibility shims
1 parent 5d3e4f9 commit 8a047c4

File tree

13 files changed

+34
-93
lines changed

13 files changed

+34
-93
lines changed

src/webassets/bundle.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from contextlib import contextmanager
22
import os
33
from os import path
4-
from webassets import six
54

65
from .filter import get_filter
76
from .merge import (FileHunk, UrlHunk, FilterTool, merge, merge_filters,

src/webassets/cache.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import errno
1919
import tempfile
2020
import warnings
21-
from webassets import six
2221
from webassets.merge import BaseHunk
2322
from webassets.filter import Filter, freezedicts
2423
from webassets.utils import md5_constructor, pickle
@@ -67,9 +66,9 @@ def walk(obj):
6766
yield obj.data().encode('utf-8')
6867
elif isinstance(obj, int):
6968
yield str(obj).encode('utf-8')
70-
elif isinstance(obj, six.text_type):
69+
elif isinstance(obj, str):
7170
yield obj.encode('utf-8')
72-
elif isinstance(obj, six.binary_type):
71+
elif isinstance(obj, bytes):
7372
yield obj
7473
elif hasattr(obj, "id"):
7574
for i in walk(obj.id()):

src/webassets/filter/compass.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import shutil
3333
import subprocess
3434
from io import open
35-
from webassets import six
3635

3736
from webassets.exceptions import FilterError
3837
from webassets.filter import Filter, option
@@ -49,13 +48,13 @@ def string_rep(val):
4948
""" Determine the correct string rep for the config file """
5049
if isinstance(val, bool):
5150
# True -> true and False -> false
52-
return six.text_type(val).lower()
53-
elif isinstance(val, six.string_types) and val.startswith(':'):
51+
return str(val).lower()
52+
elif isinstance(val, str) and val.startswith(':'):
5453
# ruby symbols, like :nested, used for "output_style"
55-
return six.text_type(val)
54+
return str(val)
5655
elif isinstance(val, dict):
5756
# ruby hashes, for "sass_options" for example
58-
return u'{%s}' % ', '.join("'%s' => '%s'" % i for i in val.items())
57+
return '{%s}' % ', '.join("'%s' => '%s'" % i for i in val.items())
5958
elif isinstance(val, tuple):
6059
val = list(val)
6160
# works fine with strings and lists

src/webassets/filter/spritemapper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from webassets.six import StringIO
21
from contextlib import contextmanager
2+
from io import StringIO
33
from webassets.filter import Filter
44

55
try:

src/webassets/loaders.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
import glob, fnmatch
1010
import inspect
1111
import types
12-
from webassets import six
1312
try:
1413
import yaml
1514
except ImportError:
1615
pass
1716

18-
from webassets import six
1917
from webassets import Environment
2018
from webassets.bundle import Bundle
2119
from webassets.exceptions import EnvironmentError
@@ -53,7 +51,7 @@ def _yield_bundle_contents(self, data):
5351
Each item yielded will be either a string representing a file path
5452
or a bundle."""
5553
contents = data.get('contents', [])
56-
if isinstance(contents, six.string_types):
54+
if isinstance(contents, str):
5755
contents = contents,
5856
for content in contents:
5957
if isinstance(content, dict):
@@ -74,7 +72,7 @@ def _get_bundle(self, data):
7472
def _get_bundles(self, obj, known_bundles=None):
7573
"""Return a dict that keys bundle names to bundles."""
7674
bundles = {}
77-
for key, data in six.iteritems(obj):
75+
for key, data in obj.items():
7876
if data is None:
7977
data = {}
8078
bundles[key] = self._get_bundle(data)
@@ -100,7 +98,7 @@ def _open(self):
10098
10199
The filename can be False if it is unknown.
102100
"""
103-
if isinstance(self.file_or_filename, six.string_types):
101+
if isinstance(self.file_or_filename, str):
104102
return open(self.file_or_filename), self.file_or_filename
105103

106104
file = self.file_or_filename
@@ -244,7 +242,7 @@ def load_environment(self):
244242

245243
# Load bundles
246244
bundles = self._get_bundles(obj.get('bundles', {}))
247-
for name, bundle in six.iteritems(bundles):
245+
for name, bundle in bundles.items():
248246
env.register(name, bundle)
249247

250248
return env

src/webassets/merge.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@
22
"""
33
import contextlib
44

5-
try:
6-
from urllib.request import Request as URLRequest, urlopen
7-
from urllib.error import HTTPError
8-
except ImportError:
9-
from urllib2 import Request as URLRequest, urlopen
10-
from urllib2 import HTTPError
115
import logging
126
from io import open
13-
from webassets import six
14-
from webassets.six.moves import filter
7+
from urllib.request import Request as URLRequest, urlopen
8+
from urllib.error import HTTPError
159

1610
from .utils import cmp_debug_levels, StringIO, hash_func
1711

@@ -120,7 +114,7 @@ def data(self):
120114
else:
121115
with contextlib.closing(response):
122116
data = response.read()
123-
if isinstance(data, six.binary_type):
117+
if isinstance(data, bytes):
124118
data = data.decode('utf-8')
125119
self._data = data
126120

src/webassets/script.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os, sys
33
import time
44
import logging
5+
from io import StringIO
56

67
from webassets.loaders import PythonLoader, YAMLLoader
78
from webassets.bundle import get_all_bundle_files
@@ -10,7 +11,6 @@
1011
from webassets.merge import MemoryHunk
1112
from webassets.version import get_manifest
1213
from webassets.cache import FilesystemCache
13-
from webassets.utils import set, StringIO
1414

1515

1616
__all__ = ('CommandError', 'CommandLineEnvironment', 'main')

src/webassets/utils.py

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,22 @@
1-
from webassets import six
1+
import base64
22
import contextlib
3+
import hashlib
34
import os
5+
import pickle
46
import sys
57
import re
8+
from io import StringIO
69
from itertools import takewhile
10+
from urllib import parse as urlparse
711

812
from .exceptions import BundleError
913

1014

1115
__all__ = ('md5_constructor', 'pickle', 'set', 'StringIO',
1216
'common_path_prefix', 'working_directory', 'is_url')
1317

14-
15-
import base64
16-
17-
if sys.version_info >= (2, 5):
18-
import hashlib
19-
md5_constructor = hashlib.md5
20-
else:
21-
import md5
22-
md5_constructor = md5.new
23-
24-
25-
try:
26-
import cPickle as pickle
27-
except ImportError:
28-
import pickle
29-
30-
31-
try:
32-
set
33-
except NameError:
34-
from sets import Set as set
35-
else:
36-
set = set
37-
38-
39-
try:
40-
FileNotFoundError
41-
except NameError:
42-
FileNotFoundError = IOError
43-
else:
44-
FileNotFoundError = FileNotFoundError
45-
46-
47-
from webassets.six import StringIO
48-
49-
50-
try:
51-
from urllib import parse as urlparse
52-
except ImportError: # Python 2
53-
import urlparse
54-
import urllib
18+
md5_constructor = hashlib.md5
19+
set = set
5520

5621
def hash_func(data):
5722
from .cache import make_md5
@@ -149,7 +114,7 @@ def resolve_option(option, env=None):
149114
return instantiate(option, env)
150115

151116
# If it is a string
152-
elif isinstance(option, six.string_types):
117+
elif isinstance(option, str):
153118
parts = option.split(':', 1)
154119
key = parts[0]
155120
arg = parts[1] if len(parts) > 1 else None

src/webassets/version.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import os
66
import pickle
7-
from webassets import six
87

98
from webassets.merge import FileHunk
109
from webassets.utils import md5_constructor, RegistryMetaclass, is_url
@@ -19,9 +18,9 @@ class VersionIndeterminableError(Exception):
1918
pass
2019

2120

22-
class Version(six.with_metaclass(RegistryMetaclass(
21+
class Version(metaclass=RegistryMetaclass(
2322
clazz=lambda: Version, attribute='determine_version',
24-
desc='a version implementation'))):
23+
desc='a version implementation')):
2524
"""A Version class that can be assigned to the ``Environment.versioner``
2625
attribute.
2726
@@ -165,8 +164,8 @@ def determine_version(self, bundle, ctx, hunk=None):
165164
return hasher.hexdigest()[:self.length]
166165

167166

168-
class Manifest(six.with_metaclass(RegistryMetaclass(
169-
clazz=lambda: Manifest, desc='a manifest implementation'))):
167+
class Manifest(metaclass=RegistryMetaclass(
168+
clazz=lambda: Manifest, desc='a manifest implementation')):
170169
"""Persists information about the versions bundles are at.
171170
172171
The Manifest plays a role only if you insert the bundle version in your

tests/test_bundle_build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def test_auto_create_target_directory(self):
147147

148148
def test_with_custom_output(self):
149149
"""build() method can write to a custom file object."""
150-
from webassets.six import StringIO
150+
from io import StringIO
151151
buffer = StringIO()
152152
self.mkbundle('in1', 'in2', output='out').build(output=buffer)
153153
assert buffer.getvalue() == 'A\nB'

0 commit comments

Comments
 (0)