Documentation for the A-A-P filetype detection module.

The filetype detection module can be used as a standalone program and from
within a Python program.  It basically takes a file name and returns the type
of the file.

A filetype 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 "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":

	type = ft_detect(fname)

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

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 [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:
				fname		the name of the file
				fname_base	the last part of the path
				ignore		1 if extra suffixes are to be
						ignored, 0 otherwise
			When the code detects the file type 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.


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.

