Mercurial > libervia-backend
annotate frontends/src/jp/pipe.py @ 814:59c7bc51c323
jp: refactoring using ArgParse
author | Dal <kedals0@gmail.com> |
---|---|
date | Wed, 05 Feb 2014 14:35:26 +0100 |
parents | frontends/src/jp/jp@1fe00f0c9a91 |
children | f8d534ed1d1e |
rev | line source |
---|---|
814 | 1 import base |
0 | 2 |
814 | 3 import tempfile |
0 | 4 import os |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
5 import shutil |
814 | 6 from sat.core.i18n import _ |
0 | 7 |
8 | |
814 | 9 parser = base.subparser.add_parser('pipe', |
10 help = "File managment") | |
0 | 11 |
814 | 12 parser.add_argument("-p", "--profile", action="store", type=str, default='@DEFAULT@', |
13 help=_("Use PROFILE profile key (default: %(default)s)")) | |
0 | 14 |
814 | 15 subparser = parser.add_subparsers() |
16 pipein = subparser.add_parser('out', | |
17 help = "Send a pipe to a contact") | |
18 pipein.add_argument("jid", type=str, | |
19 help=_("The destination jid")) | |
0 | 20 |
814 | 21 pipein.set_defaults(func=lambda args : PipeOut(args.profile, args.jid).go()) |
657 | 22 |
814 | 23 pipeout = subparser.add_parser('in', |
24 help = "Send a pipe to a contact") | |
25 pipeout.add_argument("jids", type=str, nargs="*", | |
26 help=_("The destination jid")) | |
657 | 27 |
814 | 28 pipeout.set_defaults(func=lambda args : PipeIn(args.profile, args.jids).go()) |
393 | 29 |
814 | 30 class PipeOut(base.JPWithProfile): |
31 def __init__(self, profile, dest_jid): | |
32 base.JPWithProfile.__init__(self,profile) | |
33 self.dest_jid = dest_jid | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
34 |
814 | 35 def pipe_out(self, dest_jid): |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
36 """Create named pipe, and send stdin to it""" |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
37 tmp_dir = tempfile.mkdtemp() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
38 fifopath = os.path.join(tmp_dir,"pipe_out") |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
39 os.mkfifo(fifopath) |
814 | 40 self.bridge.pipeOut(self._getFullJid(dest_jid), fifopath, {}, self.profile) |
402
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
41 with open(fifopath, 'w') as f: |
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
42 shutil.copyfileobj(sys.stdin, f) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
43 shutil.rmtree(tmp_dir) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
44 |
814 | 45 def run(self): |
46 jids = self.check_jids([self.dest_jid]) | |
47 jid = jids[0] | |
48 self.pipe_out(jid) | |
0 | 49 |
814 | 50 class PipeIn(base.JPAsk): |
51 def __init__(self, profile, dest_jids): | |
52 base.JPAsk.__init__(self,profile, start_mainloop = True) | |
53 self._dest_jids = dest_jids | |
0 | 54 |
814 | 55 def dest_jids(self): |
56 return self._dest_jids | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
57 |
814 | 58 def confirm_type(self): |
59 return "PIPE_TRANSFER" | |
0 | 60 |
814 | 61 def ask(self, data): |
62 answer_data = {} | |
63 tmp_dir = tempfile.mkdtemp() | |
64 fifopath = os.path.join(tmp_dir,"pipe_in") | |
65 answer_data["dest_path"] = fifopath | |
66 os.mkfifo(fifopath) | |
67 self.answer(True, answer_data) | |
68 with open(fifopath, 'r') as f: | |
69 shutil.copyfileobj(f, sys.stdout) | |
70 shutil.rmtree(tmp_dir) | |
71 self.loop.quit() |