changeset 1864:96ba685162f6

jp: all commands now use the new start method and set need_loop in __init__ when needed
author Goffi <goffi@goffi.org>
date Mon, 29 Feb 2016 16:52:51 +0100
parents b2ddd7f5dcdf
children fc6eeacf31bc
files frontends/src/jp/cmd_adhoc.py frontends/src/jp/cmd_blog.py frontends/src/jp/cmd_bookmarks.py frontends/src/jp/cmd_file.py frontends/src/jp/cmd_info.py frontends/src/jp/cmd_message.py frontends/src/jp/cmd_param.py frontends/src/jp/cmd_pipe.py frontends/src/jp/cmd_profile.py frontends/src/jp/cmd_roster.py
diffstat 10 files changed, 46 insertions(+), 84 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/jp/cmd_adhoc.py	Mon Feb 29 16:52:51 2016 +0100
+++ b/frontends/src/jp/cmd_adhoc.py	Mon Feb 29 16:52:51 2016 +0100
@@ -17,11 +17,7 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from logging import debug, info, error, warning
-
 import base
-import sys
-import os
 from sat.core.i18n import _
 
 __commands__ = ["AdHoc"]
@@ -31,7 +27,7 @@
 
 class Remote(base.CommandBase):
     def __init__(self, host):
-        super(Remote, self).__init__(host, 'remote', help=_('Remote control a software'))
+        super(Remote, self).__init__(host, 'remote', use_verbose=True, help=_('Remote control a software'))
 
     def add_parser_options(self):
         self.parser.add_argument("software", type=str, help=_("Software name"))
@@ -41,8 +37,7 @@
         self.parser.add_argument("--forbidden-jids", type=base.unicode_decoder, nargs='*', default=[], help=_("Jids that are *NOT* allowed to use the command"))
         self.parser.add_argument("-l", "--loop", action="store_true", help=_("Loop on the commands"))
 
-    def connected(self):
-        super(Remote, self).connected()
+    def start(self):
         name = self.args.software.lower()
         flags = []
         magics = {jid for jid in self.args.jids if jid.count('@')>1}
@@ -53,13 +48,13 @@
         bus_name, methods = self.host.bridge.adHocDBusAddAuto(name, jids, self.args.groups, magics,
                                                               self.args.forbidden_jids, self.args.forbidden_groups,
                                                               flags, self.profile)
-        debug(_("Bus name found: [%s]" % bus_name))
+        self.disp(_("Bus name found: [%s]" % bus_name), 1)
         for method in methods:
             path, iface, command = method
-            debug (_("Command found: (path:%(path)s, iface: %(iface)s) [%(command)s]" % {'path': path,
+            self.disp(_("Command found: (path:%(path)s, iface: %(iface)s) [%(command)s]" % {'path': path,
                                                                                          'iface': iface,
                                                                                          'command': command
-                                                                                         }))
+                                                                                         }),1)
 
 class AdHoc(base.CommandBase):
     subcommands = (Remote,)
--- a/frontends/src/jp/cmd_blog.py	Mon Feb 29 16:52:51 2016 +0100
+++ b/frontends/src/jp/cmd_blog.py	Mon Feb 29 16:52:51 2016 +0100
@@ -30,6 +30,7 @@
 class Import(base.CommandAnswering):
     def __init__(self, host):
         super(Import, self).__init__(host, 'import', use_progress=True, help=_(u'import an external blog'))
+        self.need_loop=True
 
     def add_parser_options(self):
         self.parser.add_argument("importer", type=base.unicode_decoder, nargs='?', help=_(u"importer name, nothing to display importers list"))
@@ -67,11 +68,7 @@
         self.disp(_("Error while trying to upload a blog: {reason}").format(reason=failure), error=True)
         self.host.quit(1)
 
-    def connected(self):
-        """Send files to jabber contact"""
-        self.need_loop=True
-        super(Import, self).connected()
-
+    def start(self):
         if self.args.location is None:
             for name in ('option', 'service', 'no_images_upload'):
                 if getattr(self.args, name):
@@ -108,7 +105,7 @@
 
 
 class Blog(base.CommandBase):
-    subcommands = (Import,)
+    subcommands = (Edit, Import)
 
     def __init__(self, host):
         super(Blog, self).__init__(host, 'blog', use_profile=False, help=_('blog/microblog management'))
--- a/frontends/src/jp/cmd_bookmarks.py	Mon Feb 29 16:52:51 2016 +0100
+++ b/frontends/src/jp/cmd_bookmarks.py	Mon Feb 29 16:52:51 2016 +0100
@@ -17,8 +17,6 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from logging import debug, info, error, warning
-
 import base
 from sat.core.i18n import _
 
@@ -43,8 +41,7 @@
     def __init__(self, host):
         super(BookmarksList, self).__init__(host, 'list', help=_('list bookmarks'))
 
-    def connected(self):
-        super(BookmarksList, self).connected()
+    def start(self):
         data = self.host.bridge.bookmarksList(self.args.type, self.args.location, self.host.profile)
         mess = []
         for location in STORAGE_LOCATIONS:
@@ -68,17 +65,16 @@
 
 
 class BookmarksRemove(BookmarksCommon):
-    need_loop = True
 
     def __init__(self, host):
         super(BookmarksRemove, self).__init__(host, 'remove', help=_('remove a bookmark'))
+        self.need_loop = True
 
     def add_parser_options(self):
         super(BookmarksRemove, self).add_parser_options()
         self.parser.add_argument('bookmark', type=base.unicode_decoder, help=_('jid (for muc bookmark) or url of to remove'))
 
-    def connected(self):
-        super(BookmarksRemove, self).connected()
+    def start(self):
         self.host.bridge.bookmarksRemove(self.args.type, self.args.bookmark, self.args.location, self.host.profile, callback = lambda: self.host.quit(), errback=self._errback)
 
 
@@ -86,6 +82,7 @@
 
     def __init__(self, host):
         super(BookmarksAdd, self).__init__(host, 'add', help=_('add a bookmark'))
+        self.need_loop = True
 
     def add_parser_options(self):
         super(BookmarksAdd, self).add_parser_options(location_default='auto')
@@ -95,9 +92,7 @@
         muc_group.add_argument('-N', '--nick', type=base.unicode_decoder, help=_('nickname'))
         muc_group.add_argument('-a', '--autojoin', action='store_true', help=_('join room on profile connection'))
 
-    def connected(self):
-        self.need_loop = True
-        super(BookmarksAdd, self).connected()
+    def start(self):
         if self.args.type == 'url' and (self.args.autojoin or self.args.nick is not None):
             # XXX: Argparse doesn't seem to manage this case, any better way ?
             print _(u"You can't use --autojoin or --nick with --type url")
--- a/frontends/src/jp/cmd_file.py	Mon Feb 29 16:52:51 2016 +0100
+++ b/frontends/src/jp/cmd_file.py	Mon Feb 29 16:52:51 2016 +0100
@@ -36,16 +36,15 @@
 class Send(base.CommandBase):
     def __init__(self, host):
         super(Send, self).__init__(host, 'send', use_progress=True, use_verbose=True, help=_('Send a file to a contact'))
+        self.need_loop=True
 
     def add_parser_options(self):
         self.parser.add_argument("files", type=str, nargs='+', metavar='file', help=_("a list of file"))
         self.parser.add_argument("jid", type=base.unicode_decoder, help=_("the destination jid"))
         self.parser.add_argument("-b", "--bz2", action="store_true", help=_("make a bzip2 tarball"))
 
-    def connected(self):
+    def start(self):
         """Send files to jabber contact"""
-        self.need_loop=True
-        super(Send, self).connected()
         self.send_files()
 
     def onProgressStarted(self, metadata):
@@ -203,8 +202,7 @@
         self.parser.add_argument("-f", "--force", action="store_true", help=_(u"force overwritting of existing files (/!\\ name is choosed by sended)"))
         self.parser.add_argument("--path", default='.', metavar='DIR', help=_(u"destination path (default: working directory)"))
 
-    def run(self):
-        super(Receive, self).run()
+    def start(self):
         self.bare_jids = [jid.JID(jid_).bare for jid_ in self.args.jids]
         self.path = os.path.abspath(self.args.path)
         if not os.path.isdir(self.path):
@@ -216,20 +214,16 @@
 
 
 class Upload(base.CommandBase):
+
     def __init__(self, host):
         super(Upload, self).__init__(host, 'upload', use_progress=True, use_verbose=True, help=_('Upload a file'))
+        self.need_loop=True
 
     def add_parser_options(self):
         self.parser.add_argument("file", type=str, help=_("file to upload"))
         self.parser.add_argument("jid", type=base.unicode_decoder, nargs='?',  help=_("jid of upload component (nothing to autodetect)"))
         self.parser.add_argument("--ignore-tls-errors", action="store_true", help=_("ignore invalide TLS certificate"))
 
-    def connected(self):
-        """Send files to jabber contact"""
-        self.need_loop=True
-        super(Upload, self).connected()
-        self.uploadFile()
-
     def onProgressStarted(self, metadata):
         self.disp(_(u'File upload started'),2)
 
@@ -264,7 +258,7 @@
         self.disp(_("Error while trying to upload a file: {reason}").format(reason=failure), error=True)
         self.host.quit(1)
 
-    def uploadFile(self):
+    def start(self):
         file_ = self.args.file
         if not os.path.exists(file_):
             self.disp(_(u"file [{}] doesn't exist !").format(file_), error=True)
--- a/frontends/src/jp/cmd_info.py	Mon Feb 29 16:52:51 2016 +0100
+++ b/frontends/src/jp/cmd_info.py	Mon Feb 29 16:52:51 2016 +0100
@@ -17,8 +17,6 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from logging import debug, info, warning
-
 import base
 from sat.core.i18n import _
 
@@ -29,13 +27,12 @@
 
     def __init__(self, host):
         super(Disco, self).__init__(host, 'disco', help=_('Service discovery'))
+        self.need_loop=True
 
     def add_parser_options(self):
         self.parser.add_argument("jid", type=str, help=_("Entity to discover"))
 
-    def connected(self):
-        self.need_loop=True
-        super(Disco, self).connected()
+    def start(self):
         jids = self.host.check_jids([self.args.jid])
         jid = jids[0]
         self.host.bridge.discoInfos(jid, profile_key=self.host.profile, callback=lambda infos: self.gotInfos(infos, jid), errback=self.error)
@@ -108,13 +105,12 @@
 
     def __init__(self, host):
         super(Version, self).__init__(host, 'version', help=_('Client version'))
+        self.need_loop=True
 
     def add_parser_options(self):
         self.parser.add_argument("jid", type=str, help=_("Entity to request"))
 
-    def connected(self):
-        self.need_loop=True
-        super(Version, self).connected()
+    def start(self):
         jids = self.host.check_jids([self.args.jid])
         jid = jids[0]
         self.host.bridge.getSoftwareVersion(jid, self.host.profile, callback=self.gotVersion, errback=self.error)
--- a/frontends/src/jp/cmd_message.py	Mon Feb 29 16:52:51 2016 +0100
+++ b/frontends/src/jp/cmd_message.py	Mon Feb 29 16:52:51 2016 +0100
@@ -35,8 +35,7 @@
         self.parser.add_argument("-n", "--new-line", action="store_true", help=_("add a new line at the beginning of the input (usefull for ascii art ;))"))
         self.parser.add_argument("jid", type=str, help=_("the destination jid"))
 
-    def connected(self):
-        super(Send, self).connected()
+    def start(self):
         jids = self.host.check_jids([self.args.jid])
         jid = jids[0]
         self.sendStdin(jid)
--- a/frontends/src/jp/cmd_param.py	Mon Feb 29 16:52:51 2016 +0100
+++ b/frontends/src/jp/cmd_param.py	Mon Feb 29 16:52:51 2016 +0100
@@ -34,8 +34,7 @@
         self.parser.add_argument("-a", "--attribute", type=str, default="value", help=_(u"Name of the attribute to get"))
         self.parser.add_argument("--security-limit", type=int, default=-1, help=_(u"Security limit"))
 
-    def connected(self):
-        super(Get, self).connected()
+    def start(self):
         if self.args.category is None:
             categories = self.host.bridge.getParamsCategories()
             print u"\n".join(categories)
@@ -67,8 +66,7 @@
         self.parser.add_argument("value", type=base.unicode_decoder, help=_(u"Name of the parameter"))
         self.parser.add_argument("--security-limit", type=int, default=-1, help=_(u"Security limit"))
 
-    def connected(self):
-        super(Set, self).connected()
+    def start(self):
         try:
             self.host.bridge.setParam(self.args.name, self.args.value, self.args.category, self.args.security_limit, self.profile)
         except Exception as e:
@@ -82,7 +80,7 @@
     def add_parser_options(self):
         self.parser.add_argument("filename", type=str, help=_("Output file"))
 
-    def run(self):
+    def start(self):
         """Save parameters template to xml file"""
         if self.host.bridge.saveParamsTemplate(self.args.filename):
             print _("Parameters saved to file %s") % self.args.filename
@@ -98,7 +96,7 @@
     def add_parser_options(self):
         self.parser.add_argument("filename", type=str, help=_("Input file"))
 
-    def run(self):
+    def start(self):
         """Load parameters template from xml file"""
         if self.host.bridge.loadParamsTemplate(self.args.filename):
             print _("Parameters loaded from file %s") % self.args.filename
--- a/frontends/src/jp/cmd_pipe.py	Mon Feb 29 16:52:51 2016 +0100
+++ b/frontends/src/jp/cmd_pipe.py	Mon Feb 29 16:52:51 2016 +0100
@@ -36,12 +36,14 @@
 
     def __init__(self, host):
         super(PipeOut, self).__init__(host, 'out', help=_('send a pipe a stream'))
+        self.need_loop = True
 
     def add_parser_options(self):
         self.parser.add_argument("jid", type=base.unicode_decoder, help=_("the destination jid"))
 
-    def pipe_out(self):
+    def start(self):
         """ Create named pipe, and send stdin to it """
+        # TODO: check_jids
         tmp_dir = tempfile.mkdtemp()
         fifopath = os.path.join(tmp_dir,"pipe_out")
         os.mkfifo(fifopath)
@@ -51,12 +53,6 @@
         shutil.rmtree(tmp_dir)
         self.host.quit()
 
-    def connected(self):
-        # TODO: check_jids
-        self.need_loop = True
-        super(PipeOut, self).connected()
-        self.pipe_out()
-
 
 class PipeIn(base.CommandAnswering):
 
@@ -104,8 +100,7 @@
             shutil.rmtree(tmp_dir)
             self.host.quit()
 
-    def run(self):
-        super(PipeIn, self).run()
+    def start(self):
         self.bare_jids = [jid.JID(jid_).bare for jid_ in self.args.jids]
 
 
--- a/frontends/src/jp/cmd_profile.py	Mon Feb 29 16:52:51 2016 +0100
+++ b/frontends/src/jp/cmd_profile.py	Mon Feb 29 16:52:51 2016 +0100
@@ -48,7 +48,7 @@
     def add_parser_options(self):
         pass
 
-    def run(self):
+    def start(self):
         print self.host.bridge.getProfileName('@DEFAULT@')
 
 
@@ -60,8 +60,7 @@
         self.parser.add_argument('profile', type=str, help=PROFILE_HELP)
         self.parser.add_argument('-f', '--force', action='store_true', help=_(u'delete profile without confirmation'))
 
-    def run(self):
-        super(ProfileDelete, self).run()
+    def start(self):
         if self.args.profile not in self.host.bridge.getProfilesList():
             log.error("Profile %s doesn't exist." % self.args.profile)
             self.host.quit(1)
@@ -78,6 +77,7 @@
 class ProfileInfo(base.CommandBase):
     def __init__(self, host):
         super(ProfileInfo, self).__init__(host, 'info', need_connect=False, help=_('get information about a profile'))
+        self.need_loop = True
         self.to_show = [(_(u"jid"), "Connection", "JabberID"),]
         self.largest = max([len(item[0]) for item in self.to_show])
 
@@ -97,9 +97,7 @@
             self.host.bridge.asyncGetParamA(name, category, profile_key=self.host.profile,
                                             callback=lambda value: self.showNextValue(label, category, value))
 
-    def connected(self):
-        self.need_loop = True
-        super(ProfileInfo, self).connected()
+    def start(self):
         if self.args.show_password:
             self.to_show.append((_(u"XMPP password"), "Connection", "Password"))
         self.showNextValue()
@@ -112,8 +110,7 @@
     def add_parser_options(self):
         pass
 
-    def run(self):
-        super(ProfileList, self).run()
+    def start(self):
         for profile in self.host.bridge.getProfilesList():
             print profile
 
@@ -121,6 +118,7 @@
 class ProfileCreate(base.CommandBase):
     def __init__(self, host):
         super(ProfileCreate, self).__init__(host, 'create', use_profile=False, help=_('create a new profile'))
+        self.need_loop = True
 
     def add_parser_options(self):
         self.parser.add_argument('profile', type=str, help=_('the name of the profile'))
@@ -140,9 +138,8 @@
     def _profile_created(self):
         self.host.bridge.profileStartSession(self.args.password, self.args.profile, callback=self._session_started, errback=None)
 
-    def run(self):
+    def start(self):
         """Create a new profile"""
-        self.need_loop = True
         if self.args.profile in self.host.bridge.getProfilesList():
             log.error("Profile %s already exists." % self.args.profile)
             self.host.quit(1)
@@ -162,8 +159,7 @@
                                  metavar='PASSWORD')
         self.parser.add_argument('-D', '--default', action='store_true', help=_(u'set as default profile'))
 
-    def connected(self):
-        super(ProfileModify, self).connected()
+    def start(self):
         if self.args.disable_password:
             self.args.password = ''
         if self.args.password is not None:
--- a/frontends/src/jp/cmd_roster.py	Mon Feb 29 16:52:51 2016 +0100
+++ b/frontends/src/jp/cmd_roster.py	Mon Feb 29 16:52:51 2016 +0100
@@ -33,14 +33,13 @@
 
     def __init__(self, host):
         super(Purge, self).__init__(host, 'purge', help=_('Purge the roster from its contacts with no subscription'))
+        self.need_loop = True
 
     def add_parser_options(self):
         self.parser.add_argument("--no_from", action="store_true", help=_("Also purge contacts with no 'from' subscription"))
         self.parser.add_argument("--no_to", action="store_true", help=_("Also purge contacts with no 'to' subscription"))
 
-    def connected(self):
-        self.need_loop = True
-        super(Purge, self).connected()
+    def start(self):
         self.host.bridge.getContacts(profile_key=self.host.profile, callback=self.gotContacts, errback=self.error)
 
     def error(self, failure):
@@ -99,13 +98,12 @@
 
     def __init__(self, host):
         super(Stats, self).__init__(host, 'stats', help=_('Show statistics about a roster'))
+        self.need_loop = True
 
     def add_parser_options(self):
         pass
 
-    def connected(self):
-        self.need_loop = True
-        super(Stats, self).connected()
+    def start(self):
         self.host.bridge.getContacts(profile_key=self.host.profile, callback=self.gotContacts, errback=self.error)
 
     def error(self, failure):
@@ -161,15 +159,14 @@
 
     def __init__(self, host):
         super(Get, self).__init__(host, 'get', help=_('Retrieve the roster contacts'))
+        self.need_loop = True
 
     def add_parser_options(self):
         self.parser.add_argument("--subscriptions", action="store_true", help=_("Show the contacts' subscriptions"))
         self.parser.add_argument("--groups", action="store_true", help=_("Show the contacts' groups"))
         self.parser.add_argument("--name", action="store_true", help=_("Show the contacts' names"))
 
-    def connected(self):
-        self.need_loop = True
-        super(Get, self).connected()
+    def start(self):
         self.host.bridge.getContacts(profile_key=self.host.profile, callback=self.gotContacts, errback=self.error)
 
     def error(self, failure):