AIL-framework/bin/modules/Tags.py

66 lines
1.6 KiB
Python
Raw Normal View History

2018-05-16 14:39:01 +02:00
#!/usr/bin/env python3
# -*-coding:UTF-8 -*
"""
The Tags Module
================================
This module add tags to an item.
2018-05-16 14:39:01 +02:00
"""
2021-04-28 15:24:33 +02:00
##################################
# Import External packages
##################################
import os
import sys
2021-04-28 15:24:33 +02:00
sys.path.append(os.environ['AIL_BIN'])
2021-04-28 15:24:33 +02:00
##################################
# Import Project packages
##################################
from modules.abstract_module import AbstractModule
from lib.objects.Items import Item
from lib import Tag
2019-02-18 14:33:34 +01:00
2018-05-16 14:39:01 +02:00
2021-04-28 15:24:33 +02:00
class Tags(AbstractModule):
"""
Tags module for AIL framework
"""
def __init__(self):
super(Tags, self).__init__()
2018-05-16 14:39:01 +02:00
# Waiting time in seconds between to message processed
2021-04-28 15:24:33 +02:00
self.pending_seconds = 10
2018-05-16 14:39:01 +02:00
2021-04-28 15:24:33 +02:00
# Send module state to logs
2023-05-12 15:29:53 +02:00
self.logger.info(f'Module {self.module_name} initialized')
2018-05-16 14:39:01 +02:00
2021-04-28 15:24:33 +02:00
def compute(self, message):
# Extract item ID and tag from message
mess_split = message.split(';')
if len(mess_split) == 2:
tag = mess_split[0]
item = Item(mess_split[1])
# Create a new tag
Tag.add_object_tag(tag, 'item', item.get_id())
2021-10-29 18:48:12 +02:00
print(f'{item.get_id()}: Tagged {tag}')
2018-05-16 14:39:01 +02:00
# Forward message to channel
self.add_message_to_queue(message, 'Tag_feed')
2021-10-29 18:48:12 +02:00
message = f'{item.get_type()};{item.get_subtype(r_str=True)};{item.get_id()}'
self.add_message_to_queue(message, 'Sync')
2021-10-29 18:48:12 +02:00
else:
# Malformed message
raise Exception(f'too many values to unpack (expected 2) given {len(mess_split)} with message {message}')
2021-04-28 15:24:33 +02:00
if __name__ == '__main__':
module = Tags()
module.run()