comparison sat_frontends/jp/cmd_adhoc.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents frontends/src/jp/cmd_adhoc.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # jp: a SAT command line tool
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 import base
21 from sat.core.i18n import _
22 from functools import partial
23 from sat_frontends.jp.constants import Const as C
24 from sat_frontends.jp import xmlui_manager
25
26 __commands__ = ["AdHoc"]
27
28 FLAG_LOOP = 'LOOP'
29 MAGIC_BAREJID = '@PROFILE_BAREJID@'
30
31
32 class Remote(base.CommandBase):
33 def __init__(self, host):
34 super(Remote, self).__init__(host, 'remote', use_verbose=True, help=_(u'remote control a software'))
35
36 def add_parser_options(self):
37 self.parser.add_argument("software", type=str, help=_(u"software name"))
38 self.parser.add_argument("-j", "--jids", type=base.unicode_decoder, nargs='*', default=[], help=_(u"jids allowed to use the command"))
39 self.parser.add_argument("-g", "--groups", type=base.unicode_decoder, nargs='*', default=[], help=_(u"groups allowed to use the command"))
40 self.parser.add_argument("--forbidden-groups", type=base.unicode_decoder, nargs='*', default=[], help=_(u"groups that are *NOT* allowed to use the command"))
41 self.parser.add_argument("--forbidden-jids", type=base.unicode_decoder, nargs='*', default=[], help=_(u"jids that are *NOT* allowed to use the command"))
42 self.parser.add_argument("-l", "--loop", action="store_true", help=_(u"loop on the commands"))
43
44 def start(self):
45 name = self.args.software.lower()
46 flags = []
47 magics = {jid for jid in self.args.jids if jid.count('@')>1}
48 magics.add(MAGIC_BAREJID)
49 jids = set(self.args.jids).difference(magics)
50 if self.args.loop:
51 flags.append(FLAG_LOOP)
52 bus_name, methods = self.host.bridge.adHocDBusAddAuto(name, jids, self.args.groups, magics,
53 self.args.forbidden_jids, self.args.forbidden_groups,
54 flags, self.profile)
55 if not bus_name:
56 self.disp(_("No bus name found"), 1)
57 return
58 self.disp(_("Bus name found: [%s]" % bus_name), 1)
59 for method in methods:
60 path, iface, command = method
61 self.disp(_("Command found: (path:%(path)s, iface: %(iface)s) [%(command)s]" % {'path': path,
62 'iface': iface,
63 'command': command
64 }),1)
65
66
67 class Run(base.CommandBase):
68 """Run an Ad-Hoc command"""
69
70 def __init__(self, host):
71 super(Run, self).__init__(host, 'run', use_verbose=True, help=_(u'run an Ad-Hoc command'))
72 self.need_loop=True
73
74 def add_parser_options(self):
75 self.parser.add_argument('-j', '--jid', type=base.unicode_decoder, default=u'', help=_(u"jid of the service (default: profile's server"))
76 self.parser.add_argument("-S", "--submit", action='append_const', const=xmlui_manager.SUBMIT, dest='workflow', help=_(u"submit form/page"))
77 self.parser.add_argument("-f",
78 "--field",
79 type=base.unicode_decoder,
80 action='append',
81 nargs=2,
82 dest='workflow',
83 metavar=(u"KEY", u"VALUE"),
84 help=_(u"field value"))
85 self.parser.add_argument('node', type=base.unicode_decoder, nargs='?', default=u'', help=_(u"node of the command (default: list commands)"))
86
87 def adHocRunCb(self, xmlui_raw):
88 xmlui = xmlui_manager.create(self.host, xmlui_raw)
89 workflow = self.args.workflow
90 xmlui.show(workflow)
91 if not workflow:
92 if xmlui.type == 'form':
93 xmlui.submitForm()
94 else:
95 self.host.quit()
96
97 def start(self):
98 self.host.bridge.adHocRun(
99 self.args.jid,
100 self.args.node,
101 self.profile,
102 callback=self.adHocRunCb,
103 errback=partial(self.errback,
104 msg=_(u"can't get ad-hoc commands list: {}"),
105 exit_code=C.EXIT_BRIDGE_ERRBACK))
106
107
108 class List(base.CommandBase):
109 """Run an Ad-Hoc command"""
110
111 def __init__(self, host):
112 super(List, self).__init__(host, 'list', use_verbose=True, help=_(u'list Ad-Hoc commands of a service'))
113 self.need_loop=True
114
115 def add_parser_options(self):
116 self.parser.add_argument('-j', '--jid', type=base.unicode_decoder, default=u'', help=_(u"jid of the service (default: profile's server"))
117
118 def adHocListCb(self, xmlui_raw):
119 xmlui = xmlui_manager.create(self.host, xmlui_raw)
120 xmlui.readonly = True
121 xmlui.show()
122 self.host.quit()
123
124 def start(self):
125 self.host.bridge.adHocList(
126 self.args.jid,
127 self.profile,
128 callback=self.adHocListCb,
129 errback=partial(self.errback,
130 msg=_(u"can't get ad-hoc commands list: {}"),
131 exit_code=C.EXIT_BRIDGE_ERRBACK))
132
133
134 class AdHoc(base.CommandBase):
135 subcommands = (Run, List, Remote)
136
137 def __init__(self, host):
138 super(AdHoc, self).__init__(host, 'ad-hoc', use_profile=False, help=_('Ad-hoc commands'))