#!/usr/bin/env python
# -*- coding: utf-8 -*-

# === haiku.environment ---------------------------------------------------===
# Copyright © 2011-2012, RokuSigma Inc. and contributors. See AUTHORS for more
# details.
#
# Some rights reserved.
#
# Redistribution and use in source and binary forms of the software as well as
# documentation, with or without modification, are permitted provided that the
# following conditions are met:
#
#  * Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#  * The names of the copyright holders or contributors may not be used to
#    endorse or promote products derived from this software without specific
#    prior written permission.
#
# THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE AND
# DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ===----------------------------------------------------------------------===

""

__all__ = [
  'Environment',
]

# ===----------------------------------------------------------------------===

# Python standard library, shallow and deep copy operations
from copy import copy, deepcopy

class Environment(dict):
  "A haiku environment space."
  def __init__(self,
    params   = None,
    defaults = None,
    ellipsis = False,
    args     = None,
    parent   = None,
    *largs, **dargs):
    ""
    params   = params   or {}
    defaults = defaults or {}
    args     = args     or {}
    super(Environment, self).__init__(*largs, **dargs)
    self.update(copy(defaults))
    self.update(deepcopy(args))
    self._parent = parent

    missing = filter(lambda key:key not in self, params.keys())
    if missing:
      raise TypeError(
        u"missing required argument(s): %s" % u", ".join(map(repr, missing)))

    wrong_type = filter(
      lambda key:key in params and not isinstance(self[key], params[key]),
      self.keys())
    if wrong_type:
      for key in wrong_type:
        raise TypeError(
          u"incorrect type for argument %s: expected %s, got %s instead" %
          (key, params[key], self[key].__class__))

    if not ellipsis:
      extra = filter(lambda key:key not in params, self.keys())
      if extra:
        raise TypeError(
          u"got unexpected argument(s) %s" % u", ".join(map(repr, extra)))

  def resolve(self, symbol):
    ""
    if symbol not in self:
      if not self._parent:
        raise KeyError(
          u"symbol not found: %s" % symbol)
      self = self._parent.resolve(symbol)
    return self

from haiku.types.tuple_ import TupleCompatible
TupleCompatible.register(Environment)

# ===----------------------------------------------------------------------===
# End of File
# ===----------------------------------------------------------------------===
