Commit 7b488c60 authored by jssuzanne's avatar jssuzanne
Browse files

Start to refactor anybus

--HG--
branch : 8.0
parent acf70e7be84d
#!/usr/bin/env python
#==============================================================================
# =
# anybus module for OpenERP, Use the lib anybus and broker rabbitmq to send
# and receive data
# Copyright (C) 2012 Anybox (<http://http://anybox.fr>)
# Jean-Sebastien SUZANNE <jssuzanne@anybox.fr>
# =
# This file is a part of anybus
# =
# anybus is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License v3 or later
# as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# =
# anybus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License v3 or later for more details.
# =
# You should have received a copy of the GNU Affero General Public License
# v3 or later along with this program.
# If not, see <http://www.gnu.org/licenses/>.
# =
#==============================================================================
# anybus module for OpenERP, Use the lib anybus and broker rabbitmq to send
# and receive data
# Copyright (C) 2012 Anybox (<http://http://anybox.fr>)
# Jean-Sebastien SUZANNE <jssuzanne@anybox.fr>
# Copyright (C) 2018 Anybox (<http://http://anybox.fr>)
# Jean-Sebastien SUZANNE <jssuzanne@anybox.fr>
#
# This file is a part of anybus
#
# anybus is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License v3 or later
# as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# anybus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License v3 or later for more details.
#
# You should have received a copy of the GNU Affero General Public License
# v3 or later along with this program.
# If not, see <http://www.gnu.org/licenses/>.
{
'name': 'AnyBus Core',
'version': '0.3',
'category': 'Anybus',
'description': """
Use the lib kombu and broker rabbitmq to send and receive data
Use the lib pika and broker rabbitmq to send and receive data
""",
'author': 'Jean-Sebastien SUZANNE',
'website': 'http://anybox.fr',
......
# -*- coding: utf-8 -*-
from openerp.osv import osv, fields
from kombu import Connection
from odoo import models, fields
import socket
from tools.translate import _
class anybusConnection(osv.Model):
class anybusConnection(models.Model):
"""
Class used to establish the connection between your ERP and server RabbitMQ
.. Note::
The BrokerConnection class supports additional keyword arguments,
these are: hostname: Default host name if not provided in the URL.
userid: Default user name if not provided in the URL. password: Default password
if not provided in the URL. virtual_host: Default virtual host if not provided in the
URL. port: Default port if not provided in the URL. transport: Default transport if
not provided in the URL. Can be a string specifying the path to the class.
(e.g. kombu.transport.pyamqplib. Transport), or one of the aliases: amqplib, pika,
redis, memory, and so on. ssl: Use SSL to connect to the server. Default is False.
Only supported by the amqp transport. insist: Insist on connecting to a server. In a
configuration with multiple load-sharing servers, the insist option tells the server
that the client is insisting on a connection to the specified server. Default is
False. Only supported by the amqp and pika transports, and not by AMQP 0-9-1.
connect_timeout: Timeout in seconds for connecting to the server. May not be
supported by the specified transport. transport_options: A dict of additional
connection arguments to pass to alternate kombu channel implementations. Consult the
transport
documentation for available options.
Class used to establish the connection between your ERP and server
RabbitMQ
"""
_name = 'anybus.connection'
_description = 'anybus connection'
_columns = {
'name': fields.char('Name', size=64, required=True),
'uri': fields.char(
'URI', size=255, required=True, help='Define the connection URI '
'for the AMQP broker (ex: RabbitMQ)'),
'state': fields.selection(
[('draft', 'Draft'), ('declared', 'Declared')], 'State',
readonly=True, help='Define the state of the connection'),
'exchange_ids': fields.one2many(
'anybus.entity.exchange', 'connection_id', 'Exchanges',
readonly=True),
'queue_ids': fields.one2many('anybus.entity.queue', 'connection_id',
'Queues', readonly=True),
}
name = fields.Char('Profil', required=True)
# type = fields.Selection(
# (('url', 'URL'), ('parameter', 'Config parameter')), default='url',
# help='Define if the connection parameter come from URL or another'
# )
url = field.Char(
size=255, required=True,
help='Define the connection URI for the AMQP broker (ex: RabbitMQ)'
default='amqp://guest:guest@localhost:5672/%2F',
)
state = fields.Selection(
(('disconnected', 'Disconnected'), ('connected', 'Connected')),
default='disconnected', readonly=True,
)
_defaults = {
'uri': lambda *a: 'amqp://guest:guest@localhost:5672//%(dbname)s',
'state': lambda *a: 'draft',
}
def _get_uri(self, cr, uid, connection, context=None):
def _get_url(self):
"""
return the uri of the connexion
return the url of the connexion
"""
return connection.uri % {'dbname': cr.dbname}
return self.url
def action_declare(self, cr, uid, ids, context=None):
def action_declare_all(self):
"""
Function to declare a connection. When a connection is declared,
then you are connected to the RabbitMQ server.
"""
for connection in self.browse(cr, uid, ids, context=context):
try:
uri = self._get_uri(cr, uid, connection, context=context)
conn = Connection(uri)
conn.connect()
conn.close()
conn.release()
connection.write({'state': 'declared'})
except socket.error, e:
raise osv.except_osv(
_('Anybus Connection error'), e.args[1].decode('utf-8'))
except IOError, e:
error = "No vhost found for %r" % uri
raise osv.except_osv(_('Anybus Connection error'),
_(error))
return {}
def action_redraft(self, cr, uid, ids, context=None):
"""
This function searches all exchanges and queues that are reported
and changes their state in ``draft``
"""
exchange_obj = self.pool.get('anybus.entity.exchange')
exchange_ids = exchange_obj.search(
cr, uid, [('state', '=', 'declared')], context=context)
exchange_obj.action_redraft(cr, uid, exchange_ids, context=context)
queue_obj = self.pool.get('anybus.entity.queue')
queue_ids = queue_obj.search(
cr, uid, [('state', '=', 'declared')], context=context)
queue_obj.action_redraft(cr, uid, queue_ids, context=context)
self.write(cr, uid, ids, {'state': 'draft'}, context=context)
return {}
def action_declare_all(self, cr, uid, ids, context=None):
"""
This function searches all exchanges and queues which are at the draft state
and ``declare`` it.
This function searches all exchanges, queues and binding,
and declare it in rabbitmq
"""
exchange_obj = self.pool.get('anybus.entity.exchange')
exchange_ids = exchange_obj.search(
......@@ -121,15 +57,3 @@ class anybusConnection(osv.Model):
cr, uid, [('state', '=', 'draft')], context=context)
queue_obj.action_purge(cr, uid, queue_ids, context=context)
return {}
def get_actions(self, cr, uid, ids, model, context=None):
"""
"""
model_ids = self.pool.get(model).search(cr, uid, [], context=context)
res = {}
for id in ids:
res[id] = model_ids
return res
## vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment