jps package

Module contents

class jps.Publisher(topic_name, host=None, pub_port=None, serializer='DEFAULT')[source]

Bases: object

Publishes data for a topic.

Example:

>>> pub = jps.Publisher('special_topic')
>>> pub.publish('{"name": "hoge"}')
Parameters:
  • topic_name – Topic name
  • host – host of subscriber/forwarder
  • pub_port – port of subscriber/forwarder
  • serializer – this function is applied before publish (default: None)
publish(payload)[source]

Publish payload to the topic

Note

If you publishes just after creating Publisher instance, it will causes lost of message. You have to add sleep if you just want to publish once.

>>> pub = jps.Publisher('topic')
>>> time.sleep(0.1)
>>> pub.publish('{data}')
Parameters:payload – data to be published. This is ok if the data is not json.
class jps.Subscriber(topic_name, callback=None, host=None, sub_port=None, deserializer='DEFAULT')[source]

Bases: object

Subscribe the topic and call the callback function

Example:

>>> def callback(msg):
...   print msg
...
>>> sub = jps.Subscriber('topic_name', callback)
>>> sub.spin()

or you can use python generator style

>>> import jps
>>> for msg in jps.Subscriber('/hoge1'):
...   print msg
Parameters:
  • topic_name – topic name
  • host – host name of publisher/forwarder
  • sub_port – port of publisher/forwarder
  • deserializer – this function is applied after received (default: None)
deserialize(msg)[source]
next()[source]

receive next data (block until next data)

spin(use_thread=False)[source]

call callback for all data forever (until C-c)

Parameters:use_thread – use thread for spin (do not block)
spin_once(polling_sec=0.01)[source]

Read the queued data and call the callback for them. You have to handle KeyboardInterrupt (C-c) manually.

Example:

>>> def callback(msg):
...   print msg
>>> sub = jps.Subscriber('topic_name', callback)
>>> try:
...   while True:
...     sub.spin_once():
...     time.sleep(0.1)
... except KeyboardInterrupt:
...   pass
class jps.ArgumentParser(subscriber=True, publisher=True, service=False, *args, **kwargs)[source]

Bases: argparse.ArgumentParser

Create ArgumentParser with args (host/subscriber_port/publisher_port)

Example:

>>> parser = jps.ArgumentParser(description='my program')
>>> args = parser.parse_args()
>>> args.host
'localhost'
>>> args.subscriber_port
54321
>>> args.publisher_port
54320
Parameters:
  • add subscriber_port (default (subscriber) – True)
  • add publisher_port (default (publisher) – True)
class jps.Authenticator(public_keys_dir)[source]

Bases: object

classmethod instance(public_keys_dir)[source]

Please avoid create multi instance

set_client_key(zmq_socket, client_secret_key_path, server_public_key_path)[source]

must call before bind

set_server_key(zmq_socket, server_secret_key_path)[source]

must call before bind

stop()[source]
jps.create_certificates(keys_dir='certificates')[source]
class jps.ServiceServer(callback, host=None, res_port=None, use_security=False)[source]

Bases: object

Example:

>>> def callback(req):
...   return 'req = {req}'.format(req=req)
...
>>> service = jps.ServiceServer(callback)
>>> service.spin()
close()[source]
spin(use_thread=False)[source]

call callback for all data forever (until C-c)

Parameters:use_thread – use thread for spin (do not block)
spin_once()[source]
class jps.ServiceClient(host=None, req_port=None, use_security=False)[source]

Bases: object

call(request)[source]
class jps.ActionServer(base_topic_name, callback, host=None, pub_port=None, sub_port=None, serializer='DEFAULT', deserializer='DEFAULT')[source]

Bases: object

serve the service which takes some long time

Example:

>>> import jps
>>> import time
>>> def callback(req):
...   time.sleep(1)
...   return req + ' received'
>>> s = jps.ActionServer('move_to', callback)
# subscribe 'move_to/request', publish 'move_to/response'
>>> s.spin()
spin(use_thread=False)[source]
spin_once()[source]
class jps.ActionClient(base_topic_name, host=None, pub_port=None, sub_port=None, serializer='DEFAULT', deserializer='DEFAULT')[source]

Bases: object

Call an action

Example:

>>> import jps
>>> import json
>>> c = jps.ActionClient('move_to')
>>> future = c(json.dumps({'x': 10.0, 'y': 0.1}))
# do something if you are busy to do something during waiting.
>>> result = future.wait()
class jps.Bridge(upload_topic_names, download_topic_names, remote_host=None, remote_pub_port=None, remote_sub_port=None)[source]

Bases: object

spin()[source]
class jps.BridgeServiceServer(download_topics, sub_port=None, pub_port=None, res_port=None, use_security=False)[source]

Bases: jps.bridge.BridgeServiceBase

callback(request)[source]
close()[source]
spin(use_thread=False)[source]
class jps.BridgeServiceClient(upload_topics, frequency=10.0, sub_port=None, pub_port=None, host=None, req_port=None, use_security=False)[source]

Bases: jps.bridge.BridgeServiceBase

spin(use_thread=False)[source]
spin_once()[source]
exception jps.Error[source]

Bases: exceptions.Exception

Submodules

jps.utils module

class jps.utils.JsonMultiplePublisher[source]

Bases: object

publish multiple topics by one json message

Example:

>>> p = JsonMultiplePublisher()
>>> p.publish('{"topic1": 1.0, "topic2": {"x": 0.1}}')
publish(json_msg)[source]

json_msg = ‘{“topic1”: 1.0, “topic2”: {“x”: 0.1}}’

class jps.utils.MultiplePublisher(base_topic_name)[source]

Bases: object

publish(msg, topic_suffix='')[source]

jps.launcher module

jps.launcher.get_launched_module_pid_file(module_name)[source]
jps.launcher.kill_module(module_name)[source]
jps.launcher.launch_modules(module_names, module_args={}, kill_before_launch=True)[source]
jps.launcher.launch_modules_with_names(modules_with_names, module_args={}, kill_before_launch=True)[source]

launch module.main functions in another process

jps.args module

class jps.args.ArgumentParser(subscriber=True, publisher=True, service=False, *args, **kwargs)[source]

Bases: argparse.ArgumentParser

Create ArgumentParser with args (host/subscriber_port/publisher_port)

Example:

>>> parser = jps.ArgumentParser(description='my program')
>>> args = parser.parse_args()
>>> args.host
'localhost'
>>> args.subscriber_port
54321
>>> args.publisher_port
54320
Parameters:
  • add subscriber_port (default (subscriber) – True)
  • add publisher_port (default (publisher) – True)

jps.tools module

jps.tools.echo(topic_name, num_print=None, out=<open file '<stdout>', mode 'w'>, host='localhost', sub_port=54321)[source]

print the data for the given topic forever

jps.tools.play(file_path, host='localhost', pub_port=54320)[source]

replay the recorded data by record()

jps.tools.pub(topic_name, json_msg, repeat_rate=None, host='localhost', pub_port=54320)[source]

publishes the data to the topic

Parameters:
  • topic_name – name of the topic
  • json_msg – data to be published
  • repeat_rate – if None, publishes once. if not None, it is used as [Hz].
jps.tools.record(file_path, topic_names=[], host='localhost', sub_port=54321)[source]

record the topic data to the file

jps.tools.show_list(timeout_in_sec, out=<open file '<stdout>', mode 'w'>, host='localhost', sub_port=54321)[source]

get the name list of the topics, and print it

jps.tools.topic_command()[source]

command line tool for jps