From 2e6420fb497ee879e07e870f59eafc17f7756a1f Mon Sep 17 00:00:00 2001 From: souliane Date: Sun, 4 May 2014 18:43:54 +0200 Subject: [PATCH] memory, jp: added jp commands to load/save parameters template --- frontends/src/bridge/DBus.py | 6 ++ frontends/src/jp/cmd_params.py | 63 +++++++++++++++++++ src/bridge/DBus.py | 12 ++++ .../bridge_constructor/bridge_template.ini | 18 ++++++ src/core/sat_main.py | 2 + src/memory/memory.py | 18 ++++-- src/memory/params.py | 2 +- 7 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 frontends/src/jp/cmd_params.py diff --git a/frontends/src/bridge/DBus.py b/frontends/src/bridge/DBus.py index b685ab69..2187da84 100644 --- a/frontends/src/bridge/DBus.py +++ b/frontends/src/bridge/DBus.py @@ -195,12 +195,18 @@ class DBusBridgeFrontend(BridgeFrontend): def launchAction(self, callback_id, data, profile_key="@DEFAULT@", callback=None, errback=None): return self.db_core_iface.launchAction(callback_id, data, profile_key, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:])) + def loadParamsTemplate(self, filename): + return self.db_core_iface.loadParamsTemplate(filename) + def paramsRegisterApp(self, xml, security_limit=-1, app=''): return self.db_core_iface.paramsRegisterApp(xml, security_limit, app) def registerNewAccount(self, login, password, email, host, port=5222): return unicode(self.db_core_iface.registerNewAccount(login, password, email, host, port)) + def saveParamsTemplate(self, filename): + return self.db_core_iface.saveParamsTemplate(filename) + def sendMessage(self, to_jid, message, subject='', mess_type="auto", extra={}, profile_key="@NONE@", callback=None, errback=None): return self.db_core_iface.sendMessage(to_jid, message, subject, mess_type, extra, profile_key, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:])) diff --git a/frontends/src/jp/cmd_params.py b/frontends/src/jp/cmd_params.py new file mode 100644 index 00000000..daf02220 --- /dev/null +++ b/frontends/src/jp/cmd_params.py @@ -0,0 +1,63 @@ +#! /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) +# Copyright (C) 2013, 2014 Adrien Cossa (souliane@mailoo.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 . + + +import base +from sat.core.i18n import _ + +__commands__ = ["Params"] + + +class SaveTemplate(base.CommandBase): + def __init__(self, host): + super(SaveTemplate, self).__init__(host, 'save', use_profile=False, help=_('Save parameters template to xml file')) + + def add_parser_options(self): + self.parser.add_argument("filename", type=str, help=_("Output file")) + + def run(self): + """Save parameters template to xml file""" + if self.host.bridge.saveParamsTemplate(self.args.filename): + print _("Parameters saved to file %s") % self.args.filename + else: + print _("Can't save parameters to file %s") % self.args.filename + + +class LoadTemplate(base.CommandBase): + + def __init__(self, host): + super(LoadTemplate, self).__init__(host, 'load', use_profile=False, help=_('Load parameters template from xml file')) + + def add_parser_options(self): + self.parser.add_argument("filename", type=str, help=_("Input file")) + + def run(self): + """Load parameters template from xml file""" + if self.host.bridge.loadParamsTemplate(self.args.filename): + print _("Parameters loaded from file %s") % self.args.filename + else: + print _("Can't load parameters from file %s") % self.args.filename + + +class Params(base.CommandBase): + subcommands = (SaveTemplate, LoadTemplate) + + def __init__(self, host): + super(Params, self).__init__(host, 'params', use_profile=False, help=_('Save/load parameters template')) diff --git a/src/bridge/DBus.py b/src/bridge/DBus.py index 166f1187..49e1fff6 100755 --- a/src/bridge/DBus.py +++ b/src/bridge/DBus.py @@ -385,6 +385,12 @@ class DbusObject(dbus.service.Object): def launchAction(self, callback_id, data, profile_key="@DEFAULT@", callback=None, errback=None): return self._callback("launchAction", unicode(callback_id), data, unicode(profile_key), callback=callback, errback=errback) + @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX, + in_signature='s', out_signature='b', + async_callbacks=None) + def loadParamsTemplate(self, filename): + return self._callback("loadParamsTemplate", unicode(filename)) + @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX, in_signature='sis', out_signature='', async_callbacks=None) @@ -397,6 +403,12 @@ class DbusObject(dbus.service.Object): def registerNewAccount(self, login, password, email, host, port=5222): return self._callback("registerNewAccount", unicode(login), unicode(password), unicode(email), unicode(host), port) + @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX, + in_signature='s', out_signature='b', + async_callbacks=None) + def saveParamsTemplate(self, filename): + return self._callback("saveParamsTemplate", unicode(filename)) + @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX, in_signature='ssssa{ss}s', out_signature='', async_callbacks=('callback', 'errback')) diff --git a/src/bridge/bridge_constructor/bridge_template.ini b/src/bridge/bridge_constructor/bridge_template.ini index 489d1748..a68aabdc 100644 --- a/src/bridge/bridge_constructor/bridge_template.ini +++ b/src/bridge/bridge_constructor/bridge_template.ini @@ -628,3 +628,21 @@ doc=Discover items of an entity doc_param_0=entity_jid: JID to discover doc_param_1=%(doc_profile_key)s doc_return=array of tuple (entity, node identifier, name) + +[saveParamsTemplate] +type=method +category=core +sig_in=s +sig_out=b +doc=Save parameters template to xml file +doc_param_0=filename: output filename +doc_return=boolean (True in case of success) + +[loadParamsTemplate] +type=method +category=core +sig_in=s +sig_out=b +doc=Load parameters template from xml file +doc_param_0=filename: input filename +doc_return=boolean (True in case of success) diff --git a/src/core/sat_main.py b/src/core/sat_main.py index 998568ea..481203c9 100644 --- a/src/core/sat_main.py +++ b/src/core/sat_main.py @@ -133,6 +133,8 @@ class SAT(service.Service): self.bridge.register("getMenuHelp", self.getMenuHelp) self.bridge.register("discoInfos", self.memory.disco._discoInfos) self.bridge.register("discoItems", self.memory.disco._discoItems) + self.bridge.register("saveParamsTemplate", self.memory.save_xml) + self.bridge.register("loadParamsTemplate", self.memory.load_xml) self.memory.initialized.addCallback(self._postMemoryInit) diff --git a/src/memory/memory.py b/src/memory/memory.py index acee86e2..6f0dc946 100644 --- a/src/memory/memory.py +++ b/src/memory/memory.py @@ -207,8 +207,12 @@ class Memory(object): return value def load_xml(self, filename): - """Load parameters template from xml file""" - if filename is None: + """Load parameters template from xml file + + @param filename (str): input file + @return bool: True in case of success + """ + if not filename: return False filename = os.path.expanduser(filename) if os.path.exists(filename): @@ -246,9 +250,13 @@ class Memory(object): except KeyError: log.error(_("Trying to purge roster status cache for a profile not in memory: [%s]") % profile) - def save_xml(self, filename=None): - """Save parameters template to xml file""" - if filename is None: + def save_xml(self, filename): + """Save parameters template to xml file + + @param filename (str): output file + @return bool: True in case of success + """ + if not filename: return False #TODO: need to encrypt files (at least passwords !) and set permissions filename = os.path.expanduser(filename) diff --git a/src/memory/params.py b/src/memory/params.py index 0a94945d..3f2cfdca 100644 --- a/src/memory/params.py +++ b/src/memory/params.py @@ -90,7 +90,7 @@ class Params(object): self._mergeParams(source_map[node_key], dest_map[node_key]) def load_xml(self, xml_file): - """Load parameters template from file""" + """Load parameters template from xml file""" self.dom = minidom.parse(xml_file) default_dom = minidom.parseString(Params.default_xml.encode('utf-8')) self._mergeParams(default_dom.documentElement, self.dom.documentElement)