use gtk.FileChooser to check mime type

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

use gtk.FileChooser to check mime type

Giuseppe Penone
Hi,
I'm using gtk.FileChooserDialog with a filter to display only "text/plain" files and this works fine:

filter = gtk.FileFilter()
filter.add_mime_type("text/plain")
chooser.add_filter(filter)

But if I need to check if a file is of this mime type, using python module mimetypes is not so good in detecting the proper mime type.

Is it possible somehow to use gtk.FileChooser engine to retrieve files with a mime type in a directory without going through the dialog?

I tried to do it without running the dialog and just doing:

...
chooser.add_filter(filter)
chooser.set_current_folder(foldername)
chooser.select_all()
print chooser.get_filenames()

but I always get an empty list, it seems that without chooser.run() it doesn't work, unfortunately I cannot instantiate just a gtk.FileChooser()

Cheers,
Giuseppe.


_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/
Reply | Threaded
Open this post in threaded view
|

Re: use gtk.FileChooser to check mime type

Niklas Koep
Hey, there are two ways to do this that I can think of off the top of my head. One is using gio.content_type_guess, the other (and likely slower albeit more accurate) way would be to use a gio.File instance like so:

    file_ = gio.File(filename)
    info = file_.query_info("standard::content-type") or equivalently info = file_.query_info(gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE)
    mimetype = info.get_content_type()

Regards.


On Wed, Sep 25, 2013 at 12:04 AM, Giuseppe Penone <[hidden email]> wrote:
Hi,
I'm using gtk.FileChooserDialog with a filter to display only "text/plain" files and this works fine:

filter = gtk.FileFilter()
filter.add_mime_type("text/plain")
chooser.add_filter(filter)

But if I need to check if a file is of this mime type, using python module mimetypes is not so good in detecting the proper mime type.

Is it possible somehow to use gtk.FileChooser engine to retrieve files with a mime type in a directory without going through the dialog?

I tried to do it without running the dialog and just doing:

...
chooser.add_filter(filter)
chooser.set_current_folder(foldername)
chooser.select_all()
print chooser.get_filenames()

but I always get an empty list, it seems that without chooser.run() it doesn't work, unfortunately I cannot instantiate just a gtk.FileChooser()

Cheers,
Giuseppe.


_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/


_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/
Reply | Threaded
Open this post in threaded view
|

Re: use gtk.FileChooser to check mime type

Giuseppe Penone
Hi Niklas, thank you very much for the tip.

What I finally impleemnted is:

            if os.path.isfile(full_element):
                gio_file = gio.File(full_element)
                gio_file_info = gio_file.query_info("*")
                if not cons.IS_WIN_OS:
                mime_types = str(gio_file_info.get_icon())
                if "text-" in mime_types:

                        self.add_file(full_element)
                else:
                mime_type = gio_file_info.get_content_type()
                if mime_type in [".txt", ".TXT"]:

                        self.add_file(full_element)


The best way to understand if it's a text file was this, using .get_content_type()
had too many mime types associated with text files but when going through the icon
I found a list of mime types that did fit my needs.

On windows OS instead the mime type just returns the file extension so unfortunately
the implementation is weak, for example a .ini/.cfg file would not be considered,
I would have to create a long list with all possible text extensions.

Cheers,
Giuseppe.



On Fri, Sep 27, 2013 at 10:25 AM, Niklas Koep <[hidden email]> wrote:
Hey, there are two ways to do this that I can think of off the top of my head. One is using gio.content_type_guess, the other (and likely slower albeit more accurate) way would be to use a gio.File instance like so:

    file_ = gio.File(filename)
    info = file_.query_info("standard::content-type") or equivalently info = file_.query_info(gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE)
    mimetype = info.get_content_type()

Regards.


On Wed, Sep 25, 2013 at 12:04 AM, Giuseppe Penone <[hidden email]> wrote:
Hi,
I'm using gtk.FileChooserDialog with a filter to display only "text/plain" files and this works fine:

filter = gtk.FileFilter()
filter.add_mime_type("text/plain")
chooser.add_filter(filter)

But if I need to check if a file is of this mime type, using python module mimetypes is not so good in detecting the proper mime type.

Is it possible somehow to use gtk.FileChooser engine to retrieve files with a mime type in a directory without going through the dialog?

I tried to do it without running the dialog and just doing:

...
chooser.add_filter(filter)
chooser.set_current_folder(foldername)
chooser.select_all()
print chooser.get_filenames()

but I always get an empty list, it seems that without chooser.run() it doesn't work, unfortunately I cannot instantiate just a gtk.FileChooser()

Cheers,
Giuseppe.


_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/



_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/