Ejemplo n.º 1
0
    def test_tuplify(self):
        tv = [
            'node',
            [['test:str', 'test'], {
                'tags': {
                    'beep': [None, None],
                    'beep.boop': [0x01020304, 0x01020305]
                },
                'props': {
                    'tick': 0x01020304,
                    'tock': ['hehe', ['haha', 1]]
                }
            }]
        ]
        ev = ('node', (('test:str', 'test'), {
            'tags': {
                'beep': (None, None),
                'beep.boop': (0x01020304, 0x01020305)
            },
            'props': {
                'tick': 0x01020304,
                'tock': ('hehe', ('haha', 1))
            }
        }))

        self.eq(ev, s_common.tuplify(tv))
        tv = ('foo', ['bar', 'bat'])
        ev = ('foo', ('bar', 'bat'))
        self.eq(ev, s_common.tuplify(tv))
Ejemplo n.º 2
0
    async def set(self, full, valu, nexs=False):
        '''
        A set operation at the hive level (full path).
        '''
        valu = s_common.tuplify(valu)
        if nexs:
            return await self._push('hive:set', full, valu)

        return await self._set(full, valu)
Ejemplo n.º 3
0
    async def _checkCore(self, core):
        with self.getRegrDir('assets', REGR_VER) as assetdir:
            podesj = getAssetJson(assetdir, 'splicepodes.json')
            podesj = [p for p in podesj if p[0] not in NOMIGR_NDEF]
            tpodes = s_common.tuplify(podesj)

            # check all nodes (removing empty nodedata key)
            nodes = await core.nodes('.created -meta:source:name=test')

            podes = [n.pack(dorepr=True) for n in nodes]
            podes = [(p[0], {k: v
                             for k, v in p[1].items() if k != 'nodedata'})
                     for p in podes]
            self.gt(len(podes), 0)

            # handle the case where a tag with tagprops was deleted but tag:prop:del splices aren't generated
            for pode in podes:
                tags = pode[1]['tags'].keys()
                pode[1]['tagprops'] = {
                    k: v
                    for k, v in pode[1]['tagprops'].items() if k in tags
                }
                pode[1]['tagpropreprs'] = {
                    k: v
                    for k, v in pode[1]['tagpropreprs'].items() if k in tags
                }

            try:
                self.eq(podes, tpodes)
            except AssertionError:
                # print a more useful diff on error
                notincore = list(
                    itertools.filterfalse(lambda x: x in podes, tpodes))
                notintest = list(
                    itertools.filterfalse(lambda x: x in tpodes, podes))
                self.eq(notincore,
                        notintest)  # should be empty, therefore equal
                raise

            # manually check node subset
            self.len(1, await core.nodes('inet:ipv4=1.2.3.4'))
            self.len(2, await core.nodes('inet:dns:a:ipv4=1.2.3.4'))
Ejemplo n.º 4
0
    async def _initCellHive(self):
        isnew = not self.slab.dbexists('hive')

        db = self.slab.initdb('hive')
        hive = await s_hive.SlabHive.anit(self.slab,
                                          db=db,
                                          nexsroot=self.nexsroot)
        self.onfini(hive)

        if isnew:
            path = os.path.join(self.dirn, 'hiveboot.yaml')
            if os.path.isfile(path):
                logger.debug(f'Loading cell hive from {path}')
                tree = s_common.yamlload(path)
                if tree is not None:
                    # Pack and unpack the tree to avoid tuple/list issues
                    # for in-memory structures.
                    tree = s_common.tuplify(tree)
                    await hive.loadHiveTree(tree)

        return hive
Ejemplo n.º 5
0
 async def loadSplicelog(self, splicelog):
     for lyriden, items in splicelog.items():
         self.splicelog[lyriden] = s_common.tuplify(items)
Ejemplo n.º 6
0
    async def _handle_edit(self, core, opts):
        path = self.parsepath(opts.path)

        if opts.value is not None:
            if opts.value[0] not in '([{"':
                data = opts.value
            else:
                data = json.loads(opts.value)
            await core.setHiveKey(path, data)
            return
        elif opts.file is not None:
            with open(opts.file) as fh:
                s = fh.read()
                if len(s) == 0:
                    self.printf('Empty file.  Not writing key.')
                    return
                data = s if opts.string else json.loads(s)
                await core.setHiveKey(path, data)
                return

        editor = os.getenv('VISUAL', (os.getenv('EDITOR', None)))
        if editor is None or editor == '':
            self.printf(
                'Environment variable VISUAL or EDITOR must be set for --editor'
            )
            return
        tnam = None
        try:
            with tempfile.NamedTemporaryFile(mode='w', delete=False) as fh:
                old_valu = await core.getHiveKey(path)
                if old_valu is not None:
                    if opts.string:
                        if not isinstance(old_valu, str):
                            self.printf(
                                'Existing value is not a string, therefore not editable as a string'
                            )
                            return
                        data = old_valu
                    else:
                        try:
                            data = json.dumps(old_valu,
                                              indent=4,
                                              sort_keys=True)
                        except (ValueError, TypeError):
                            self.printf(
                                'Value is not JSON-encodable, therefore not editable.'
                            )
                            return
                    fh.write(data)
                tnam = fh.name
            while True:
                retn = subprocess.call(f'{editor} {tnam}', shell=True)
                if retn != 0:  # pragma: no cover
                    self.printf('Editor failed with non-zero code.  Aborting.')
                    return
                with open(tnam) as fh:
                    rawval = fh.read()
                    if len(rawval) == 0:  # pragma: no cover
                        self.printf('Empty file.  Not writing key.')
                        return
                    try:
                        valu = rawval if opts.string else json.loads(rawval)
                    except json.JSONDecodeError as e:  # pragma: no cover
                        self.printf(f'JSON decode failure: [{e}].  Reopening.')
                        await asyncio.sleep(1)
                        continue

                    # We lose the tuple/list distinction in the telepath round trip, so tuplify everything to compare
                    if (opts.string and valu == old_valu) or (
                            not opts.string
                            and s_common.tuplify(valu) == old_valu):
                        self.printf('Valu not changed.  Not writing key.')
                        return
                    await core.setHiveKey(path, valu)
                    break

        finally:
            if tnam is not None:
                os.unlink(tnam)