Chapter 38. Filetype Detection

The filetype detection module basically takes a file name and returns the type of the file.

The A-A-P filetype detection is a separate module. You can use the filetype detection in recipes, as a standalone program and from any Python program.

A filetype name is made of lowercase ASCII letters and digits: a-z and 0-9.

The Program

Usage:

    Filetype.py [-I ruledir] ...  [-f rulefile] ... filename  

This will print the filetype of "filename" on stdout. When the type could not be detected the result is the string "None".

The "-I ruledir" argument can be used to specify a directory to load *.afd (Aap Filetype Detection) files from. These add rules for filetype detection. These are the default directories which are always scanned:

/usr/local/share/aap/afd/
~/.aap/afd/ 

The "-f rulefile" argument can be used to specify a file to load rules from.

Detection

Detection is done in this order:

  1. early Python items

  2. check the file name extensions

  3. match the regular expressions with the file name

  4. check the first line in the file for a matching script name

  5. later Python items

When on a non-Posix system, the file name is forced to be lower case, so that case differences are ignored. The rules must use lower case names for this to work properly. Rules with an upper case letter will only match on a Posix system (this can be used for *.H to be recognized as cpp only on systems that make a difference between *.h and *.H).

The Python Module

The "ft_detect" function can be called to detect the type of file "fname":

        from Filetype import ft_detect
	type = ft_detect(fname)

A string with the detected filetype is returned. If the type is not recognized, ft_detect() returns the None value.

To ignore extra suffixes like ".in", ".gz", add an extra non-zero argument:

	type = ft_detect(fname, 1)

To influence the messages given, add an extra "dict" argument. The "MESSAGE" item will be used, see its explanation in the main documenation.

For more info about the Filetype module, see the comments at the start of Filetype.py.

Format Of Filetype Detection Rules

Blank lines and lines starting with "#" (preceded by any amount of white space) are ignored.

These filetype detection lines are supported:

suffix suffix type

Add detection of a filetype with a file name suffix. When a file name ends in .{suffix} it gets filetype {type}. {suffix} is taken literally, it is not a regular expression.

When {type} is "ignore" filetype detection is done on the file name with this suffix is removed. For example, "suffix gz ignore" causes "foo.c.gz" to be handled like "foo.c".

When {type} is "remove" a previously defined filetype detection for {suffix} is removed. This can be used to remove a suffix rule and add another kind of detection instead.

regexp regexp type [append] [tail]

Add detection of a filetype with a Python regular expression. When {regexp} matches with the name of a file it gets filetype {type}.

When "tail" is given, matching is done with the tail of the filename (without the path).

When {type} is "remove" a previously defined filetype detection for {regexp} is removed.

When "append" isn't given, the new detection is put before existing regexp detections, thus overruling them. When "append" is used it is put after the existing regexp detections.

script script type [append]

Add detection of a filetype by examining the first line of the file. When it starts with "#!" and {script} matches with the script program name it gets filetype {type}.

{script} is used as a Python regular expression. It must match at the start of the program name. Use ".*" to ignore a path. End with "$" to match at the end of the program name

When {type} is "remove" a previously defined filetype detection for {script} is removed.

When "append" isn't given, the new detection is put before existing script detections. When "append" is used the new detection is put after the existing script detections.

python [after] [append] [suffixlist]
  python-code

Add detection of a filetype by executing Python code. When the optional "suffixlist" is specified the Python code is only executed when the file name matches a suffix in this comma separated list of suffixes. This speeds up detection by only executing the Python code on relevant files. For example, to only check *.bas and *.frm files:

        python bas,frm

The code is executed with these variables set:

fnamethe name of the file
fname_basethe last part of the path
ignore1 if extra suffixes are to be ignored, 0 otherwise

When the code detects the filetype it must assing it to the variable "type".

An IOError in the code is ignored. Other errors are reported. Thus an open() call can be used without handling exceptions (when the file doesn't exist).

When "after" isn't given, the detection is done before the suffix, regexp and script detection. When "after" is given it's done last.

When "append" isn't given, the new detection is put before existing python detections. When "append" is used it is put after the existing python detections. The Python-code can use the ft_detect() function on a modified fname when needed. Example:

        python after
            if ignore and fname[-1] == '~':
                type = ft_detect(fname[:-1], ignore)

This is actually one of the default rules. When the file name ends in "~" detection is done on the name with this character removed. This finds the type of backup files.

declare type

Declare {type} to be a recognized filetype. This is needed for filetypes that are recognized through Python code only. All other filetypes (those that appear in suffix, regexp, and script rules) need not be separately declared.

When you use an unknown filetype in a recipe, Aap prints a warning to alert you to the possibility of a misspelling. The declare rule is needed because Aap cannot tell what filetype the Python code is capable of detecting, so the declare rule is used to tell Aap specifically that the filetype {type} is a known and recognized type.

In the above the first argument can be put in quotes to include white space. {type} can only consist of ASCII lowercase letters and digits.