Mercurial > libervia-backend
comparison frontends/src/jp/profile.py @ 814:59c7bc51c323
jp: refactoring using ArgParse
author | Dal <kedals0@gmail.com> |
---|---|
date | Wed, 05 Feb 2014 14:35:26 +0100 |
parents | frontends/src/jp/jp@1fe00f0c9a91 |
children | f8d534ed1d1e |
comparison
equal
deleted
inserted
replaced
813:1a1600491d9d | 814:59c7bc51c323 |
---|---|
1 """This module permits to manage profiles. It can list, create, delete | |
2 and retrieve informations about a profile.""" | |
3 | |
4 import base | |
5 import sys | |
6 | |
7 from logging import debug, info, error, warning | |
8 from sat.core.i18n import _ | |
9 | |
10 profile_parser = base.subparser.add_parser('profile', | |
11 help = "Profile Commands") | |
12 profile_subparser = profile_parser.add_subparsers() | |
13 | |
14 ################################################################################ | |
15 # DELETE # | |
16 ################################################################################ | |
17 profile_delete = profile_subparser.add_parser('delete', | |
18 help = "Delete a profile") | |
19 profile_delete.add_argument('profile', type=str, | |
20 help='the name of the profile') | |
21 profile_delete.set_defaults( | |
22 func = lambda args : ProfileDelete(args.profile).go()) | |
23 | |
24 class ProfileDelete(base.JP): | |
25 def __init__(self, profile_name): | |
26 base.JP.__init__(self) | |
27 self.profile_name = profile_name | |
28 | |
29 def run(self): | |
30 if self.profile_name not in self.bridge.getProfilesList(): | |
31 error("Profile %s doesn't exist."%self.profile_name) | |
32 exit(1) | |
33 self.bridge.deleteProfile(self.profile_name) | |
34 | |
35 ################################################################################ | |
36 # INFO # | |
37 ################################################################################ | |
38 profile_info = profile_subparser.add_parser('info', | |
39 help = "Get informations about a profile") | |
40 profile_info.add_argument('profile', type=str, | |
41 help='the name of the profile') | |
42 profile_info.set_defaults( | |
43 func = lambda args : ProfileInfo(args.profile).go()) | |
44 | |
45 class ProfileInfo(base.JP): | |
46 def __init__(self, profile_name): | |
47 base.JP.__init__(self) | |
48 self.profile_name = profile_name | |
49 | |
50 def run(self): | |
51 def getJID(jid): | |
52 info("jid: %s"%jid) | |
53 self.bridge.asyncGetParamA("Password", "Connection", profile_key=self.profile_name, callback=getPassword) | |
54 def getPassword(password): | |
55 info("pwd: %s"%password) | |
56 self.loop.quit() | |
57 if self.profile_name not in self.bridge.getProfilesList(): | |
58 error("Profile %s doesn't exist."%self.profile_name) | |
59 exit(1) | |
60 | |
61 self.start_mainloop() | |
62 self.bridge.asyncGetParamA("JabberID", "Connection", profile_key=self.profile_name, callback=getJID) | |
63 | |
64 ################################################################################ | |
65 # LIST # | |
66 ################################################################################ | |
67 profile_list = profile_subparser.add_parser('list', | |
68 help = "List profiles") | |
69 profile_list.set_defaults( | |
70 func = lambda args : ProfileList().go()) | |
71 | |
72 class ProfileList(base.JP): | |
73 def run(self): | |
74 for p in self.bridge.getProfilesList(): | |
75 info(p) | |
76 | |
77 ################################################################################ | |
78 # CREATE # | |
79 ################################################################################ | |
80 create_parser = profile_subparser.add_parser('create', | |
81 help = "Create a new profile") | |
82 create_parser.add_argument('profile', type=str, | |
83 help='the name of the profile') | |
84 create_parser.add_argument('jid', type=str, | |
85 help='the jid of the profile') | |
86 create_parser.add_argument('password', type=str, | |
87 help='the password of the profile') | |
88 create_parser.set_defaults( | |
89 func = lambda args : ProfileCreate(args.profile, args.jid, args.password).go()) | |
90 | |
91 class ProfileCreate(base.JP): | |
92 def __init__(self, profile_name, jid, password): | |
93 base.JP.__init__(self) | |
94 self.profile_name = profile_name | |
95 self.jid = jid | |
96 self.password = password | |
97 | |
98 def _create_profile(self, profile_name, jid, password): | |
99 self.bridge.setParam("JabberID", jid, "Connection" ,profile_key=profile_name) | |
100 self.bridge.setParam("Server", base.JID(jid).domain, "Connection", profile_key=profile_name) | |
101 self.bridge.setParam("Password", password, "Connection", profile_key=profile_name) | |
102 self.loop.quit() | |
103 | |
104 def run(self): | |
105 """Create a new profile""" | |
106 if self.profile_name in self.bridge.getProfilesList(): | |
107 error("Profile %s already exists."%self.profile_name) | |
108 exit(1) | |
109 self.start_mainloop() | |
110 self.bridge.asyncCreateProfile(self.profile_name, lambda : self._create_profile(self.profile_name, self.jid, self.password), None) | |
111 |