changeset 1015:fee00f2e11c2

memory, jp: added jp commands to load/save parameters template
author souliane <souliane@mailoo.org>
date Sun, 04 May 2014 18:43:54 +0200
parents e40d9858cb83
children 0c361fdc76af
files frontends/src/bridge/DBus.py frontends/src/jp/cmd_params.py src/bridge/DBus.py src/bridge/bridge_constructor/bridge_template.ini src/core/sat_main.py src/memory/memory.py src/memory/params.py
diffstat 7 files changed, 115 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/bridge/DBus.py	Mon May 05 20:16:14 2014 +0200
+++ b/frontends/src/bridge/DBus.py	Sun May 04 18:43:54 2014 +0200
@@ -195,12 +195,18 @@
     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:]))
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/frontends/src/jp/cmd_params.py	Sun May 04 18:43:54 2014 +0200
@@ -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 <http://www.gnu.org/licenses/>.
+
+
+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'))
--- a/src/bridge/DBus.py	Mon May 05 20:16:14 2014 +0200
+++ b/src/bridge/DBus.py	Sun May 04 18:43:54 2014 +0200
@@ -386,6 +386,12 @@
         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)
     def paramsRegisterApp(self, xml, security_limit=-1, app=''):
@@ -398,6 +404,12 @@
         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'))
     def sendMessage(self, to_jid, message, subject='', mess_type="auto", extra={}, profile_key="@NONE@", callback=None, errback=None):
--- a/src/bridge/bridge_constructor/bridge_template.ini	Mon May 05 20:16:14 2014 +0200
+++ b/src/bridge/bridge_constructor/bridge_template.ini	Sun May 04 18:43:54 2014 +0200
@@ -628,3 +628,21 @@
 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)
--- a/src/core/sat_main.py	Mon May 05 20:16:14 2014 +0200
+++ b/src/core/sat_main.py	Sun May 04 18:43:54 2014 +0200
@@ -133,6 +133,8 @@
         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)
 
--- a/src/memory/memory.py	Mon May 05 20:16:14 2014 +0200
+++ b/src/memory/memory.py	Sun May 04 18:43:54 2014 +0200
@@ -207,8 +207,12 @@
         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 @@
         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)
--- a/src/memory/params.py	Mon May 05 20:16:14 2014 +0200
+++ b/src/memory/params.py	Sun May 04 18:43:54 2014 +0200
@@ -90,7 +90,7 @@
             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)