Re: [PATCH] Added JSON languange to func-transmit

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

 



Hi guys,

is there a little thing to change to making other languages able to convert json serialized object with async call (the answer is "21312321.211233", and, example in java, Json converter says that is impossibile to convert that value).
So:

if (language.json):
   output = simplejson.dumps({"job_id":results})

Making this little change the output jsonized will be: {"job_id":"13232231.233223"} that we can convert again from json to an object.

I'll post the correct patch later...

Bye
Marco

Marco Mornati wrote:
---

func.spec                           |    3 +-
scripts/func-transmit               |   26 ++++++++++++++--
test/unittest/test_func_transmit.py | 55 ++++++++++++++++++++++++++++-------
3 files changed, 68 insertions(+), 16 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..5b8727b 100644
--- a/scripts/func-transmit
+++ b/scripts/func-transmit
@@ -36,14 +36,29 @@ 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)

+print "Params: " + str(params)

-# scan arguments

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

@@ -74,8 +89,11 @@ else:
    else:
        results = method_handle()

-# convert to YAML
-output = yaml.dump(results)
+# convert to selected language (default is YAML)
+if (language.json):
+    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]})


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