<commit_before>import imp
import logging

import synapse.eventbus as s_eventbus

logger = logging.getLogger(__name__)

class Plugins(s_eventbus.EventBus):
    '''
    Helper class for managing/calling plugins in a cortex.

    Example:

        core = s_cortex.openurl(url)

        plugs = Plugins(core)

        srccode = """

            # make plugin accessible to others by name

            _syn_plug_name = 'foo'
            _syn_plug_vers = (0,2,3)

            def onfoobar(mesg):
                dostuff( mesg[1].get('baz') )

            def _syn_plug_init( plugins ):
                plugins.on('foo:bar', onfoobar)

            def _syn_plug_fini( plugins ):
                plugins.off('foo:bar',onfoobar)

        """

        core.formTufoByProp('plugin', guid(), en=True, source=srccode)

        plugs.fire('foo:bar',baz='faz')

    '''
    def __init__(self, core, form='plugin'):

        s_eventbus.EventBus.__init__(self)

        self.core = core
        self.form = form

        self.plugs = {}
        self.plugmods = {}
        self.plugfuncs = {}

        self.plugbus = s_eventbus.EventBus()

        model = core.genDataModel()

        model.addTufoForm(form,ptype='guid')
        model.addTufoProp(form,'en',ptype='bool')
        model.addTufoProp(form,'name',ptype='str')
        model.addTufoProp(form,'source',ptype='str')

        self.core.on('tufo:add:%s' % (form,), self._onPlugAdd )
        self.core.on('tufo:set:%s:en' % (form,), self._onPlugSetEn )
        self.core.on('tufo:set:%s:source' % (form,), self._onPlugSetSource )

        [ self._runInitPlug(plug) for plug in core.getTufosByProp('%s:en' % form, 1) ]

        self.onfini( self._onPlugsFini )

    def _onPlugsFini(self):
        plugs = list( self.plugs.values() )
        [ self._runFiniPlug(plug) for plug in plugs ]

    def call(self, name, *args, **kwargs):
        '''
        Call a method on all loaded plugins which implement it.<commit_msg>update for core-is-model change and syn: prefix<commit_after>import imp
import logging

import synapse.eventbus as s_eventbus

logger = logging.getLogger(__name__)

class Plugins(s_eventbus.EventBus):
    '''
    Helper class for managing/calling plugins in a cortex.

    Example:

        core = s_cortex.openurl(url)

        plugs = Plugins(core)

        srccode = """

            # make plugin accessible to others by name

            _syn_plug_name = 'foo'
            _syn_plug_vers = (0,2,3)

            def onfoobar(mesg):
                dostuff( mesg[1].get('baz') )

            def _syn_plug_init( plugins ):
                plugins.on('foo:bar', onfoobar)

            def _syn_plug_fini( plugins ):
                plugins.off('foo:bar',onfoobar)

        """

        core.formTufoByProp('plugin', guid(), en=True, source=srccode)

        plugs.fire('foo:bar',baz='faz')

    '''
    def __init__(self, core, form='syn:plugin'):

        s_eventbus.EventBus.__init__(self)

        self.core = core
        self.form = form

        self.plugs = {}
        self.plugmods = {}
        self.plugfuncs = {}

        self.core.addTufoForm(form,ptype='guid')
        self.core.addTufoProp(form,'en',ptype='bool')
        self.core.addTufoProp(form,'name',ptype='str')
        self.core.addTufoProp(form,'source',ptype='str')

        self.core.on('tufo:add:%s' % (form,), self._onPlugAdd )
        self.core.on('tufo:set:%s:en' % (form,), self._onPlugSetEn )
        self.core.on('tufo:set:%s:source' % (form,), self._onPlugSetSource )

        [ self._runInitPlug(plug) for plug in core.getTufosByProp('%s:en' % form, 1) ]

        self.onfini( self._onPlugsFini )

    def _onPlugsFini(self):
        plugs = list( self.plugs.values() )
        [ self._runFiniPlug(plug) for plug in plugs ]

    def call(self, name, *args, **kwargs):
        '''
        Call a method on all loaded plugins which implement it.
{'fraction_non_alphanumeric': 0.13616473616473618, 'fraction_numerical': 0.002574002574002574, 'mean_word_length': 2.338487972508591, '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': '6401133', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1287, 'n_words': 237}