Mercurial > libervia-backend
annotate sat_frontends/jp/cmd_profile.py @ 3039:a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
`pb` bridge can now be used with asyncio by instantiating AIOBridge.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 25 Sep 2019 08:53:38 +0200 |
parents | ab2696e34d29 |
children | fee60f17ebac |
rev | line source |
---|---|
1960 | 1 #!/usr/bin/env python2 |
815 | 2 # -*- coding: utf-8 -*- |
3 | |
4 # jp: a SAT command line tool | |
2771 | 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
815 | 6 |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
814 | 20 """This module permits to manage profiles. It can list, create, delete |
1199 | 21 and retrieve information about a profile.""" |
0 | 22 |
2492
fcf0ae8102b8
jp (profile/create): errback handling
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
23 from sat_frontends.jp.constants import Const as C |
2164
63d191c05ecd
jp (blog): set default template and data mapping for the new template output
Goffi <goffi@goffi.org>
parents:
2146
diff
changeset
|
24 from sat.core.log import getLogger |
63d191c05ecd
jp (blog): set default template and data mapping for the new template output
Goffi <goffi@goffi.org>
parents:
2146
diff
changeset
|
25 log = getLogger(__name__) |
771 | 26 from sat.core.i18n import _ |
817 | 27 from sat_frontends.jp import base |
2492
fcf0ae8102b8
jp (profile/create): errback handling
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
28 from functools import partial |
402
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
29 |
817 | 30 __commands__ = ["Profile"] |
31 | |
32 PROFILE_HELP = _('The name of the profile') | |
0 | 33 |
34 | |
1597 | 35 class ProfileConnect(base.CommandBase): |
36 """Dummy command to use profile_session parent, i.e. to be able to connect without doing anything else""" | |
37 | |
38 def __init__(self, host): | |
39 # it's weird to have a command named "connect" with need_connect=False, but it can be handy to be able | |
2336
a7438fe4e24d
jp (profile): added disconnect command
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
40 # to launch just the session, so some paradoxes don't hurt |
3028 | 41 super(ProfileConnect, self).__init__(host, 'connect', need_connect=False, help=('connect a profile')) |
1597 | 42 |
43 def add_parser_options(self): | |
44 pass | |
45 | |
46 | |
2336
a7438fe4e24d
jp (profile): added disconnect command
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
47 class ProfileDisconnect(base.CommandBase): |
a7438fe4e24d
jp (profile): added disconnect command
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
48 |
a7438fe4e24d
jp (profile): added disconnect command
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
49 def __init__(self, host): |
3028 | 50 super(ProfileDisconnect, self).__init__(host, 'disconnect', need_connect=False, help=('disconnect a profile')) |
2336
a7438fe4e24d
jp (profile): added disconnect command
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
51 self.need_loop = True |
a7438fe4e24d
jp (profile): added disconnect command
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
52 |
a7438fe4e24d
jp (profile): added disconnect command
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
53 def add_parser_options(self): |
a7438fe4e24d
jp (profile): added disconnect command
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
54 pass |
a7438fe4e24d
jp (profile): added disconnect command
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
55 |
a7438fe4e24d
jp (profile): added disconnect command
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
56 def start(self): |
a7438fe4e24d
jp (profile): added disconnect command
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
57 self.host.bridge.disconnect(self.args.profile, callback=self.host.quit) |
a7438fe4e24d
jp (profile): added disconnect command
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
58 |
a7438fe4e24d
jp (profile): added disconnect command
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
59 |
1596 | 60 class ProfileDefault(base.CommandBase): |
61 def __init__(self, host): | |
3028 | 62 super(ProfileDefault, self).__init__(host, 'default', use_profile=False, help=('print default profile')) |
1596 | 63 |
64 def add_parser_options(self): | |
65 pass | |
66 | |
1864
96ba685162f6
jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents:
1861
diff
changeset
|
67 def start(self): |
3028 | 68 print(self.host.bridge.profileNameGet('@DEFAULT@')) |
1596 | 69 |
70 | |
817 | 71 class ProfileDelete(base.CommandBase): |
72 def __init__(self, host): | |
3028 | 73 super(ProfileDelete, self).__init__(host, 'delete', use_profile=False, help=('delete a profile')) |
817 | 74 |
75 def add_parser_options(self): | |
76 self.parser.add_argument('profile', type=str, help=PROFILE_HELP) | |
3028 | 77 self.parser.add_argument('-f', '--force', action='store_true', help=_('delete profile without confirmation')) |
0 | 78 |
1864
96ba685162f6
jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents:
1861
diff
changeset
|
79 def start(self): |
2146
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
80 if self.args.profile not in self.host.bridge.profilesListGet(): |
1596 | 81 log.error("Profile %s doesn't exist." % self.args.profile) |
817 | 82 self.host.quit(1) |
1861
3b2a236fa743
jp (profile): added confirmation and --force arguement to profile/delete
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
83 if not self.args.force: |
3028 | 84 message = "Are you sure to delete profile [{}] ?".format(self.args.profile) |
85 res = input("{} (y/N)? ".format(message)) | |
1861
3b2a236fa743
jp (profile): added confirmation and --force arguement to profile/delete
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
86 if res not in ("y", "Y"): |
3028 | 87 self.disp(_("Profile deletion cancelled")) |
1861
3b2a236fa743
jp (profile): added confirmation and --force arguement to profile/delete
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
88 self.host.quit(2) |
3b2a236fa743
jp (profile): added confirmation and --force arguement to profile/delete
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
89 |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
90 self.host.bridge.asyncDeleteProfile(self.args.profile, callback=lambda __: None) |
817 | 91 |
0 | 92 |
817 | 93 class ProfileInfo(base.CommandBase): |
94 def __init__(self, host): | |
3028 | 95 super(ProfileInfo, self).__init__(host, 'info', need_connect=False, help=_('get information about a profile')) |
1864
96ba685162f6
jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents:
1861
diff
changeset
|
96 self.need_loop = True |
3028 | 97 self.to_show = [(_("jid"), "Connection", "JabberID"),] |
1402
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
98 self.largest = max([len(item[0]) for item in self.to_show]) |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
99 |
0 | 100 |
817 | 101 def add_parser_options(self): |
3028 | 102 self.parser.add_argument('--show-password', action='store_true', help=_('show the XMPP password IN CLEAR TEXT')) |
814 | 103 |
1402
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
104 def showNextValue(self, label=None, category=None, value=None): |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
105 """Show next value from self.to_show and quit on last one""" |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
106 if label is not None: |
3028 | 107 print((("{label:<"+str(self.largest+2)+"}{value}").format(label=label+": ", value=value))) |
1402
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
108 try: |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
109 label, category, name = self.to_show.pop(0) |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
110 except IndexError: |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
111 self.host.quit() |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
112 else: |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
113 self.host.bridge.asyncGetParamA(name, category, profile_key=self.host.profile, |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
114 callback=lambda value: self.showNextValue(label, category, value)) |
814 | 115 |
1864
96ba685162f6
jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents:
1861
diff
changeset
|
116 def start(self): |
1596 | 117 if self.args.show_password: |
3028 | 118 self.to_show.append((_("XMPP password"), "Connection", "Password")) |
1402
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
119 self.showNextValue() |
817 | 120 |
121 | |
122 class ProfileList(base.CommandBase): | |
123 def __init__(self, host): | |
3028 | 124 super(ProfileList, self).__init__(host, 'list', use_profile=False, use_output='list', help=('list profiles')) |
657 | 125 |
817 | 126 def add_parser_options(self): |
2146
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
127 group = self.parser.add_mutually_exclusive_group() |
3028 | 128 group.add_argument('-c', '--clients', action='store_true', help=_('get clients profiles only')) |
129 group.add_argument('-C', '--components', action='store_true', help=('get components profiles only')) | |
2146
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
130 |
817 | 131 |
1864
96ba685162f6
jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents:
1861
diff
changeset
|
132 def start(self): |
2146
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
133 if self.args.clients: |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
134 clients, components = True, False |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
135 elif self.args.components: |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
136 clients, components = False, True |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
137 else: |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
138 clients, components = True, True |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
139 self.output(self.host.bridge.profilesListGet(clients, components)) |
817 | 140 |
814 | 141 |
817 | 142 class ProfileCreate(base.CommandBase): |
143 def __init__(self, host): | |
3028 | 144 super(ProfileCreate, self).__init__(host, 'create', use_profile=False, help=('create a new profile')) |
1864
96ba685162f6
jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents:
1861
diff
changeset
|
145 self.need_loop = True |
657 | 146 |
817 | 147 def add_parser_options(self): |
3028 | 148 self.parser.add_argument('profile', type=str, help=_('the name of the profile')) |
149 self.parser.add_argument('-p', '--password', type=str, default='', help=_('the password of the profile')) | |
150 self.parser.add_argument('-j', '--jid', type=str, help=_('the jid of the profile')) | |
151 self.parser.add_argument('-x', '--xmpp-password', type=str, help=_('the password of the XMPP account (use profile password if not specified)'), | |
1403
f913b09cd9cc
jp (profile): in "profile create", jid and password arguments are now optional + added a new --xmpp-password option to set XMPP password separately (default to the same password as for profile).
Goffi <goffi@goffi.org>
parents:
1402
diff
changeset
|
152 metavar='PASSWORD') |
3028 | 153 self.parser.add_argument('-C', '--component', default='', |
154 help=_('set to component import name (entry point) if this is a component')) | |
817 | 155 |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
156 def _session_started(self, __): |
1403
f913b09cd9cc
jp (profile): in "profile create", jid and password arguments are now optional + added a new --xmpp-password option to set XMPP password separately (default to the same password as for profile).
Goffi <goffi@goffi.org>
parents:
1402
diff
changeset
|
157 if self.args.jid: |
2179
e42c6c131b52
jp (profile): fixed help gettext calls
Goffi <goffi@goffi.org>
parents:
2174
diff
changeset
|
158 self.host.bridge.setParam("JabberID", self.args.jid, "Connection", profile_key=self.args.profile) |
1403
f913b09cd9cc
jp (profile): in "profile create", jid and password arguments are now optional + added a new --xmpp-password option to set XMPP password separately (default to the same password as for profile).
Goffi <goffi@goffi.org>
parents:
1402
diff
changeset
|
159 xmpp_pwd = self.args.password or self.args.xmpp_password |
f913b09cd9cc
jp (profile): in "profile create", jid and password arguments are now optional + added a new --xmpp-password option to set XMPP password separately (default to the same password as for profile).
Goffi <goffi@goffi.org>
parents:
1402
diff
changeset
|
160 if xmpp_pwd: |
f913b09cd9cc
jp (profile): in "profile create", jid and password arguments are now optional + added a new --xmpp-password option to set XMPP password separately (default to the same password as for profile).
Goffi <goffi@goffi.org>
parents:
1402
diff
changeset
|
161 self.host.bridge.setParam("Password", xmpp_pwd, "Connection", profile_key=self.args.profile) |
817 | 162 self.host.quit() |
657 | 163 |
1702
bc7e3ff3c838
jp (profile): fixed creating profile with given JID and XMPP password
souliane <souliane@mailoo.org>
parents:
1687
diff
changeset
|
164 def _profile_created(self): |
bc7e3ff3c838
jp (profile): fixed creating profile with given JID and XMPP password
souliane <souliane@mailoo.org>
parents:
1687
diff
changeset
|
165 self.host.bridge.profileStartSession(self.args.password, self.args.profile, callback=self._session_started, errback=None) |
bc7e3ff3c838
jp (profile): fixed creating profile with given JID and XMPP password
souliane <souliane@mailoo.org>
parents:
1687
diff
changeset
|
166 |
1864
96ba685162f6
jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents:
1861
diff
changeset
|
167 def start(self): |
814 | 168 """Create a new profile""" |
2146
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
169 if self.args.profile in self.host.bridge.profilesListGet(): |
1596 | 170 log.error("Profile %s already exists." % self.args.profile) |
817 | 171 self.host.quit(1) |
2492
fcf0ae8102b8
jp (profile/create): errback handling
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
172 self.host.bridge.profileCreate(self.args.profile, self.args.password, self.args.component, |
fcf0ae8102b8
jp (profile/create): errback handling
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
173 callback=self._profile_created, |
fcf0ae8102b8
jp (profile/create): errback handling
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
174 errback=partial(self.errback, |
3028 | 175 msg=_("can't create profile: {}"), |
2492
fcf0ae8102b8
jp (profile/create): errback handling
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
176 exit_code=C.EXIT_BRIDGE_ERRBACK)) |
0 | 177 |
817 | 178 |
1404
e4e960d285b0
jp (profile): added "profile modify" command
Goffi <goffi@goffi.org>
parents:
1403
diff
changeset
|
179 class ProfileModify(base.CommandBase): |
e4e960d285b0
jp (profile): added "profile modify" command
Goffi <goffi@goffi.org>
parents:
1403
diff
changeset
|
180 def __init__(self, host): |
3028 | 181 super(ProfileModify, self).__init__(host, 'modify', need_connect=False, help=_('modify an existing profile')) |
1404
e4e960d285b0
jp (profile): added "profile modify" command
Goffi <goffi@goffi.org>
parents:
1403
diff
changeset
|
182 |
e4e960d285b0
jp (profile): added "profile modify" command
Goffi <goffi@goffi.org>
parents:
1403
diff
changeset
|
183 def add_parser_options(self): |
1687
de9cc4d62a4a
jp (profile): fixed profile/modify command:
Goffi <goffi@goffi.org>
parents:
1597
diff
changeset
|
184 profile_pwd_group = self.parser.add_mutually_exclusive_group() |
3028 | 185 profile_pwd_group.add_argument('-w', '--password', help=_('change the password of the profile')) |
186 profile_pwd_group.add_argument('--disable-password', action='store_true', help=_('disable profile password (dangerous!)')) | |
187 self.parser.add_argument('-j', '--jid', help=_('the jid of the profile')) | |
188 self.parser.add_argument('-x', '--xmpp-password', help=_('change the password of the XMPP account'), | |
1404
e4e960d285b0
jp (profile): added "profile modify" command
Goffi <goffi@goffi.org>
parents:
1403
diff
changeset
|
189 metavar='PASSWORD') |
3028 | 190 self.parser.add_argument('-D', '--default', action='store_true', help=_('set as default profile')) |
1404
e4e960d285b0
jp (profile): added "profile modify" command
Goffi <goffi@goffi.org>
parents:
1403
diff
changeset
|
191 |
1864
96ba685162f6
jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents:
1861
diff
changeset
|
192 def start(self): |
1687
de9cc4d62a4a
jp (profile): fixed profile/modify command:
Goffi <goffi@goffi.org>
parents:
1597
diff
changeset
|
193 if self.args.disable_password: |
de9cc4d62a4a
jp (profile): fixed profile/modify command:
Goffi <goffi@goffi.org>
parents:
1597
diff
changeset
|
194 self.args.password = '' |
1404
e4e960d285b0
jp (profile): added "profile modify" command
Goffi <goffi@goffi.org>
parents:
1403
diff
changeset
|
195 if self.args.password is not None: |
e4e960d285b0
jp (profile): added "profile modify" command
Goffi <goffi@goffi.org>
parents:
1403
diff
changeset
|
196 self.host.bridge.setParam("Password", self.args.password, "General", profile_key=self.host.profile) |
e4e960d285b0
jp (profile): added "profile modify" command
Goffi <goffi@goffi.org>
parents:
1403
diff
changeset
|
197 if self.args.jid is not None: |
e4e960d285b0
jp (profile): added "profile modify" command
Goffi <goffi@goffi.org>
parents:
1403
diff
changeset
|
198 self.host.bridge.setParam("JabberID", self.args.jid, "Connection", profile_key=self.host.profile) |
e4e960d285b0
jp (profile): added "profile modify" command
Goffi <goffi@goffi.org>
parents:
1403
diff
changeset
|
199 if self.args.xmpp_password is not None: |
e4e960d285b0
jp (profile): added "profile modify" command
Goffi <goffi@goffi.org>
parents:
1403
diff
changeset
|
200 self.host.bridge.setParam("Password", self.args.xmpp_password, "Connection", profile_key=self.host.profile) |
1596 | 201 if self.args.default: |
202 self.host.bridge.profileSetDefault(self.host.profile) | |
1404
e4e960d285b0
jp (profile): added "profile modify" command
Goffi <goffi@goffi.org>
parents:
1403
diff
changeset
|
203 |
e4e960d285b0
jp (profile): added "profile modify" command
Goffi <goffi@goffi.org>
parents:
1403
diff
changeset
|
204 |
817 | 205 class Profile(base.CommandBase): |
2336
a7438fe4e24d
jp (profile): added disconnect command
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
206 subcommands = (ProfileConnect, ProfileDisconnect, ProfileCreate, ProfileDefault, ProfileDelete, ProfileInfo, ProfileList, ProfileModify) |
817 | 207 |
208 def __init__(self, host): | |
3028 | 209 super(Profile, self).__init__(host, 'profile', use_profile=False, help=_('profile commands')) |