示例#1
0
    def gen(self, name):
        '''
        Atomically get/gen an EventBus and incref.
        (requires ctor during BusRef init)

        Args:
            name (str): The name/iden of the EventBus instance.
        '''
        if self.ctor is None:
            raise s_exc.NoSuchCtor(name=name, mesg='BusRef.gen() requires ctor')

        with self.lock:

            ebus = self.ebus_by_name.get(name)

            if ebus is None:
                ebus = self.ctor(name)

                if ebus is not None:

                    def fini():
                        self.ebus_by_name.pop(name, None)

                    ebus.onfini(fini)
                    self.ebus_by_name[name] = ebus

            else:
                ebus.incref()

            return ebus
示例#2
0
文件: cell.py 项目: wesinator/synapse
async def main(argv, outp=s_output.stdout):

    outp.printf(f'Resolving cellpath: {argv[0]}')
    ctor = s_dyndeps.getDynLocal(argv[0])
    if ctor is None:
        raise s_exc.NoSuchCtor(name=argv[0], mesg='No Cell ctor found.')

    return await ctor.initFromArgv(argv[1:], outp=outp)
示例#3
0
async def getCell(ctor, conf):
    loc = s_dyndeps.getDynLocal(ctor)
    if loc is None:
        raise s_exc.NoSuchCtor(mesg=f'Unable to resolve ctor [{ctor}]',
                               ctor=ctor)
    with s_common.getTempDir() as dirn:
        async with await loc.anit(dirn, conf=conf) as cell:
            yield cell
示例#4
0
 async def _handleStormVcrCallback(self, text):
     '''
     Get a callback function as a dynlocal
     '''
     cb = s_dyndeps.getDynLocal(text)
     if cb is None:
         raise s_exc.NoSuchCtor(mesg=f'Failed to get callback "{text}"',
                                ctor=text)
     self.context['storm-vcr-callback'] = cb
示例#5
0
def load_ctor(name, opts):
    '''
    Load the given ctor path as a synapse CoreModule for extending the Cortex implementation.

    Args:
        name (str): Python path to a class ctor to load.
        opts (dict): Dictionary of configuration options.

    Example:
        Load the foopkg.barmod.Baz ctor::

            import synapse.lib.modules as s_modules
            s_modules.load_ctor('foopkg.barmod.Baz', {})

    Notes:
        This can only be used to dynamically load a subclass of the CoreModule class.
        Users should be aware that the import process can perform arbitrary
        code execution by imported modules.

    Returns:
        The loaded class is returned.

    Raises:
        NoSuchCtor: If the imported module does not have the listed ctor
        BadCtorType: If the ctor is not a subclass of the CoreModule class.
    '''
    # Deferred import to prevent a import-loop upon __init__.py
    # execution of the main library.
    import synapse.lib.module as s_module
    modpath, ctor = name.rsplit('.', 1)
    smod = ctors.get(name)
    if smod is None:
        smod = s_dyndeps.tryDynMod(modpath)
        cls = getattr(smod, ctor, None)
        if cls is None:
            raise s_exc.NoSuchCtor(name=name, mesg='Ctor not found.')
        if not issubclass(cls, s_module.CoreModule):
            raise s_exc.BadCtorType(
                name=name, mesg='Ctor is not a CoreModule implementation.')
        ctors[name] = smod
        ctorlist.append((name, smod, opts))
    return ctor
示例#6
0
    async def gen(self, name):
        '''
        Atomically get/gen a Base and incref.
        (requires ctor during BaseRef init)

        Args:
            name (str): The name/iden of the Base instance.
        '''
        if self.ctor is None:
            raise s_exc.NoSuchCtor(name=name, mesg='BaseRef.gen() requires ctor')

        base = self.base_by_name.get(name)

        if base is None:
            base = await self.ctor(name)
            self.put(name, base)
        else:
            base.incref()

        return base
示例#7
0
文件: cell.py 项目: neelsenc/synapse
async def getCell(
    outp,
    celldir,
    ctorpath,
    httpport,
    telepath,
    name=None,
):

    outp.printf(f'Resolving cellpath: {ctorpath}')

    ctor = s_dyndeps.getDynLocal(ctorpath)
    if ctor is None:
        raise s_exc.NoSuchCtor(name=ctorpath, mesg='No Cell ctor found.')

    outp.printf(f'starting cell: {celldir}')

    cell = await ctor.anit(celldir)

    try:

        outp.printf(f'...cell API (telepath): {telepath}')
        await cell.dmon.listen(telepath)

        outp.printf(f'...cell API (https): {httpport}')
        await cell.addHttpsPort(httpport)

        if name:
            outp.printf(f'...cell additional share name: {name}')
            cell.dmon.share(name, cell)

        return cell

    except Exception:
        await cell.fini()
        raise
示例#8
0
def getStemCell(dirn):

    if not os.path.isdir(dirn):
        mesg = f'Directory {dirn} does not exist!'
        raise s_exc.NoSuchDir(mesg=mesg)

    ctorname = os.getenv('SYN_STEM_CELL_CTOR')

    cellyaml = os.path.join(dirn, 'cell.yaml')

    if os.path.isfile(cellyaml):
        conf = s_common.yamlload(cellyaml)
        ctorname = conf.get('cell:ctor', ctorname)

    if ctorname is not None:
        ctorname = ctorname.strip()
        ctor = s_dyndeps.getDynLocal(ctorname)
        if ctor is None:
            raise s_exc.NoSuchCtor(mesg=f'Unable to resolve ctor [{ctorname}]',
                                   ctor=ctorname)
        return ctor

    mesg = f'No such file: {cellyaml} and SYN_STEM_CELL_CTOR environmt variable is not set.'
    raise s_exc.NoSuchFile(mesg=mesg, path=cellyaml)