Mercurial > libervia-backend
annotate frontends/src/jp/pipe.py @ 816:4429bd7d5efb
misc (README): updated Link Mauve and Dal contributions, moved Souliane as a main contributor
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 05 Feb 2014 14:52:40 +0100 |
parents | f8d534ed1d1e |
children |
rev | line source |
---|---|
815 | 1 #! /usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # jp: a SAT command line tool | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
814 | 20 import base |
0 | 21 |
814 | 22 import tempfile |
0 | 23 import os |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
24 import shutil |
814 | 25 from sat.core.i18n import _ |
0 | 26 |
27 | |
814 | 28 parser = base.subparser.add_parser('pipe', |
29 help = "File managment") | |
0 | 30 |
814 | 31 parser.add_argument("-p", "--profile", action="store", type=str, default='@DEFAULT@', |
32 help=_("Use PROFILE profile key (default: %(default)s)")) | |
0 | 33 |
814 | 34 subparser = parser.add_subparsers() |
35 pipein = subparser.add_parser('out', | |
36 help = "Send a pipe to a contact") | |
37 pipein.add_argument("jid", type=str, | |
38 help=_("The destination jid")) | |
0 | 39 |
814 | 40 pipein.set_defaults(func=lambda args : PipeOut(args.profile, args.jid).go()) |
657 | 41 |
814 | 42 pipeout = subparser.add_parser('in', |
43 help = "Send a pipe to a contact") | |
44 pipeout.add_argument("jids", type=str, nargs="*", | |
45 help=_("The destination jid")) | |
657 | 46 |
814 | 47 pipeout.set_defaults(func=lambda args : PipeIn(args.profile, args.jids).go()) |
393 | 48 |
814 | 49 class PipeOut(base.JPWithProfile): |
50 def __init__(self, profile, dest_jid): | |
51 base.JPWithProfile.__init__(self,profile) | |
52 self.dest_jid = dest_jid | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
53 |
814 | 54 def pipe_out(self, dest_jid): |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
55 """Create named pipe, and send stdin to it""" |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
56 tmp_dir = tempfile.mkdtemp() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
57 fifopath = os.path.join(tmp_dir,"pipe_out") |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
58 os.mkfifo(fifopath) |
814 | 59 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
|
60 with open(fifopath, 'w') as f: |
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
61 shutil.copyfileobj(sys.stdin, f) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
62 shutil.rmtree(tmp_dir) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
63 |
814 | 64 def run(self): |
65 jids = self.check_jids([self.dest_jid]) | |
66 jid = jids[0] | |
67 self.pipe_out(jid) | |
0 | 68 |
814 | 69 class PipeIn(base.JPAsk): |
70 def __init__(self, profile, dest_jids): | |
71 base.JPAsk.__init__(self,profile, start_mainloop = True) | |
72 self._dest_jids = dest_jids | |
0 | 73 |
814 | 74 def dest_jids(self): |
75 return self._dest_jids | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
76 |
814 | 77 def confirm_type(self): |
78 return "PIPE_TRANSFER" | |
0 | 79 |
814 | 80 def ask(self, data): |
81 answer_data = {} | |
82 tmp_dir = tempfile.mkdtemp() | |
83 fifopath = os.path.join(tmp_dir,"pipe_in") | |
84 answer_data["dest_path"] = fifopath | |
85 os.mkfifo(fifopath) | |
86 self.answer(True, answer_data) | |
87 with open(fifopath, 'r') as f: | |
88 shutil.copyfileobj(f, sys.stdout) | |
89 shutil.rmtree(tmp_dir) | |
90 self.loop.quit() |