annotate sat_frontends/jp/base.py @ 2764:92af49cde255

jp (base): MAM and RSM arguments can now be used for pubsub commands: - RSM and MAM args arguments have been added to pubsub group, and activated when multiple items are possible - /!\ --max-items (i.e. pubsub original max) short option has been change for -M, and -m is now used for RSM max - pubsub/search -m option has been renamed -M for same reason as above - pubsub/search -o option has been replaced by -k (for keep), to avoid conflict with order-by (and long version is now --node-max)
author Goffi <goffi@goffi.org>
date Fri, 11 Jan 2019 10:18:02 +0100
parents 4b693ea24d5f
children 378188abe941
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1945
2c75011d7b2d jp: forgotten shebang fixes
Goffi <goffi@goffi.org>
parents: 1918
diff changeset
1 #!/usr/bin/env python2
0
goffi@necton2
parents:
diff changeset
2 # -*- coding: utf-8 -*-
goffi@necton2
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
4 # jp: a SAT command line tool
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2457
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
0
goffi@necton2
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
10 # (at your option) any later version.
0
goffi@necton2
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
15 # GNU Affero General Public License for more details.
0
goffi@necton2
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
0
goffi@necton2
parents:
diff changeset
19
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 721
diff changeset
20 from sat.core.i18n import _
402
f03688bdb858 jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents: 401
diff changeset
21
0
goffi@necton2
parents:
diff changeset
22 ### logging ###
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
23 import logging as log
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
24 log.basicConfig(level=log.DEBUG,
2162
c9a67eb5bf72 jp (base): improved module import:
Goffi <goffi@goffi.org>
parents: 2161
diff changeset
25 format='%(message)s')
0
goffi@necton2
parents:
diff changeset
26 ###
goffi@necton2
parents:
diff changeset
27
goffi@necton2
parents:
diff changeset
28 import sys
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
29 import locale
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
30 import os.path
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
31 import argparse
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
32 from glob import iglob
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
33 from importlib import import_module
1139
75025461141f move sat.tools.jid to sat_frontends.tools.jid
souliane <souliane@mailoo.org>
parents: 1033
diff changeset
34 from sat_frontends.tools.jid import JID
2098
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
35 from sat.tools import config
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
36 from sat.tools.common import dynamic_import
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
37 from sat.tools.common import uri
2704
ab37d1c7c38c jp (base): new date_decoder to specify dates in arguments
Goffi <goffi@goffi.org>
parents: 2692
diff changeset
38 from sat.tools.common import date_utils
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
39 from sat.core import exceptions
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
40 import sat_frontends.jp
970
2e052998c7eb jp: using C.APP_URL for application url
Goffi <goffi@goffi.org>
parents: 965
diff changeset
41 from sat_frontends.jp.constants import Const as C
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
42 from sat_frontends.tools import misc
1840
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
43 import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1869
diff changeset
44 import shlex
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
45 from collections import OrderedDict
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1869
diff changeset
46
2098
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
47 ## bridge handling
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
48 # we get bridge name from conf and initialise the right class accordingly
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
49 main_config = config.parseMainConf()
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
50 bridge_name = config.getConfig(main_config, '', 'bridge', 'dbus')
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
51
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
52
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
53 # TODO: move loops handling in a separated module
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
54 if 'dbus' in bridge_name:
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
55 from gi.repository import GLib
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
56
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
57
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
58 class JPLoop(object):
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
59
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
60 def __init__(self):
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
61 self.loop = GLib.MainLoop()
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
62
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
63 def run(self):
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
64 self.loop.run()
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
65
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
66 def quit(self):
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
67 self.loop.quit()
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
68
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
69 def call_later(self, delay, callback, *args):
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
70 """call a callback repeatedly
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
71
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
72 @param delay(int): delay between calls in ms
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
73 @param callback(callable): method to call
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
74 if the callback return True, the call will continue
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
75 else the calls will stop
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
76 @param *args: args of the callbac
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
77 """
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
78 GLib.timeout_add(delay, callback, *args)
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
79
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
80 else:
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
81 print u"can't start jp: only D-Bus bridge is currently handled"
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
82 sys.exit(C.EXIT_ERROR)
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
83 # FIXME: twisted loop can be used when jp can handle fully async bridges
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
84 # from twisted.internet import reactor
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
85
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
86 # class JPLoop(object):
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
87
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
88 # def run(self):
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
89 # reactor.run()
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
90
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
91 # def quit(self):
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
92 # reactor.stop()
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
93
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
94 # def _timeout_cb(self, args, callback, delay):
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
95 # ret = callback(*args)
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
96 # if ret:
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
97 # reactor.callLater(delay, self._timeout_cb, args, callback, delay)
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
98
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
99 # def call_later(self, delay, callback, *args):
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
100 # delay = float(delay) / 1000
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
101 # reactor.callLater(delay, self._timeout_cb, args, callback, delay)
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
102
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
103 if bridge_name == "embedded":
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
104 from sat.core import sat_main
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
105 sat = sat_main.SAT()
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
106
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1869
diff changeset
107 if sys.version_info < (2, 7, 3):
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1869
diff changeset
108 # XXX: shlex.split only handle unicode since python 2.7.3
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1869
diff changeset
109 # this is a workaround for older versions
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1869
diff changeset
110 old_split = shlex.split
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1869
diff changeset
111 new_split = (lambda s, *a, **kw: [t.decode('utf-8') for t in old_split(s.encode('utf-8'), *a, **kw)]
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1869
diff changeset
112 if isinstance(s, unicode) else old_split(s, *a, **kw))
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1869
diff changeset
113 shlex.split = new_split
1840
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
114
0
goffi@necton2
parents:
diff changeset
115 try:
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
116 import progressbar
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
117 except ImportError:
2188
052d560d0dce jp (base): replaced log.warning by stderr prints
Goffi <goffi@goffi.org>
parents: 2187
diff changeset
118 msg = (_(u'ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar\n') +
052d560d0dce jp (base): replaced log.warning by stderr prints
Goffi <goffi@goffi.org>
parents: 2187
diff changeset
119 _(u'Progress bar deactivated\n--\n'))
052d560d0dce jp (base): replaced log.warning by stderr prints
Goffi <goffi@goffi.org>
parents: 2187
diff changeset
120 print >>sys.stderr,msg.encode('utf-8')
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
121 progressbar=None
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
122
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
123 #consts
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
124 PROG_NAME = u"jp"
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
125 DESCRIPTION = """This software is a command line tool for XMPP.
970
2e052998c7eb jp: using C.APP_URL for application url
Goffi <goffi@goffi.org>
parents: 965
diff changeset
126 Get the latest version at """ + C.APP_URL
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
127
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2457
diff changeset
128 COPYLEFT = u"""Copyright (C) 2009-2018 Jérôme Poisson, Adrien Cossa
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
129 This program comes with ABSOLUTELY NO WARRANTY;
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
130 This is free software, and you are welcome to redistribute it under certain conditions.
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
131 """
0
goffi@necton2
parents:
diff changeset
132
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
133 PROGRESS_DELAY = 10 # the progression will be checked every PROGRESS_DELAY ms
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
134
0
goffi@necton2
parents:
diff changeset
135
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
136 def unicode_decoder(arg):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
137 # Needed to have unicode strings from arguments
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
138 return arg.decode(locale.getpreferredencoding())
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
139
0
goffi@necton2
parents:
diff changeset
140
2704
ab37d1c7c38c jp (base): new date_decoder to specify dates in arguments
Goffi <goffi@goffi.org>
parents: 2692
diff changeset
141 def date_decoder(arg):
ab37d1c7c38c jp (base): new date_decoder to specify dates in arguments
Goffi <goffi@goffi.org>
parents: 2692
diff changeset
142 return date_utils.date_parse_ext(arg, default_tz=date_utils.TZ_LOCAL)
ab37d1c7c38c jp (base): new date_decoder to specify dates in arguments
Goffi <goffi@goffi.org>
parents: 2692
diff changeset
143
ab37d1c7c38c jp (base): new date_decoder to specify dates in arguments
Goffi <goffi@goffi.org>
parents: 2692
diff changeset
144
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
145 class Jp(object):
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
146 """
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
147 This class can be use to establish a connection with the
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
148 bridge. Moreover, it should manage a main loop.
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
149
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
150 To use it, you mainly have to redefine the method run to perform
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
151 specify what kind of operation you want to perform.
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
152
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
153 """
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
154 def __init__(self):
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
155 """
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
156
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
157 @attribute quit_on_progress_end (bool): set to False if you manage yourself exiting,
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
158 or if you want the user to stop by himself
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
159 @attribute progress_success(callable): method to call when progress just started
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
160 by default display a message
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
161 @attribute progress_success(callable): method to call when progress is successfully finished
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
162 by default display a message
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
163 @attribute progress_failure(callable): method to call when progress failed
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
164 by default display a message
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
165 """
2098
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
166 # FIXME: need_loop should be removed, everything must be async in bridge so
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
167 # loop will always be needed
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
168 bridge_module = dynamic_import.bridge(bridge_name, 'sat_frontends.bridge')
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
169 if bridge_module is None:
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
170 log.error(u"Can't import {} bridge".format(bridge_name))
165
8a2053de6f8c Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents: 156
diff changeset
171 sys.exit(1)
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
172
2098
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
173 self.bridge = bridge_module.Bridge()
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
174 self.bridge.bridgeConnect(callback=self._bridgeCb, errback=self._bridgeEb)
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
175
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
176 def _bridgeCb(self):
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
177 self.parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
178 description=DESCRIPTION)
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
179 self._make_parents()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
180 self.add_parser_options()
2312
eaff25529c53 jp (base): typo
Goffi <goffi@goffi.org>
parents: 2309
diff changeset
181 self.subparsers = self.parser.add_subparsers(title=_(u'Available commands'), dest='subparser_name')
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
182 self._auto_loop = False # when loop is used for internal reasons
1863
b2ddd7f5dcdf jp (base): refactored need_loop so it is set only when the command is run. It can now be set in __init__ methods of commands classes
Goffi <goffi@goffi.org>
parents: 1862
diff changeset
183 self._need_loop = False
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
184
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
185 # progress attributes
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
186 self._progress_id = None # TODO: manage several progress ids
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
187 self.quit_on_progress_end = True
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
188
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
189 # outputs
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
190 self._outputs = {}
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
191 for type_ in C.OUTPUT_TYPES:
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
192 self._outputs[type_] = OrderedDict()
2189
a25a256688e2 jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents: 2188
diff changeset
193 self.default_output = {}
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
194
2705
0bb811aaf11d jp (base): new own_jid host attribute:
Goffi <goffi@goffi.org>
parents: 2704
diff changeset
195 self.own_jid = None # must be filled at runtime if needed
0bb811aaf11d jp (base): new own_jid host attribute:
Goffi <goffi@goffi.org>
parents: 2704
diff changeset
196
2098
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
197 def _bridgeEb(self, failure):
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
198 if isinstance(failure, exceptions.BridgeExceptionNoService):
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
199 print(_(u"Can't connect to SàT backend, are you sure it's launched ?"))
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
200 elif isinstance(failure, exceptions.BridgeInitError):
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
201 print(_(u"Can't init bridge"))
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
202 else:
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
203 print(_(u"Error while initialising bridge: {}".format(failure)))
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
204 sys.exit(C.EXIT_BRIDGE_ERROR)
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
205
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
206 @property
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
207 def version(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
208 return self.bridge.getVersion()
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
209
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
210 @property
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
211 def progress_id(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
212 return self._progress_id
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
213
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
214 @progress_id.setter
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
215 def progress_id(self, value):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
216 self._progress_id = value
1642
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
217 self.replayCache('progress_ids_cache')
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
218
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
219 @property
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
220 def watch_progress(self):
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
221 try:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
222 self.pbar
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
223 except AttributeError:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
224 return False
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
225 else:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
226 return True
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
227
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
228 @watch_progress.setter
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
229 def watch_progress(self, watch_progress):
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
230 if watch_progress:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
231 self.pbar = None
1604
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
232
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
233 @property
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
234 def verbosity(self):
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
235 try:
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
236 return self.args.verbose
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
237 except AttributeError:
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
238 return 0
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
239
1642
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
240 def replayCache(self, cache_attribute):
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
241 """Replay cached signals
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
242
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
243 @param cache_attribute(str): name of the attribute containing the cache
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
244 if the attribute doesn't exist, there is no cache and the call is ignored
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
245 else the cache must be a list of tuples containing the replay callback as first item,
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
246 then the arguments to use
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
247 """
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
248 try:
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
249 cache = getattr(self, cache_attribute)
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
250 except AttributeError:
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
251 pass
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
252 else:
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
253 for cache_data in cache:
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
254 cache_data[0](*cache_data[1:])
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
255
2277
637886ac35f6 jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents: 2237
diff changeset
256 def disp(self, msg, verbosity=0, error=False, no_lf=False):
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
257 """Print a message to user
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
258
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
259 @param msg(unicode): message to print
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
260 @param verbosity(int): minimal verbosity to display the message
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
261 @param error(bool): if True, print to stderr instead of stdout
2277
637886ac35f6 jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents: 2237
diff changeset
262 @param no_lf(bool): if True, do not emit line feed at the end of line
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
263 """
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
264 if self.verbosity >= verbosity:
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
265 if error:
2277
637886ac35f6 jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents: 2237
diff changeset
266 if no_lf:
637886ac35f6 jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents: 2237
diff changeset
267 print >>sys.stderr,msg.encode('utf-8'),
637886ac35f6 jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents: 2237
diff changeset
268 else:
637886ac35f6 jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents: 2237
diff changeset
269 print >>sys.stderr,msg.encode('utf-8')
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
270 else:
2277
637886ac35f6 jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents: 2237
diff changeset
271 if no_lf:
637886ac35f6 jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents: 2237
diff changeset
272 print msg.encode('utf-8'),
637886ac35f6 jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents: 2237
diff changeset
273 else:
637886ac35f6 jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents: 2237
diff changeset
274 print msg.encode('utf-8')
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
275
2155
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
276 def output(self, type_, name, extra_outputs, data):
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
277 if name in extra_outputs:
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
278 extra_outputs[name](data)
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
279 else:
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
280 self._outputs[type_][name]['callback'](data)
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
281
1604
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
282 def addOnQuitCallback(self, callback, *args, **kwargs):
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
283 """Add a callback which will be called on quit command
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
284
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
285 @param callback(callback): method to call
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
286 """
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
287 try:
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
288 callbacks_list = self._onQuitCallbacks
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
289 except AttributeError:
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
290 callbacks_list = self._onQuitCallbacks = []
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
291 finally:
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
292 callbacks_list.append((callback, args, kwargs))
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
293
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
294 def getOutputChoices(self, output_type):
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
295 """Return valid output filters for output_type
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
296
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
297 @param output_type: True for default,
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
298 else can be any registered type
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
299 """
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
300 return self._outputs[output_type].keys()
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
301
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
302 def _make_parents(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
303 self.parents = {}
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
304
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
305 # we have a special case here as the start-session option is present only if connection is not needed,
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
306 # so we create two similar parents, one with the option, the other one without it
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
307 for parent_name in ('profile', 'profile_session'):
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
308 parent = self.parents[parent_name] = argparse.ArgumentParser(add_help=False)
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
309 parent.add_argument("-p", "--profile", action="store", type=str, default='@DEFAULT@', help=_("Use PROFILE profile key (default: %(default)s)"))
2151
fff88c33442f jp (info/disco): added --node argument
Goffi <goffi@goffi.org>
parents: 2142
diff changeset
310 parent.add_argument("--pwd", action="store", type=unicode_decoder, default='', metavar='PASSWORD', help=_("Password used to connect profile, if necessary"))
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
311
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
312 profile_parent, profile_session_parent = self.parents['profile'], self.parents['profile_session']
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
313
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
314 connect_short, connect_long, connect_action, connect_help = "-c", "--connect", "store_true", _(u"Connect the profile before doing anything else")
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
315 profile_parent.add_argument(connect_short, connect_long, action=connect_action, help=connect_help)
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
316
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
317 profile_session_connect_group = profile_session_parent.add_mutually_exclusive_group()
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
318 profile_session_connect_group.add_argument(connect_short, connect_long, action=connect_action, help=connect_help)
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
319 profile_session_connect_group.add_argument("--start-session", action="store_true", help=_("Start a profile session without connecting"))
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
320
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
321 progress_parent = self.parents['progress'] = argparse.ArgumentParser(add_help=False)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
322 if progressbar:
823
300b4de701a6 jp: short option for progress is now -P instead of -g, so -g can be used for groups
Goffi <goffi@goffi.org>
parents: 817
diff changeset
323 progress_parent.add_argument("-P", "--progress", action="store_true", help=_("Show progress bar"))
0
goffi@necton2
parents:
diff changeset
324
1600
8d41cd4da2f6 jp: added a --verbose command
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
325 verbose_parent = self.parents['verbose'] = argparse.ArgumentParser(add_help=False)
1641
44a14f83e64b jp (base): fixed verbose option + there is a new line on ProgressErrorHandler only if the progress bar is actually displayed
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
326 verbose_parent.add_argument('--verbose', '-v', action='count', default=0, help=_(u"Add a verbosity level (can be used multiple times)"))
1600
8d41cd4da2f6 jp: added a --verbose command
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
327
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
328 draft_parent = self.parents['draft'] = argparse.ArgumentParser(add_help=False)
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
329 draft_group = draft_parent.add_argument_group(_('draft handling'))
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
330 draft_group.add_argument("-D", "--current", action="store_true", help=_(u"load current draft"))
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
331 draft_group.add_argument("-F", "--draft-path", type=unicode_decoder, help=_(u"path to a draft file to retrieve"))
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
332
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
333
2547
2d69a0afe039 jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents: 2539
diff changeset
334 def make_pubsub_group(self, flags, defaults):
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
335 """generate pubsub options according to flags
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
336
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
337 @param flags(iterable[unicode]): see [CommandBase.__init__]
2547
2d69a0afe039 jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents: 2539
diff changeset
338 @param defaults(dict[unicode, unicode]): help text for default value
2d69a0afe039 jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents: 2539
diff changeset
339 key can be "service" or "node"
2d69a0afe039 jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents: 2539
diff changeset
340 value will be set in " (DEFAULT: {value})", or can be None to remove DEFAULT
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
341 @return (ArgumentParser): parser to add
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
342 """
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
343 flags = misc.FlagsHandler(flags)
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
344 parent = argparse.ArgumentParser(add_help=False)
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
345 pubsub_group = parent.add_argument_group('pubsub')
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
346 pubsub_group.add_argument("-u", "--pubsub-url", type=unicode_decoder,
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
347 help=_(u"Pubsub URL (xmpp or http)"))
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
348
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
349 service_help = _(u"JID of the PubSub service")
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
350 if not flags.service:
2547
2d69a0afe039 jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents: 2539
diff changeset
351 default = defaults.pop(u'service', _(u'PEP service'))
2d69a0afe039 jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents: 2539
diff changeset
352 if default is not None:
2d69a0afe039 jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents: 2539
diff changeset
353 service_help += _(u" (DEFAULT: {default})".format(default=default))
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
354 pubsub_group.add_argument("-s", "--service", type=unicode_decoder, default=u'',
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
355 help=service_help)
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
356
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
357 node_help = _(u"node to request")
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
358 if not flags.node:
2547
2d69a0afe039 jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents: 2539
diff changeset
359 default = defaults.pop(u'node', _(u'standard node'))
2d69a0afe039 jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents: 2539
diff changeset
360 if default is not None:
2d69a0afe039 jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents: 2539
diff changeset
361 node_help += _(u" (DEFAULT: {default})".format(default=default))
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
362 pubsub_group.add_argument("-n", "--node", type=unicode_decoder, default=u'', help=node_help)
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
363
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
364 if flags.single_item:
2552
38e1e29c48e9 jp (base): added C.ITEM pubsub flag when --item is required
Goffi <goffi@goffi.org>
parents: 2550
diff changeset
365 item_help = (u"item to retrieve")
38e1e29c48e9 jp (base): added C.ITEM pubsub flag when --item is required
Goffi <goffi@goffi.org>
parents: 2550
diff changeset
366 if not flags.item:
38e1e29c48e9 jp (base): added C.ITEM pubsub flag when --item is required
Goffi <goffi@goffi.org>
parents: 2550
diff changeset
367 default = defaults.pop(u'item', _(u'last item'))
38e1e29c48e9 jp (base): added C.ITEM pubsub flag when --item is required
Goffi <goffi@goffi.org>
parents: 2550
diff changeset
368 if default is not None:
38e1e29c48e9 jp (base): added C.ITEM pubsub flag when --item is required
Goffi <goffi@goffi.org>
parents: 2550
diff changeset
369 item_help += _(u" (DEFAULT: {default})".format(default=default))
38e1e29c48e9 jp (base): added C.ITEM pubsub flag when --item is required
Goffi <goffi@goffi.org>
parents: 2550
diff changeset
370 pubsub_group.add_argument("-i", "--item", type=unicode_decoder, help=item_help)
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
371 pubsub_group.add_argument("-L", "--last-item", action='store_true', help=_(u'retrieve last item'))
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
372 elif flags.multi_items:
2764
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
373 # mutiple items, this activate several features: max-items, RSM, MAM
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
374 # and Orbder-by
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
375 pubsub_group.add_argument("-i", "--item", type=unicode_decoder, action='append', dest='items', default=[], help=_(u"items to retrieve (DEFAULT: all)"))
2539
dcc77f23e370 jp (base): add --max option:
Goffi <goffi@goffi.org>
parents: 2538
diff changeset
376 if not flags.no_max:
2761
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
377 pubsub_group.add_argument(
2764
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
378 "-M", "--max-items", dest="max", type=int, default=10,
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
379 help=_(u"maximum number of items to get ({no_limit} to get all items)"
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
380 .format(no_limit=C.NO_LIMIT)))
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
381
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
382 # RSM
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
383
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
384 # FIXME: it could be possible to no duplicate max (between pubsub max-items
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
385 # and RSM max)should not be duplicated, RSM could be used when
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
386 # available and pubsub max otherwise
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
387 pubsub_group.add_argument(
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
388 "-m", "--max", dest="rsm_max", type=int, default=10,
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
389 help=_(u"maximum number of items to get per page (DEFAULT: 10)"))
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
390 rsm_page_group = pubsub_group.add_mutually_exclusive_group()
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
391 rsm_page_group.add_argument(
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
392 "-a", "--after", dest="rsm_after", type=unicode_decoder,
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
393 help=_(u"find page after this item"), metavar='ITEM_ID')
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
394 rsm_page_group.add_argument(
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
395 "-b", "--before", dest="rsm_before", type=unicode_decoder,
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
396 help=_(u"find page before this item"), metavar='ITEM_ID')
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
397 rsm_page_group.add_argument(
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
398 "--index", dest="rsm_index", type=int,
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
399 help=_(u"index of the page to retrieve"))
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
400
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
401
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
402 # MAM
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
403
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
404 pubsub_group.add_argument(
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
405 "-f", "--filter", dest='mam_filters', type=unicode_decoder, nargs=2,
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
406 action='append', default=[], help=_(u"MAM filters to use"),
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
407 metavar=(u"FILTER_NAME", u"VALUE")
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
408 )
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
409
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
410 # Order-By
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
411
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
412 # TODO: order-by should be a list to handle several levels of ordering
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
413 # but this is not yet done in SàT (and not really useful with
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
414 # current specifications, as only "creation" and "modification" are
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
415 # available)
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
416 pubsub_group.add_argument(
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
417 "-o", "--order-by", choices=[C.ORDER_BY_CREATION,
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
418 C.ORDER_BY_MODIFICATION],
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
419 help=_(u"how items should be ordered"))
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
420
2615
b4ecbcc2fd08 tools (misc): do not remove flags while using them anymore, instead added "all_used" and "unused" properties in FlagsHandler
Goffi <goffi@goffi.org>
parents: 2614
diff changeset
421 if not flags.all_used:
b4ecbcc2fd08 tools (misc): do not remove flags while using them anymore, instead added "all_used" and "unused" properties in FlagsHandler
Goffi <goffi@goffi.org>
parents: 2614
diff changeset
422 raise exceptions.InternalError('unknown flags: {flags}'.format(flags=u', '.join(flags.unused)))
2547
2d69a0afe039 jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents: 2539
diff changeset
423 if defaults:
2d69a0afe039 jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents: 2539
diff changeset
424 raise exceptions.InternalError('unused defaults: {defaults}'.format(defaults=defaults))
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
425
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
426 return parent
2235
4db836386641 jp: added use_pubsub and use_pubsub_node_req arguments to CommandBase
Goffi <goffi@goffi.org>
parents: 2213
diff changeset
427
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
428 def add_parser_options(self):
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
429 self.parser.add_argument('--version', action='version', version=("%(name)s %(version)s %(copyleft)s" % {'name': PROG_NAME, 'version': self.version, 'copyleft': COPYLEFT}))
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
430
2189
a25a256688e2 jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents: 2188
diff changeset
431 def register_output(self, type_, name, callback, description="", default=False):
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
432 if type_ not in C.OUTPUT_TYPES:
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
433 log.error(u"Invalid output type {}".format(type_))
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
434 return
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
435 self._outputs[type_][name] = {'callback': callback,
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
436 'description': description
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
437 }
2189
a25a256688e2 jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents: 2188
diff changeset
438 if default:
a25a256688e2 jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents: 2188
diff changeset
439 if type_ in self.default_output:
a25a256688e2 jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents: 2188
diff changeset
440 self.disp(_(u'there is already a default output for {}, ignoring new one').format(type_))
a25a256688e2 jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents: 2188
diff changeset
441 else:
a25a256688e2 jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents: 2188
diff changeset
442 self.default_output[type_] = name
a25a256688e2 jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents: 2188
diff changeset
443
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
444
2187
4ec72927a222 jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents: 2178
diff changeset
445 def parse_output_options(self):
4ec72927a222 jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents: 2178
diff changeset
446 options = self.command.args.output_opts
4ec72927a222 jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents: 2178
diff changeset
447 options_dict = {}
4ec72927a222 jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents: 2178
diff changeset
448 for option in options:
4ec72927a222 jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents: 2178
diff changeset
449 try:
4ec72927a222 jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents: 2178
diff changeset
450 key, value = option.split(u'=', 1)
4ec72927a222 jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents: 2178
diff changeset
451 except ValueError:
4ec72927a222 jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents: 2178
diff changeset
452 key, value = option, None
4ec72927a222 jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents: 2178
diff changeset
453 options_dict[key.strip()] = value.strip() if value is not None else None
4ec72927a222 jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents: 2178
diff changeset
454 return options_dict
4ec72927a222 jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents: 2178
diff changeset
455
4ec72927a222 jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents: 2178
diff changeset
456 def check_output_options(self, accepted_set, options):
4ec72927a222 jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents: 2178
diff changeset
457 if not accepted_set.issuperset(options):
4ec72927a222 jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents: 2178
diff changeset
458 self.disp(u"The following output options are invalid: {invalid_options}".format(
4ec72927a222 jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents: 2178
diff changeset
459 invalid_options = u', '.join(set(options).difference(accepted_set))),
4ec72927a222 jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents: 2178
diff changeset
460 error=True)
4ec72927a222 jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents: 2178
diff changeset
461 self.quit(C.EXIT_BAD_ARG)
4ec72927a222 jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents: 2178
diff changeset
462
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
463 def import_plugins(self):
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
464 """Automaticaly import commands and outputs in jp
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
465
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
466 looks from modules names cmd_*.py in jp path and import them
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
467 """
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
468 path = os.path.dirname(sat_frontends.jp.__file__)
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
469 # XXX: outputs must be imported before commands as they are used for arguments
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
470 for type_, pattern in ((C.PLUGIN_OUTPUT, 'output_*.py'), (C.PLUGIN_CMD, 'cmd_*.py')):
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
471 modules = (os.path.splitext(module)[0] for module in map(os.path.basename, iglob(os.path.join(path, pattern))))
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
472 for module_name in modules:
2162
c9a67eb5bf72 jp (base): improved module import:
Goffi <goffi@goffi.org>
parents: 2161
diff changeset
473 module_path = "sat_frontends.jp." + module_name
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
474 try:
2162
c9a67eb5bf72 jp (base): improved module import:
Goffi <goffi@goffi.org>
parents: 2161
diff changeset
475 module = import_module(module_path)
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
476 self.import_plugin_module(module, type_)
2178
b9bfc45cea22 jp (base): added generic errback to CommandBase + show error message on plugin ImportError
Goffi <goffi@goffi.org>
parents: 2168
diff changeset
477 except ImportError as e:
2188
052d560d0dce jp (base): replaced log.warning by stderr prints
Goffi <goffi@goffi.org>
parents: 2187
diff changeset
478 self.disp(_(u"Can't import {module_path} plugin, ignoring it: {msg}".format(
2178
b9bfc45cea22 jp (base): added generic errback to CommandBase + show error message on plugin ImportError
Goffi <goffi@goffi.org>
parents: 2168
diff changeset
479 module_path = module_path,
2188
052d560d0dce jp (base): replaced log.warning by stderr prints
Goffi <goffi@goffi.org>
parents: 2187
diff changeset
480 msg = e)), error=True)
2162
c9a67eb5bf72 jp (base): improved module import:
Goffi <goffi@goffi.org>
parents: 2161
diff changeset
481 except exceptions.CancelError:
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
482 continue
2162
c9a67eb5bf72 jp (base): improved module import:
Goffi <goffi@goffi.org>
parents: 2161
diff changeset
483 except exceptions.MissingModule as e:
2188
052d560d0dce jp (base): replaced log.warning by stderr prints
Goffi <goffi@goffi.org>
parents: 2187
diff changeset
484 self.disp(_(u"Missing module for plugin {name}: {missing}".format(
2162
c9a67eb5bf72 jp (base): improved module import:
Goffi <goffi@goffi.org>
parents: 2161
diff changeset
485 name = module_path,
2188
052d560d0dce jp (base): replaced log.warning by stderr prints
Goffi <goffi@goffi.org>
parents: 2187
diff changeset
486 missing = e)), error=True)
2162
c9a67eb5bf72 jp (base): improved module import:
Goffi <goffi@goffi.org>
parents: 2161
diff changeset
487
0
goffi@necton2
parents:
diff changeset
488
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
489 def import_plugin_module(self, module, type_):
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
490 """add commands or outpus from a module to jp
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
491
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
492 @param module: module containing commands or outputs
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
493 @param type_(str): one of C_PLUGIN_*
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
494 """
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
495 try:
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
496 class_names = getattr(module, '__{}__'.format(type_))
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
497 except AttributeError:
2188
052d560d0dce jp (base): replaced log.warning by stderr prints
Goffi <goffi@goffi.org>
parents: 2187
diff changeset
498 log.disp(_(u"Invalid plugin module [{type}] {module}").format(type=type_, module=module), error=True)
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
499 raise ImportError
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
500 else:
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
501 for class_name in class_names:
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
502 cls = getattr(module, class_name)
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
503 cls(self)
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
504
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
505 def get_xmpp_uri_from_http(self, http_url):
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
506 """parse HTML page at http(s) URL, and looks for xmpp: uri"""
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
507 if http_url.startswith('https'):
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
508 scheme = u'https'
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
509 elif http_url.startswith('http'):
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
510 scheme = u'http'
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
511 else:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
512 raise exceptions.InternalError(u'An HTTP scheme is expected in this method')
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
513 self.disp(u"{scheme} URL found, trying to find associated xmpp: URI".format(scheme=scheme.upper()),1)
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
514 # HTTP URL, we try to find xmpp: links
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
515 try:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
516 from lxml import etree
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
517 except ImportError:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
518 self.disp(u"lxml module must be installed to use http(s) scheme, please install it with \"pip install lxml\"", error=True)
2672
0bed6df9ee5d jp: fixed bad call to quit
Goffi <goffi@goffi.org>
parents: 2671
diff changeset
519 self.quit(1)
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
520 import urllib2
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
521 parser = etree.HTMLParser()
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
522 try:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
523 root = etree.parse(urllib2.urlopen(http_url), parser)
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
524 except etree.XMLSyntaxError as e:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
525 self.disp(_(u"Can't parse HTML page : {msg}").format(msg=e))
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
526 links = []
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
527 else:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
528 links = root.xpath("//link[@rel='alternate' and starts-with(@href, 'xmpp:')]")
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
529 if not links:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
530 self.disp(u'Could not find alternate "xmpp:" URI, can\'t find associated XMPP PubSub node/item', error=True)
2672
0bed6df9ee5d jp: fixed bad call to quit
Goffi <goffi@goffi.org>
parents: 2671
diff changeset
531 self.quit(1)
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
532 xmpp_uri = links[0].get('href')
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
533 return xmpp_uri
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
534
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
535 def parse_pubsub_args(self):
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
536 if self.args.pubsub_url is not None:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
537 url = self.args.pubsub_url
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
538
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
539 if url.startswith('http'):
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
540 # http(s) URL, we try to retrieve xmpp one from there
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
541 url = self.get_xmpp_uri_from_http(url)
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
542
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
543 try:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
544 uri_data = uri.parseXMPPUri(url)
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
545 except ValueError:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
546 self.parser.error(_(u'invalid XMPP URL: {url}').format(url=url))
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
547 else:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
548 if uri_data[u'type'] == 'pubsub':
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
549 # URL is alright, we only set data not already set by other options
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
550 if not self.args.service:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
551 self.args.service = uri_data[u'path']
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
552 if not self.args.node:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
553 self.args.node = uri_data[u'node']
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
554 uri_item = uri_data.get(u'item')
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
555 if uri_item:
2556
340128e0b354 jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents: 2552
diff changeset
556 # there is an item in URI
340128e0b354 jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents: 2552
diff changeset
557 # we use it only if item is not already set
340128e0b354 jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents: 2552
diff changeset
558 # and item_last is not used either
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
559 try:
2556
340128e0b354 jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents: 2552
diff changeset
560 item = self.args.item
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
561 except AttributeError:
2609
75d2ac872965 jp (base): do not crash when item is specified (e.g. in URL) but not needed in command, but display a message on stderr.
Goffi <goffi@goffi.org>
parents: 2588
diff changeset
562 try:
75d2ac872965 jp (base): do not crash when item is specified (e.g. in URL) but not needed in command, but display a message on stderr.
Goffi <goffi@goffi.org>
parents: 2588
diff changeset
563 items = self.args.items
75d2ac872965 jp (base): do not crash when item is specified (e.g. in URL) but not needed in command, but display a message on stderr.
Goffi <goffi@goffi.org>
parents: 2588
diff changeset
564 except AttributeError:
75d2ac872965 jp (base): do not crash when item is specified (e.g. in URL) but not needed in command, but display a message on stderr.
Goffi <goffi@goffi.org>
parents: 2588
diff changeset
565 self.disp(_(u"item specified in URL but not needed in command, ignoring it"), error=True)
75d2ac872965 jp (base): do not crash when item is specified (e.g. in URL) but not needed in command, but display a message on stderr.
Goffi <goffi@goffi.org>
parents: 2588
diff changeset
566 else:
75d2ac872965 jp (base): do not crash when item is specified (e.g. in URL) but not needed in command, but display a message on stderr.
Goffi <goffi@goffi.org>
parents: 2588
diff changeset
567 if not items:
75d2ac872965 jp (base): do not crash when item is specified (e.g. in URL) but not needed in command, but display a message on stderr.
Goffi <goffi@goffi.org>
parents: 2588
diff changeset
568 self.args.items = [uri_item]
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
569 else:
2556
340128e0b354 jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents: 2552
diff changeset
570 if not item:
340128e0b354 jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents: 2552
diff changeset
571 try:
340128e0b354 jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents: 2552
diff changeset
572 item_last = self.args.item_last
340128e0b354 jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents: 2552
diff changeset
573 except AttributeError:
340128e0b354 jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents: 2552
diff changeset
574 item_last = False
340128e0b354 jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents: 2552
diff changeset
575 if not item_last:
340128e0b354 jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents: 2552
diff changeset
576 self.args.item = uri_item
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
577 else:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
578 self.parser.error(_(u'XMPP URL is not a pubsub one: {url}').format(url=url))
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
579 flags = self.args._cmd._pubsub_flags
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
580 # we check required arguments here instead of using add_arguments' required option
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
581 # because the required argument can be set in URL
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
582 if C.SERVICE in flags and not self.args.service:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
583 self.parser.error(_(u"argument -s/--service is required"))
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
584 if C.NODE in flags and not self.args.node:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
585 self.parser.error(_(u"argument -n/--node is required"))
2552
38e1e29c48e9 jp (base): added C.ITEM pubsub flag when --item is required
Goffi <goffi@goffi.org>
parents: 2550
diff changeset
586 if C.ITEM in flags and not self.args.item:
38e1e29c48e9 jp (base): added C.ITEM pubsub flag when --item is required
Goffi <goffi@goffi.org>
parents: 2550
diff changeset
587 self.parser.error(_(u"argument -i/--item is required"))
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
588
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
589 # FIXME: mutually groups can't be nested in a group and don't support title
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
590 # so we check conflict here. This may be fixed in Python 3, to be checked
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
591 try:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
592 if self.args.item and self.args.item_last:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
593 self.parser.error(_(u"--item and --item-last can't be used at the same time"))
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
594 except AttributeError:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
595 pass
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
596
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents: 2277
diff changeset
597 def run(self, args=None, namespace=None):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents: 2277
diff changeset
598 self.args = self.parser.parse_args(args, namespace=None)
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
599 if self.args._cmd._use_pubsub:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
600 self.parse_pubsub_args()
1884
0fe69871b71f jp: moved KeyboardInterrupt catch one level higher, so it is also catched if the loop is not started
Goffi <goffi@goffi.org>
parents: 1872
diff changeset
601 try:
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
602 self.args._cmd.run()
1884
0fe69871b71f jp: moved KeyboardInterrupt catch one level higher, so it is also catched if the loop is not started
Goffi <goffi@goffi.org>
parents: 1872
diff changeset
603 if self._need_loop or self._auto_loop:
0fe69871b71f jp: moved KeyboardInterrupt catch one level higher, so it is also catched if the loop is not started
Goffi <goffi@goffi.org>
parents: 1872
diff changeset
604 self._start_loop()
0fe69871b71f jp: moved KeyboardInterrupt catch one level higher, so it is also catched if the loop is not started
Goffi <goffi@goffi.org>
parents: 1872
diff changeset
605 except KeyboardInterrupt:
2671
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2615
diff changeset
606 self.disp(_("User interruption: good bye"))
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
607
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
608 def _start_loop(self):
2098
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
609 self.loop = JPLoop()
1884
0fe69871b71f jp: moved KeyboardInterrupt catch one level higher, so it is also catched if the loop is not started
Goffi <goffi@goffi.org>
parents: 1872
diff changeset
610 self.loop.run()
0
goffi@necton2
parents:
diff changeset
611
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
612 def stop_loop(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
613 try:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
614 self.loop.quit()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
615 except AttributeError:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
616 pass
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
617
2550
1d754bc14381 jp (base): new confirmOrQuit helper method to ask confirmation to user, and quit if he cancel
Goffi <goffi@goffi.org>
parents: 2547
diff changeset
618 def confirmOrQuit(self, message, cancel_message=_(u"action cancelled by user")):
1d754bc14381 jp (base): new confirmOrQuit helper method to ask confirmation to user, and quit if he cancel
Goffi <goffi@goffi.org>
parents: 2547
diff changeset
619 """Request user to confirm action, and quit if he doesn't"""
1d754bc14381 jp (base): new confirmOrQuit helper method to ask confirmation to user, and quit if he cancel
Goffi <goffi@goffi.org>
parents: 2547
diff changeset
620
1d754bc14381 jp (base): new confirmOrQuit helper method to ask confirmation to user, and quit if he cancel
Goffi <goffi@goffi.org>
parents: 2547
diff changeset
621 res = raw_input("{} (y/N)? ".format(message))
1d754bc14381 jp (base): new confirmOrQuit helper method to ask confirmation to user, and quit if he cancel
Goffi <goffi@goffi.org>
parents: 2547
diff changeset
622 if res not in ("y", "Y"):
1d754bc14381 jp (base): new confirmOrQuit helper method to ask confirmation to user, and quit if he cancel
Goffi <goffi@goffi.org>
parents: 2547
diff changeset
623 self.disp(cancel_message)
1d754bc14381 jp (base): new confirmOrQuit helper method to ask confirmation to user, and quit if he cancel
Goffi <goffi@goffi.org>
parents: 2547
diff changeset
624 self.quit(C.EXIT_USER_CANCELLED)
1d754bc14381 jp (base): new confirmOrQuit helper method to ask confirmation to user, and quit if he cancel
Goffi <goffi@goffi.org>
parents: 2547
diff changeset
625
1604
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
626 def quitFromSignal(self, errcode=0):
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
627 """Same as self.quit, but from a signal handler
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
628
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
629 /!\: return must be used after calling this method !
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
630 """
1863
b2ddd7f5dcdf jp (base): refactored need_loop so it is set only when the command is run. It can now be set in __init__ methods of commands classes
Goffi <goffi@goffi.org>
parents: 1862
diff changeset
631 assert self._need_loop
1918
01d56efd488b jp: fixed traceback shown on a normal quit when a progression is finished
Goffi <goffi@goffi.org>
parents: 1884
diff changeset
632 # XXX: python-dbus will show a traceback if we exit in a signal handler
1604
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
633 # so we use this little timeout trick to avoid it
2098
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
634 self.loop.call_later(0, self.quit, errcode)
1604
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
635
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
636 def quit(self, errcode=0):
1604
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
637 # first the onQuitCallbacks
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
638 try:
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
639 callbacks_list = self._onQuitCallbacks
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
640 except AttributeError:
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
641 pass
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
642 else:
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
643 for callback, args, kwargs in callbacks_list:
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
644 callback(*args, **kwargs)
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
645
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
646 self.stop_loop()
2313
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2312
diff changeset
647 sys.exit(errcode)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
648
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
649 def check_jids(self, jids):
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
650 """Check jids validity, transform roster name to corresponding jids
110
cb904fa7de3c jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents: 70
diff changeset
651
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
652 @param profile: profile name
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
653 @param jids: list of jids
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
654 @return: List of jids
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
655
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
656 """
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
657 names2jid = {}
493
b7c4bb2c0668 jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents: 480
diff changeset
658 nodes2jid = {}
393
393b35aa86d2 jp: added --connect option
Goffi <goffi@goffi.org>
parents: 391
diff changeset
659
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
660 for contact in self.bridge.getContacts(self.profile):
1395
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
661 jid_s, attr, groups = contact
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
662 _jid = JID(jid_s)
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
663 try:
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
664 names2jid[attr["name"].lower()] = jid_s
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
665 except KeyError:
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
666 pass
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
667
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
668 if _jid.node:
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
669 nodes2jid[_jid.node.lower()] = jid_s
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
670
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
671 def expand_jid(jid):
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
672 _jid = jid.lower()
493
b7c4bb2c0668 jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents: 480
diff changeset
673 if _jid in names2jid:
b7c4bb2c0668 jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents: 480
diff changeset
674 expanded = names2jid[_jid]
b7c4bb2c0668 jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents: 480
diff changeset
675 elif _jid in nodes2jid:
b7c4bb2c0668 jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents: 480
diff changeset
676 expanded = nodes2jid[_jid]
b7c4bb2c0668 jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents: 480
diff changeset
677 else:
b7c4bb2c0668 jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents: 480
diff changeset
678 expanded = jid
2588
4011e4ee3151 jp (base): fixed jid encoding
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
679 return expanded
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
680
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
681 def check(jid):
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
682 if not jid.is_valid:
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
683 log.error (_("%s is not a valid JID !"), jid)
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
684 self.quit(1)
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
685
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
686 dest_jids=[]
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
687 try:
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
688 for i in range(len(jids)):
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
689 dest_jids.append(expand_jid(jids[i]))
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
690 check(dest_jids[i])
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
691 except AttributeError:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
692 pass
0
goffi@necton2
parents:
diff changeset
693
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
694 return dest_jids
0
goffi@necton2
parents:
diff changeset
695
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
696 def connect_profile(self, callback):
1588
823a385235ef jp: fixed bad --connect option check
Goffi <goffi@goffi.org>
parents: 1544
diff changeset
697 """ Check if the profile is connected and do it if requested
1401
265ff2bd8d67 jp: fixed crash on commands using profile without "connect" option
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
698
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
699 @param callback: method to call when profile is connected
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
700 @exit: - 1 when profile is not connected and --connect is not set
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
701 - 1 when the profile doesn't exists
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
702 - 1 when there is a connection error
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
703 """
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
704 # FIXME: need better exit codes
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
705
971
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 970
diff changeset
706 def cant_connect(failure):
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
707 log.error(_(u"Can't connect profile: {reason}").format(reason=failure))
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
708 self.quit(1)
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
709
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
710 def cant_start_session(failure):
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
711 log.error(_(u"Can't start {profile}'s session: {reason}").format(profile=self.profile, reason=failure))
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
712 self.quit(1)
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
713
2168
255830fdb80b core, bridge: renamed getProfileName to profileNameGet according to new conventions
Goffi <goffi@goffi.org>
parents: 2162
diff changeset
714 self.profile = self.bridge.profileNameGet(self.args.profile)
0
goffi@necton2
parents:
diff changeset
715
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
716 if not self.profile:
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
717 log.error(_("The profile [{profile}] doesn't exist").format(profile=self.args.profile))
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
718 self.quit(1)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
719
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
720 try:
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
721 start_session = self.args.start_session
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
722 except AttributeError:
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
723 pass
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
724 else:
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
725 if start_session:
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
726 self.bridge.profileStartSession(self.args.pwd, self.profile, lambda dummy: callback(), cant_start_session)
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
727 self._auto_loop = True
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
728 return
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
729 elif not self.bridge.profileIsSessionStarted(self.profile):
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
730 if not self.args.connect:
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
731 log.error(_(u"Session for [{profile}] is not started, please start it before using jp, or use either --start-session or --connect option").format(profile=self.profile))
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
732 self.quit(1)
2692
7d4679ee7ded jp (base): fixed connection when start_session arg exists but is False
Goffi <goffi@goffi.org>
parents: 2672
diff changeset
733 elif not getattr(self.args, "connect", False):
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
734 callback()
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
735 return
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
736
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
737
1401
265ff2bd8d67 jp: fixed crash on commands using profile without "connect" option
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
738 if not hasattr(self.args, 'connect'):
1588
823a385235ef jp: fixed bad --connect option check
Goffi <goffi@goffi.org>
parents: 1544
diff changeset
739 # a profile can be present without connect option (e.g. on profile creation/deletion)
1401
265ff2bd8d67 jp: fixed crash on commands using profile without "connect" option
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
740 return
1588
823a385235ef jp: fixed bad --connect option check
Goffi <goffi@goffi.org>
parents: 1544
diff changeset
741 elif self.args.connect is True: # if connection is asked, we connect the profile
2142
be96beb7ca14 core, bridge, frontends: renamed asyncConnect to connect, and added options parameters (not used yet)
Goffi <goffi@goffi.org>
parents: 2119
diff changeset
742 self.bridge.connect(self.profile, self.args.pwd, {}, lambda dummy: callback(), cant_connect)
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
743 self._auto_loop = True
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
744 return
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
745 else:
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
746 if not self.bridge.isConnected(self.profile):
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
747 log.error(_(u"Profile [{profile}] is not connected, please connect it before using jp, or use --connect option").format(profile=self.profile))
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
748 self.quit(1)
0
goffi@necton2
parents:
diff changeset
749
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
750 callback()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
751
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
752 def get_full_jid(self, param_jid):
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
753 """Return the full jid if possible (add main resource when find a bare jid)"""
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
754 _jid = JID(param_jid)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
755 if not _jid.resource:
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
756 #if the resource is not given, we try to add the main resource
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
757 main_resource = self.bridge.getMainResource(param_jid, self.profile)
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
758 if main_resource:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
759 return "%s/%s" % (_jid.bare, main_resource)
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
760 return param_jid
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
761
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
762
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
763 class CommandBase(object):
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
764
2155
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
765 def __init__(self, host, name, use_profile=True, use_output=False, extra_outputs=None,
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
766 need_connect=None, help=None, **kwargs):
2098
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
767 """Initialise CommandBase
e0066920a661 primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 2086
diff changeset
768
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
769 @param host: Jp instance
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
770 @param name(unicode): name of the new command
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
771 @param use_profile(bool): if True, add profile selection/connection commands
2114
dc5d214f0a3b jp (info/session): added a command to get data on current session
Goffi <goffi@goffi.org>
parents: 2098
diff changeset
772 @param use_output(bool, unicode): if not False, add --output option
2155
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
773 @param extra_outputs(dict): list of command specific outputs:
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
774 key is output name ("default" to use as main output)
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
775 value is a callable which will format the output (data will be used as only argument)
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
776 if a key already exists with normal outputs, the extra one will be used
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
777 @param need_connect(bool, None): True if profile connection is needed
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
778 False else (profile session must still be started)
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
779 None to set auto value (i.e. True if use_profile is set)
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
780 Can't be set if use_profile is False
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
781 @param help(unicode): help message to display
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
782 @param **kwargs: args passed to ArgumentParser
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
783 use_* are handled directly, they can be:
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
784 - use_progress(bool): if True, add progress bar activation option
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
785 progress* signals will be handled
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
786 - use_verbose(bool): if True, add verbosity option
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
787 - use_pubsub(bool): if True, add pubsub options
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
788 mandatory arguments are controlled by pubsub_req
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
789 - use_draft(bool): if True, add draft handling options
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
790 ** other arguments **
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
791 - pubsub_flags(iterable[unicode]): tuple of flags to set pubsub options, can be:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
792 C.SERVICE: service is required
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
793 C.NODE: node is required
2614
a5b96950b81a jp (event): fixed crash on missing item in get and inviteeGet by making it mandatory.
Goffi <goffi@goffi.org>
parents: 2609
diff changeset
794 C.ITEM: item is required
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
795 C.SINGLE_ITEM: only one item is allowed
1863
b2ddd7f5dcdf jp (base): refactored need_loop so it is set only when the command is run. It can now be set in __init__ methods of commands classes
Goffi <goffi@goffi.org>
parents: 1862
diff changeset
796 @attribute need_loop(bool): to set by commands when loop is needed
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
797 """
1863
b2ddd7f5dcdf jp (base): refactored need_loop so it is set only when the command is run. It can now be set in __init__ methods of commands classes
Goffi <goffi@goffi.org>
parents: 1862
diff changeset
798 self.need_loop = False # to be set by commands when loop is needed
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
799 try: # If we have subcommands, host is a CommandBase and we need to use host.host
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
800 self.host = host.host
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
801 except AttributeError:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
802 self.host = host
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
803
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
804 # --profile option
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
805 parents = kwargs.setdefault('parents', set())
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
806 if use_profile:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
807 #self.host.parents['profile'] is an ArgumentParser with profile connection arguments
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
808 if need_connect is None:
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
809 need_connect = True
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
810 parents.add(self.host.parents['profile' if need_connect else 'profile_session'])
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
811 else:
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
812 assert need_connect is None
1860
afc7f6328394 jp (base): use need_connect attribue to detect if connection is needed instead of self.args.profile, as profile argument can be used by any command even if use_profile is False
Goffi <goffi@goffi.org>
parents: 1840
diff changeset
813 self.need_connect = need_connect
afc7f6328394 jp (base): use need_connect attribue to detect if connection is needed instead of self.args.profile, as profile argument can be used by any command even if use_profile is False
Goffi <goffi@goffi.org>
parents: 1840
diff changeset
814 # from this point, self.need_connect is None if connection is not needed at all
afc7f6328394 jp (base): use need_connect attribue to detect if connection is needed instead of self.args.profile, as profile argument can be used by any command even if use_profile is False
Goffi <goffi@goffi.org>
parents: 1840
diff changeset
815 # False if session starting is needed, and True if full connection is needed
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
816
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
817 # --output option
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
818 if use_output:
2155
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
819 if extra_outputs is None:
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
820 extra_outputs = {}
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
821 self.extra_outputs = extra_outputs
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
822 if use_output == True:
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
823 use_output = C.OUTPUT_TEXT
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
824 assert use_output in C.OUTPUT_TYPES
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
825 self._output_type = use_output
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
826 output_parent = argparse.ArgumentParser(add_help=False)
2155
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
827 choices = set(self.host.getOutputChoices(use_output))
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
828 choices.update(extra_outputs)
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
829 if not choices:
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
830 raise exceptions.InternalError("No choice found for {} output type".format(use_output))
2189
a25a256688e2 jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents: 2188
diff changeset
831 try:
a25a256688e2 jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents: 2188
diff changeset
832 default = self.host.default_output[use_output]
a25a256688e2 jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents: 2188
diff changeset
833 except KeyError:
a25a256688e2 jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents: 2188
diff changeset
834 if u'default' in choices:
a25a256688e2 jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents: 2188
diff changeset
835 default = u'default'
a25a256688e2 jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents: 2188
diff changeset
836 elif u'simple' in choices:
a25a256688e2 jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents: 2188
diff changeset
837 default = u'simple'
a25a256688e2 jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents: 2188
diff changeset
838 else:
a25a256688e2 jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents: 2188
diff changeset
839 default = list(choices)[0]
2155
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
840 output_parent.add_argument('--output', '-O', choices=sorted(choices), default=default, help=_(u"select output format (default: {})".format(default)))
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
841 output_parent.add_argument('--output-option', '--oo', type=unicode_decoder, action="append", dest='output_opts', default=[], help=_(u"output specific option"))
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
842 parents.add(output_parent)
2155
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
843 else:
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
844 assert extra_outputs is None
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
845
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
846 self._use_pubsub = kwargs.pop('use_pubsub', False)
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
847 if self._use_pubsub:
2538
0a22dc80d671 jp (base): fixed use_pubsub when pubsub_flags is not needed
Goffi <goffi@goffi.org>
parents: 2532
diff changeset
848 flags = kwargs.pop('pubsub_flags', [])
2547
2d69a0afe039 jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents: 2539
diff changeset
849 defaults = kwargs.pop('pubsub_defaults', {})
2d69a0afe039 jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents: 2539
diff changeset
850 parents.add(self.host.make_pubsub_group(flags, defaults))
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
851 self._pubsub_flags = flags
2235
4db836386641 jp: added use_pubsub and use_pubsub_node_req arguments to CommandBase
Goffi <goffi@goffi.org>
parents: 2213
diff changeset
852
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
853 # other common options
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
854 use_opts = {k:v for k,v in kwargs.iteritems() if k.startswith('use_')}
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
855 for param, do_use in use_opts.iteritems():
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
856 opt=param[4:] # if param is use_verbose, opt is verbose
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
857 if opt not in self.host.parents:
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
858 raise exceptions.InternalError(u"Unknown parent option {}".format(opt))
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
859 del kwargs[param]
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
860 if do_use:
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
861 parents.add(self.host.parents[opt])
1600
8d41cd4da2f6 jp: added a --verbose command
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
862
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
863 self.parser = host.subparsers.add_parser(name, help=help, **kwargs)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
864 if hasattr(self, "subcommands"):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
865 self.subparsers = self.parser.add_subparsers()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
866 else:
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2490
diff changeset
867 self.parser.set_defaults(_cmd=self)
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
868 self.add_parser_options()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
869
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
870 @property
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
871 def args(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
872 return self.host.args
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
873
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
874 @property
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
875 def profile(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
876 return self.host.profile
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
877
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
878 @property
2490
b4bf282d6354 jp (base): added verbosity property to CommandBase
Goffi <goffi@goffi.org>
parents: 2486
diff changeset
879 def verbosity(self):
b4bf282d6354 jp (base): added verbosity property to CommandBase
Goffi <goffi@goffi.org>
parents: 2486
diff changeset
880 return self.host.verbosity
b4bf282d6354 jp (base): added verbosity property to CommandBase
Goffi <goffi@goffi.org>
parents: 2486
diff changeset
881
b4bf282d6354 jp (base): added verbosity property to CommandBase
Goffi <goffi@goffi.org>
parents: 2486
diff changeset
882 @property
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
883 def progress_id(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
884 return self.host.progress_id
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
885
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
886 @progress_id.setter
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
887 def progress_id(self, value):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
888 self.host.progress_id = value
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
889
1627
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
890 def progressStartedHandler(self, uid, metadata, profile):
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
891 if profile != self.profile:
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
892 return
1642
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
893 if self.progress_id is None:
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
894 # the progress started message can be received before the id
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
895 # so we keep progressStarted signals in cache to replay they
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
896 # when the progress_id is received
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
897 cache_data = (self.progressStartedHandler, uid, metadata, profile)
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
898 try:
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
899 self.host.progress_ids_cache.append(cache_data)
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
900 except AttributeError:
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
901 self.host.progress_ids_cache = [cache_data]
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
902 else:
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
903 if self.host.watch_progress and uid == self.progress_id:
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
904 self.onProgressStarted(metadata)
2237
fd4111075e00 jp: fixed progress
Goffi <goffi@goffi.org>
parents: 2235
diff changeset
905 self.host.loop.call_later(PROGRESS_DELAY, self.progressUpdate)
1627
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
906
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
907 def progressFinishedHandler(self, uid, metadata, profile):
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
908 if profile != self.profile:
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
909 return
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
910 if uid == self.progress_id:
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
911 try:
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
912 self.host.pbar.finish()
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
913 except AttributeError:
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
914 pass
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
915 self.onProgressFinished(metadata)
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
916 if self.host.quit_on_progress_end:
1918
01d56efd488b jp: fixed traceback shown on a normal quit when a progression is finished
Goffi <goffi@goffi.org>
parents: 1884
diff changeset
917 self.host.quitFromSignal()
1627
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
918
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
919 def progressErrorHandler(self, uid, message, profile):
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
920 if profile != self.profile:
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
921 return
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
922 if uid == self.progress_id:
1641
44a14f83e64b jp (base): fixed verbose option + there is a new line on ProgressErrorHandler only if the progress bar is actually displayed
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
923 if self.args.progress:
44a14f83e64b jp (base): fixed verbose option + there is a new line on ProgressErrorHandler only if the progress bar is actually displayed
Goffi <goffi@goffi.org>
parents: 1627
diff changeset
924 self.disp('') # progress is not finished, so we skip a line
1627
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
925 if self.host.quit_on_progress_end:
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
926 self.onProgressError(message)
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
927 self.host.quitFromSignal(1)
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
928
1832
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
929 def progressUpdate(self):
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
930 """This method is continualy called to update the progress bar"""
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
931 data = self.host.bridge.progressGet(self.progress_id, self.profile)
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
932 if data:
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
933 try:
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
934 size = data['size']
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
935 except KeyError:
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
936 self.disp(_(u"file size is not known, we can't show a progress bar"), 1, error=True)
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
937 return False
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
938 if self.host.pbar is None:
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
939 #first answer, we must construct the bar
2368
3865a772c360 jp (base): fixed CommandAnswering and progress bar
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
940 self.host.pbar = progressbar.ProgressBar(max_value=int(size),
3865a772c360 jp (base): fixed CommandAnswering and progress bar
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
941 widgets=[_(u"Progress: "),progressbar.Percentage(),
3865a772c360 jp (base): fixed CommandAnswering and progress bar
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
942 " ",
3865a772c360 jp (base): fixed CommandAnswering and progress bar
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
943 progressbar.Bar(),
3865a772c360 jp (base): fixed CommandAnswering and progress bar
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
944 " ",
3865a772c360 jp (base): fixed CommandAnswering and progress bar
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
945 progressbar.FileTransferSpeed(),
3865a772c360 jp (base): fixed CommandAnswering and progress bar
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
946 " ",
3865a772c360 jp (base): fixed CommandAnswering and progress bar
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
947 progressbar.ETA()])
1832
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
948 self.host.pbar.start()
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
949
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
950 self.host.pbar.update(int(data['position']))
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
951
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
952 elif self.host.pbar is not None:
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
953 return False
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
954
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
955 self.onProgressUpdate(data)
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
956
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
957 return True
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
958
1627
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
959 def onProgressStarted(self, metadata):
1832
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
960 """Called when progress has just started
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
961
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
962 can be overidden by a command
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
963 @param metadata(dict): metadata as sent by bridge.progressStarted
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
964 """
1627
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
965 self.disp(_(u"Operation started"), 2)
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
966
1832
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
967 def onProgressUpdate(self, metadata):
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
968 """Method called on each progress updata
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
969
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
970 can be overidden by a command to handle progress metadata
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
971 @para metadata(dict): metadata as returned by bridge.progressGet
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
972 """
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
973 pass
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
974
1627
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
975 def onProgressFinished(self, metadata):
1832
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
976 """Called when progress has just finished
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
977
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
978 can be overidden by a command
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
979 @param metadata(dict): metadata as sent by bridge.progressFinished
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
980 """
1627
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
981 self.disp(_(u"Operation successfully finished"), 2)
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
982
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
983 def onProgressError(self, error_msg):
1832
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
984 """Called when a progress failed
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
985
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
986 @param error_msg(unicode): error message as sent by bridge.progressError
39545dc527a1 jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
987 """
1627
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
988 self.disp(_(u"Error while doing operation: {}").format(error_msg), error=True)
5a641e7b858a jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents: 1623
diff changeset
989
2277
637886ac35f6 jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents: 2237
diff changeset
990 def disp(self, msg, verbosity=0, error=False, no_lf=False):
637886ac35f6 jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents: 2237
diff changeset
991 return self.host.disp(msg, verbosity, error, no_lf)
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
992
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
993 def output(self, data):
2193
33b82250eadd jp (base): raise InternalError if output is used when use_output is not declared
Goffi <goffi@goffi.org>
parents: 2189
diff changeset
994 try:
33b82250eadd jp (base): raise InternalError if output is used when use_output is not declared
Goffi <goffi@goffi.org>
parents: 2189
diff changeset
995 output_type = self._output_type
33b82250eadd jp (base): raise InternalError if output is used when use_output is not declared
Goffi <goffi@goffi.org>
parents: 2189
diff changeset
996 except AttributeError:
33b82250eadd jp (base): raise InternalError if output is used when use_output is not declared
Goffi <goffi@goffi.org>
parents: 2189
diff changeset
997 raise exceptions.InternalError(_(u'trying to use output when use_output has not been set'))
33b82250eadd jp (base): raise InternalError if output is used when use_output is not declared
Goffi <goffi@goffi.org>
parents: 2189
diff changeset
998 return self.host.output(output_type, self.args.output, self.extra_outputs, data)
1950
227a4e617549 jp: --output option:
Goffi <goffi@goffi.org>
parents: 1945
diff changeset
999
2457
49aa1e3a90b8 jp (base): added exitCb generic callback to display a message and quit
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
1000 def exitCb(self, msg=None):
49aa1e3a90b8 jp (base): added exitCb generic callback to display a message and quit
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
1001 """generic callback for success
49aa1e3a90b8 jp (base): added exitCb generic callback to display a message and quit
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
1002
49aa1e3a90b8 jp (base): added exitCb generic callback to display a message and quit
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
1003 optionally print a message, and quit
49aa1e3a90b8 jp (base): added exitCb generic callback to display a message and quit
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
1004 msg(None, unicode): if not None, print this message
49aa1e3a90b8 jp (base): added exitCb generic callback to display a message and quit
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
1005 """
49aa1e3a90b8 jp (base): added exitCb generic callback to display a message and quit
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
1006 if msg is not None:
49aa1e3a90b8 jp (base): added exitCb generic callback to display a message and quit
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
1007 self.disp(msg)
49aa1e3a90b8 jp (base): added exitCb generic callback to display a message and quit
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
1008 self.host.quit(C.EXIT_OK)
49aa1e3a90b8 jp (base): added exitCb generic callback to display a message and quit
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
1009
2213
1010054cf771 jp (base): added msg and exit_code argument to errback so it's more generic
Goffi <goffi@goffi.org>
parents: 2193
diff changeset
1010 def errback(self, failure_, msg=None, exit_code=C.EXIT_ERROR):
2178
b9bfc45cea22 jp (base): added generic errback to CommandBase + show error message on plugin ImportError
Goffi <goffi@goffi.org>
parents: 2168
diff changeset
1011 """generic callback for errbacks
b9bfc45cea22 jp (base): added generic errback to CommandBase + show error message on plugin ImportError
Goffi <goffi@goffi.org>
parents: 2168
diff changeset
1012
b9bfc45cea22 jp (base): added generic errback to CommandBase + show error message on plugin ImportError
Goffi <goffi@goffi.org>
parents: 2168
diff changeset
1013 display failure_ then quit with generic error
b9bfc45cea22 jp (base): added generic errback to CommandBase + show error message on plugin ImportError
Goffi <goffi@goffi.org>
parents: 2168
diff changeset
1014 @param failure_: arguments returned by errback
2213
1010054cf771 jp (base): added msg and exit_code argument to errback so it's more generic
Goffi <goffi@goffi.org>
parents: 2193
diff changeset
1015 @param msg(unicode, None): message template
1010054cf771 jp (base): added msg and exit_code argument to errback so it's more generic
Goffi <goffi@goffi.org>
parents: 2193
diff changeset
1016 use {} if you want to display failure message
1010054cf771 jp (base): added msg and exit_code argument to errback so it's more generic
Goffi <goffi@goffi.org>
parents: 2193
diff changeset
1017 @param exit_code(int): shell exit code
2178
b9bfc45cea22 jp (base): added generic errback to CommandBase + show error message on plugin ImportError
Goffi <goffi@goffi.org>
parents: 2168
diff changeset
1018 """
2213
1010054cf771 jp (base): added msg and exit_code argument to errback so it's more generic
Goffi <goffi@goffi.org>
parents: 2193
diff changeset
1019 if msg is None:
1010054cf771 jp (base): added msg and exit_code argument to errback so it's more generic
Goffi <goffi@goffi.org>
parents: 2193
diff changeset
1020 msg = _(u"error: {}")
1010054cf771 jp (base): added msg and exit_code argument to errback so it's more generic
Goffi <goffi@goffi.org>
parents: 2193
diff changeset
1021 self.disp(msg.format(failure_), error=True)
1010054cf771 jp (base): added msg and exit_code argument to errback so it's more generic
Goffi <goffi@goffi.org>
parents: 2193
diff changeset
1022 self.host.quit(exit_code)
2178
b9bfc45cea22 jp (base): added generic errback to CommandBase + show error message on plugin ImportError
Goffi <goffi@goffi.org>
parents: 2168
diff changeset
1023
2761
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
1024 def getPubsubExtra(self, extra=None):
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
1025 """Helper method to compute extra data from pubsub arguments
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
1026
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
1027 @param extra(None, dict): base extra dict, or None to generate a new one
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
1028 @return (dict): dict which can be used directly in the bridge for pubsub
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
1029 """
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
1030 if extra is None:
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
1031 extra = {}
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
1032 else:
2764
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1033 intersection = {C.KEY_ORDER_BY}.intersection(extra.keys())
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1034 if intersection:
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1035 raise exceptions.ConflictError(
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1036 u"given extra dict has conflicting keys with pubsub keys "
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1037 u"{intersection}".format(intersection=intersection))
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1038
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1039 # RSM
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1040
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1041 for attribute in (u'max', u'after', u'before', 'index'):
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1042 key = u'rsm_' + attribute
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1043 if key in extra:
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1044 raise exceptions.ConflictError(
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1045 u"This key already exists in extra: u{key}".format(key=key))
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1046 value = getattr(self.args, key, None)
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1047 if value is not None:
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1048 extra[key] = unicode(value)
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1049
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1050 # MAM
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1051
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1052 if hasattr(self.args, u'mam_filters'):
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1053 for key, value in self.args.mam_filters:
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1054 key = u'filter_' + key
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1055 if key in extra:
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1056 raise exceptions.ConflictError(
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1057 u"This key already exists in extra: u{key}".format(key=key))
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1058 extra[key] = value
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1059
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1060 # Order-By
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
1061
2761
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
1062 try:
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
1063 order_by = self.args.order_by
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
1064 except AttributeError:
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
1065 pass
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
1066 else:
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
1067 if order_by is not None:
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
1068 extra[C.KEY_ORDER_BY] = self.args.order_by
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
1069 return extra
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2706
diff changeset
1070
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1071 def add_parser_options(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1072 try:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1073 subcommands = self.subcommands
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1074 except AttributeError:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1075 # We don't have subcommands, the class need to implements add_parser_options
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1076 raise NotImplementedError
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1077
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1078 # now we add subcommands to ourself
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1079 for cls in subcommands:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1080 cls(self)
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
1081
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1082 def run(self):
1862
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1083 """this method is called when a command is actually run
1395
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
1084
1862
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1085 It set stuff like progression callbacks and profile connection
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1086 You should not overide this method: you should call self.start instead
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1087 """
2155
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
1088 # we keep a reference to run command, it may be useful e.g. for outputs
cee6561ad086 jp (core): added extra_outputs:
Goffi <goffi@goffi.org>
parents: 2151
diff changeset
1089 self.host.command = self
1863
b2ddd7f5dcdf jp (base): refactored need_loop so it is set only when the command is run. It can now be set in __init__ methods of commands classes
Goffi <goffi@goffi.org>
parents: 1862
diff changeset
1090 # host._need_loop is set here from our current value and not before
b2ddd7f5dcdf jp (base): refactored need_loop so it is set only when the command is run. It can now be set in __init__ methods of commands classes
Goffi <goffi@goffi.org>
parents: 1862
diff changeset
1091 # as the need_loop decision must be taken only by then running command
b2ddd7f5dcdf jp (base): refactored need_loop so it is set only when the command is run. It can now be set in __init__ methods of commands classes
Goffi <goffi@goffi.org>
parents: 1862
diff changeset
1092 self.host._need_loop = self.need_loop
b2ddd7f5dcdf jp (base): refactored need_loop so it is set only when the command is run. It can now be set in __init__ methods of commands classes
Goffi <goffi@goffi.org>
parents: 1862
diff changeset
1093
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1094 try:
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1095 show_progress = self.args.progress
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1096 except AttributeError:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1097 # the command doesn't use progress bar
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1098 pass
1395
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
1099 else:
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1100 if show_progress:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1101 self.host.watch_progress = True
1642
7ec7ce9cdc4c jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents: 1641
diff changeset
1102 # we need to register the following signal even if we don't display the progress bar
2119
469702b46768 jp (core): fixed signal registration for progressions
Goffi <goffi@goffi.org>
parents: 2114
diff changeset
1103 self.host.bridge.register_signal("progressStarted", self.progressStartedHandler)
469702b46768 jp (core): fixed signal registration for progressions
Goffi <goffi@goffi.org>
parents: 2114
diff changeset
1104 self.host.bridge.register_signal("progressFinished", self.progressFinishedHandler)
469702b46768 jp (core): fixed signal registration for progressions
Goffi <goffi@goffi.org>
parents: 2114
diff changeset
1105 self.host.bridge.register_signal("progressError", self.progressErrorHandler)
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1106
1862
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1107 if self.need_connect is not None:
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1108 self.host.connect_profile(self.connected)
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1109 else:
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1110 self.start()
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1111
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1112 def connected(self):
1862
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1113 """this method is called when profile is connected (or session is started)
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1114
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1115 this method is only called when use_profile is True
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1116 most of time you should override self.start instead of this method, but if loop
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1117 if not always needed depending on your arguments, you may override this method,
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1118 but don't forget to call the parent one (i.e. this one) after self.need_loop is set
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1119 """
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1120 if not self.need_loop:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1121 self.host.stop_loop()
1862
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1122 self.start()
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1123
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1124 def start(self):
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1125 """This is the starting point of the command, this method should be overriden
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1126
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1127 at this point, profile are connected if needed
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1128 """
6d9c87bdc452 jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents: 1860
diff changeset
1129 pass
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1130
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1131
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1132 class CommandAnswering(CommandBase):
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1133 """Specialised commands which answer to specific actions
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1134
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1135 to manage action_types answer,
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1136 """
2409
d2ff5ff3de77 jp (ad-hoc): new "list" and "run" commands:
Goffi <goffi@goffi.org>
parents: 2368
diff changeset
1137 action_callbacks = {} # XXX: set managed action types in a dict here:
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1138 # key is the action_type, value is the callable
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1139 # which will manage the answer. profile filtering is
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1140 # already managed when callback is called
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1141
2486
031aa3cc67ac jp (base): fixed assertion error when using CommandAnswering
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1142 def __init__(self, *args, **kwargs):
031aa3cc67ac jp (base): fixed assertion error when using CommandAnswering
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1143 super(CommandAnswering, self).__init__(*args, **kwargs)
031aa3cc67ac jp (base): fixed assertion error when using CommandAnswering
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1144 self.need_loop = True
031aa3cc67ac jp (base): fixed assertion error when using CommandAnswering
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1145
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1146 def onActionNew(self, action_data, action_id, security_limit, profile):
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 493
diff changeset
1147 if profile != self.profile:
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 493
diff changeset
1148 return
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1149 try:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1150 action_type = action_data['meta_type']
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1151 except KeyError:
1840
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1152 try:
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1153 xml_ui = action_data["xmlui"]
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1154 except KeyError:
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1155 pass
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1156 else:
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1157 self.onXMLUI(xml_ui)
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1158 else:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1159 try:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1160 callback = self.action_callbacks[action_type]
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1161 except KeyError:
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1162 pass
0
goffi@necton2
parents:
diff changeset
1163 else:
1606
de785fcf9a7b jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents: 1605
diff changeset
1164 callback(action_data, action_id, security_limit, profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
1165
1840
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1166 def onXMLUI(self, xml_ui):
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1167 """Display a dialog received from the backend.
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1168
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1169 @param xml_ui (unicode): dialog XML representation
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1170 """
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1171 # FIXME: we temporarily use ElementTree, but a real XMLUI managing module
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1172 # should be available in the future
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1173 # TODO: XMLUI module
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1174 ui = ET.fromstring(xml_ui.encode('utf-8'))
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1175 dialog = ui.find("dialog")
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1176 if dialog is not None:
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1177 self.disp(dialog.findtext("message"), error=dialog.get("level") == "error")
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1832
diff changeset
1178
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1179 def connected(self):
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
1180 """Auto reply to confirmations requests"""
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1181 self.need_loop = True
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
1182 super(CommandAnswering, self).connected()
2368
3865a772c360 jp (base): fixed CommandAnswering and progress bar
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
1183 self.host.bridge.register_signal("actionNew", self.onActionNew)
1623
ec48b35309dc jp: use the new actionsGet method
Goffi <goffi@goffi.org>
parents: 1606
diff changeset
1184 actions = self.host.bridge.actionsGet(self.profile)
ec48b35309dc jp: use the new actionsGet method
Goffi <goffi@goffi.org>
parents: 1606
diff changeset
1185 for action_data, action_id, security_limit in actions:
ec48b35309dc jp: use the new actionsGet method
Goffi <goffi@goffi.org>
parents: 1606
diff changeset
1186 self.onActionNew(action_data, action_id, security_limit, self.profile)