2023-02-03 16:13:57 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
"""
|
|
|
|
Importer Class
|
|
|
|
================
|
|
|
|
|
|
|
|
Import Content
|
|
|
|
|
|
|
|
"""
|
2023-06-02 11:03:32 +02:00
|
|
|
import base64
|
|
|
|
import gzip
|
|
|
|
import logging
|
|
|
|
import logging.config
|
2023-02-03 16:13:57 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
|
|
|
|
2023-06-02 11:03:32 +02:00
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
2023-02-03 16:13:57 +01:00
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
# from ConfigLoader import ConfigLoader
|
2023-06-02 11:03:32 +02:00
|
|
|
from lib import ail_logger
|
|
|
|
from lib.ail_queues import AILQueue
|
2023-02-03 16:13:57 +01:00
|
|
|
|
2023-06-02 11:03:32 +02:00
|
|
|
logging.config.dictConfig(ail_logger.get_config(name='modules'))
|
|
|
|
|
|
|
|
# TODO Clean queue one object destruct
|
|
|
|
|
|
|
|
class AbstractImporter(ABC): # TODO ail queues
|
|
|
|
def __init__(self, queue=False):
|
2023-02-03 16:13:57 +01:00
|
|
|
"""
|
2023-06-02 11:03:32 +02:00
|
|
|
AIL Importer
|
|
|
|
:param queue: Allow to push messages to other modules
|
2023-02-03 16:13:57 +01:00
|
|
|
"""
|
|
|
|
# Module name if provided else instance className
|
|
|
|
self.name = self._name()
|
2023-06-02 11:03:32 +02:00
|
|
|
self.logger = logging.getLogger(f'{self.__class__.__name__}')
|
|
|
|
|
|
|
|
# Setup the I/O queues for one shot importers
|
|
|
|
if queue:
|
|
|
|
self.queue = AILQueue(self.name, 'importer_manual')
|
2023-02-03 16:13:57 +01:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def importer(self, *args, **kwargs):
|
|
|
|
"""Importer function"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _name(self):
|
|
|
|
"""
|
|
|
|
Returns the instance class name (ie. the Exporter Name)
|
|
|
|
"""
|
|
|
|
return self.__class__.__name__
|
|
|
|
|
2023-10-11 12:06:01 +02:00
|
|
|
def add_message_to_queue(self, obj, message='', queue=None):
|
2023-06-02 11:03:32 +02:00
|
|
|
"""
|
|
|
|
Add message to queue
|
2023-10-11 12:06:01 +02:00
|
|
|
:param obj: AILObject
|
2023-06-02 11:03:32 +02:00
|
|
|
:param message: message to send in queue
|
2023-10-11 12:06:01 +02:00
|
|
|
:param queue: queue name or module name
|
2023-06-02 11:03:32 +02:00
|
|
|
|
|
|
|
ex: add_message_to_queue(item_id, 'Mail')
|
|
|
|
"""
|
2023-10-11 12:06:01 +02:00
|
|
|
if not obj:
|
|
|
|
raise Exception(f'Invalid AIL object, {obj}')
|
|
|
|
obj_global_id = obj.get_global_id()
|
|
|
|
self.queue.send_message(obj_global_id, message, queue)
|
|
|
|
|
|
|
|
def get_available_queues(self):
|
|
|
|
return self.queue.get_out_queues()
|
2023-06-02 11:03:32 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def b64(content):
|
|
|
|
if isinstance(content, str):
|
|
|
|
content = content.encode()
|
|
|
|
return base64.b64encode(content).decode()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create_gzip(content):
|
|
|
|
if isinstance(content, str):
|
|
|
|
content = content.encode()
|
|
|
|
return gzip.compress(content)
|
|
|
|
|
|
|
|
def b64_gzip(self, content):
|
|
|
|
try:
|
|
|
|
gziped = self.create_gzip(content)
|
|
|
|
return self.b64(gziped)
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.warning(e)
|
|
|
|
return ''
|
|
|
|
|
2023-10-11 12:06:01 +02:00
|
|
|
def create_message(self, content, b64=False, gzipped=False, source=None):
|
2023-06-02 11:03:32 +02:00
|
|
|
if not source:
|
|
|
|
source = self.name
|
2023-10-05 16:24:28 +02:00
|
|
|
|
2023-10-11 12:06:01 +02:00
|
|
|
if content:
|
|
|
|
if not gzipped:
|
|
|
|
content = self.b64_gzip(content)
|
|
|
|
elif not b64:
|
|
|
|
content = self.b64(content)
|
|
|
|
if not content:
|
|
|
|
return None
|
|
|
|
if isinstance(content, bytes):
|
|
|
|
content = content.decode()
|
|
|
|
return f'{source} {content}'
|
|
|
|
else:
|
|
|
|
return f'{source}'
|
2023-02-03 16:13:57 +01:00
|
|
|
|