Skip to content

Commit d93d18d

Browse files
TheBukyTheBuky
authored andcommitted
Repport the add_metaclass decorator to handle metaclass overwriting
1 parent 58c9ddf commit d93d18d

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

pipeline/decorator.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
"""
5+
This code is a part of django.utils.six on https://github.com/django/django/blob/stable/2.2.x/django/utils/six.py removed form Django 3.0
6+
To keep the backward compatibility between python 2 and 3 the decorator need to be used as well, during the time we find a proper way to
7+
handle MetaClass overwright working on both versions (or dropping python 2 support).
8+
"""
9+
10+
def with_metaclass(meta, *bases):
11+
"""Create a base class with a metaclass."""
12+
# This requires a bit of explanation: the basic idea is to make a dummy
13+
# metaclass for one level of class instantiation that replaces itself with
14+
# the actual metaclass.
15+
class metaclass(meta):
16+
17+
def __new__(cls, name, this_bases, d):
18+
return meta(name, bases, d)
19+
return type.__new__(metaclass, 'temporary_class', (), {})
20+
21+
22+
def add_metaclass(metaclass):
23+
"""Class decorator for creating a class with a metaclass."""
24+
def wrapper(cls):
25+
orig_vars = cls.__dict__.copy()
26+
slots = orig_vars.get('__slots__')
27+
if slots is not None:
28+
if isinstance(slots, str):
29+
slots = [slots]
30+
for slots_var in slots:
31+
orig_vars.pop(slots_var)
32+
orig_vars.pop('__dict__', None)
33+
orig_vars.pop('__weakref__', None)
34+
return metaclass(cls.__name__, cls.__bases__, orig_vars)
35+
return wrapper

pipeline/forms.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
from __future__ import unicode_literals
44

55
from django.contrib.staticfiles.storage import staticfiles_storage
6-
from django.utils import six
76
from django.utils.functional import cached_property
7+
try:
8+
from django.utils.six import iteritems
9+
except ImportError:
10+
def iteritems(dictionary):
11+
return dictionary.items()
812

913
from .collector import default_collector
1014
from .conf import settings
15+
from .decorator import add_metaclass
1116
from .packager import Packager
1217

1318

@@ -168,7 +173,7 @@ def _get_css_files(cls, extra_files):
168173
media_type='css',
169174
extra_files=extra_files.get(media_target,
170175
[])))
171-
for media_target, media_packages in six.iteritems(css_packages)
176+
for media_target, media_packages in iteritems(css_packages)
172177
)
173178

174179
def _get_js_files(cls, extra_files):
@@ -233,7 +238,7 @@ def _get_media_files(cls, packager, media_packages, media_type,
233238
return source_files
234239

235240

236-
@six.add_metaclass(PipelineFormMediaMetaClass)
241+
@add_metaclass(PipelineFormMediaMetaClass)
237242
class PipelineFormMedia(object):
238243
"""Base class for form or widget Media classes that use Pipeline packages.
239244

0 commit comments

Comments
 (0)