最近は溜まりまくっている Python 平書きコードの整理をしている。
といっても大半が PyGtk の覚書だったものを PyGI に作り直しであるが。
こんな状態だっったりする。
ソレより GNOME3 で gnomevfs が使えなくなったのをなんとかしないと。
Gio で「ファイルの種類」を得る方法が中々見つからず苦労した。
一旦 Content Type を得て変換するようだと解った。
とりあえず GLocalFile を作成。
query_info の引数に得たい情報文字列をコンマ区切りで突っ込む。
Gio.FILE_ATTRIBUTE_STANDARD_SIZE 等は只の #define された文字列。
GFileInfo
後はメソッドで取り出し、こんな感じかな。
ということで解ったところまで。
ファイルを何かドロップすると詳細が書き出されるウインドウ。
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 | #!/usr/bin/env python #-*- coding:utf-8 -*- import sys import gi try : gi.require_version( "Gtk" , "3.0" ) except : print "This Program is GTK+ 3.0 or later." sys.exit() from gi.repository import Gtk, Gdk, Gio res = """---------- Name: {0} Size: {1} byte Content Type: {2} Mime Type: {3} Description: {4} ---------- """ attr = ( Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, Gio.FILE_ATTRIBUTE_STANDARD_SIZE ) class Win(Gtk.Window): """ Information at Dropped File """ def __init__( self ): Gtk.Window.__init__( self ) # DnD dnd_list = Gtk.TargetEntry.new( "text/uri-list" , 0 , 0 ) self .drag_dest_set( Gtk.DestDefaults.MOTION | Gtk.DestDefaults.HIGHLIGHT | Gtk.DestDefaults.DROP, [dnd_list], Gdk.DragAction.MOVE ) # GtkLabel self .label = Gtk.Label( "Please drop your files" ) self .add( self .label) # Gtk.Window self .drag_dest_add_uri_targets() self .connect( "drag-data-received" , self .on_drag_data_received) self .set_title( "dnd_type" ) self .show_all() def on_drag_data_received( self , widget, drag_context, x, y, data, info, time): uris = data.get_uris() s = "" for uri in uris: # Create GLocalFile f = Gio.file_new_for_uri(uri) # Create GFileInfo info = f.query_info( "," .join(attr), Gio.FileQueryInfoFlags.NONE, None ) # Anyway Get Content Type ct = info.get_content_type() # splintf s + = res. format ( info.get_display_name(), info.get_size(), ct, Gio.content_type_get_mime_type(ct), Gio.content_type_get_description(ct) ) self .label.set_text(s) class App(Gtk.Application): def __init__( self ): Gtk.Application.__init__( self , application_id = "apps.test.akemi.homura" , flags = Gio.ApplicationFlags.FLAGS_NONE) self .connect( "activate" , self .on_activate) def on_activate( self , application, user_data = None ): w = Win() w.set_application( self ) if __name__ = = "__main__" : app = App() app.run( None ) |
うんコレで拡張子無しでもキチンと description が得られる。
Mime Type って Content Type と何が違うのかイマイチ解らない。
後は Last Write Time 等なんだがまだ解っていない。
os.stat から得られるのは知っているが Gio でやりたいので。
てゆーか昔のコードはほとんど役に立たなくなってしまったような。
もう少し纏めたら又 Tips ページを作ります。