## $Id: zcml.py 12060 2014-11-25 18:44:01Z uli $
##
## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
import os
from zope.component.zcml import handler
from zope.configuration.exceptions import ConfigurationError
from waeup.ikoba.interfaces import IDataCenterConfig, IPayPalConfig
from waeup.ikoba.payments.paypal import configure_sdk


def data_center_conf(context, path):
    """Handler for ZCML ``datacenter`` directive.

    Registers a global utility under IDataCenterConfig and containing
    a dictionary with (currently) only one entry: `path`.

    The directive can be put into site.zcml like this:

    - Add to the header:
        ``xmlns:ikoba="http://namespaces.waeup.org/ikoba"``

    - Then, after including waeup.ikoba:
        ``<ikoba:datacenter path="some/existing/file/path" />``

    In a running instance (where some directive like above was
    processed during startup), one can then ask for the
    IDataCenterConfig utility:

      >>> from waeup.ikoba.interfaces import IDataCenterConfig
      >>> from zope.component import getUtility
      >>> getUtility(IDataCenterConfig)
      {'path': 'some/existing/file/path'}

    """
    context.action(
        discriminator=('utility', IDataCenterConfig, ''),
        callable=handler,
        args=('registerUtility',
              {'path': path}, IDataCenterConfig, '')
        )


def paypal_handler(path):
    """ZCML handler that registers paypal configuration.

    We expect paypal credentials written down in a config file. The
    path to this config file can be set with the ZCML `paypalconf`
    directive, which is handled here and looks like this::

      <ikoba:paypalconf path='/some/path/to/paypal.conf' />

    This handler requires the given path to exist and to be a file.

    If the file exists and is readable, a dict with ``path`` and other
    values read from the configuration file is registered as a global
    unnamed utility for the `IPayPalConfig` interface.

    See :func:`waeup.ikoba.payments.paypal.configure_sdk for details
    of the set dict.
    """
    if not os.path.exists(path):
        raise ConfigurationError("No such file: %s" % path)
    if not os.path.isfile(path):
        raise ConfigurationError("Not a regular file: %s" % path)
    config_dict = configure_sdk(path)
    return handler(
        'registerUtility', config_dict, IPayPalConfig, '')


def paypal_conf(context, path):
    """Handler for ZCML paypalconf directive.

    This handler registers the `paypal_handler` above to perform the
    real configuration action.
    """
    context.action(
        discriminator=('utility', IPayPalConfig, ''),
        callable=paypal_handler,
        args=(path, )
        )
