Skip to content

# `` rdfpipe

A commandline tool for parsing RDF in different formats and serializing the resulting graph to a chosen format.

Functions:

- `main` –
- `make_option_parser` –
- `parse_and_serialize` –

Attributes:

- `DEFAULT_INPUT_FORMAT` –
- `DEFAULT_OUTPUT_FORMAT` –

## `` DEFAULT_INPUT_FORMAT `module-attribute`

    DEFAULT_INPUT_FORMAT = 'xml'

## `` DEFAULT_OUTPUT_FORMAT `module-attribute`

    DEFAULT_OUTPUT_FORMAT = 'n3'

## `` main

    main()

Source code in `rdflib/tools/rdfpipe.py`

    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204

    def main():
        oparser = make_option_parser()
        opts, args = oparser.parse_args()
        if len(args) < 1:
            oparser.print_usage()
            oparser.exit()

        if opts.warn:
            loglevel = logging.WARNING
        else:
            loglevel = logging.CRITICAL
        logging.basicConfig(level=loglevel)

        ns_bindings = {}
        if opts.ns:
            for ns_kw in opts.ns:
                pfx, uri = ns_kw.split("=")
                ns_bindings[pfx] = uri

        outfile: Optional[BinaryIO] = sys.stdout.buffer

        if opts.no_out:
            outfile = None

        parse_and_serialize(
            args, opts.input_format, opts.guess, outfile, opts.output_format, ns_bindings
        )

## `` make_option_parser

    make_option_parser()

Source code in `rdflib/tools/rdfpipe.py`

     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171

    def make_option_parser():
        parser_names = _get_plugin_names(Parser)
        serializer_names = _get_plugin_names(Serializer)
        kw_example = "FORMAT:(+)KW1,-KW2,KW3=VALUE"

        oparser = OptionParser(
            "%prog [-h] [-i INPUT_FORMAT] [-o OUTPUT_FORMAT] "
            + "[--ns=PFX=NS ...] [-] [FILE ...]",
            description=__doc__.strip()
            + (
                " Reads file system paths, URLs or from stdin if '-' is given."
                " The result is serialized to stdout."
            ),
            version="%prog " + "(using rdflib %s)" % rdflib.__version__,
        )

        oparser.add_option(
            "-i",
            "--input-format",
            type=str,  # default=DEFAULT_INPUT_FORMAT,
            help="Format of the input document(s)."
            " Available input formats are: %s." % parser_names
            + " If no format is given, it will be "
            + "guessed from the file name extension."
            + " Keywords to parser can be given after format like: %s." % kw_example,
            metavar="INPUT_FORMAT",
        )

        oparser.add_option(
            "-o",
            "--output-format",
            type=str,
            default=DEFAULT_OUTPUT_FORMAT,
            help="Format of the graph serialization."
            " Available output formats are: %s." % serializer_names
            + " Default format is: '%default'."
            + " Keywords to serializer can be given after format like: %s." % kw_example,
            metavar="OUTPUT_FORMAT",
        )

        oparser.add_option(
            "--ns",
            action="append",
            type=str,
            help="Register a namespace binding (QName prefix to a base URI). "
            "This can be used more than once.",
            metavar="PREFIX=NAMESPACE",
        )

        oparser.add_option(
            "--no-guess",
            dest="guess",
            action="store_false",
            default=True,
            help="Don't guess format based on file suffix.",
        )

        oparser.add_option(
            "--no-out",
            action="store_true",
            default=False,
            help="Don't output the resulting graph "
            + "(useful for checking validity of input).",
        )

        oparser.add_option(
            "-w",
            "--warn",
            action="store_true",
            default=False,
            help="Output warnings to stderr (by default only critical errors).",
        )

        return oparser

## `` parse_and_serialize

    parse_and_serialize(input_files, input_format, guess, outfile, output_format, ns_bindings, store_conn='', store_type=None)

Source code in `rdflib/tools/rdfpipe.py`

    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61

    def parse_and_serialize(
        input_files,
        input_format,
        guess,
        outfile,
        output_format,
        ns_bindings,
        store_conn="",
        store_type=None,
    ):
        if store_type:
            store = plugin.get(store_type, Store)()
            store.open(store_conn)
            graph = ConjunctiveGraph(store)
        else:
            store = None
            graph = ConjunctiveGraph()

        for prefix, uri in ns_bindings.items():
            graph.namespace_manager.bind(prefix, uri, override=False)

        for fpath in input_files:
            use_format, kws = _format_and_kws(input_format)
            if fpath == "-":
                fpath = sys.stdin
            elif not input_format and guess:
                use_format = guess_format(fpath) or DEFAULT_INPUT_FORMAT
            graph.parse(fpath, format=use_format, **kws)

        if outfile:
            output_format, kws = _format_and_kws(output_format)
            kws.setdefault("base", None)
            graph.serialize(destination=outfile, format=output_format, **kws)

        if store:
            store.rollback()