initial commit of v0.2.3
[dragbox.git] / Dragbox / utils.py
blob819949c941526d187fceae9b2c1844b8d331ce1f
1 # Copyright (C) 2006 Ulrik Sverdrup
3 # dragbox -- Commandline drag-and-drop tool for GNOME
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18 # USA
21 def get_icon_for_uri(uri, icon_size=48):
22 """
23 Returns a pixbuf representing the file at
24 the URI generally (mime-type based)
26 @param icon_size: a pixel size of the icon
27 @type icon_size: an integer object.
29 """
30 from gtk import icon_theme_get_default, ICON_LOOKUP_USE_BUILTIN
31 from gnomevfs import get_mime_type
32 from gnome.ui import ThumbnailFactory, icon_lookup
34 mtype = get_mime_type(uri)
35 icon_theme = icon_theme_get_default()
36 thumb_factory = ThumbnailFactory(16)
37 icon_name, num = icon_lookup(icon_theme, thumb_factory, file_uri=uri, custom_icon="")
38 icon = icon_theme.load_icon(icon_name, icon_size, ICON_LOOKUP_USE_BUILTIN)
39 return icon
41 def run_process(proc, args):
42 """
43 Launches a process
45 @param proc: The name of the executable
46 @type proc: a string object.
48 @param args: A list of arguments to the program
49 @type args: a list object.
50 """
51 from os import fork, execvp
52 args = [proc] + args # strange but true
53 pid = fork()
54 if not pid:
55 try:
56 execvp(proc, args)
57 except:
58 from sys import exit
59 print_error("Failed to run %s %s" % (proc, str(args)))
60 exit(1)
62 def quit_dragbox():
63 """
64 convenience method
65 terinates the gtk loop and exits
67 Does not return
68 """
69 from gtk import main_quit
70 main_quit()
72 # Make sure we output it all
73 from sys import exit, stdout, stderr
74 stdout.flush()
75 stderr.flush()
77 exit(0)
79 def print_error(string):
80 """
81 Convenience method
82 writes an error to stderr, with a special prefix
83 """
84 string = str(string)
85 from sys import stderr
86 stderr.write("Error: " + string + "\n")
88 def print_debug(string):
89 """
90 Convenience method
91 writes an debug message to stderr, with a special prefix
92 """
93 debug = False
94 if debug:
95 string = str(string)
96 from sys import stderr
97 stderr.write("Debug: " + string + "\n")
99 def guess_destdir():
100 """
101 This will find the $DESTDIR if that has been used
102 Useful for finding back to datafiles
104 Limitation: the binary must be installed "inside" the prefix
106 from os import path
107 from sys import argv
108 from globals import prefix
109 pre = path.normpath(path.expanduser(prefix.strip()))
110 if pre =="/":
111 pre = "/bin" # Trick that works for this special case
112 absp = path.normpath(path.abspath(argv[0]))
113 datadir_index = absp.find(pre)
114 if datadir_index == -1:
115 return None
116 datadir_guess = absp[:datadir_index]
117 print_debug("It seems like DESTDIR is " + datadir_guess)
118 return datadir_guess