I've written an import converter for a custom file format, and it all seems to be working as expected, except for the fallback behavior with no arguments:
Import["C:\\Data\\samplefile.xxx"]
(*Import::infer: Cannot infer format of file samplefile.xxx. >> *)
(* Out[1]= $Failed *)
Does anyone know how I go about informing Import
of the file extension so that it can process a file using the default converter from the filename alone (i.e., without an argument to specify the type)?
Answer
New solution using RegisterFormat from Wolfram Function Repository:
ResourceFunction["RegisterFormat"]["BVH", <|"Extension" -> "bvh"|>]
Original Post:
Copy/paste from the excellent BVH package by @Sjoerd C. de Vries:
The code in this question (Registering/detecting an importer by file name extension) did not work. Neither did the answer. Wolfram support could not provide a more elegant solution so far. We use a trick here. In fact we don't have any options, but we need to add the option part to the argument template to be slightly more specific overall than an existing one that would also match. In this way we get to be evaluated before the other one, otherwise we'd be shadowed.
ImportExport`RegisterImport["BVH", BVHImporter`BVHImport];
BVHImporter`BVHImport[filename_String] := BVHImporter`BVHGet[Import[filename, "String"]];
Unprotect[Import];
Import[name_String, opts___?OptionQ] :=
Import[name, "BVH", opts] /; FileExtension[name] === "bvh";
Protect[Import];
Related tutorial from the documentation: Developing an Import Converter
Comments
Post a Comment