<commit_before>
import logging
import hubblestack.splunklogging
import copy
import time

log = logging.getLogger(__name__)

def publish():
    log.info('Started publishing config to splunk')
    
    initialize_splunklogging()
    filtered_conf=filter_config()
    class MockRecord(object):
            def __init__(self, message, levelname, asctime, name):
                self.message = message
                self.levelname = levelname
                self.asctime = asctime
                self.name = name

    handler = hubblestack.splunklogging.SplunkHandler()
    handler.emit(MockRecord(filtered_conf, 'INFO', time.asctime(), 'hubblestack.hubble_config'))
    log.info('Published config to splunk')	

def initialize_splunklogging():
    hubblestack.splunklogging.__grains__ = __grains__
    hubblestack.splunklogging.__salt__ = __salt__

#Filter out keys containing certain patterns to avoid sensitive information being sent to splunk
def filter_config():
    opts_copy = copy.deepcopy(__opts__)
    patterns_to_filter = ["password", "token"]
    filtered_conf = remove_sensitive_info(opts_copy, patterns_to_filter)
    return filtered_conf

#Recursively removes key value pairs where key contains any of patterns_to_filter
def remove_sensitive_info(obj, patterns_to_filter):
    if isinstance(obj, dict):
         obj = {
             key: remove_sensitive_info(value, patterns_to_filter)
             for key, value in obj.iteritems()
             if not any(patt in key for patt in patterns_to_filter)}
    elif isinstance(obj, list):
         obj = [remove_sensitive_info(item, patterns_to_filter)
                    for item in obj]
    return obj<commit_msg>changes as suggested by basepi (module to publish passed opts, added docstrings)<commit_after>
import logging
import hubblestack.splunklogging
import copy
import time

log = logging.getLogger(__name__)

def publish(*args):

    '''
    Publishes config to splunk at an interval defined in schedule
	
	*args
	   Tuple of opts to log (keys in __opts__). Only those key-value pairs would be published, keys for which are in *args
	   If not passed, entire __opts__ (excluding password/token) would be published 
	
    '''
    log.debug('Started publishing config to splunk')
	
    opts_to_log={}
    if not args:
        opts_to_log=copy.deepcopy(__opts__)
    else:
        for arg in args:
            if arg in  __opts__:
                opts_to_log[arg]=__opts__[arg]

    hubblestack.splunklogging.__grains__ = __grains__
    hubblestack.splunklogging.__salt__ = __salt__

    filtered_conf=filter_config(opts_to_log)
    class MockRecord(object):
            def __init__(self, message, levelname, asctime, name):
                self.message = message
                self.levelname = levelname
                self.asctime = asctime
                self.name = name

    handler = hubblestack.splunklogging.SplunkHandler()
    handler.emit(MockRecord(filtered_conf, 'INFO', time.asctime(), 'hubblestack.hubble_config'))
    log.debug('Published config to splunk')		
	
def filter_config(opts_to_log):
    '''
    Filters out keys containing certain patterns to avoid sensitive information being sent to splunk
    '''   
    patterns_to_filter = ["password", "token"]
    filtered_conf = remove_sensitive_info(opts_to_log, patterns_to_filter)
    return filtered_conf

def remove_sensitive_info(obj, patterns_to_filter):
    if isinstance(obj, dict):
         obj = {
             key: remove_sensitive_info(value, patterns_to_filter)
             for key, value in obj.iteritems()
             if not any(patt in key for patt in patterns_to_filter)}
    elif isinstance(obj, list):
         obj = [remove_sensitive_info(item, patterns_to_filter)
                    for item in obj]
    return obj
{'fraction_non_alphanumeric': 0.0751919512840879, 'fraction_numerical': 0.0, 'mean_word_length': 3.0976138828633406, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2485103', 'n_tokens_mistral': 1109, 'n_tokens_neox': 1061, 'n_words': 301}