__init__.py revision 1448
1516N/A#!/usr/bin/python2.4
281N/A#
281N/A# CDDL HEADER START
281N/A#
281N/A# The contents of this file are subject to the terms of the
281N/A# Common Development and Distribution License (the "License").
281N/A# You may not use this file except in compliance with the License.
281N/A#
281N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
281N/A# or http://www.opensolaris.org/os/licensing.
281N/A# See the License for the specific language governing permissions
281N/A# and limitations under the License.
281N/A#
281N/A# When distributing Covered Code, include this CDDL HEADER in each
281N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
281N/A# If applicable, add the following below this CDDL HEADER, with the
281N/A# fields enclosed by brackets "[]" replaced with your own identifying
281N/A# information: Portions Copyright [yyyy] [name of copyright owner]
281N/A#
281N/A# CDDL HEADER END
281N/A#
281N/A
281N/A# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2602N/A# Use is subject to license terms.
281N/A
281N/Aimport logging
289N/Aimport os
289N/Aimport sys
289N/A
1507N/A__all__ = ["global_settings"]
2003N/A
1507N/Aclass _LogFilter(logging.Filter):
281N/A def __init__(self, max_level=logging.CRITICAL):
1507N/A logging.Filter.__init__(self)
1507N/A self.max_level = max_level
281N/A
281N/A def filter(self, record):
281N/A return record.levelno <= self.max_level
2043N/A
2043N/Aclass GlobalSettings(object):
281N/A """ This class defines settings which are global
281N/A to the client instance """
281N/A
281N/A def __init__(self):
1507N/A object.__init__(self)
281N/A self.__info_log_handler = None
1507N/A self.__error_log_handler = None
281N/A self.__verbose = False
281N/A self.client_name = None
281N/A # Default maximum number of redirects received before
281N/A # aborting a connection.
281N/A self.pkg_client_max_redirect_default = 5
281N/A # Default number of retries per-host
281N/A self.pkg_client_max_timeout_default = 4
281N/A # Default number of seconds to give up if not connected
281N/A self.pkg_client_connect_timeout_default = 60
281N/A # Default number of seconds beneath low-speed limit before
281N/A # giving up.
281N/A self.pkg_client_lowspeed_timeout_default = 30
281N/A # Minimum bytes/sec before client thinks about giving up
281N/A # on connection.
281N/A self.pkg_client_lowspeed_limit = 1024
281N/A # Maximum number of transient errors before we abort an
281N/A # endpoint.
1507N/A self.pkg_client_max_consecutive_error_default = 4
281N/A try:
281N/A # Maximum number of timeouts before client gives up.
281N/A self.PKG_CLIENT_MAX_TIMEOUT = int(os.environ.get(
281N/A "PKG_CLIENT_MAX_TIMEOUT",
281N/A self.pkg_client_max_timeout_default))
281N/A except ValueError:
1507N/A self.PKG_CLIENT_MAX_TIMEOUT = \
281N/A self.pkg_client_max_timeout_default
281N/A try:
281N/A # Number of seconds trying to connect before client
281N/A # aborts.
281N/A self.PKG_CLIENT_CONNECT_TIMEOUT = int(os.environ.get(
281N/A "PKG_CLIENT_CONNECT_TIMEOUT",
1507N/A self.pkg_client_connect_timeout_default))
281N/A except ValueError:
2602N/A self.PKG_CLIENT_CONNECT_TIMEOUT = \
281N/A self.pkg_client_connect_timeout_default
281N/A try:
281N/A # Number of seconds below lowspeed limit before
281N/A # transaction is aborted.
281N/A self.PKG_CLIENT_LOWSPEED_TIMEOUT = int(os.environ.get(
281N/A "PKG_CLIENT_LOWSPEED_TIMEOUT",
281N/A self.pkg_client_lowspeed_timeout_default))
281N/A except ValueError:
281N/A self.PKG_CLIENT_LOWSPEED_TIMEOUT = \
281N/A self.pkg_client_lowspeed_timeout_default
281N/A try:
281N/A # Number of transient errors before transaction
281N/A # is aborted.
281N/A self.PKG_CLIENT_MAX_CONSECUTIVE_ERROR = int(
281N/A os.environ.get("PKG_CLIENT_MAX_CONSECUTIVE_ERROR",
281N/A self.pkg_client_max_consecutive_error_default))
281N/A except ValueError:
281N/A self.PKG_CLIENT_MAX_CONSECUTIVE_ERROR = \
281N/A self.pkg_client_max_consecutive_error_default
1507N/A try:
281N/A # Number of redirects before a connection is
281N/A # aborted.
281N/A self.PKG_CLIENT_MAX_REDIRECT = int(
281N/A os.environ.get("PKG_CLIENT_MAX_REDIRECT",
281N/A self.pkg_client_max_redirect_default))
281N/A except ValueError:
2602N/A self.PKG_CLIENT_MAX_REDIRECT = \
2602N/A self.pkg_client_max_redirect_default
2602N/A self.reset_logging()
2602N/A
2602N/A def __get_error_log_handler(self):
2602N/A return self.__error_log_handler
281N/A
281N/A def __get_info_log_handler(self):
281N/A return self.__info_log_handler
281N/A
281N/A def __get_verbose(self):
281N/A return self.__verbose
281N/A
281N/A def __set_error_log_handler(self, val):
281N/A logger = logging.getLogger("pkg")
281N/A if self.__error_log_handler:
281N/A logger.removeHandler(self.__error_log_handler)
281N/A self.__error_log_handler = val
281N/A if val:
281N/A logger.addHandler(val)
281N/A
281N/A def __set_info_log_handler(self, val):
281N/A logger = logging.getLogger("pkg")
1507N/A if self.__info_log_handler:
281N/A logger.removeHandler(self.__info_log_handler)
1507N/A self.__info_log_handler = val
281N/A if val:
281N/A logger.addHandler(val)
281N/A
281N/A def __set_verbose(self, val):
281N/A if self.__info_log_handler:
281N/A if val:
281N/A level = logging.DEBUG
281N/A else:
281N/A level = logging.INFO
281N/A self.__info_log_handler.setLevel(level)
281N/A self.__verbose = val
281N/A
281N/A @property
281N/A def logger(self):
281N/A return logging.getLogger("pkg")
281N/A
281N/A def reset_logging(self):
281N/A """Resets client logging to its default state. This will cause
281N/A all logging.INFO entries to go to sys.stdout, and all entries of
281N/A logging.WARNING or higher to go to sys.stderr."""
281N/A
281N/A logger = logging.getLogger("pkg")
281N/A logger.setLevel(logging.DEBUG)
281N/A
281N/A # Don't pass messages that are rejected to the root logger.
281N/A logger.propagate = 0
2003N/A
2003N/A # By default, log all informational messages, but not warnings
281N/A # and above to stdout.
2003N/A info_h = logging.StreamHandler(sys.stdout)
281N/A
281N/A # Minimum logging level for informational messages.
281N/A if self.verbose:
281N/A info_h.setLevel(logging.DEBUG)
281N/A else:
281N/A info_h.setLevel(logging.INFO)
281N/A
281N/A log_fmt = logging.Formatter()
281N/A
281N/A # Enforce maximum logging level for informational messages.
281N/A info_f = _LogFilter(logging.INFO)
281N/A info_h.addFilter(info_f)
281N/A info_h.setFormatter(log_fmt)
281N/A logger.addHandler(info_h)
281N/A
1507N/A # By default, log all warnings and above to stderr.
281N/A error_h = logging.StreamHandler(sys.stderr)
1507N/A error_h.setFormatter(log_fmt)
281N/A error_h.setLevel(logging.WARNING)
281N/A logger.addHandler(error_h)
281N/A
281N/A # Stash the handles so they can be removed later.
281N/A self.info_log_handler = info_h
281N/A self.error_log_handler = error_h
281N/A
281N/A error_log_handler = property(__get_error_log_handler,
281N/A __set_error_log_handler)
281N/A
281N/A info_log_handler = property(__get_info_log_handler,
281N/A __set_info_log_handler)
281N/A
281N/A verbose = property(__get_verbose, __set_verbose)
1507N/A
281N/A
281N/Aglobal_settings = GlobalSettings()
281N/A