Shouldn't get_content_type() in git_fs.py use mimetypes.guess_type()?

15 views
Skip to first unread message

Lele Gaifax

unread,
Mar 23, 2013, 5:34:02 AM3/23/13
to trac...@googlegroups.com
Hi all,

in trac-darcs I implemented the get_content_type() of its Node subclass
using the guess_type() of the mimetypes package, as an almost obvious
counterpart of Subversion's svn:mime-type property.

In this way, the Trac browser is able to properly render say a
README.rst[1] using docutils to transform it in HTML, instead of showing
its raw format.

Is there any drawback in the approach? Should git_fs.py do the same[2]?

Thank you,
ciao, lele.

[1] TBH, that needs a ``mimetypes.add_type('text/x-rst', '.rst')``
somewhere in the code...
[2] https://github.com/lelit/trac/commit/0a2c0baba5653a9d3647561fe9133201d28725cc
--
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
le...@metapensiero.it | -- Fortunato Depero, 1929.

Peter Stuge

unread,
Mar 23, 2013, 5:58:43 AM3/23/13
to trac...@googlegroups.com
Lele Gaifax wrote:
> Is there any drawback in the approach?

Maybe if the guess is wrong sometimes..


> Should git_fs.py do the same[2]?

It doesn't seem like an operation that belongs in version control
code in the first place..


//Peter

Christian Boos

unread,
Mar 23, 2013, 7:22:28 AM3/23/13
to trac...@googlegroups.com
On 3/23/2013 10:58 AM, Peter Stuge wrote:
> Lele Gaifax wrote:
>> Is there any drawback in the approach?
>
> Maybe if the guess is wrong sometimes..
>

Note that mimetypes.guess_type() is also called as part of
Mimeview.get_mimetype() [1] and that one is called when the
Node.content_type returns None.

>
>> Should git_fs.py do the same[2]?
>
> It doesn't seem like an operation that belongs in version control
> code in the first place..
>

Right, this Node.get_content_type() method is there to get a chance to
take advantage of special conventions about how to store the MIME type
in the version control system, like the svn:mime-type property in
Subversion. For git, .gitattributes could be used for that although I
don't think there are widespread conventions for this.

-- Christian

[1]
http://www.edgewall.org/docs/branches-1.0-stable/html/api/trac_mimeview.html#trac.mimeview.api.Mimeview.get_mimetype

Lele Gaifax

unread,
Mar 23, 2013, 1:10:33 PM3/23/13
to trac...@googlegroups.com
Christian Boos <christi...@free.fr> writes:

> Note that mimetypes.guess_type() is also called as part of
> Mimeview.get_mimetype() [1] and that one is called when the
> Node.content_type returns None.

Thank you Christian, I really missed that part of the machinery. In
particular, I was tricked by the default value for the option
"mimeviewer.mime_map_patterns":

text/plain:README|INSTALL|COPYING.*

that obviously prevented "README.rst" to get considered as
reStructuredText. As soon as I prepended "text/x-rst:.*\.rst$" to that
setting, everything seems working the way I like :-)

One minor point is that the fallback is triggered when Node.content_type
returns either None or '', so apparently the VC backend cannot prevent
the heuristic to happen for example on a directory with an unusual
"INSTALLABLE.STUFF" name...

ciao, lele.
Reply all
Reply to author
Forward
0 new messages