comparison 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
comparison
equal deleted inserted replaced
69:86f1f7f6d332 70:8f2ed279784b
42 import logging 42 import logging
43 from logging import debug, info, error, warning 43 from logging import debug, info, error, warning
44 logging.basicConfig(level=logging.DEBUG, 44 logging.basicConfig(level=logging.DEBUG,
45 format='%(message)s') 45 format='%(message)s')
46 ### 46 ###
47
48 import gettext
49 gettext.install('jp', "i18n", unicode=True)
47 50
48 import sys 51 import sys
49 import os 52 import os
50 from os.path import abspath, basename, dirname 53 from os.path import abspath, basename, dirname
51 from optparse import OptionParser 54 from optparse import OptionParser
55 from sat_bridge_frontend.DBus import DBusBridgeFrontend 58 from sat_bridge_frontend.DBus import DBusBridgeFrontend
56 import tarfile 59 import tarfile
57 try: 60 try:
58 from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed 61 from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed
59 except ImportError, e: 62 except ImportError, e:
60 info ('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar') 63 info (_('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar'))
61 info ('Progress bar deactivated\n--\n') 64 info (_('Progress bar deactivated\n--\n'))
62 pbar_available=False 65 pbar_available=False
63 66
64 67
65 68
66 69
69 self.bridge=DBusBridgeFrontend() 72 self.bridge=DBusBridgeFrontend()
70 self.transfert_id = None 73 self.transfert_id = None
71 74
72 def check_options(self): 75 def check_options(self):
73 """Check command line options""" 76 """Check command line options"""
74 usage=""" 77 usage=_("""
75 %prog [options] [FILE1 FILE2 ...] JID 78 %prog [options] [FILE1 FILE2 ...] JID
76 %prog -w [options] [JID1 JID2 ...] 79 %prog -w [options] [JID1 JID2 ...]
77 80
78 %prog --help for options list 81 %prog --help for options list
79 """ 82 """)
80 parser = OptionParser(usage=usage,version=about) 83 parser = OptionParser(usage=usage,version=about)
81 84
82 parser.add_option("-b", "--bz2", action="store_true", default=False, 85 parser.add_option("-b", "--bz2", action="store_true", default=False,
83 help="Make a bzip2 tarball") 86 help=_("Make a bzip2 tarball"))
84 parser.add_option("-w", "--wait-file", action="store_true", default=False, 87 parser.add_option("-w", "--wait-file", action="store_true", default=False,
85 help="Wait for a file to be sent by a contact") 88 help=_("Wait for a file to be sent by a contact"))
86 parser.add_option("-m", "--multiple", action="store_true", default=False, 89 parser.add_option("-m", "--multiple", action="store_true", default=False,
87 help="Accept multiple files (you'll have to stop manually)") 90 help=_("Accept multiple files (you'll have to stop manually)"))
88 parser.add_option("-f", "--force", action="store_true", default=False, 91 parser.add_option("-f", "--force", action="store_true", default=False,
89 help="Force overwritting of existing files") 92 help=_("Force overwritting of existing files"))
90 parser.add_option("-p", "--progress", action="store_true", default=False, 93 parser.add_option("-p", "--progress", action="store_true", default=False,
91 help="Show progress bar") 94 help=_("Show progress bar"))
92 parser.add_option("-s", "--separate", action="store_true", default=False, 95 parser.add_option("-s", "--separate", action="store_true", default=False,
93 help="Separate xmpp messages: send one message per line instead of one message alone.") 96 help=_("Separate xmpp messages: send one message per line instead of one message alone."))
94 parser.add_option("-n", "--new-line", action="store_true", default=False, 97 parser.add_option("-n", "--new-line", action="store_true", default=False,
95 help="Add a new line at the beginning of the input (usefull for ascii art ;))") 98 help=_("Add a new line at the beginning of the input (usefull for ascii art ;))"))
96 99
97 (self.options, args) = parser.parse_args() 100 (self.options, args) = parser.parse_args()
98 101
99 if len(args) < 1 and not self.options.wait_file: 102 if len(args) < 1 and not self.options.wait_file:
100 parser.error("You must specify the destination JID (Jabber ID)") 103 parser.error(_("You must specify the destination JID (Jabber ID)").encode('utf-8'))
101 104
102 if self.options.wait_file: 105 if self.options.wait_file:
103 #several jid 106 #several jid
104 self.dest_jids = args 107 self.dest_jids = args
105 else: 108 else:
106 #one dest_jid, other args are files 109 #one dest_jid, other args are files
107 self.dest_jid = JID(args[-1]) 110 self.dest_jid = JID(args[-1])
108 if not self.dest_jid.is_valid: 111 if not self.dest_jid.is_valid:
109 error ("%s is not a valid JID !", self.dest_jid) 112 error (_("%s is not a valid JID !"), self.dest_jid)
110 exit(1) 113 exit(1)
111 self.files = args[:-1] 114 self.files = args[:-1]
112 115
113 if not pbar_available and self.options.progress: 116 if not pbar_available and self.options.progress:
114 self.options.progress = False 117 self.options.progress = False
115 error ("Option progress is not available, deactivated.") 118 error (_("Option progress is not available, deactivated."))
116 119
117 if self.options.progress or self.options.wait_file: 120 if self.options.progress or self.options.wait_file:
118 self.start_loop = True #We have to use loop for these options 121 self.start_loop = True #We have to use loop for these options
119 else: 122 else:
120 self.start_loop = False 123 self.start_loop = False
123 return args 126 return args
124 127
125 def check_jabber_status(self): 128 def check_jabber_status(self):
126 """Check that jabber status is allright""" 129 """Check that jabber status is allright"""
127 if not self.bridge.isConnected(): 130 if not self.bridge.isConnected():
128 error("SAT is not conneted, please connect before using jp") 131 error(_("SAT is not conneted, please connect before using jp"))
129 exit(1) 132 exit(1)
130 133
131 134
132 def send_stdin(self): 135 def send_stdin(self):
133 """Send incomming data on stdin to jabber contact""" 136 """Send incomming data on stdin to jabber contact"""
147 def send_files(self): 150 def send_files(self):
148 """Send files to jabber contact""" 151 """Send files to jabber contact"""
149 152
150 for file in self.files: 153 for file in self.files:
151 if not os.path.exists(file): 154 if not os.path.exists(file):
152 error ("File [%s] doesn't exist !" % file) 155 error (_("File [%s] doesn't exist !") % file)
153 exit(1) 156 exit(1)
154 if not self.options.bz2 and os.path.isdir(file): 157 if not self.options.bz2 and os.path.isdir(file):
155 error ("[%s] is a dir ! Please send files inside or use compression" % file) 158 error (_("[%s] is a dir ! Please send files inside or use compression") % file)
156 exit(1) 159 exit(1)
157 160
158 if self.options.bz2: 161 if self.options.bz2:
159 tmpfile = (basename(self.files[0]) or basename(dirname(self.files[0])) ) + '.tar.bz2' #FIXME: tmp, need an algorithm to find a good name/path 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
160 if os.path.exists(tmpfile): 163 if os.path.exists(tmpfile):
161 error ("tmp file (%s) already exists ! Please remove it", tmpfile) 164 error (_("tmp file (%s) already exists ! Please remove it"), tmpfile)
162 exit(1) 165 exit(1)
163 warning("bz2 is an experimental option at an early dev stage, use with caution") 166 warning(_("bz2 is an experimental option at an early dev stage, use with caution"))
164 #FIXME: check free space, writting perm, tmp dir, filename (watch for OS used) 167 #FIXME: check free space, writting perm, tmp dir, filename (watch for OS used)
165 info("Starting compression, please wait...") 168 info(_("Starting compression, please wait..."))
166 sys.stdout.flush() 169 sys.stdout.flush()
167 bz2=tarfile.open(tmpfile, "w:bz2") 170 bz2=tarfile.open(tmpfile, "w:bz2")
168 for file in self.files: 171 for file in self.files:
169 info("Adding %s", file) 172 info(_("Adding %s"), file)
170 bz2.add(file) 173 bz2.add(file)
171 bz2.close() 174 bz2.close()
172 info("OK !") 175 info(_("OK !"))
173 path = abspath(tmpfile) 176 path = abspath(tmpfile)
174 self.transfert_id = self.bridge.sendFile(self.dest_jid, path) 177 self.transfert_id = self.bridge.sendFile(self.dest_jid, path)
175 else: 178 else:
176 for file in self.files: 179 for file in self.files:
177 path = abspath(file) 180 path = abspath(file)
188 191
189 answer_data["dest_path"] = os.getcwd()+'/'+data['filename'] 192 answer_data["dest_path"] = os.getcwd()+'/'+data['filename']
190 193
191 if self.options.force or not os.path.exists(answer_data["dest_path"]): 194 if self.options.force or not os.path.exists(answer_data["dest_path"]):
192 self.bridge.confirmationAnswer(id, True, answer_data) 195 self.bridge.confirmationAnswer(id, True, answer_data)
193 info("Accepted file [%s] from %s", data['filename'], data['from']) 196 info(_("Accepted file [%(filename)s] from %(sender)s") % {'filename':data['filename'], 'sender':data['from']})
194 self.transfert_id = id 197 self.transfert_id = id
195 else: 198 else:
196 self.bridge.confirmationAnswer(id, False, answer_data) 199 self.bridge.confirmationAnswer(id, False, answer_data)
197 warning("Refused file [%s] from %s: a file with the same name already exist", data['filename'], data['from']) 200 warning(_("Refused file [%(filename)s] from %(sender)s: a file with the same name already exist") % {'filename':data['filename'], 'sender':data['from']})
198 201
199 202
200 if not self.options.multiple and not self.options.progress: 203 if not self.options.multiple and not self.options.progress:
201 #we just accept one file 204 #we just accept one file
202 self.loop.quit() 205 self.loop.quit()
203 206
204 def actionResult(self, type, id, data): 207 def actionResult(self, type, id, data):
205 #FIXME 208 #FIXME
206 info ("FIXME: actionResult not implemented") 209 info (_("FIXME: actionResult not implemented"))
207 210
208 def wait_file(self): 211 def wait_file(self):
209 """Wait for a file and write it on local dir""" 212 """Wait for a file and write it on local dir"""
210 self.bridge.register("askConfirmation", self.askConfirmation, "request") 213 self.bridge.register("askConfirmation", self.askConfirmation, "request")
211 214
215 if data: 218 if data:
216 if not data['position']: 219 if not data['position']:
217 data['position'] = '0' 220 data['position'] = '0'
218 if not self.pbar: 221 if not self.pbar:
219 #first answer, we must construct the bar 222 #first answer, we must construct the bar
220 self.pbar = ProgressBar(int(data['size']),["Progress: ",Percentage()," ",Bar()," ",FileTransferSpeed()," ",ETA()]) 223 self.pbar = ProgressBar(int(data['size']),[_("Progress: "),Percentage()," ",Bar()," ",FileTransferSpeed()," ",ETA()])
221 self.pbar.start() 224 self.pbar.start()
222 225
223 self.pbar.update(int(data['position'])) 226 self.pbar.update(int(data['position']))
224 elif self.pbar: 227 elif self.pbar:
225 self.pbar.finish() 228 self.pbar.finish()
246 self.pbar = None 249 self.pbar = None
247 gobject.timeout_add(10, self.progressCB) 250 gobject.timeout_add(10, self.progressCB)
248 try: 251 try:
249 self.loop.run() 252 self.loop.run()
250 except KeyboardInterrupt: 253 except KeyboardInterrupt:
251 info("User interruption: good bye") 254 info(_("User interruption: good bye"))
252 255
253 256
254 if __name__ == "__main__": 257 if __name__ == "__main__":
255 jp = JP() 258 jp = JP()
256 jp.go() 259 jp.go()