Esempio n. 1
0
def main(argv: List[str] = None):
    """
    Process RDF files in indir, doing ShEx validation and/or comparing the RDF to the files in outdir
    :param argv: Argument list.  If None, use sys.argv
    :return: 0 if all RDF files that had valid FHIR in them were successful, 1 otherwise
    """
    dlp = dirlistproc.DirectoryListProcessor(argv, "Validate FHIR RDF", ".ttl", None, addargs=addargs,
                                             postparse=add_cache)
    if not (dlp.opts.infile or dlp.opts.indir):
        dirlistproc.DirectoryListProcessor(["--help"], "Validate FHIR RDF", ".ttl", None, addargs=addargs)
    nfiles, nsuccess = dlp.run(validate_rdf, file_filter_2=input_filter)
    print(f"Total={nfiles} Successful={nsuccess}")
    return 0 if nfiles == nsuccess else 1
Esempio n. 2
0
def main(argv: List[str] = None):
    """
    Turn any ShEx files in indir into ShExJ in outdir
    :param argv: Argument list.  If None, use sys.argv
    :return: 0 if conversion was successful, 1 if errors
    """
    dlp = dirlistproc.DirectoryListProcessor(argv, "ShEx to ShExJ converter",
                                             ".shex", ".shexj")
    if not (dlp.opts.infile or dlp.opts.indir):
        dirlistproc.DirectoryListProcessor(["--help"],
                                           "ShEx to ShExJ converter", ".shex",
                                           ".shexj")
    nfiles, nsuccess = dlp.run(do_shex)
    print(f"Total={nfiles} Successful={nsuccess}")
    return 0 if nfiles == nsuccess else 1
Esempio n. 3
0
 def gen_dlp(args: List[str]) -> dirlistproc.DirectoryListProcessor:
     return dirlistproc.DirectoryListProcessor(
         args,
         "Add FHIR R4 edits to JSON file",
         '.json',
         '.json',
         addargs=addargs)
Esempio n. 4
0
def main(argv: List[str] = None):
    """
    Convert RDF files into JSON-LD format

    :param argv: Argument list.  If None, use sys.argv
    :return: 0 if all RDF files that had valid FHIR in them were successful, 1 otherwise
    """
    def dlp(args: List[str]) -> dirlistproc.DirectoryListProcessor:
        return dirlistproc.DirectoryListProcessor(args,
                                                  "RDF Format Converter",
                                                  '.ttl',
                                                  'jsonld',
                                                  addargs=addargs)

    def set_suffix(fname: Optional[List[str]],
                   fmt: Optional[str]) -> Optional[Tuple[str, str]]:
        """
        If a format isn't explicated, see whether it can be determined from the list of file names.  Also deal with the
        ".json" / ".json-ld" issue.
        :param fname: possible list of file names
        :param fmt: user specified format
        :return: File suffix / conversion format.  None if undeterminable
        """
        if fname and not fmt:
            if '.' in fname[0]:
                fmt = fname[0].rsplit('.', 1)[1]
        return \
            (".json", "json-ld") if fmt == 'json' else\
            (".jsonld", "json-ld") if fmt == 'jsonld' else \
            (".xml", "pretty-xml") if fmt == "xml" else\
            (('' if fmt.startswith('.') else '.') + fmt, (fmt[1:] if fmt.startswith('.') else fmt)) if fmt else\
            (None, None)

    dlp = dlp(argv)
    if not (dlp.opts.infile or dlp.opts.indir):
        dirlistproc.DirectoryListProcessor(argv + ["--help"])
    dlp.infile_suffix, dlp.opts.informat = set_suffix(dlp.opts.infile,
                                                      dlp.opts.informat)
    dlp.outfile_suffix, dlp.opts.outformat = set_suffix(
        dlp.opts.outfile, dlp.opts.outformat)
    if not dlp.opts.informat:
        print("Unable to determine input format", file=sys.stderr)
    if not dlp.opts.outformat:
        print("Unable to determine output format", file=sys.stderr)

    nfiles, nsuccess = dlp.run(
        convert_rdf, file_filter=file_filter
    ) if dlp.opts.informat and dlp.opts.outformat else (0, 0)
    print(f"Total={nfiles} Successful={nsuccess}")
    return 0 if nfiles == nsuccess else 1
Esempio n. 5
0
def main(argv: List[str], default_exit: bool = True) -> bool:
    """ Entry point for command line utility """
    dlp = dirlistproc.DirectoryListProcessor(
        argv,
        description="Convert FHIR JSON into RDF",
        infile_suffix=".json",
        outfile_suffix=".ttl",
        addargs=addargs,
        noexit=not default_exit)

    if not dlp.successful_parse:
        return False

    # Version
    if dlp.opts.version:
        print("FHIR to RDF Conversion Tool -- Version {}".format(__version__))

    # We either have to have an input file or an input directory
    if not dlp.opts.infile and not dlp.opts.indir:
        if not dlp.opts.version:
            dlp.parser.error(
                "Either an input file or an input directory must be supplied")
        return dlp.opts.version

    # Create the output directory if needed
    if dlp.opts.outdir and not os.path.exists(dlp.opts.outdir):
        os.makedirs(dlp.opts.outdir)

    # If we are going to a single output file or stdout, gather all the input
    dlp.opts.graph = Graph() if (not dlp.opts.outfile and not dlp.opts.outdir) or\
                                (dlp.opts.outfile and len(dlp.opts.outfile) == 1) else None
    dlp.opts.fhir_metavoc = load_fhir_ontology(dlp.opts)

    # If it looks like we're processing a URL as an input file, skip the suffix check
    if dlp.opts.infile and len(
            dlp.opts.infile
    ) == 1 and not dlp.opts.indir and "://" in dlp.opts.infile[0]:
        dlp.infile_suffix = ""
    dlp.outfile_suffix = '.' + suffix_for(dlp.opts.format)
    nfiles, nsuccess = dlp.run(proc=proc_file, file_filter_2=file_filter)
    if nfiles:
        if dlp.opts.graph:
            serialize_graph(dlp.opts.graph,
                            dlp.opts.outfile[0] if dlp.opts.outfile else None,
                            dlp.opts)
        return nsuccess > 0
    return False
 def dlp(args: List[str]) -> dirlistproc.DirectoryListProcessor:
     return dirlistproc.DirectoryListProcessor(args,
                                               "RDF Format Converter",
                                               '.ttl',
                                               'jsonld',
                                               addargs=addargs)
Esempio n. 7
0
 def testSpecSchema(self):
     dlp = dirlistproc.DirectoryListProcessor(["-id", testDir], "Validate ShExJ", ".json", None)
     nfiles, nsuccess = dlp.run(lambda *argv: proc_shexj(*argv), file_filter_2=file_filter)
     print("Files: {}, Errors: {}".format(nfiles, nfiles-nsuccess))