You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.3 KiB

# Copyright (C) 2005-2024 Splunk Inc. All Rights Reserved.
import re
from itsi.content_packs.constants import ContentType
from itsi.content_packs.itoa import get_itoa_object_title, get_itoa_object_title_field
class ContentObjectsPrefixer(object):
"""prefixer that prefixes all objects that are selected to be installed."""
def __init__(self, logger, session_key, prefix='', length_limit=50):
"""
:param logger: a logger instance
:type logger: Logger
:param session_key: the session key
:type session_key: str
:param prefix: the object title prefix
:type prefix: str
"""
self.logger = logger
self.session_key = session_key
self.prefix = prefix
self.length_limit = length_limit
def process_objects(self, content_objects, **kwargs):
"""
Prefixes title of the given content objects.
:param content_objects: the content objects data
:type content_objects: dict
:return: the content objects data with title being prefixed
:rtype: dict
"""
if not self.is_prefix_valid():
return content_objects
processed_objects = {}
for content_type, objects in content_objects.items():
if content_type != ContentType.CORRELATION_SEARCH and content_type != ContentType.GLASS_TABLE_IMAGE\
and content_type != ContentType.GLASS_TABLE_ICON:
for obj in objects:
obj[get_itoa_object_title_field(content_type)] = \
self.prefix + get_itoa_object_title(content_type, obj)
processed_objects[content_type] = objects
return processed_objects
def is_prefix_valid(self):
"""
Determines whether user input prefix is valid or not
:return: whether prefix is valid or not
:rtype: boolean
"""
if not self.prefix:
return False
if len(self.prefix) > self.length_limit:
self.logger.error('Failed to prefix objects because it exceeds length 50.')
return False
if re.search('[="\']+', self.prefix):
self.logger.error('Failed to prefix objects because characters ' " = are not allowed in prefix.")
return False
return True