Mercurial > libervia-backend
annotate frontends/jp/jp @ 70:8f2ed279784b
i18n
- gettext support added in frontends
- first draft of frontends french translation
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 05 Mar 2010 20:33:10 +1100 |
parents | a5b5fb5fc9fd |
children | cb904fa7de3c |
rev | line source |
---|---|
0 | 1 #! /usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 jp: a SAT command line tool | |
57 | 6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) |
0 | 7 |
8 This program 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 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program 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 this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 #consts | |
23 name = "jp" | |
24 version = "0.0.1" | |
57 | 25 about = name+" v"+version+""" (c) Jérôme Poisson (aka Goffi) 2009, 2010 |
0 | 26 |
27 --- | |
57 | 28 """+name+""" Copyright (C) 2009, 2010 Jérôme Poisson (aka Goffi) |
0 | 29 This program comes with ABSOLUTELY NO WARRANTY; |
30 This is free software, and you are welcome to redistribute it | |
31 under certain conditions. | |
32 --- | |
33 | |
34 This software is a command line tool for jabber | |
35 Get the latest version at http://www.goffi.org | |
36 """ | |
37 | |
38 global pbar_available | |
39 pbar_available = True #checked before using ProgressBar | |
40 | |
41 ### logging ### | |
42 import logging | |
43 from logging import debug, info, error, warning | |
44 logging.basicConfig(level=logging.DEBUG, | |
45 format='%(message)s') | |
46 ### | |
47 | |
70 | 48 import gettext |
49 gettext.install('jp', "i18n", unicode=True) | |
50 | |
0 | 51 import sys |
52 import os | |
53 from os.path import abspath, basename, dirname | |
54 from optparse import OptionParser | |
55 import pdb | |
56 from tools.jid import JID | |
57 import gobject | |
58 from sat_bridge_frontend.DBus import DBusBridgeFrontend | |
59 import tarfile | |
60 try: | |
61 from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed | |
62 except ImportError, e: | |
70 | 63 info (_('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar')) |
64 info (_('Progress bar deactivated\n--\n')) | |
0 | 65 pbar_available=False |
66 | |
67 | |
68 | |
69 | |
70 class JP(): | |
71 def __init__(self): | |
72 self.bridge=DBusBridgeFrontend() | |
73 self.transfert_id = None | |
74 | |
75 def check_options(self): | |
76 """Check command line options""" | |
70 | 77 usage=_(""" |
0 | 78 %prog [options] [FILE1 FILE2 ...] JID |
79 %prog -w [options] [JID1 JID2 ...] | |
80 | |
81 %prog --help for options list | |
70 | 82 """) |
0 | 83 parser = OptionParser(usage=usage,version=about) |
84 | |
85 parser.add_option("-b", "--bz2", action="store_true", default=False, | |
70 | 86 help=_("Make a bzip2 tarball")) |
0 | 87 parser.add_option("-w", "--wait-file", action="store_true", default=False, |
70 | 88 help=_("Wait for a file to be sent by a contact")) |
0 | 89 parser.add_option("-m", "--multiple", action="store_true", default=False, |
70 | 90 help=_("Accept multiple files (you'll have to stop manually)")) |
0 | 91 parser.add_option("-f", "--force", action="store_true", default=False, |
70 | 92 help=_("Force overwritting of existing files")) |
0 | 93 parser.add_option("-p", "--progress", action="store_true", default=False, |
70 | 94 help=_("Show progress bar")) |
0 | 95 parser.add_option("-s", "--separate", action="store_true", default=False, |
70 | 96 help=_("Separate xmpp messages: send one message per line instead of one message alone.")) |
0 | 97 parser.add_option("-n", "--new-line", action="store_true", default=False, |
70 | 98 help=_("Add a new line at the beginning of the input (usefull for ascii art ;))")) |
0 | 99 |
100 (self.options, args) = parser.parse_args() | |
101 | |
102 if len(args) < 1 and not self.options.wait_file: | |
70 | 103 parser.error(_("You must specify the destination JID (Jabber ID)").encode('utf-8')) |
0 | 104 |
105 if self.options.wait_file: | |
106 #several jid | |
107 self.dest_jids = args | |
108 else: | |
109 #one dest_jid, other args are files | |
110 self.dest_jid = JID(args[-1]) | |
111 if not self.dest_jid.is_valid: | |
70 | 112 error (_("%s is not a valid JID !"), self.dest_jid) |
0 | 113 exit(1) |
114 self.files = args[:-1] | |
115 | |
116 if not pbar_available and self.options.progress: | |
117 self.options.progress = False | |
70 | 118 error (_("Option progress is not available, deactivated.")) |
0 | 119 |
120 if self.options.progress or self.options.wait_file: | |
121 self.start_loop = True #We have to use loop for these options | |
122 else: | |
123 self.start_loop = False | |
124 | |
125 | |
126 return args | |
127 | |
128 def check_jabber_status(self): | |
129 """Check that jabber status is allright""" | |
130 if not self.bridge.isConnected(): | |
70 | 131 error(_("SAT is not conneted, please connect before using jp")) |
0 | 132 exit(1) |
133 | |
134 | |
135 def send_stdin(self): | |
136 """Send incomming data on stdin to jabber contact""" | |
137 header = "\n" if self.options.new_line else "" | |
138 | |
139 if self.options.separate: #we send stdin in several messages | |
140 if header: | |
141 self.bridge.sendMessage(self.dest_jid, header) | |
142 while (True): | |
143 line = sys.stdin.readline() | |
144 if not line: | |
145 break | |
146 self.bridge.sendMessage(self.dest_jid, line.replace("\n","")) | |
147 else: | |
148 self.bridge.sendMessage(self.dest_jid, header + "".join(sys.stdin.readlines())) | |
149 | |
150 def send_files(self): | |
151 """Send files to jabber contact""" | |
152 | |
153 for file in self.files: | |
154 if not os.path.exists(file): | |
70 | 155 error (_("File [%s] doesn't exist !") % file) |
0 | 156 exit(1) |
157 if not self.options.bz2 and os.path.isdir(file): | |
70 | 158 error (_("[%s] is a dir ! Please send files inside or use compression") % file) |
0 | 159 exit(1) |
160 | |
161 if self.options.bz2: | |
162 tmpfile = (basename(self.files[0]) or basename(dirname(self.files[0])) ) + '.tar.bz2' #FIXME: tmp, need an algorithm to find a good name/path | |
163 if os.path.exists(tmpfile): | |
70 | 164 error (_("tmp file (%s) already exists ! Please remove it"), tmpfile) |
0 | 165 exit(1) |
70 | 166 warning(_("bz2 is an experimental option at an early dev stage, use with caution")) |
0 | 167 #FIXME: check free space, writting perm, tmp dir, filename (watch for OS used) |
70 | 168 info(_("Starting compression, please wait...")) |
0 | 169 sys.stdout.flush() |
170 bz2=tarfile.open(tmpfile, "w:bz2") | |
171 for file in self.files: | |
70 | 172 info(_("Adding %s"), file) |
0 | 173 bz2.add(file) |
174 bz2.close() | |
70 | 175 info(_("OK !")) |
0 | 176 path = abspath(tmpfile) |
177 self.transfert_id = self.bridge.sendFile(self.dest_jid, path) | |
178 else: | |
179 for file in self.files: | |
180 path = abspath(file) | |
181 self.transfert_id = self.bridge.sendFile(self.dest_jid, path) #FIXME: show progress only for last transfert_id | |
182 | |
183 #TODO: manage ProgressBar | |
184 | |
185 def askConfirmation(self, type, id, data): | |
186 """CB used for file transfert, accept files depending on parameters""" | |
187 answer_data={} | |
188 if type == "FILE_TRANSFERT": | |
189 if self.dest_jids and not data['from'] in self.dest_jids: | |
190 return #file is not sent by a filtered jid | |
191 | |
192 answer_data["dest_path"] = os.getcwd()+'/'+data['filename'] | |
193 | |
194 if self.options.force or not os.path.exists(answer_data["dest_path"]): | |
195 self.bridge.confirmationAnswer(id, True, answer_data) | |
70 | 196 info(_("Accepted file [%(filename)s] from %(sender)s") % {'filename':data['filename'], 'sender':data['from']}) |
0 | 197 self.transfert_id = id |
198 else: | |
199 self.bridge.confirmationAnswer(id, False, answer_data) | |
70 | 200 warning(_("Refused file [%(filename)s] from %(sender)s: a file with the same name already exist") % {'filename':data['filename'], 'sender':data['from']}) |
0 | 201 |
202 | |
203 if not self.options.multiple and not self.options.progress: | |
204 #we just accept one file | |
205 self.loop.quit() | |
206 | |
22
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
207 def actionResult(self, type, id, data): |
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
208 #FIXME |
70 | 209 info (_("FIXME: actionResult not implemented")) |
0 | 210 |
211 def wait_file(self): | |
212 """Wait for a file and write it on local dir""" | |
213 self.bridge.register("askConfirmation", self.askConfirmation, "request") | |
214 | |
215 def progressCB(self): | |
216 if self.transfert_id: | |
217 data = self.bridge.getProgress(self.transfert_id) | |
218 if data: | |
219 if not data['position']: | |
220 data['position'] = '0' | |
221 if not self.pbar: | |
222 #first answer, we must construct the bar | |
70 | 223 self.pbar = ProgressBar(int(data['size']),[_("Progress: "),Percentage()," ",Bar()," ",FileTransferSpeed()," ",ETA()]) |
0 | 224 self.pbar.start() |
225 | |
226 self.pbar.update(int(data['position'])) | |
227 elif self.pbar: | |
228 self.pbar.finish() | |
229 if not self.options.multiple: | |
230 self.loop.quit() | |
231 return False | |
232 | |
233 return True | |
234 | |
235 def go(self): | |
236 self.check_options() | |
237 self.check_jabber_status() | |
238 if self.options.wait_file: | |
239 self.wait_file() | |
240 else: | |
241 if not self.files: #we send message only if there are no files to send | |
242 self.send_stdin() | |
243 else: | |
244 self.send_files() | |
245 | |
246 if self.start_loop: | |
247 self.loop = gobject.MainLoop() | |
248 if self.options.progress: | |
249 self.pbar = None | |
250 gobject.timeout_add(10, self.progressCB) | |
251 try: | |
252 self.loop.run() | |
253 except KeyboardInterrupt: | |
70 | 254 info(_("User interruption: good bye")) |
0 | 255 |
256 | |
257 if __name__ == "__main__": | |
258 jp = JP() | |
259 jp.go() |