~vcs-imports/kupfer/master-new

« back to all changes in this revision

Viewing changes to utils.py

  • Committer: Ulrik Sverdrup
  • Date: 2009-02-18 00:41:20 UTC
  • Revision ID: git-v1:4debb0ece5bd0731b612db62749b84ee97258e71
Move main application into kupfer/ package

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from os import path
2
 
 
3
 
 
4
 
def get_dirlist(folder, depth=0, include=None, exclude=None):
5
 
        """
6
 
        Return a list of absolute paths in folder
7
 
        include, exclude: a function returning a boolean
8
 
        def include(filename):
9
 
                return ShouldInclude
10
 
        """
11
 
        from os import walk
12
 
        paths = []
13
 
        def include_file(file):
14
 
                return (not include or include(file)) and (not exclude or not exclude(file))
15
 
                
16
 
        for dirname, dirnames, fnames in walk(folder):
17
 
                # skip deep directories
18
 
                head, dp = dirname, 0
19
 
                while not path.samefile(head, folder):
20
 
                        head, tail = path.split(head)
21
 
                        dp += 1
22
 
                if dp > depth:
23
 
                        del dirnames[:]
24
 
                        continue
25
 
                
26
 
                excl_dir = []
27
 
                for dir in dirnames:
28
 
                        if not include_file(dir):
29
 
                                excl_dir.append(dir)
30
 
                                continue
31
 
                        abspath = path.join(dirname, dir)
32
 
                        paths.append(abspath)
33
 
                
34
 
                for file in fnames:
35
 
                        if not include_file(file):
36
 
                                continue
37
 
                        abspath = path.join(dirname, file)
38
 
                        paths.append(abspath)
39
 
 
40
 
                for dir in reversed(excl_dir):
41
 
                        dirnames.remove(dir)
42
 
 
43
 
        return paths
44
 
 
45
 
def spawn_async(argv, in_dir=None):
46
 
        import gobject
47
 
        from os import chdir, getcwd
48
 
        if in_dir:
49
 
                oldwd = getcwd()
50
 
                chdir(in_dir)
51
 
        ret = gobject.spawn_async (argv, flags=gobject.SPAWN_SEARCH_PATH)
52
 
        if in_dir:
53
 
                chdir(oldwd)
54
 
 
55
 
def get_xdg_data_dirs():
56
 
        """
57
 
        Return a list of XDG data directories
58
 
 
59
 
        From the deskbar applet project
60
 
        """
61
 
        import os
62
 
 
63
 
        dirs = os.getenv("XDG_DATA_HOME")
64
 
        if dirs == None:
65
 
                dirs = os.path.expanduser("~/.local/share")
66
 
        
67
 
        sysdirs = os.getenv("XDG_DATA_DIRS")
68
 
        if sysdirs == None:
69
 
                sysdirs = "/usr/local/share:/usr/share"
70
 
        
71
 
        dirs = "%s:%s" % (dirs, sysdirs)
72
 
        return [dir for dir in dirs.split(":") if dir.strip() != "" and path.exists(dir)]
73
 
 
74
 
def new_desktop_item(exec_path, in_terminal=False):
75
 
        """
76
 
        Return a new desktop item with given exec_path
77
 
 
78
 
        It will set name from the path, and set the item to to
79
 
        Application. Some additional properties can be set too.
80
 
        """
81
 
        from gnomedesktop import item_new_from_string
82
 
        import gnomedesktop as gd
83
 
        name = path.basename(exec_path)
84
 
 
85
 
        # Do escaping
86
 
        # Escape \ " ` $ with another \
87
 
        # and enclose in ""
88
 
        bsl, quot = "\\", '"'
89
 
        esc_chars = (bsl, quot, '`', '$')
90
 
        escaped = [bsl*(c in esc_chars) + c for c in unicode(exec_path)]
91
 
        escaped.insert(0, quot)
92
 
        escaped.append(quot)
93
 
        exec_path_escaped = u"".join(escaped)
94
 
 
95
 
        item = gd.DesktopItem()
96
 
        item.set_entry_type(gd.TYPE_APPLICATION)
97
 
        item.set_string(gd.KEY_NAME, name)
98
 
        item.set_string(gd.KEY_EXEC, exec_path_escaped)
99
 
        item.set_boolean(gd.KEY_TERMINAL, in_terminal)
100
 
        return item