Esempio n. 1
0
def _add_wrapper_to_bulk_transaction(transaction, wrapper, index):
    required_keys = {"doc_format", "doc", "name"}
    # Check object keys
    if required_keys - set(wrapper.keys()):
        raise UserError("Missing required field in document {}: {}".format(
            index, list(required_keys - set(wrapper.keys()))))

    name, doc, doc_format = unpack_bulk_wrapper(wrapper)

    # Parse doc
    doc_format = wrapper["doc_format"].lower()
    if doc_format == "json":
        try:
            data = utils.parse.parse_json(doc)
        except Exception as e:
            raise UserError("Unable to parse doc {}: {}".format(name, e))
    elif doc_format == "tsv":
        data, errors = utils.transforms.TSVToJSONConverter().convert(doc)
    elif doc_format == "csv":
        data, errors = utils.transforms.CSVToJSONConverter().convert(doc)
    else:
        raise UnsupportedError(doc_format)

    # Add doc to transaction
    transaction.add_doc(name, doc_format, doc, data)
Esempio n. 2
0
def _add_wrapper_to_bulk_transaction(transaction, wrapper, index):
    required_keys = {'doc_format', 'doc', 'name'}
    # Check object keys
    if required_keys - set(wrapper.keys()):
        raise UserError('Missing required field in document {}: {}'.format(
            index, list(required_keys - set(wrapper.keys()))))

    name, doc, doc_format = unpack_bulk_wrapper(wrapper)

    # Parse doc
    doc_format = wrapper['doc_format'].upper()
    if doc_format == 'json':
        try:
            data = utils.parse.parse_json(doc)
        except Exception as e:
            raise UserError('Unable to parse doc {}: {}'.format(name, e))
    elif doc_format == 'tsv':
        data, errors = utils.transforms.TSVToJSONConverter().convert(doc)
    elif doc_format == 'csv':
        data, errors = utils.transforms.CSVToJSONConverter().convert(doc)
    else:
        raise UnsupportedError(doc_format)

    # Add doc to transaction
    transaction.add_doc(name, doc_format, doc, data)
Esempio n. 3
0
 def get_response(self):
     """Return response based on format and number of results."""
     if self.is_delimited:
         return self.get_delimited_response()
     elif self.is_json:
         return self.get_json_response()
     else:
         raise UnsupportedError(self.file_format)
Esempio n. 4
0
def entity_to_template(label, exclude_id=True, file_format="tsv", **kwargs):
    """Return template dict for given label."""
    if label not in dictionary.schema:
        raise NotFoundError("Entity type {} is not in dictionary".format(label))
    if file_format not in SUPPORTED_FORMATS:
        raise UnsupportedError(file_format)
    schema = dictionary.schema[label]
    links = _get_links(file_format, schema["links"], exclude_id)
    if file_format == "json":
        return entity_to_template_json(links, schema, exclude_id)
    else:
        return entity_to_template_delimited(links, schema, exclude_id)
Esempio n. 5
0
def entity_to_template_str(label, file_format, **kwargs):
    """Return template based on file_format for a given node label."""
    template = entity_to_template(label, file_format=file_format)
    if file_format == 'json':
        return json_dumps_formatted(template)
    elif file_format in DELIMITERS:
        output = StringIO.StringIO()
        writer = csv.writer(output, delimiter=DELIMITERS[file_format])
        writer.writerow(template)
        writer.writerow(tsv_example_row(label, template))
        return output.getvalue()
    else:
        raise UnsupportedError(file_format)