Skip to content
Snippets Groups Projects
Forked from Social Cognitive Systems / ipaaca
251 commits behind the upstream repository.
ipaaca-iu-injector 3.43 KiB
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This file is part of IPAACA, the
#  "Incremental Processing Architecture
#   for Artificial Conversational Agents".
#
# Copyright (c) 2009-2015 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.

from __future__ import division, print_function

import itertools
import os
import platform
import sys
import time

import ipaaca


def iu_update_handler(iu, event_type, local):
	print(event_type + ': ' + str(iu))


parser = ipaaca.IpaacaArgumentParser(description='Ipaaca IU Injector -- Sent ipaaca messages or IUs from the command line')
parser.add_argument(
	'-t', '--type',
	default='Message',
	dest='iu_type',
	choices = ['Message', 'IU'],
	help="choose IU type to be send (default: 'Message')")
parser.add_argument(
	'-k', '--keep-alive',
	default=3.0,
	dest='keep_alive',
	metavar='SECONDS',
	type=float,
	help='set time in seconds to wait for potential IU updates (default: 3.0)')
parser.add_argument(
	'-j', '--json-payload',
	dest='json_payload',
	action='store_true',
	help='allow structured data to be sent (treats payload VALUE arguments as Python expressions)'))
parser.add_argument(
	'-c', '--category',
	dest='category',
	metavar='CATEGORY',
	required=True,
	help='set Message/IU category')
parser.add_argument(
	'-p', '--payload',
	default=[],
	dest='payload',
	metavar='KEY VALUE',
	nargs='+',
	help='set Message/IU payload ')


if __name__ == '__main__':
	arguments = parser.parse_args()

	ob = ipaaca.OutputBuffer('IpaacaIUInjector')
	ob.register_handler(iu_update_handler)
	iu = ipaaca.Message(arguments.category) if arguments.iu_type == 'Message' else ipaaca.IU(arguments.category)
	if arguments.json_payload:
		# Treat payload values as Python expressions
		iu.payload = {k: eval(v) for (k, v) in itertools.izip_longest(arguments.payload[::2],	arguments.payload[1::2])}
	else:
		iu.payload = {k: v for (k, v) in itertools.izip_longest(arguments.payload[::2],	arguments.payload[1::2])}
	ob.add(iu)

	print(
		'Sent {iu_type} with category "{category}" and payload {{'.format(**vars(arguments)),
		end='\n' if len(iu.payload) > 0 else '')
	for k, v in iu.payload.items():
		print("  '{key}': {value},".format(key=k, value=v))
	print('}.')
	
	# Wait for updates to the IU
	try:
		if arguments.iu_type == 'IU':
			print('Waiting %s s for the IU to be updated.' % arguments.keep_alive)
			time.sleep(arguments.keep_alive)
		else:
			time.sleep(0.1)
	except KeyboardInterrupt:
		pass

	if platform.system() == 'Windows':
		os._exit(0)
	else:
		sys.exit(0)