Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > a6d417e36f6bb1154f4c003e6717e298 > files > 127

a-a-p-1.090-2mdv2009.0.noarch.rpm

<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Chapter 38. Filetype Detection</title><meta name="generator" content="DocBook XSL Stylesheets V1.71.1"><link rel="start" href="index.html" title="A-A-P Recipe Executive"><link rel="up" href="reference.html" title="Part III. Reference Manual"><link rel="prev" href="ref-attributes.html" title="Chapter 37. Attributes"><link rel="next" href="ref-modules.html" title="Chapter 39. Standard Modules"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table width="100%" id="navtable"><tbody><tr><td align="left" class="left" width="33%"><b><a href="http://www.a-a-p.org">A-A-P home page</a></b></td><td align="center" class="center" width="34%"><b><a href="index.html">A-A-P Recipe Executive</a></b></td><td align="right" class="right" width="33%"></td></tr><tr><td align="left" class="left"><a accesskey="p" href="ref-attributes.html">Prev</a></td><td align="center" class="center">Reference Manual</td><td align="right" class="right"><a accesskey="n" href="ref-modules.html">Next</a></td></tr></tbody></table><hr><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="ref-filetype"></a>Chapter 38. Filetype Detection</h2></div></div></div><p>
The filetype detection module basically takes a file name and returns the type
of the file.
</p><p>
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.
</p><p>
A filetype name is made of lowercase ASCII letters and digits: a-z and 0-9.
</p><h2><a name="id2684096"></a>The Program</h2><p>
Usage:
</p><div class="literallayout"><p>    <strong class="userinput"><code>Filetype.py [-I ruledir] ...  [-f rulefile] ... filename </code></strong> </p></div><p>
</p><p>
This will print the filetype of "filename" on stdout.  When the type could not
be detected the result is the string "None".
</p><p>
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:
</p><div class="literallayout"><p>	/usr/local/share/aap/afd/<br>
	~/.aap/afd/ </p></div><p>
The "-f rulefile" argument can be used to specify a file to load rules from.
</p><h2><a name="id2684954"></a>Detection</h2><p>
Detection is done in this order:
</p><div class="orderedlist"><ol type="1"><li><p>
early Python items
</p></li><li><p>
check the file name extensions
</p></li><li><p>
match the regular expressions with the file name
</p></li><li><p>
check the first line in the file for a matching script name
</p></li><li><p>
later Python items
</p></li></ol></div><p>
</p><p>
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).
</p><h2><a name="id2684997"></a>The Python Module</h2><p>
The "ft_detect" function can be called to detect the type of file "fname":
</p><pre class="programlisting">
        from Filetype import ft_detect
	type = ft_detect(fname)
</pre><p>
A string with the detected filetype is returned.  If the type is not
recognized, ft_detect() returns the None value.
</p><p>
To ignore extra suffixes like ".in", ".gz", add an extra non-zero argument:
</p><pre class="programlisting">
	type = ft_detect(fname, 1)
</pre><p>
To influence the messages given, add an extra "dict" argument.  The
"MESSAGE" item will be used, see its explanation in the main documenation.
</p><p>
For more info about the Filetype module, see the comments at the start of
Filetype.py.
</p><h2><a name="id2685027"></a>Format Of Filetype Detection Rules</h2><p>
Blank lines and lines starting with "#" (preceded by any amount of white
space) are ignored.
</p><p>
These filetype detection lines are supported:
</p><div class="variablelist"><dl><dt><a name="filetype-suffix"></a><span class="term"><div class="cmdsynopsis"><p><code class="command">suffix</code>   <em class="replaceable"><code>suffix</code></em>   <em class="replaceable"><code>type</code></em> </p></div></span></dt><dd><p>
        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.
        </p><p>
        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".
        </p><p>
        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.
        </p></dd><dt><a name="filetype-regexp"></a><span class="term"><div class="cmdsynopsis"><p><code class="command">regexp</code>   <em class="replaceable"><code>regexp</code></em>   <em class="replaceable"><code>type</code></em>  [append] [tail]</p></div></span></dt><dd><p>
        Add detection of a filetype with a Python regular
        expression.  When {regexp} matches with the name of a
        file it gets filetype {type}.
        </p><p>
        When "tail" is given, matching is done with the tail
        of the filename (without the path).
        </p><p>
        When {type} is "remove" a previously defined filetype
        detection for {regexp} is removed.
        </p><p>
        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.
        </p></dd><dt><a name="filetype-script"></a><span class="term"><div class="cmdsynopsis"><p><code class="command">script</code>   <em class="replaceable"><code>script</code></em>   <em class="replaceable"><code>type</code></em>  [append]</p></div></span></dt><dd><p>
        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}.
        </p><p>
        {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
        </p><p>
        When {type} is "remove" a previously defined filetype
        detection for {script} is removed.
        </p><p>
        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.
        </p></dd><dt><a name="filetype-python"></a><span class="term"><div class="cmdsynopsis"><p><code class="command">python</code>  [after] [append] [<em class="replaceable"><code>suffixlist</code></em>]<br><code class="command"> </code>   <em class="replaceable"><code>python-code</code></em> </p></div></span></dt><dd><p>
        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:
        </p><pre class="programlisting">
        python bas,frm
</pre><p>
        The code is executed with these variables set:
        </p><div class="informaltable"><table border="0"><colgroup><col width="150"><col></colgroup><tbody><tr><td>fname</td><td>the name of the file</td></tr><tr><td>fname_base</td><td>the last part of the path</td></tr><tr><td>ignore</td><td>1 if extra suffixes are to be ignored, 0
                otherwise</td></tr></tbody></table></div><p>
        </p><p>
        When the code detects the filetype it must assing it
        to the variable "type".
        </p><p>
        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).
        </p><p>
        When "after" isn't given, the detection is done before
        the suffix, regexp and script detection.  When
        "after" is given it's done last.
        </p><p>
        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:
        </p><p>
        </p><pre class="programlisting">
        python after
            if ignore and fname[-1] == '~':
                type = ft_detect(fname[:-1], ignore)
</pre><p>
        </p><p>
        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.
        </p></dd><dt><a name="filetype-declare"></a><span class="term"><div class="cmdsynopsis"><p><code class="command">declare</code>   <em class="replaceable"><code>type</code></em> </p></div></span></dt><dd><p>
    
    Declare {type} to be a recognized filetype.
    This is needed for filetypes that are recognized through
    Python code <span class="emphasis"><em>only</em></span>.
    All other filetypes (those that appear in suffix,
    regexp, and script rules) need not be separately declared.
    </p><p>
    When you use an unknown filetype in a recipe,
    <span class="application">Aap</span> prints a warning to alert you to the possibility of
    a misspelling.
    The declare rule  is needed
    because <span class="application">Aap</span> cannot tell what filetype the 
    Python code is capable of detecting,
    so the declare rule is used to tell <span class="application">Aap</span> 
    specifically that the filetype {type} is a known and recognized type.
    </p></dd></dl></div><p>
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.
</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ref-attributes.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="reference.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ref-modules.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 37. Attributes </td><td width="20%" align="center"><a accesskey="h" href="index.html">
		    Contents</a></td><td width="40%" align="right" valign="top"> Chapter 39. Standard Modules</td></tr></table></div></body></html>