# Path: elasticsearch_dsl/utils.py
# class AttrDict(object):
#     """
#     Helper class to provide attribute like access (read and write) to
#     dictionaries. Used to provide a convenient way to access both results and
#     nested dsl dicts.
#     """
# 
#     def __init__(self, d):
#         # assign the inner dict manually to prevent __setattr__ from firing
#         super(AttrDict, self).__setattr__("_d_", d)
# 
#     def __contains__(self, key):
#         return key in self._d_
# 
#     def __nonzero__(self):
#         return bool(self._d_)
# 
#     __bool__ = __nonzero__
# 
#     def __dir__(self):
#         # introspection for auto-complete in IPython etc
#         return list(self._d_.keys())
# 
#     def __eq__(self, other):
#         if isinstance(other, AttrDict):
#             return other._d_ == self._d_
#         # make sure we still equal to a dict with the same data
#         return other == self._d_
# 
#     def __ne__(self, other):
#         return not self == other
# 
#     def __repr__(self):
#         r = repr(self._d_)
#         if len(r) > 60:
#             r = r[:60] + "...}"
#         return r
# 
#     def __getstate__(self):
#         return (self._d_,)
# 
#     def __setstate__(self, state):
#         super(AttrDict, self).__setattr__("_d_", state[0])
# 
#     def __getattr__(self, attr_name):
#         try:
#             return self.__getitem__(attr_name)
#         except KeyError:
#             raise AttributeError(
#                 "{!r} object has no attribute {!r}".format(
#                     self.__class__.__name__, attr_name
#                 )
#             )
# 
#     def __delattr__(self, attr_name):
#         try:
#             del self._d_[attr_name]
#         except KeyError:
#             raise AttributeError(
#                 "{!r} object has no attribute {!r}".format(
#                     self.__class__.__name__, attr_name
#                 )
#             )
# 
#     def __getitem__(self, key):
#         return _wrap(self._d_[key])
# 
#     def __setitem__(self, key, value):
#         self._d_[key] = value
# 
#     def __delitem__(self, key):
#         del self._d_[key]
# 
#     def __setattr__(self, name, value):
#         if name in self._d_ or not hasattr(self.__class__, name):
#             self._d_[name] = value
#         else:
#             # there is an attribute on the class (could be property, ..) - don't add it as field
#             super(AttrDict, self).__setattr__(name, value)
# 
#     def __iter__(self):
#         return iter(self._d_)
# 
#     def to_dict(self):
#         return self._d_
# 
# class HitMeta(AttrDict):
#     def __init__(self, document, exclude=("_source", "_fields")):
#         d = {
#             k[1:] if k.startswith("_") else k: v
#             for (k, v) in iteritems(document)
#             if k not in exclude
#         }
#         if "type" in d:
#             # make sure we are consistent everywhere in python
#             d["doc_type"] = d.pop("type")
#         super(HitMeta, self).__init__(d)


from ..utils import AttrDict, HitMeta
#  Licensed to Elasticsearch B.V. under one or more contributor
#  license agreements. See the NOTICE file distributed with
#  this work for additional information regarding copyright
#  ownership. Elasticsearch B.V. licenses this file to you under
#  the Apache License, Version 2.0 (the "License"); you may
#  not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
# 	http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an
#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
#  specific language governing permissions and limitations
#  under the License.



class Hit(AttrDict):
    def __init__(self, document):
        data = {}
        if "_source" in document:
            data = document["_source"]
        if "fields" in document:
            data.update(document["fields"])

        super(Hit, self).__init__(data)
        # assign meta as attribute and not as key in self._d_

# Path: elasticsearch_dsl/utils.py
# class AttrDict(object):
#     """
#     Helper class to provide attribute like access (read and write) to
#     dictionaries. Used to provide a convenient way to access both results and
#     nested dsl dicts.
#     """
# 
#     def __init__(self, d):
#         # assign the inner dict manually to prevent __setattr__ from firing
#         super(AttrDict, self).__setattr__("_d_", d)
# 
#     def __contains__(self, key):
#         return key in self._d_
# 
#     def __nonzero__(self):
#         return bool(self._d_)
# 
#     __bool__ = __nonzero__
# 
#     def __dir__(self):
#         # introspection for auto-complete in IPython etc
#         return list(self._d_.keys())
# 
#     def __eq__(self, other):
#         if isinstance(other, AttrDict):
#             return other._d_ == self._d_
#         # make sure we still equal to a dict with the same data
#         return other == self._d_
# 
#     def __ne__(self, other):
#         return not self == other
# 
#     def __repr__(self):
#         r = repr(self._d_)
#         if len(r) > 60:
#             r = r[:60] + "...}"
#         return r
# 
#     def __getstate__(self):
#         return (self._d_,)
# 
#     def __setstate__(self, state):
#         super(AttrDict, self).__setattr__("_d_", state[0])
# 
#     def __getattr__(self, attr_name):
#         try:
#             return self.__getitem__(attr_name)
#         except KeyError:
#             raise AttributeError(
#                 "{!r} object has no attribute {!r}".format(
#                     self.__class__.__name__, attr_name
#                 )
#             )
# 
#     def __delattr__(self, attr_name):
#         try:
#             del self._d_[attr_name]
#         except KeyError:
#             raise AttributeError(
#                 "{!r} object has no attribute {!r}".format(
#                     self.__class__.__name__, attr_name
#                 )
#             )
# 
#     def __getitem__(self, key):
#         return _wrap(self._d_[key])
# 
#     def __setitem__(self, key, value):
#         self._d_[key] = value
# 
#     def __delitem__(self, key):
#         del self._d_[key]
# 
#     def __setattr__(self, name, value):
#         if name in self._d_ or not hasattr(self.__class__, name):
#             self._d_[name] = value
#         else:
#             # there is an attribute on the class (could be property, ..) - don't add it as field
#             super(AttrDict, self).__setattr__(name, value)
# 
#     def __iter__(self):
#         return iter(self._d_)
# 
#     def to_dict(self):
#         return self._d_
# 
# class HitMeta(AttrDict):
#     def __init__(self, document, exclude=("_source", "_fields")):
#         d = {
#             k[1:] if k.startswith("_") else k: v
#             for (k, v) in iteritems(document)
#             if k not in exclude
#         }
#         if "type" in d:
#             # make sure we are consistent everywhere in python
#             d["doc_type"] = d.pop("type")
#         super(HitMeta, self).__init__(d)


# Path: elasticsearch_dsl/response/hit.py
from ..utils import AttrDict, HitMeta

#  Licensed to Elasticsearch B.V. under one or more contributor
#  license agreements. See the NOTICE file distributed with
#  this work for additional information regarding copyright
#  ownership. Elasticsearch B.V. licenses this file to you under
#  the Apache License, Version 2.0 (the "License"); you may
#  not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
# 	http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an
#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
#  specific language governing permissions and limitations
#  under the License.



class Hit(AttrDict):
    def __init__(self, document):
        data = {}
        if "_source" in document:
            data = document["_source"]
        if "fields" in document:
            data.update(document["fields"])

        super(Hit, self).__init__(data)
        # assign meta as attribute and not as key in self._d_

        super(AttrDict, self).__setattr__("meta", HitMeta(document))