File: ref-filetype.sgml

package info (click to toggle)
aap 1.072-1.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 4,976 kB
  • ctags: 2,160
  • sloc: python: 15,113; makefile: 62; sh: 13
file content (335 lines) | stat: -rw-r--r-- 10,480 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<!--  vim: set sw=2 sts=2 et ft=docbk:

  Part of the A-A-P recipe executive: Filetype detection

  Copyright (C) 2002-2003 Stichting NLnet Labs
  Permission to copy and use this file is specified in the file COPYING.
  If this file is missing you can find it here: http://www.a-a-p.org/COPYING

-->

<para>
The filetype detection module basically takes a file name and returns the type
of the file.
</para>

<para>
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.
</para>

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


<bridgehead>The Program</bridgehead>

<para>
Usage:
<literallayout>    <userinput>Filetype.py [-I ruledir] ...  [-f rulefile] ... filename </userinput> </literallayout>
</para>

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

<para>
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:
</para>
<literallayout>	/usr/local/share/aap/afd/
	~/.aap/afd/ </literallayout>

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


<bridgehead>Detection</bridgehead>

<para>
Detection is done in this order:
<orderedlist>
<listitem><para>
early Python items
</para></listitem>
<listitem><para>
check the file name extensions
</para></listitem>
<listitem><para>
match the regular expressions with the file name
</para></listitem>
<listitem><para>
check the first line in the file for a matching script name
</para></listitem>
<listitem><para>
later Python items
</para></listitem>
</orderedlist>
</para>

<para>
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).
</para>


<bridgehead>The Python Module</bridgehead>

<para>
The "ft_detect" function can be called to detect the type of file "fname":
</para>

<programlisting>
        from Filetype import ft_detect
	type = ft_detect(fname)
</programlisting>

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

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

<programlisting>
	type = ft_detect(fname, 1)
</programlisting>

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

<para>
For more info about the Filetype module, see the comments at the start of
Filetype.py.
</para>


<bridgehead>Format Of Filetype Detection Rules</bridgehead>

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

<para>
These filetype detection lines are supported:
</para>

<variablelist>

  <varlistentry id='filetype-suffix'><term><cmdsynopsis>
    <command>suffix</command>
      <arg choice='plain'><replaceable>suffix</replaceable></arg>
      <arg choice='plain'><replaceable>type</replaceable></arg>
    </cmdsynopsis></term>
    <listitem>
      <para>
        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.
        </para>
        <para>
        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".
        </para>
        <para>
        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.
        </para>
    </listitem>
  </varlistentry>

  <varlistentry id='filetype-regexp'><term><cmdsynopsis>
    <command>regexp</command>
      <arg choice='plain'><replaceable>regexp</replaceable></arg>
      <arg choice='plain'><replaceable>type</replaceable></arg>
      <arg>append</arg>
      <arg>tail</arg>
    </cmdsynopsis></term>
    <listitem>
      <para>
        Add detection of a filetype with a Python regular
        expression.  When {regexp} matches with the name of a
        file it gets filetype {type}.
        </para>
        <para>
        When "tail" is given, matching is done with the tail
        of the filename (without the path).
        </para>
        <para>
        When {type} is "remove" a previously defined filetype
        detection for {regexp} is removed.
        </para>
        <para>
        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.
        </para>
    </listitem>
  </varlistentry>

  <varlistentry id='filetype-script'><term><cmdsynopsis>
    <command>script</command>
      <arg choice='plain'><replaceable>script</replaceable></arg>
      <arg choice='plain'><replaceable>type</replaceable></arg>
      <arg>append</arg>
    </cmdsynopsis></term>
    <listitem>
      <para>
        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}.
        </para>
        <para>
        {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
        </para>
        <para>
        When {type} is "remove" a previously defined filetype
        detection for {script} is removed.
        </para>
        <para>
        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.
        </para>
    </listitem>
  </varlistentry>

  <varlistentry id='filetype-python'><term><cmdsynopsis>
    <command>python</command>
      <arg>after</arg>
      <arg>append</arg>
      <arg><replaceable>suffixlist</replaceable></arg>
    <command>&nbsp;</command>
      <arg choice='plain'><replaceable>python-code</replaceable></arg>
    </cmdsynopsis></term>
    <listitem>
      <para>
        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:
        </para>
        <programlisting>
        python bas,frm
</programlisting>
        <para>
        The code is executed with these variables set:
        <informaltable frame='none'>
          <tgroup cols='2'>
            <colspec colwidth="150"/>
            <tbody>
              <row>
                <entry>fname</entry>
                <entry>the name of the file</entry>
                </row>
                <row>
                <entry>fname_base</entry>
                <entry>the last part of the path</entry>
              </row>
              <row>
                <entry>ignore</entry>
                <entry>1 if extra suffixes are to be ignored, 0
                otherwise</entry>
            </row>
            </tbody>
          </tgroup>
          </informaltable>
        </para>

        <para>
        When the code detects the filetype it must assing it
        to the variable "type".
        </para>
        <para>
        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).
        </para>

        <para>
        When "after" isn't given, the detection is done before
        the suffix, regexp and script detection.  When
        "after" is given it's done last.
        </para>
        <para>
        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:
        </para>
        <para>
        <programlisting>
        python after
            if ignore and fname[-1] == '~':
                type = ft_detect(fname[:-1], ignore)
</programlisting>
        </para>
        <para>
        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.
        </para>
    </listitem>
  </varlistentry>

  <varlistentry id="filetype-declare"><term><cmdsynopsis>
    <command>declare</command>
    <arg choice="plain"><replaceable>type</replaceable></arg>
    </cmdsynopsis></term>
    <listitem>
    <para>
    <!-- rather inconsistent use of {} - it looks like an attribute -->
    Declare {type} to be a recognized filetype.
    This is needed for filetypes that are recognized through
    Python code <emphasis>only</emphasis>.
    All other filetypes (those that appear in suffix,
    regexp, and script rules) need not be separately declared.
    </para>
    <para>
    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.
    </para>
    </listitem>
  </varlistentry>
</variablelist>

<para>
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.
</para>