[go: up one dir, main page]

Menu

[r47]: / Core.py  Maximize  Restore  History

Download this file

178 lines (163 with data), 6.2 kB

  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
#!/usr/bin/env python
"""
Copyright (C) 2009 Programble
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import pygtk
pygtk.require('2.0')
import gtk
import gobject
import os
import mimetypes
import thread
import gobject
DEBUG = False
folder_icon = gtk.Button("").render_icon(gtk.STOCK_DIRECTORY, gtk.ICON_SIZE_DIALOG)
def listdir(path, liststore, show_hidden=False):
"""Add files and directories in path to liststore"""
if (DEBUG): print "listdir", path, liststore
dirs = []
files = []
# List files/dirs
for fd in os.listdir(path):
# Show/Hide hidden files
if (show_hidden == False and fd[0] == "."):
continue
# Get absolute path
abspath = os.path.join(path, fd)
# Sort files/dirs
if (os.path.isfile(abspath)):
files.append((fd, abspath))
elif (os.path.isdir(abspath)):
dirs.append((fd, abspath))
# Sort alphabetically and case-insensitive
dirs.sort(key=lambda x: (x[0].lower(),) + x[1:])
files.sort(key=lambda x: (x[0].lower(),) + x[1:])
# Add dirs to liststore
for d in dirs:
liststore.append([d[0], folder_icon, d[1]])
# Add files to liststore with icons
for f in files:
ico = get_icon(f[1])
liststore.append([f[0], ico, f[1]])
if (DEBUG): print "(listdir)files", len(files), "dirs", len(dirs)
return False
icon_cache = {}
mime_cache = {}
def get_icon(path):
"""Return a pixbuf containing an icon for the file path"""
global icon_cache, mime_cache
if (DEBUG): print "get_icon", path
# Limit size of icon cache
if (len(icon_cache) > 1000):
icon_cache.popitem()
ret = None
# Load icon from icon cache if possible
if (icon_cache.has_key(path)):
if (DEBUG): print "-icon_cache"
return icon_cache[path]
# First try to guess mime type from extension
mimetypes.init()
mime = mimetypes.guess_type(path, False)[0]
# If python cannot find mime, use command line utility 'file'
if (mime == None):
import commands
mime = commands.getoutput('file --mime-type -L "' + path + '"')
mime = mime[mime.find(":")+2:]
generic = ""
genericx = ""
genericf = ""
# Load current icon theme
icosd = gtk.icon_theme_get_default()
# Load default gnome icon theme
icosg = gtk.IconTheme()
icosg.set_custom_theme("gnome")
# If no mime found, return default file icon
if (mime == None):
ret = icosd.load_icon("gtk-file", 48, 0)
else:
# Find all variations of mime type (generic, x, and short)
mime = mime.replace("/", "-").replace("-httpd", "").replace("program", "executable").replace("msdos", "ms-dos")
generic = mime.split("-")
generic = generic[:-1]
genericx = generic[0:len(generic)]
genericx.append("x")
generic.append("generic")
genericx.append("generic")
genericf = generic[0]
generic = "-".join(tuple(generic))
genericx = "-".join(tuple(genericx))
if (DEBUG): print "-mime", mime
# Load icon from mime cache if possible
if (mime_cache.has_key(mime)):
if (DEBUG): print "-mime_cache"
if (DEBUG): print "-icon_cache", len(icon_cache), "mime_cache", len(mime_cache)
# Also add file to icon cache
icon_cache[path] = mime_cache[mime]
return mime_cache[mime]
# For each mime variation, try in current theme, then in default theme
if (icosd.has_icon(mime)):
ret = icosd.load_icon(mime, 48, 0)
elif (icosg.has_icon(mime)):
ret = icosg.load_icon(mime, 48, 0)
elif (icosd.has_icon("gnome-mime-" + mime)):
ret = icosd.load_icon("gnome-mime-" + mime, 48, 0)
elif (icosg.has_icon("gnome-mime-" + mime)):
ret = icosg.load_icon("gnome-mime-" + mime, 48, 0)
elif (icosd.has_icon(genericx)):
ret = icosd.load_icon(genericx, 48, 0)
elif (icosg.has_icon(genericx)):
ret = icosg.load_icon(genericx, 48, 0)
elif (icosd.has_icon(generic)):
ret = icosd.load_icon(generic, 48, 0)
elif (icosg.has_icon(generic)):
ret = icosg.load_icon(generic, 48, 0)
elif (icosd.has_icon(genericf)):
ret = icosd.load_icon(genericf, 48, 0)
elif (icosg.has_icon(genericf)):
ret = icosg.load_icon(genericf, 48, 0)
else:
# If no icon found, return default file icon
if (DEBUG): print "-no_icon"
ret = icosd.load_icon("gtk-file", 48, 0)
# Add file to icon cache
icon_cache[path] = ret
# Add mime to mime cache
mime_cache[mime] = ret
if (DEBUG): print "-icon", ret
if (DEBUG): print "-icon_cache", len(icon_cache), "mime_cache", len(mime_cache)
return ret
def get_home():
"""Return the home directory of the current user"""
return os.path.expanduser("~")
def isdir(path):
"""Returns true if path is a directory"""
return os.path.isdir(path)
def isfile(path):
"""Returns true if path is a file"""
return os.path.isfile(path)
def get_parentdir(path):
"""Get the parent directory of path"""
return os.path.split(path)[0]
def execute(path):
"""Use the command line 'exo-open' to open/execute a file"""
if (DEBUG): print "execute", path
os.system('exo-open "' + path + '"')
def get_home_dirs():
"""List all directories in the current users home"""
home = get_home()
dirs = []
for fd in os.listdir(home):
if (isdir(os.path.join(home, fd)) and fd[0] != "."):
dirs.append((fd, os.path.join(home, fd)))
dirs.sort(key=lambda x: (x[0].lower(),) + x[1:])
return dirs