jp: refactoring using ArgParse
This commit is contained in:
parent
eeff94469a
commit
f4de26e4e8
1 changed files with 631 additions and 441 deletions
111
frontends/src/jp/profile.py
Normal file
111
frontends/src/jp/profile.py
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
"""This module permits to manage profiles. It can list, create, delete
|
||||
and retrieve informations about a profile."""
|
||||
|
||||
import base
|
||||
import sys
|
||||
|
||||
from logging import debug, info, error, warning
|
||||
from sat.core.i18n import _
|
||||
|
||||
profile_parser = base.subparser.add_parser('profile',
|
||||
help = "Profile Commands")
|
||||
profile_subparser = profile_parser.add_subparsers()
|
||||
|
||||
################################################################################
|
||||
# DELETE #
|
||||
################################################################################
|
||||
profile_delete = profile_subparser.add_parser('delete',
|
||||
help = "Delete a profile")
|
||||
profile_delete.add_argument('profile', type=str,
|
||||
help='the name of the profile')
|
||||
profile_delete.set_defaults(
|
||||
func = lambda args : ProfileDelete(args.profile).go())
|
||||
|
||||
class ProfileDelete(base.JP):
|
||||
def __init__(self, profile_name):
|
||||
base.JP.__init__(self)
|
||||
self.profile_name = profile_name
|
||||
|
||||
def run(self):
|
||||
if self.profile_name not in self.bridge.getProfilesList():
|
||||
error("Profile %s doesn't exist."%self.profile_name)
|
||||
exit(1)
|
||||
self.bridge.deleteProfile(self.profile_name)
|
||||
|
||||
################################################################################
|
||||
# INFO #
|
||||
################################################################################
|
||||
profile_info = profile_subparser.add_parser('info',
|
||||
help = "Get informations about a profile")
|
||||
profile_info.add_argument('profile', type=str,
|
||||
help='the name of the profile')
|
||||
profile_info.set_defaults(
|
||||
func = lambda args : ProfileInfo(args.profile).go())
|
||||
|
||||
class ProfileInfo(base.JP):
|
||||
def __init__(self, profile_name):
|
||||
base.JP.__init__(self)
|
||||
self.profile_name = profile_name
|
||||
|
||||
def run(self):
|
||||
def getJID(jid):
|
||||
info("jid: %s"%jid)
|
||||
self.bridge.asyncGetParamA("Password", "Connection", profile_key=self.profile_name, callback=getPassword)
|
||||
def getPassword(password):
|
||||
info("pwd: %s"%password)
|
||||
self.loop.quit()
|
||||
if self.profile_name not in self.bridge.getProfilesList():
|
||||
error("Profile %s doesn't exist."%self.profile_name)
|
||||
exit(1)
|
||||
|
||||
self.start_mainloop()
|
||||
self.bridge.asyncGetParamA("JabberID", "Connection", profile_key=self.profile_name, callback=getJID)
|
||||
|
||||
################################################################################
|
||||
# LIST #
|
||||
################################################################################
|
||||
profile_list = profile_subparser.add_parser('list',
|
||||
help = "List profiles")
|
||||
profile_list.set_defaults(
|
||||
func = lambda args : ProfileList().go())
|
||||
|
||||
class ProfileList(base.JP):
|
||||
def run(self):
|
||||
for p in self.bridge.getProfilesList():
|
||||
info(p)
|
||||
|
||||
################################################################################
|
||||
# CREATE #
|
||||
################################################################################
|
||||
create_parser = profile_subparser.add_parser('create',
|
||||
help = "Create a new profile")
|
||||
create_parser.add_argument('profile', type=str,
|
||||
help='the name of the profile')
|
||||
create_parser.add_argument('jid', type=str,
|
||||
help='the jid of the profile')
|
||||
create_parser.add_argument('password', type=str,
|
||||
help='the password of the profile')
|
||||
create_parser.set_defaults(
|
||||
func = lambda args : ProfileCreate(args.profile, args.jid, args.password).go())
|
||||
|
||||
class ProfileCreate(base.JP):
|
||||
def __init__(self, profile_name, jid, password):
|
||||
base.JP.__init__(self)
|
||||
self.profile_name = profile_name
|
||||
self.jid = jid
|
||||
self.password = password
|
||||
|
||||
def _create_profile(self, profile_name, jid, password):
|
||||
self.bridge.setParam("JabberID", jid, "Connection" ,profile_key=profile_name)
|
||||
self.bridge.setParam("Server", base.JID(jid).domain, "Connection", profile_key=profile_name)
|
||||
self.bridge.setParam("Password", password, "Connection", profile_key=profile_name)
|
||||
self.loop.quit()
|
||||
|
||||
def run(self):
|
||||
"""Create a new profile"""
|
||||
if self.profile_name in self.bridge.getProfilesList():
|
||||
error("Profile %s already exists."%self.profile_name)
|
||||
exit(1)
|
||||
self.start_mainloop()
|
||||
self.bridge.asyncCreateProfile(self.profile_name, lambda : self._create_profile(self.profile_name, self.jid, self.password), None)
|
||||
|
||||
Loading…
Reference in a new issue