From 7e210ae2bcb00160ca9c6324d32e7436f7671cfb Mon Sep 17 00:00:00 2001 From: Hendrik Buschmeier <hbuschme@uni-bielefeld.de> Date: Fri, 16 Jan 2015 16:43:29 +0100 Subject: [PATCH] Added ipaaca.misc.IpaacaArgumentParser. --- ipaacalib/python/src/ipaaca/__init__.py | 6 +-- ipaacalib/python/src/ipaaca/buffer.py | 15 ++++--- ipaacalib/python/src/ipaaca/defaults.py | 34 ++++++++++++++++ ipaacalib/python/src/ipaaca/misc.py | 53 +++++++++++++++++++++++-- 4 files changed, 92 insertions(+), 16 deletions(-) create mode 100644 ipaacalib/python/src/ipaaca/defaults.py diff --git a/ipaacalib/python/src/ipaaca/__init__.py b/ipaacalib/python/src/ipaaca/__init__.py index 89d4b1d..bbf51e3 100755 --- a/ipaacalib/python/src/ipaaca/__init__.py +++ b/ipaacalib/python/src/ipaaca/__init__.py @@ -35,7 +35,7 @@ from __future__ import division, print_function import rsb import rsb.converter -from ipaaca.misc import logger +from ipaaca.misc import logger, IpaacaArgumentParser import ipaaca_pb2 import ipaaca.converter @@ -77,15 +77,13 @@ def initialize_ipaaca_rsb(): rsb.converter.registerGlobalConverter( rsb.converter.ProtocolBufferConverter( - messageClass=ipaaca_pb2.IUResendRequest)) # dlw + messageClass=ipaaca_pb2.IUResendRequest)) rsb.converter.registerGlobalConverter( rsb.converter.ProtocolBufferConverter( messageClass=ipaaca_pb2.IURetraction)) rsb.__defaultParticipantConfig = rsb.ParticipantConfig.fromDefaultSources() - # t = rsb.ParticipantConfig.Transport('spread', {'enabled':'true'}) - # rsb.__defaultParticipantConfig = rsb.ParticipantConfig.fromFile('rsb.cfg') ## --- Module initialisation ------------------------------------------------- diff --git a/ipaacalib/python/src/ipaaca/buffer.py b/ipaacalib/python/src/ipaaca/buffer.py index c899f4a..93f9743 100644 --- a/ipaacalib/python/src/ipaaca/buffer.py +++ b/ipaacalib/python/src/ipaaca/buffer.py @@ -39,6 +39,7 @@ import uuid import rsb import ipaaca_pb2 +import ipaaca.defaults import ipaaca.exception import ipaaca.converter import ipaaca.iu @@ -123,7 +124,7 @@ class Buffer(object): """Base class for InputBuffer and OutputBuffer.""" - def __init__(self, owning_component_name, participant_config=None): + def __init__(self, owning_component_name, channel=None, participant_config=None): '''Create a Buffer. Keyword arguments: @@ -132,6 +133,7 @@ class Buffer(object): ''' super(Buffer, self).__init__() self._owning_component_name = owning_component_name + self._channel = channel if channel is not None else ipaaca.defaults.IPAACA_DEFAULT_CHANNEL self._participant_config = rsb.ParticipantConfig.fromDefaultSources() if participant_config is None else participant_config self._uuid = str(uuid.uuid4())[0:8] # Initialise with a temporary, but already unique, name @@ -176,7 +178,7 @@ class InputBuffer(Buffer): """An InputBuffer that holds remote IUs.""" - def __init__(self, owning_component_name, category_interests=None, channel="default", participant_config=None, resend_active = False ): + def __init__(self, owning_component_name, category_interests=None, channel=None, participant_config=None, resend_active = False ): '''Create an InputBuffer. Keyword arguments: @@ -184,12 +186,11 @@ class InputBuffer(Buffer): category_interests -- list of IU categories this Buffer is interested in participant_config = RSB configuration ''' - super(InputBuffer, self).__init__(owning_component_name, participant_config) + super(InputBuffer, self).__init__(owning_component_name, channel, participant_config) self._resend_active = resend_active self._unique_name = '/ipaaca/component/'+str(owning_component_name)+'ID'+self._uuid+'/IB' self._listener_store = {} # one per IU category self._remote_server_store = {} # one per remote-IU-owning Component - self._channel = channel self._category_interests = [] if category_interests is not None: for cat in category_interests: @@ -321,15 +322,14 @@ class OutputBuffer(Buffer): """An OutputBuffer that holds local IUs.""" - def __init__(self, owning_component_name, channel='default', participant_config=None): - + def __init__(self, owning_component_name, channel=None, participant_config=None): '''Create an Output Buffer. Keyword arguments: owning_component_name -- name of the entity that own this buffer participant_config -- RSB configuration ''' - super(OutputBuffer, self).__init__(owning_component_name, participant_config) + super(OutputBuffer, self).__init__(owning_component_name, channel, participant_config) self._unique_name = '/ipaaca/component/' + str(owning_component_name) + 'ID' + self._uuid + '/OB' self._server = rsb.createServer(rsb.Scope(self._unique_name)) self._server.addMethod('updateLinks', self._remote_update_links, ipaaca.converter.IULinkUpdate, int) @@ -339,7 +339,6 @@ class OutputBuffer(Buffer): self._informer_store = {} self._id_prefix = str(owning_component_name)+'-'+str(self._uuid)+'-IU-' self.__iu_id_counter_lock = threading.Lock() - self._channel = channel def _remote_update_links(self, update): '''Apply a remotely requested update to one of the stored IU's links.''' diff --git a/ipaacalib/python/src/ipaaca/defaults.py b/ipaacalib/python/src/ipaaca/defaults.py new file mode 100644 index 0000000..46908a1 --- /dev/null +++ b/ipaacalib/python/src/ipaaca/defaults.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- + +# This file is part of IPAACA, the +# "Incremental Processing Architecture +# for Artificial Conversational Agents". +# +# Copyright (c) 2009-2014 Social Cognitive Systems Group +# CITEC, Bielefeld University +# +# http://opensource.cit-ec.de/projects/ipaaca/ +# http://purl.org/net/ipaaca +# +# This file may be licensed under the terms of of the +# GNU Lesser General Public License Version 3 (the ``LGPL''), +# or (at your option) any later version. +# +# Software distributed under the License is distributed +# on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either +# express or implied. See the LGPL for the specific language +# governing rights and limitations. +# +# You should have received a copy of the LGPL along with this +# program. If not, go to http://www.gnu.org/licenses/lgpl.html +# or write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# The development of this software was supported by the +# Excellence Cluster EXC 277 Cognitive Interaction Technology. +# The Excellence Cluster EXC 277 is a grant of the Deutsche +# Forschungsgemeinschaft (DFG) in the context of the German +# Excellence Initiative. + +IPAACA_DEFAULT_CHANNEL = 'default' +IPAACA_DEFAULT_LOGGING_LEVEL = 'WARNING' \ No newline at end of file diff --git a/ipaacalib/python/src/ipaaca/misc.py b/ipaacalib/python/src/ipaaca/misc.py index 846295e..8e3575b 100644 --- a/ipaacalib/python/src/ipaaca/misc.py +++ b/ipaacalib/python/src/ipaaca/misc.py @@ -33,12 +33,16 @@ from __future__ import division, print_function +import argparse import logging +import ipaaca.defaults + __all__ = [ - 'enum' - 'logger' + 'enum', + 'logger', + 'IpaacaArgumentParser', ] @@ -56,7 +60,7 @@ def enum(*sequential, **named): # Create a global logger for ipaaca class IpaacaLoggingHandler(logging.Handler): - def __init__(self, level=logging.DEBUG): + def __init__(self, level=logging.NOTSET): logging.Handler.__init__(self, level) def emit(self, record): @@ -64,5 +68,46 @@ class IpaacaLoggingHandler(logging.Handler): msg = str(record.msg.format(record.args)) print(meta + msg) + logger = logging.getLogger('ipaaca') -logger.addHandler(IpaacaLoggingHandler(level=logging.INFO)) +logger.addHandler(IpaacaLoggingHandler()) +logger.setLevel(level=ipaaca.defaults.IPAACA_DEFAULT_LOGGING_LEVEL) + + + +class IpaacaArgumentParser(argparse.ArgumentParser): + + + class IpaacaDefaultChannelAction(argparse.Action): + + def __call__(self, parser, namespace, values, option_string=None): + ipaaca.defaults.IPAACA_DEFAULT_CHANNEL = values + + class IpaacaLoggingLevelAction(argparse.Action): + + def __call__(self, parser, namespace, values, option_string=None): + logger.setLevel(level=values) + + + def __init__(self, prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=argparse.HelpFormatter, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True): + super(IpaacaArgumentParser, self).__init__(prog=prog, usage=usage, description=description, epilog=epilog, parents=parents, formatter_class=formatter_class, prefix_chars=prefix_chars, fromfile_prefix_chars=fromfile_prefix_chars, argument_default=argument_default, conflict_handler=conflict_handler, add_help=add_help) + + self.add_argument( + '--ipaaca-default-channel', action=self.IpaacaDefaultChannelAction, + default='default', metavar='NAME', dest='_ipaaca_default_channel_', + help='ipaaca channel name which is used if a buffer does not define one locally') + + self.add_argument( + '--ipaaca-logging-level', action=self.IpaacaLoggingLevelAction, + choices=['NOTSET','CRITICAL','ERROR','WARNING','INFO','DEBUG'], + dest='_ipaaca_logging_level_', + help='logging threshold for ipaaca') + + def parse_args(self, args=None, namespace=None): + result = super(IpaacaArgumentParser, self).parse_args(args, namespace) + for item in dir(result): + if item.startswith('_ipaaca') and item.endswith('_'): + delattr(result, item) + return result + + -- GitLab