2
$\begingroup$

I have defined a function in a package to import some data files under a directory which is produced by a scanning device (LS55):

ls55ImportSPDirectory[dir_String?DirectoryQ,  OptionsPattern[]] := doStuff[dir]

Now I want to integrate the functionality into the import framework, so that the name ls55ImportSPDirectory won't be exposed. When I do

ImportExport`RegisterImport["LS55", ls55ImportSPDirectory]

and tried e.g. to

Import["C:\\Users\\rzhang\\work\\LS55\\", {"LS55", "Data"}]

I get this error:

(*Out: Import::nodirsup: Cannot import directory C:\Users\rzhang\work\LS55\ as LS55 *)

It seems the "Directory" type has already been registered which clashes with my customized type "LS55".

$\endgroup$
1
  • 1
    $\begingroup$ There is an Import.m under $InstallationDirectory\SystemFiles\Formats\Directory which i think registers directories to use the default importer, leaving no rooms for customization? $\endgroup$
    – asterix314
    Oct 6, 2014 at 9:14

1 Answer 1

1
$\begingroup$

Here is a way that works for me (V9) by altering the normal pipeline of the Import/Export Framework.

The usual disclaimer about using undocumented functionality applies.

(* Setup *)
ls55ImportSPDirectory[dir_String, OptionsPattern[]] :=
 Module[{fname = dir <> "a.png", ret},
         If[FileExistsQ@fname, ret = "File " <> fname <> " Exists", 
                               ret = "File " <> fname <> " doesn't Exist"];
         {"existence" :> ret}]

ImportExport`RegisterImport["LS55", 
                        {"existence" :> ls55ImportSPDirectory,            
                        ls55ImportSPDirectory}, {}, 
                        "FunctionChannels" -> {"Directories"}]

(* Usage *)
Import["C:\\test\\", {"LS55", "existence"}]

(* "File C:\\test\\a.png doesn't Exist" *)
$\endgroup$
2
  • $\begingroup$ This works for me. Thanks! Did you mean Import["C:\\test\\", {"LS55", "existence"}] in the usage? $\endgroup$
    – asterix314
    Oct 7, 2014 at 3:07
  • $\begingroup$ @asterix314 Yep, going to edit it. I was testing it by importing images and that is a piece of old code. Thanks. $\endgroup$ Oct 7, 2014 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.