~tualatrix/ubuntu-tweak/arb-packaging

« back to all changes in this revision

Viewing changes to src/ScriptWorker.py

  • Committer: TualatriX
  • Date: 2009-10-22 14:14:56 UTC
  • Revision ID: git-v1:455f01496d7149fb9832dabdf1bf0eef506a0101
WIP, make most things works

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
# coding: utf-8
3
 
 
4
 
# Ubuntu Tweak - PyGTK based desktop configure tool
5
 
#
6
 
# Copyright (C) 2007-2008 TualatriX <tualatrix@gmail.com>
7
 
#
8
 
# Ubuntu Tweak is free software; you can redistribute it and/or modify
9
 
# it under the terms of the GNU General Public License as published by
10
 
# the Free Software Foundation; either version 2 of the License, or
11
 
# (at your option) any later version.
12
 
#
13
 
# Ubuntu Tweak is distributed in the hope that it will be useful,
14
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 
# GNU General Public License for more details.
17
 
#
18
 
# You should have received a copy of the GNU General Public License
19
 
# along with Ubuntu Tweak; if not, write to the Free Software Foundation, Inc.,
20
 
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21
 
 
22
 
import os
23
 
import sys
24
 
import gtk
25
 
import gobject
26
 
import gnomevfs
27
 
import gettext
28
 
import shutil
29
 
import subprocess
30
 
 
31
 
from userdir import UserdirFile
32
 
from common.consts import *
33
 
from common.debug import run_traceback
34
 
from common.widgets import ErrorDialog
35
 
from common.utils import get_command_for_type
36
 
 
37
 
class FileChooserDialog(gtk.FileChooserDialog):
38
 
    """Show a dialog to select a folder, or to do more thing
39
 
    The default operation is select folder"""
40
 
    def __init__(self,
41
 
            title = None,
42
 
            parent = None,
43
 
            action = gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
44
 
            buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT)
45
 
            ):
46
 
        gtk.FileChooserDialog.__init__(self, title, parent, action, buttons)
47
 
 
48
 
class FileOperation:
49
 
    """Do the real operation"""
50
 
    @classmethod
51
 
    def do_copy(cls, source, dest):
52
 
        if os.path.isfile(source):
53
 
            if not os.path.exists(dest):
54
 
                shutil.copy(source, dest)
55
 
            else:
56
 
                ErrorDialog(_('The file "%s" already exists!') % dest).launch()
57
 
        elif os.path.isdir(source):
58
 
            if not os.path.exists(dest):
59
 
                shutil.copytree(source, dest)
60
 
            else:
61
 
                ErrorDialog(_('The folder "%s" already exists!') % dest).launch()
62
 
 
63
 
    @classmethod
64
 
    def do_move(cls, source, dest):
65
 
        if not os.path.exists(dest):
66
 
            shutil.move(source, dest)
67
 
        else:
68
 
            ErrorDialog(_('The target "%s" already exists!') % dest).launch()
69
 
 
70
 
    @classmethod
71
 
    def do_link(cls, source, dest):
72
 
        if not os.path.exists(dest):
73
 
            os.symlink(source, dest)
74
 
        else:
75
 
            ErrorDialog(_('The target "%s" already exists!') % dest).launch()
76
 
 
77
 
    @classmethod
78
 
    def copy(cls, source, dest):
79
 
        """Copy the file or folder with necessary notice"""
80
 
        dest = os.path.join(dest, os.path.basename(source))
81
 
        cls.do_copy(source, dest)
82
 
 
83
 
    @classmethod
84
 
    def copy_to_xdg(cls, source, xdg):
85
 
        if xdg == 'HOME':
86
 
            dest = os.path.join(os.path.expanduser('~'), os.path.basename(source))
87
 
        else:
88
 
            dest = os.path.join(UserdirFile()[xdg], os.path.basename(source))
89
 
        cls.do_copy(source, dest)
90
 
 
91
 
    @classmethod
92
 
    def move(cls, source, dest):
93
 
        """Move the file or folder with necessary notice"""
94
 
        dest = os.path.join(dest, os.path.basename(source))
95
 
        cls.do_move(source, dest)
96
 
 
97
 
    @classmethod
98
 
    def move_to_xdg(cls, source, xdg):
99
 
        if xdg == 'HOME':
100
 
            dest = os.path.join(os.path.expanduser('~'), os.path.basename(source))
101
 
        else:
102
 
            dest = os.path.join(UserdirFile()[xdg], os.path.basename(source))
103
 
        cls.do_move(source, dest)
104
 
 
105
 
    @classmethod
106
 
    def link(cls, source, dest):
107
 
        """Link the file or folder with necessary notice"""
108
 
        dest = os.path.join(dest, os.path.basename(source))
109
 
        cls.do_link(source, dest)
110
 
 
111
 
    @classmethod
112
 
    def link_to_xdg(cls, source, xdg):
113
 
        if xdg == 'HOME':
114
 
            dest = os.path.join(os.path.expanduser('~'), os.path.basename(source))
115
 
        else:
116
 
            dest = os.path.join(UserdirFile()[xdg], os.path.basename(source))
117
 
        cls.do_link(source, dest)
118
 
 
119
 
    @classmethod
120
 
    def open(cls, source):
121
 
        """Open the file with gedit"""
122
 
        exe = get_command_for_type('text/plain')
123
 
        if exe:
124
 
            if source[-1] == "root":
125
 
                cmd = ["gksu", "-m", _("Enter your password to perform the administrative tasks") , exe]
126
 
                cmd.extend(source[:-1])
127
 
            else:
128
 
                cmd = [exe]
129
 
                cmd.extend(source)
130
 
            subprocess.call(cmd)
131
 
        else:
132
 
            ErrorDialog(_("Coudn't find any text editor in your system!")).launch()
133
 
 
134
 
    @classmethod
135
 
    def browse(cls, source):
136
 
        """Browser the folder as root"""
137
 
        if source[-1] == "root":
138
 
            cmd = ["gksu", "-m", _("Enter your password to perform the administrative tasks") , "nautilus"]
139
 
            cmd.extend(source[:-1])
140
 
            subprocess.call(cmd)
141
 
        else:
142
 
            cmd = ["nautilus"]
143
 
            cmd.extend(source)
144
 
            subprocess.call(cmd)
145
 
 
146
 
    @classmethod
147
 
    def get_local_path(cls, uri):
148
 
        """Convert the URI to local path"""
149
 
        return gnomevfs.get_local_path_from_uri(uri)
150
 
 
151
 
class Worker:
152
 
    """The worker to do the real operation, with getattr to instrospect the operation"""
153
 
    def __init__(self, argv):
154
 
        try:
155
 
            command = argv[1]
156
 
            paras = argv[2:]
157
 
 
158
 
            if command in ('copy', 'move', 'link'):
159
 
                dialog = FileChooserDialog(_("Select a folder"))
160
 
                dialog.set_current_folder(os.path.expanduser('~'))
161
 
                if dialog.run() == gtk.RESPONSE_ACCEPT:
162
 
                    folder = dialog.get_filename()
163
 
 
164
 
                    dialog.destroy()
165
 
 
166
 
                    work = getattr(FileOperation, command)
167
 
                    for file in paras:
168
 
                        if file.startswith('file'):
169
 
                            file = FileOperation.get_local_path(file)
170
 
                        work(file, folder)
171
 
            elif command in ('copy_to_xdg', 'move_to_xdg', 'link_to_xdg'):
172
 
                xdg = paras[-1]
173
 
                paras = paras[:-1]
174
 
 
175
 
                work = getattr(FileOperation, command)
176
 
 
177
 
                for file in paras:
178
 
                    if file.startswith('file'):
179
 
                        file = FileOperation.get_local_path(file)
180
 
                    work(file, xdg)
181
 
            else:
182
 
                getattr(FileOperation, command)(paras)
183
 
        except:
184
 
            run_traceback('fatal')
185
 
 
186
 
if __name__ == "__main__":
187
 
#    ErrorDialog(`sys.argv`).launch()
188
 
    if len(sys.argv) <= 2:
189
 
        ErrorDialog(_("Please select a target (files or folders).")).launch()
190
 
    if len(sys.argv) > 2:
191
 
        Worker(sys.argv)