Mercurial > libervia-backend
annotate frontends/src/jp/cmd_profile.py @ 1402:391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 06 Apr 2015 17:32:28 +0200 |
parents | 069ad98b360d |
children | f913b09cd9cc |
rev | line source |
---|---|
815 | 1 #! /usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # jp: a SAT command line tool | |
1396 | 5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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 |
814 | 23 from logging import debug, info, error, warning |
771 | 24 from sat.core.i18n import _ |
817 | 25 from sat_frontends.jp import base |
402
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
26 |
817 | 27 __commands__ = ["Profile"] |
28 | |
29 PROFILE_HELP = _('The name of the profile') | |
0 | 30 |
31 | |
817 | 32 class ProfileDelete(base.CommandBase): |
33 def __init__(self, host): | |
34 super(ProfileDelete, self).__init__(host, 'delete', use_profile=False, help=_('Delete a profile')) | |
35 | |
36 def add_parser_options(self): | |
37 self.parser.add_argument('profile', type=str, help=PROFILE_HELP) | |
0 | 38 |
814 | 39 def run(self): |
817 | 40 super(ProfileDelete, self).run() |
41 if self.args.profile not in self.host.bridge.getProfilesList(): | |
42 error("Profile %s doesn't exist." % self.args.profile) | |
43 self.host.quit(1) | |
893
308a96bc7c1b
core, frontends: add method asyncDeleteProfile, remove synchronous methods createProfile and deleteProfile
souliane <souliane@mailoo.org>
parents:
817
diff
changeset
|
44 self.host.bridge.asyncDeleteProfile(self.args.profile, callback=lambda dummy: None) |
817 | 45 |
0 | 46 |
817 | 47 class ProfileInfo(base.CommandBase): |
48 def __init__(self, host): | |
1402
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
49 super(ProfileInfo, self).__init__(host, 'info', help=_('Get information about a profile')) |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
50 self.to_show = [(_(u"jid"), "Connection", "JabberID"), |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
51 (_(u"XMPP password"), "Connection", "Password"), |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
52 ] |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
53 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
|
54 |
0 | 55 |
817 | 56 def add_parser_options(self): |
1402
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
57 pass |
814 | 58 |
1402
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
59 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
|
60 """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
|
61 if label is not None: |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
62 print((u"{label:<"+unicode(self.largest+2)+"}{value}").format(label=label+": ", value=value)) |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
63 try: |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
64 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
|
65 except IndexError: |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
66 self.host.quit() |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
67 else: |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
68 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
|
69 callback=lambda value: self.showNextValue(label, category, value)) |
814 | 70 |
1402
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
71 def connected(self): |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
72 self.need_loop = True |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
73 super(ProfileInfo, self).connected() |
391b0c21f4be
jp (profile): fixed "profile info" to use profile, and then manage connection.
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
74 self.showNextValue() |
817 | 75 |
76 | |
77 class ProfileList(base.CommandBase): | |
78 def __init__(self, host): | |
79 super(ProfileList, self).__init__(host, 'list', use_profile=False, help=_('List profiles')) | |
657 | 80 |
817 | 81 def add_parser_options(self): |
82 pass | |
83 | |
84 def run(self): | |
85 super(ProfileList, self).run() | |
86 for profile in self.host.bridge.getProfilesList(): | |
87 print profile | |
88 | |
814 | 89 |
817 | 90 class ProfileCreate(base.CommandBase): |
91 def __init__(self, host): | |
92 super(ProfileCreate, self).__init__(host, 'create', use_profile=False, help=_('Create a new profile')) | |
657 | 93 |
817 | 94 def add_parser_options(self): |
95 self.parser.add_argument('profile', type=str, help=_('the name of the profile')) | |
96 self.parser.add_argument('jid', type=str, help=_('the jid of the profile')) | |
97 self.parser.add_argument('password', type=str, help=_('the password of the profile')) | |
98 | |
99 def _profile_created(self): | |
100 self.host.bridge.setParam("JabberID", self.args.jid, "Connection" ,profile_key=self.args.profile) | |
101 self.host.bridge.setParam("Password", self.args.password, "Connection", profile_key=self.args.profile) | |
102 self.host.quit() | |
657 | 103 |
814 | 104 def run(self): |
105 """Create a new profile""" | |
817 | 106 self.need_loop = True |
107 if self.args.profile in self.host.bridge.getProfilesList(): | |
108 error("Profile %s already exists." % self.args.profile) | |
109 self.host.quit(1) | |
1033
d87aa6bdb0b4
jp: option '-c' is not longer a flag but a string to define the profile password:
souliane <souliane@mailoo.org>
parents:
893
diff
changeset
|
110 self.host.bridge.asyncCreateProfile(self.args.profile, callback=self._profile_created, errback=None) |
0 | 111 |
817 | 112 |
113 class Profile(base.CommandBase): | |
114 subcommands = (ProfileDelete, ProfileInfo, ProfileList, ProfileCreate) | |
115 | |
116 def __init__(self, host): | |
117 super(Profile, self).__init__(host, 'profile', use_profile=False, help=_('Profile commands')) |