kassoulet / nautilus-folder-actions

A small python-nautilus script to add buttons in Nautilus toolbar, with commands read from a file in the current folder.

This URL has Read+Write access

nautilus-folder-actions / nautilus-folder-actions.py
100755 116 lines (86 sloc) 2.839 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
#!/usr/bin/env python
# nautilus-folder-actions by Gautier Portet <kassoulet gmail.com>
 
__author__ = 'Gautier Portet <kassoulet gmail.com>'
__version__ = '0.1.0'
 
'''
This little script for Nautilus allows you to add actions in Nautilus.
For now, only toolbar buttons are supported.
'''
 
import os
import sys
import urllib
from threading import Thread
 
TERMINAL_KEY = '/desktop/gnome/applications/terminal/exec'
 
print 'Initializing nautilus-folder-actions ' + __version__
 
import gtk
import nautilus
import gconf
 
CONF_FILENAME = 'nautilus-folder-actions'
 
 
class Command(Thread):
    """
Launch a background command.
"""
 
    def __init__(self, command, folder):
        Thread.__init__(self)
        self.command = command
        self.folder = folder
    
    def run(self):
        os.chdir(self.folder)
        os.system(self.command)
 
 
class Action():
    """
One action from the current folder.
"""
    def __init__(self):
        self.name = ''
        self.icon = 'extension'
        self.command = ''
        self.comment = ''
 
    def run(self, folder):
        command = Command(self.command, folder)
        command.start()
 
 
def get_folder_actions(folder):
    """
Return the list of action for the given folder.
"""
    import ConfigParser
    config = ConfigParser.SafeConfigParser()
    filename = os.path.join(folder, CONF_FILENAME)
    print 'reading:', filename
    config.read(filename)
 
    actions = []
    for section in config.sections():
        action = Action()
        action.name = section
        items = dict(config.items(section))
 
        if 'exec' in items:
            action.command = items['exec']
        if 'name' in items:
            action.name = items['name']
        if 'icon' in items:
            action.icon = items['icon']
        if 'comment' in items:
            action.comment = items['comment']
 
        actions.append(action)
    return actions
 
 
class NautilusBuildExtension(nautilus.MenuProvider):
    def __init__(self):
        self.client = gconf.client_get_default()
 
    def activate_cb(self, menu, params):
        file, action = params
        print 'click:', file.get_uri()
        folder = urllib.unquote(file.get_uri()[7:])
        action.run(folder)
 
    def get_toolbar_items(self, window, file):
        print file.get_uri()
 
        folder = urllib.unquote(file.get_uri()[7:])
        actions = get_folder_actions(folder)
 
        items = []
        for action in actions:
            item = nautilus.MenuItem('NautilusPython::folder-actions::%s' % action.name,
                                     action.name,
                                     action.comment,
                                     action.icon)
            item.connect('activate', self.activate_cb, [file, action])
            items.append(item)
 
        return items