예제 #1
0
파일: util.py 프로젝트: nichtich/manubot
def citation_to_citeproc(citation, prune=True):
    """
    Return a dictionary with citation metadata
    """
    citation == standardize_citation(citation, warn_if_changed=True)
    source, identifier = citation.split(':', 1)

    if source in citeproc_retrievers:
        citeproc_retriever = import_function(citeproc_retrievers[source])
        csl_item = citeproc_retriever(identifier)
    else:
        msg = f'Unsupported citation source {source} in {citation}'
        raise ValueError(msg)

    from manubot import __version__ as manubot_version
    from manubot.cite.citeproc import (
        citeproc_passthrough,
        append_to_csl_item_note,
    )

    note_text = f'This CSL JSON Item was automatically generated by Manubot v{manubot_version} using citation-by-identifier.'
    note_dict = {
        'standard_id': citation,
    }
    append_to_csl_item_note(csl_item, note_text, note_dict)

    short_id = get_citation_short_id(citation)
    csl_item = citeproc_passthrough(csl_item, set_id=short_id, prune=prune)

    return csl_item
예제 #2
0
def citekey_to_csl_item(citekey, prune=True):
    """
    Generate a CSL Item (Python dictionary) for the input citekey.
    """
    from manubot.cite.csl_item import CSL_Item
    from manubot import __version__ as manubot_version

    citekey == standardize_citekey(citekey, warn_if_changed=True)
    source, identifier = citekey.split(':', 1)

    if source not in citeproc_retrievers:
        msg = f'Unsupported citation source {source!r} in {citekey!r}'
        raise ValueError(msg)
    citeproc_retriever = import_function(citeproc_retrievers[source])
    csl_item = citeproc_retriever(identifier)
    csl_item = CSL_Item(csl_item)

    note_text = f'This CSL JSON Item was automatically generated by Manubot v{manubot_version} using citation-by-identifier.'
    note_dict = {
        'standard_id': citekey,
    }
    csl_item.note_append_text(note_text)
    csl_item.note_append_dict(note_dict)

    short_citekey = shorten_citekey(citekey)
    csl_item.set_id(short_citekey)
    csl_item.clean(prune=prune)

    return csl_item
예제 #3
0
def citekey_to_csl_item(citekey, prune=True):
    """
    Generate a CSL Item (Python dictionary) for the input citekey.
    """
    citekey == standardize_citekey(citekey, warn_if_changed=True)
    source, identifier = citekey.split(':', 1)

    if source in citeproc_retrievers:
        citeproc_retriever = import_function(citeproc_retrievers[source])
        csl_item = citeproc_retriever(identifier)
    else:
        msg = f'Unsupported citation source {source!r} in {citekey!r}'
        raise ValueError(msg)

    from manubot import __version__ as manubot_version
    from manubot.cite.citeproc import (
        csl_item_passthrough,
        append_to_csl_item_note,
    )

    note_text = f'This CSL JSON Item was automatically generated by Manubot v{manubot_version} using citation-by-identifier.'
    note_dict = {
        'standard_id': citekey,
    }
    append_to_csl_item_note(csl_item, note_text, note_dict)

    short_citekey = shorten_citekey(citekey)
    csl_item = csl_item_passthrough(csl_item,
                                    set_id=short_citekey,
                                    prune=prune)

    return csl_item
예제 #4
0
def main():
    """
    Called as a console_scripts entry point in setup.py. This function defines
    the manubot command line script.
    """
    # Track if message gets logged with severity of error or greater
    # See https://stackoverflow.com/a/45446664/4651668
    import errorhandler
    error_handler = errorhandler.ErrorHandler()

    # Log DeprecationWarnings
    warnings.simplefilter('always', DeprecationWarning)
    logging.captureWarnings(True)

    # Log to stderr
    logger = logging.getLogger()
    stream_handler = logging.StreamHandler(stream=sys.stderr)
    stream_handler.setFormatter(
        logging.Formatter('## {levelname}\n{message}', style='{'))
    logger.addHandler(stream_handler)

    args = parse_arguments()
    logger.setLevel(getattr(logging, args.log_level))

    function = import_function(args.function)
    function(args)

    if error_handler.fired:
        logging.critical('Failure: exiting with code 1 due to logged errors')
        raise SystemExit(1)
예제 #5
0
파일: handlers.py 프로젝트: xbee/manubot
def get_handler(prefix_lower):
    if not isinstance(prefix_lower, str):
        raise TypeError(
            f"prefix_lower should be a str, instead received {prefix_lower.__class__.__name__}"
        )
    assert prefix_lower == prefix_lower.lower()
    handler = prefix_to_handler[prefix_lower]
    handler = import_function(handler)(prefix_lower)
    return handler
예제 #6
0
def main():
    """
    Called as a console_scripts entry point in setup.cfg. This function defines
    the manubot command line script.
    """
    diagnostics = setup_logging_and_errors()
    args = parse_arguments()
    diagnostics["logger"].setLevel(getattr(logging, args.log_level))
    function = import_function(args.function)
    function(args)
    exit_if_error_handler_fired(diagnostics["error_handler"])
예제 #7
0
def citation_to_citeproc(citation, prune=True):
    """
    Return a dictionary with citation metadata
    """
    assert citation == standardize_citation(citation)
    source, identifier = citation.split(':', 1)

    if source in citeproc_retrievers:
        citeproc_retriever = import_function(citeproc_retrievers[source])
        citeproc = citeproc_retriever(identifier)
    else:
        msg = f'Unsupported citation source {source} in {citation}'
        raise ValueError(msg)

    citation_id = get_citation_id(citation)
    from manubot.cite.citeproc import citeproc_passthrough
    citeproc = citeproc_passthrough(citeproc, set_id=citation_id, prune=prune)

    return citeproc
예제 #8
0
파일: handlers.py 프로젝트: xbee/manubot
def _generate_prefix_to_handler() -> typing.Dict[str, str]:
    """
    Generate complete dictionary for prefix_to_handler.
    """
    import inspect

    from .curie import get_curie_handlers

    curie_handlers = get_curie_handlers()
    pth = {}
    for handler in curie_handlers + _local_handlers:
        if isinstance(handler, str):
            handler = import_function(handler)("dummy_prefix_lower")
        for prefix in handler.prefixes:
            pth[
                prefix
            ] = f"{inspect.getmodule(handler).__name__}.{handler.__class__.__name__}"
    pth = dict(sorted(pth.items()))  # sort for clean diffs of serialized dict
    return pth