Skip to content

# `` cmdlineutils

Functions:

- `main` –

  A main function for tools that read RDF from files given on commandline

## `` main

    main(target, _help=_help, options='', stdin=True)

A main function for tools that read RDF from files given on commandline or from STDIN (if stdin parameter is true)

Source code in `rdflib/extras/cmdlineutils.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
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77

    def main(target, _help=_help, options="", stdin=True):
        """
        A main function for tools that read RDF from files given on commandline
        or from STDIN (if stdin parameter is true)
        """

        args, files = getopt.getopt(sys.argv[1:], "hf:o:" + options)
        dargs = dict(args)

        if "-h" in dargs:
            _help()
            sys.exit(-1)

        g = rdflib.Graph()

        if "-f" in dargs:
            f = dargs["-f"]
        else:
            f = None

        out: Union[TextIO, codecs.StreamReaderWriter]
        if "-o" in dargs:
            sys.stderr.write("Output to %s\n" % dargs["-o"])
            out = codecs.open(dargs["-o"], "w", "utf-8")
        else:
            out = sys.stdout

        start = time.time()
        if len(files) == 0 and stdin:
            sys.stderr.write("Reading from stdin as %s..." % f)
            g.parse(sys.stdin, format=f)
            sys.stderr.write("[done]\n")
        else:
            size = 0
            for x in files:
                if f is None:
                    f = guess_format(x)
                start1 = time.time()
                sys.stderr.write("Loading %s as %s... " % (x, f))
                g.parse(x, format=f)
                sys.stderr.write(
                    "done.\t(%d triples\t%.2f seconds)\n"
                    % (len(g) - size, time.time() - start1)
                )
                size = len(g)

        sys.stderr.write(
            "Loaded a total of %d triples in %.2f seconds.\n"
            % (len(g), time.time() - start)
        )

        target(g, out, args)