Пример #1
0
def _add_igt(igt, cdir, cindex=None, refresh=True):
    if cindex is None:
        cindex = _load_index(cdir)

    if igt.id in cindex['igt_index']:
        raise SleipnirDbError(
            'Igt ID "{}" already exists in corpus.'.format(igt.id),
        )

    while True:
        fn = '%s.json.gz' % _make_new_id(4)
        igtpath = os.path.join(cdir, fn)
        if not os.path.exists(igtpath):
            break
    _jsondump(xigtjson.encode_igt(igt), igtpath)
    cindex['igts'].append({'id': igt.id, 'tier_count': len(igt), 'path': fn})
    if refresh:
        _refresh_igt_index(cindex)
        _dump_index(cindex, cdir)
    # return the updated cindex so the caller can see the effect (in case
    # it didn't pass in a cindex)
    return cindex
Пример #2
0
    def set_igt(self, corpus_id, igt_id, igt):
        if igt is None:
            raise SleipnirDbError(
                'Cannot assign empty IGT; delete the IGT instead.'
            )
        # ensure new igt's ID maps the target
        if igt.id is None:
            try:
                igt.id = igt_id
            except ValueError:
                raise SleipnirDbError(
                    'Invalid ID: {}'.format(igt_id),
                    status_code=400
                )
        elif igt.id != igt_id:
            raise SleipnirDbError(
                'Igt ID must match requested ID: {} != {}'
                .format(str(igt.id), igt_id),
                status_code=400
            )
        cdir = self._corpus_path(corpus_id)
        cindex = _load_index(cdir)
        igt_entry_idx = cindex['igt_index'].get(igt.id)
        if igt_entry_idx is None:  # target doesn't exist; just add
            _add_igt(igt, cdir, cindex=cindex)
            created = True
        else:  # target exists; replace
            igt_entry = cindex['igts'][igt_entry_idx]
            igt_path = os.path.join(cdir, igt_entry['path'])
            _jsondump(xigtjson.encode_igt(igt), igt_path)
            igt_entry['tier_count'] = len(igt)
            created = False
        _dump_index(cindex, cdir)
        self._update_index_entry(corpus_id, igt_count=len(cindex['igts']))

        return {'id': igt_id, 'created': created}