Skip to content

Commit b0a6ba1

Browse files
committed
Even less metaclass usage
1 parent c30eb92 commit b0a6ba1

File tree

5 files changed

+42
-49
lines changed

5 files changed

+42
-49
lines changed

elasticsearch_dsl/aggs.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from six import add_metaclass
21

3-
from .utils import DslMeta, DslBase, _make_dsl_class
2+
from .utils import DslBase, _make_dsl_class
43

54
def A(name_or_agg, **params):
65
# {"terms": {"field": "tags"}, "aggs": {...}}
@@ -29,7 +28,6 @@ def A(name_or_agg, **params):
2928
# "terms", field="tags"
3029
return Agg.get_dsl_class(name_or_agg)(**params)
3130

32-
@add_metaclass(DslMeta)
3331
class Agg(DslBase):
3432
_type_name = 'agg'
3533
_type_shortcut = staticmethod(A)

elasticsearch_dsl/filter.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
from six import add_metaclass
2-
3-
from .utils import DslMeta, DslBase, BoolMixin, _make_dsl_class
1+
from .utils import DslBase, BoolMixin, _make_dsl_class
42

53
def F(name_or_filter, filters=None, **params):
64
# 'and/or', [F(), F()]
@@ -26,7 +24,6 @@ def F(name_or_filter, filters=None, **params):
2624
# 'term', tag='python', ...
2725
return Filter.get_dsl_class(name_or_filter)(**params)
2826

29-
@add_metaclass(DslMeta)
3027
class Filter(DslBase):
3128
_type_name = 'filter'
3229
_type_shortcut = staticmethod(F)

elasticsearch_dsl/function.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
from six import add_metaclass
2-
3-
from .utils import DslMeta, DslBase
1+
from .utils import DslBase
42

53
def SF(name_or_sf, **params):
64
# {"script_score": {"script": "_score"}, "filter": {}}
@@ -30,7 +28,6 @@ def SF(name_or_sf, **params):
3028
# "script_score", script="_score", filter=F()
3129
return ScoreFunction.get_dsl_class(name_or_sf)(**params)
3230

33-
@add_metaclass(DslMeta)
3431
class ScoreFunction(DslBase):
3532
_type_name = 'score_function'
3633
_type_shortcut = staticmethod(SF)

elasticsearch_dsl/query.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
from six import add_metaclass
2-
3-
from .utils import DslMeta, DslBase, BoolMixin, _make_dsl_class
1+
from .utils import DslBase, BoolMixin, _make_dsl_class
42
from .function import SF, ScoreFunction
53

64
def Q(name_or_query, **params):
@@ -23,7 +21,6 @@ def Q(name_or_query, **params):
2321
# "match", title="python"
2422
return Query.get_dsl_class(name_or_query)(**params)
2523

26-
@add_metaclass(DslMeta)
2724
class Query(DslBase):
2825
_type_name = 'query'
2926
_type_shortcut = staticmethod(Q)

elasticsearch_dsl/utils.py

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import unicode_literals
22

3-
from six import iteritems
3+
from six import iteritems, add_metaclass
44

55
from .exceptions import UnknownDslObject
66

@@ -81,6 +81,43 @@ def to_dict(self):
8181
return self._d_
8282

8383

84+
class DslMeta(type):
85+
"""
86+
Base Metaclass for DslBase subclasses that builds a registry of all classes
87+
for given DslBase subclass (== all the query types for the Query subclass
88+
of DslBase).
89+
90+
It then uses the information from that registry (as well as `name` and
91+
`shortcut` attributes from the base class) to construct any subclass based
92+
on it's name.
93+
94+
For typical use see `QueryMeta` and `Query` in `elasticsearch_dsl.query`.
95+
"""
96+
_types = {}
97+
def __init__(cls, name, bases, attrs):
98+
super(DslMeta, cls).__init__(name, bases, attrs)
99+
# skip for DslBase
100+
if not hasattr(cls, '_type_shortcut'):
101+
return
102+
if cls.name is None:
103+
# abstract base class, register it's shortcut
104+
cls._types[cls._type_name] = cls._type_shortcut
105+
# and create a registry for subclasses
106+
if not hasattr(cls, '_classes'):
107+
cls._classes = {}
108+
else:
109+
# normal class, register it
110+
cls._classes[cls.name] = cls
111+
112+
@classmethod
113+
def get_dsl_type(cls, name):
114+
try:
115+
return cls._types[name]
116+
except KeyError:
117+
raise UnknownDslObject('DSL type %s does not exist.' % name)
118+
119+
120+
@add_metaclass(DslMeta)
84121
class DslBase(object):
85122
"""
86123
Base class for all DSL objects - queries, filters, aggregations etc. Wraps
@@ -243,39 +280,6 @@ def __and__(self, other):
243280
return self._bool(must=[self, other])
244281

245282

246-
class DslMeta(type):
247-
"""
248-
Base Metaclass for DslBase subclasses that builds a registry of all classes
249-
for given DslBase subclass (== all the query types for the Query subclass
250-
of DslBase).
251-
252-
It then uses the information from that registry (as well as `name` and
253-
`shortcut` attributes from the base class) to construct any subclass based
254-
on it's name.
255-
256-
For typical use see `QueryMeta` and `Query` in `elasticsearch_dsl.query`.
257-
"""
258-
_types = {}
259-
def __init__(cls, name, bases, attrs):
260-
super(DslMeta, cls).__init__(name, bases, attrs)
261-
if cls.name is None:
262-
# abstract base class, register it's shortcut
263-
cls._types[cls._type_name] = cls._type_shortcut
264-
# and create a registry for subclasses
265-
if not hasattr(cls, '_classes'):
266-
cls._classes = {}
267-
else:
268-
# normal class, register it
269-
cls._classes[cls.name] = cls
270-
271-
@classmethod
272-
def get_dsl_type(cls, name):
273-
try:
274-
return cls._types[name]
275-
except KeyError:
276-
raise UnknownDslObject('DSL type %s does not exist.' % name)
277-
278-
279283
class BoolMixin(object):
280284
"""
281285
Mixin containing all the operator overrides for Bool queries and filters.

0 commit comments

Comments
 (0)