view frontends/src/jp/profile.py @ 815:f8d534ed1d1e

jp: added missing license headers
author Goffi <goffi@goffi.org>
date Wed, 05 Feb 2014 14:52:38 +0100
parents 59c7bc51c323
children
line wrap: on
line source

#! /usr/bin/python
# -*- coding: utf-8 -*-

# jp: a SAT command line tool
# Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""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)