[PATCH] Added json to func-transmit

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is the complete json patch (diff from origin).

Marco

---
func.spec                           |    3 +-
scripts/func-transmit               |   34 ++++++++++++++++------
test/unittest/test_func_transmit.py |   55 ++++++++++++++++++++++++++++-------
3 files changed, 71 insertions(+), 21 deletions(-)

diff --git a/func.spec b/func.spec
index bf6d3df..3510597 100644
--- a/func.spec
+++ b/func.spec
@@ -13,7 +13,8 @@ License: GPLv2+
Group: Applications/System
Requires: python >= 2.3
Requires: pyOpenSSL
-Requires: pyYaml
+Requires: PyYAML
+Requires: python-simplejson
Requires: certmaster >= 0.1
BuildRequires: python-devel
%if %is_suse
diff --git a/scripts/func-transmit b/scripts/func-transmit
index e43714f..989d450 100644
--- a/scripts/func-transmit
+++ b/scripts/func-transmit
@@ -36,32 +36,42 @@ import distutils.sysconfig
import func.yaml as yaml # FIXME: need to subpackage this as part of Func
import func.overlord.func_command as func_command
import func.overlord.client as fc
+from optparse import OptionParser
+import simplejson
+
+# load input parameters
+parser = OptionParser()
+parser.add_option("-j","--json",help="Json Parser",action="store_true",dest="json",default=False)
+parser.add_option("-y","--yaml",help="Yaml Parser",action="store_true",dest="yaml",default=False)
+(language,args)=parser.parse_args()
+# Setting default language if no one is selected by user
+if (language.json==False and language.yaml==False):
+    language.yaml = True

# load input from stdin
input = sys.stdin.read()
-params = yaml.load(input).next()
-
+if (language.yaml):
+    params = yaml.load(input).next()
+elif (language.json):
+    params = simplejson.loads(input)

# scan arguments
-
clients    = params.get('clients', "*")
method     = params.get('method','unknown')
+async      = params.get('async', False)

if method == "list_minions":
    server_spec = "*"
    minion_set = fc.Minions(server_spec)
    servers = minion_set.get_all_hosts()
    servers.sort()
-    results = servers
- + results = {"list_minions":servers}
else:
    # scan more arguments
-    async      = params.get('async', False)
    nforks     = params.get('nforks', 1)
    module     = params.get('module','unknown')
    parameters = params.get('parameters', None)

-
    # make the call
    client = fc.Overlord(clients, async=async, nforks=nforks)
    module_handle = getattr(client, module)
@@ -74,8 +84,14 @@ else:
    else:
        results = method_handle()

-# convert to YAML
-output = yaml.dump(results)
+# convert to selected language (default is YAML)
+if (language.json):
+    if (async):
+        output = simplejson.dumps({"job_id":results})
+    else:
+        output = simplejson.dumps(results)
+elif (language.yaml):
+    output = yaml.dump(results)

# write to stdout
print output
diff --git a/test/unittest/test_func_transmit.py b/test/unittest/test_func_transmit.py
index 70578ec..e6dfe45 100644
--- a/test/unittest/test_func_transmit.py
+++ b/test/unittest/test_func_transmit.py
@@ -19,6 +19,8 @@ from func import yaml
import StringIO
import cStringIO

+import simplejson
+


def structToYaml(data):
@@ -29,6 +31,13 @@ def structToYaml(data):
    buf = yaml.dump(data)
    return buf

+def structToJason(data):
+    #Take data structure for the test
+    #and serializes it using json
+ + serialized = simplejson.dumps(input)
+    return serialized
+
class BaseTest:
    # assume we are talking to localhost
    # th = socket.gethostname()
@@ -37,21 +46,37 @@ class BaseTest:
    async=False

    # just so we can change it easy later
-    def __serialize(self, data):
-        buf = yaml.dump(data)
+    def __serialize(self, data, language):
+        if (language=="JSON"):
+ buf = simplejson.dumps(input) + elif (language=="YAML"):
+            buf = yaml.dump(data)
+ + print "BUF: " + str(buf)
        return buf

-    def __deserialize(self, buf):
-        data = yaml.load(buf).next()
+    def __deserialize(self, buf, language):
+        if (language=="JSON"):
+            data = simplejson.loads(buf)
+        elif (language=="YAML"):
+            data = yaml.load(buf).next()
        return data

+    def callYaml(self, data):
+        f = self.__serialize(data,"YAML")
+        output = self.call(f)
+        return self.__deserialize(output[0],"YAML")
+ + def callJson(self, data):
+        f = self.__serialize(data,"JSON")
+        output = self.call(f)
+        return self.__deserialize(output[0],"JSON")
+ def call(self, data):
-        f = self.__serialize(data)
        p = subprocess.Popen("func-transmit", shell=True,
                             stdin=subprocess.PIPE, stdout=subprocess.PIPE)
-        output = p.communicate(input=f)
-
-        return self.__deserialize(output[0])
+        output = p.communicate(input=data)
+        return output
def __init__(self):
        pass
@@ -66,15 +91,23 @@ class BaseTest:
class TestListMinion(BaseTest):
def test_list_minions(self):
-        out = self.call({'clients': '*',
+        out = self.callYaml({'clients': '*',
                          'method': 'list_minions'})

        print out
+ +class TestListMinionJson(BaseTest): + + def test_list_minions(self):
+        out = self.callJson({'clients': '*',
+                             'method': 'list_minions'})
+
+        print out


class TestTest(BaseTest):
    def _echo_test(self, data):
-        result = self.call({'clients':'*',
+        result = self.callYaml({'clients':'*',
                             'method': 'echo',
                             'module': 'test',
                             'parameters': [data]})
@@ -84,7 +117,7 @@ class TestTest(BaseTest):

def test_add(self):
-        result = self.call({'clients':'*',
+        result = self.callYaml({'clients':'*',
                             'method': 'add',
                             'module': 'test',
                             'parameters': [1,2]})
--
1.5.5.1



--
Dott. Ing. Mornati Marco
Byte-Code s.r.l
via Antonio Cechov, 1
San Giuliano Milanese (MI)
E-Mail: mmornati@xxxxxxxxxxxxx


_______________________________________________
Func-list mailing list
Func-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/func-list

[Index of Archives]     [Fedora Users]     [Linux Networking]     [Fedora Legacy List]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]

  Powered by Linux