Mercurial > libervia-backend
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/frontends/src/jp/pipe.py Wed Feb 05 14:35:26 2014 +0100 @@ -0,0 +1,71 @@ +import base + +import tempfile +import os +import shutil +from sat.core.i18n import _ + + +parser = base.subparser.add_parser('pipe', + help = "File managment") + +parser.add_argument("-p", "--profile", action="store", type=str, default='@DEFAULT@', + help=_("Use PROFILE profile key (default: %(default)s)")) + +subparser = parser.add_subparsers() +pipein = subparser.add_parser('out', + help = "Send a pipe to a contact") +pipein.add_argument("jid", type=str, + help=_("The destination jid")) + +pipein.set_defaults(func=lambda args : PipeOut(args.profile, args.jid).go()) + +pipeout = subparser.add_parser('in', + help = "Send a pipe to a contact") +pipeout.add_argument("jids", type=str, nargs="*", + help=_("The destination jid")) + +pipeout.set_defaults(func=lambda args : PipeIn(args.profile, args.jids).go()) + +class PipeOut(base.JPWithProfile): + def __init__(self, profile, dest_jid): + base.JPWithProfile.__init__(self,profile) + self.dest_jid = dest_jid + + def pipe_out(self, dest_jid): + """Create named pipe, and send stdin to it""" + tmp_dir = tempfile.mkdtemp() + fifopath = os.path.join(tmp_dir,"pipe_out") + os.mkfifo(fifopath) + self.bridge.pipeOut(self._getFullJid(dest_jid), fifopath, {}, self.profile) + with open(fifopath, 'w') as f: + shutil.copyfileobj(sys.stdin, f) + shutil.rmtree(tmp_dir) + + def run(self): + jids = self.check_jids([self.dest_jid]) + jid = jids[0] + self.pipe_out(jid) + +class PipeIn(base.JPAsk): + def __init__(self, profile, dest_jids): + base.JPAsk.__init__(self,profile, start_mainloop = True) + self._dest_jids = dest_jids + + def dest_jids(self): + return self._dest_jids + + def confirm_type(self): + return "PIPE_TRANSFER" + + def ask(self, data): + answer_data = {} + tmp_dir = tempfile.mkdtemp() + fifopath = os.path.join(tmp_dir,"pipe_in") + answer_data["dest_path"] = fifopath + os.mkfifo(fifopath) + self.answer(True, answer_data) + with open(fifopath, 'r') as f: + shutil.copyfileobj(f, sys.stdout) + shutil.rmtree(tmp_dir) + self.loop.quit()