Here's a nice trick. We can simply figure out how FileFormat works and write a little wrapper, which frees us from overloading Import:
withFileFormat[formatName_ -> fileExtensions_, expr_] :=
Internal`InheritedBlock[{
FileFormatDump`$FILEFORMATS,
FileFormatDump`$FILEFORMATMATRIX
},
FileFormatDump`$FILEFORMATS =
Append[FileFormatDump`$FILEFORMATS, formatName];
FileFormatDump`$FILEFORMATMATRIX[formatName] =
{
formatName,
False, False, False, False, False,
"*." <> # & /@ fileExtensions,
{}, None, {}
};
expr
];
withFileFormat~SetAttributes~HoldRest
Then we check it on FileFormat:
withFileFormat["SHELL" -> {"sh"},
FileFormat["~/Desktop/Bugs.sh"]
]
"SHELL"
And if I had a definition for "SHELL" this should work:
withFileFormat["SHELL" -> {"sh"},
Import["~/Desktop/Bugs.sh"]
]
Import::infer: Cannot infer format of file Bugs.sh.
$Failed
On the plus side this differs from the default behavior in 11.3 which is to import as "Text".
Registering an alias for a new format will probably require more digging.
One noteworthy thing: if you drop the Internal`InheritedBlock this is literally just registering a new FileFormat.
Importas a nice supplement. This makes the problem hardly searchable, whereas the posed question here is both specific and general enough to be easily searchable and of interest to others. – LLlAMnYP Aug 21 '15 at 09:55FileFormat. Finally, would overloadingFileFormatbe a better solution than overloadingImport? – Szabolcs Jul 12 '18 at 10:51