Skip to content

Commit 6898ace

Browse files
committed
Initial implementation
1 parent 3757289 commit 6898ace

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed

setup.cfg

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[options]
2+
package_dir=
3+
=src
4+
packages=find:
5+
6+
[options.packages.find]
7+
where=src
8+
9+
[metadata]
10+
description-file = README.md

setup.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from setuptools import setup, find_packages
2+
3+
setup(
4+
name='client_python_m2p',
5+
version='1.0.0',
6+
url='https://github.com/easytopic-project/client-python-m2p',
7+
license='MIT License',
8+
author='Bruno Jucá',
9+
author_email='brunoolijuca@gmail.com',
10+
keywords='python client for m2p',
11+
description='Python client for M2P',
12+
packages=find_packages('src'),
13+
package_dir={'': 'src'},
14+
install_requires=['pika'],
15+
classifiers=[
16+
"Programming Language :: Python :: 3",
17+
"License :: OSI Approved :: MIT License",
18+
"Operating System :: OS Independent",
19+
],
20+
)

src/client_python_m2p/__init__.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import pika
2+
import time
3+
import json
4+
5+
class M2P:
6+
7+
def __init__(self, queue_host, host_port, specs, user_callback):
8+
self.queue_host = queue_host
9+
self.host_port = host_port
10+
self.specs = specs
11+
self.RECONNECT_TIMEOUT = 3
12+
self.RECONNECT_MAX = 30
13+
self.ch = None
14+
self.user_callback = user_callback
15+
16+
17+
def __getConnection(self):
18+
retries = 0
19+
try:
20+
print("Connection succeed")
21+
return pika.BlockingConnection(
22+
pika.ConnectionParameters(
23+
host=self.queue_host, port=self.host_port, client_properties={"module_specs": self.specs})
24+
)
25+
except:
26+
retries += 1
27+
if (retries > self.RECONNECT_MAX):
28+
raise Exception("Failed to connect to server")
29+
print("Connection failed, attempt " + str(retries) +
30+
". Retrying in " + str(self.RECONNECT_TIMEOUT) + " seconds...", flush=True)
31+
time.sleep(self.RECONNECT_TIMEOUT)
32+
return self.__getConnection(self.queue_host, self.host_port, self.specs)
33+
34+
def callback(self, ch, method, properties, body, user_callback, id):
35+
print("Running python test", flush=True)
36+
37+
msg = json.loads(body)
38+
39+
msg = user_callback(msg)
40+
41+
ch.basic_publish(exchange="", routing_key=id + "-out", body=json.dumps(msg))
42+
43+
print("Results sent.", flush=True)
44+
45+
def connect(self):
46+
connection = self.__getConnection()
47+
ch = connection.channel()
48+
ch.queue_declare(self.specs["id"] + "-in", durable=True)
49+
ch.queue_declare(self.specs["id"] + "-out", durable=True)
50+
self.ch = ch
51+
52+
def run(self):
53+
self.ch.basic_consume(queue=self.specs["id"] + "-in", on_message_callback=self.callback, auto_ack=True)
54+
print("Waiting for messages in " + self.specs["id"] + "-in" +
55+
". Output will be sended to " + self.specs["id"] + "-out" + ". To exit press CTRL+C", flush=True)
56+
self.ch.start_consuming()

test/example.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys, os.path
2+
modules_dir = (os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))+ '/src/')
3+
sys.path.append(modules_dir)
4+
5+
from client_python_m2p import M2P
6+
7+
import os
8+
import json
9+
10+
f = open(os.path.dirname(__file__) + '/module-name-specs.json')
11+
specs = json.load(f)
12+
13+
QUEUE_SERVER_HOST, QUEUE_SERVER_PORT = os.environ.get(
14+
"QUEUE_SERVER", "localhost:3002").split(":")
15+
16+
def myCode(msg):
17+
18+
print(msg)
19+
20+
msg["echo-image"] = msg["input-image"]
21+
msg["echo-video"] = msg["input-video"]
22+
msg["backwords-text"] = msg["input-text"][::-1]
23+
24+
return msg
25+
26+
MyM2P = M2P(QUEUE_SERVER_HOST, QUEUE_SERVER_PORT, specs, myCode)
27+
28+
MyM2P.connect()
29+
30+
MyM2P.run()

test/module-name-specs.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"id": "template-module",
3+
"name": "Template Module",
4+
"description": "Template module that receives an image, a video and a text, and give everything back as output, but with the text backwords",
5+
"author": "Bruno Jucá",
6+
"email": "brunojuca@ice.ufjf.br",
7+
"input_queue": "template-module-python-in",
8+
"output_queue": "template-module-python-out",
9+
"input": [
10+
{
11+
"id": "input-image",
12+
"link": true,
13+
"type": "file",
14+
"required": true,
15+
"accept": ["video/mp4"]
16+
},
17+
{
18+
"id": "input-video",
19+
"link": true,
20+
"type": "file",
21+
"required": true,
22+
"accept": ["video/mp4"]
23+
},
24+
{
25+
"id": "input-text",
26+
"link": false,
27+
"type": "text",
28+
"required": true
29+
}
30+
],
31+
"output": [
32+
{
33+
"id": "echo-image",
34+
"link": true,
35+
"type": "file",
36+
"accept": ["video/mp4"]
37+
},
38+
{
39+
"id": "echo-video",
40+
"link": true,
41+
"type": "file",
42+
"accept": ["video/mp4"]
43+
},
44+
{
45+
"id": "echo-text",
46+
"link": false,
47+
"type": "text"
48+
}
49+
]
50+
}

0 commit comments

Comments
 (0)