diff options
Diffstat (limited to 'sca-cpp/trunk/modules/wsgi')
-rwxr-xr-x | sca-cpp/trunk/modules/wsgi/atom-test.py | 36 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/wsgi/atomutil.py | 88 | ||||
-rwxr-xr-x | sca-cpp/trunk/modules/wsgi/composite.py | 32 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/wsgi/server-test.py | 15 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/wsgi/util.py | 6 |
5 files changed, 90 insertions, 87 deletions
diff --git a/sca-cpp/trunk/modules/wsgi/atom-test.py b/sca-cpp/trunk/modules/wsgi/atom-test.py index 81a6106519..9ada53bea5 100755 --- a/sca-cpp/trunk/modules/wsgi/atom-test.py +++ b/sca-cpp/trunk/modules/wsgi/atom-test.py @@ -71,7 +71,7 @@ completedEntry = \ def testEntry(): i = (element, "'item", (element, "'name", "Apple"), (element, "'price", "$2.99")) - a = ("item", "cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b", i) + a = ((element, "'entry", (element, "'title", "item"), (element, "'id", "cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), (element, "'content", i)),) s = writeATOMEntry(a); assert car(s) == itemEntry @@ -123,41 +123,29 @@ itemFeed = \ "</feed>\n" def testFeed(): - s = writeATOMFeed(("Feed", "1234")) + a = ((element, "'feed", (element, "'title", "Feed"), (element, "'id", "1234")),) + s = writeATOMFeed(a) assert car(s) == emptyFeed a2 = readATOMFeed((emptyFeed,)) s2 = writeATOMFeed(a2) assert car(s2) == emptyFeed - i3 = (("item", "cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b", - (element, "'item", (element, "'name", "Apple"), (element, "'price", "$2.99"))), - ("item", "cart-53d67a61-aa5e-4e5e-8401-39edeba8b83c", - (element, "'item", (element, "'name", "Orange"), (element, "'price", "$3.55")))) - a3 = cons("Feed", cons("1234", i3)) + i3 = ((element, "'entry", (element, "'title", "item"), (element, "'id", "cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), + (element, "'content", (element, "'item", (element, "'name", "Apple"), (element, "'price", "$2.99")))), + (element, "'entry", (element, "'title", "item"), (element, "'id", "cart-53d67a61-aa5e-4e5e-8401-39edeba8b83c"), + (element, "'content", (element, "'item", (element, "'name", "Orange"), (element, "'price", "$3.55"))))) + a3 = (append((element, "'feed", (element, "'title", "Feed"), (element, "'id", "1234")), i3),) s3 = writeATOMFeed(a3) assert car(s3) == itemFeed - i4 = (("item", "cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b", - valueToElement(("'item", ("'name", "Apple"), ("'price", "$2.99")))), - ("item", "cart-53d67a61-aa5e-4e5e-8401-39edeba8b83c", - valueToElement(("'item", ("'name", "Orange"), ("'price", "$3.55"))))) - a4 = cons("Feed", cons("1234", i4)) - s4 = writeATOMFeed(a4) + a4 = readATOMFeed((itemFeed,)); + s4 = writeATOMFeed(a4); assert car(s4) == itemFeed - a5 = readATOMFeed((itemFeed,)); - s5 = writeATOMFeed(a5); + a5 = elementsToValues(readATOMFeed((itemFeed,))); + s5 = writeATOMFeed(valuesToElements(a5)); assert car(s5) == itemFeed - - i6 = (("item", "cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b", - (("'name", "Apple"), ("'price", "$2.99"))), - ("item", "cart-53d67a61-aa5e-4e5e-8401-39edeba8b83c", - (("'name", "Orange"), ("'price", "$3.55")))) - a6 = cons("Feed", cons("1234", i6)) - s6 = writeATOMFeed(feedValuesToElements(a6)) - assert car(s6) == itemFeed - return True if __name__ == "__main__": diff --git a/sca-cpp/trunk/modules/wsgi/atomutil.py b/sca-cpp/trunk/modules/wsgi/atomutil.py index 8e812abbe9..ad6425f062 100644 --- a/sca-cpp/trunk/modules/wsgi/atomutil.py +++ b/sca-cpp/trunk/modules/wsgi/atomutil.py @@ -22,31 +22,27 @@ from elemutil import * from xmlutil import * # Convert a list of elements to a list of values representing an ATOM entry -def entryElementsToValues(e): +def entryElementValues(e): lt = filter(selector((element, "'title")), e) t = "" if isNil(lt) else elementValue(car(lt)) li = filter(selector((element, "'id")), e) i = "" if isNil(li) else elementValue(car(li)) lc = filter(selector((element, "'content")), e) - return (t, i, elementValue(car(lc))) + return append((element, "'entry", (element, "'title", t), (element, "'id", i)), + () if isNil(lc) else ((element, "'content", elementValue(car(lc))),)) # Convert a list of elements to a list of values representing ATOM entries -def entriesElementsToValues(e): +def entriesElementValues(e): if isNil(e): return e - return cons(entryElementsToValues(car(e)), entriesElementsToValues(cdr(e))) + return cons(entryElementValues(car(e)), entriesElementValues(cdr(e))) # Convert a list of strings to a list of values representing an ATOM entry def readATOMEntry(l): e = readXML(l) if isNil(e): return () - return entryElementsToValues(car(e)) - -# Convert a list of values representing an ATOM entry to a value -def entryValue(e): - v = elementsToValues((caddr(e),)) - return cons(car(e), (cadr(e), cdr(car(v)))) + return (entryElementValues(car(e)),) # Return true if a list of strings represents an ATOM feed def isATOMFeed(l): @@ -68,26 +64,23 @@ def readATOMFeed(l): t = filter(selector((element, "'title")), car(f)) i = filter(selector((element, "'id")), car(f)) e = filter(selector((element, "'entry")), car(f)) - if isNil(e): - return (elementValue(car(t)), elementValue(car(i))) - return cons(elementValue(car(t)), cons(elementValue(car(i)), entriesElementsToValues(e))) - -# Convert an ATOM feed containing elements to an ATOM feed containing values -def feedValuesLoop(e): - if (isNil(e)): - return e - return cons(entryValue(car(e)), feedValuesLoop(cdr(e))) - -def feedValues(e): - return cons(car(e), cons(cadr(e), feedValuesLoop(cddr(e)))) + return (append( + (element, "'feed", (element, "'title", elementValue(car(t))), (element, "'id", elementValue(car(i)))), + entriesElementValues(e)),) # Convert a list of values representy an ATOM entry to a list of elements def entryElement(l): - return (element, "'entry", (attribute, "'xmlns", "http://www.w3.org/2005/Atom"), - (element, "'title", (attribute, "'type", "text"), car(l)), - (element, "'id", cadr(l)), - (element, "'content", (attribute, "'type", ("application/xml" if isList(caddr(l)) else "text")), caddr(l)), - (element, "'link", (attribute, "'href", cadr(l)))) + title = elementValue(namedElementChild("'title", l)) + id = elementValue(namedElementChild("'id", l)) + content = namedElementChild("'content", l) + text = False if isNil(content) else elementHasValue(content) + return append(append( + (element, "'entry", (attribute, "'xmlns", "http://www.w3.org/2005/Atom"), + (element, "'title", (attribute, "'type", "text"), title), + (element, "'id", id)), + () if isNil(content) else (append( + (element, "'content", (attribute, "'type", "text" if text else "application/xml")), (elementValue(content),) if text else elementChildren(content)),)), + ((element, "'link", (attribute, "'href", id)),)) # Convert a list of values representing ATOM entries to a list of elements def entriesElements(l): @@ -96,31 +89,32 @@ def entriesElements(l): return cons(entryElement(car(l)), entriesElements(cdr(l))) # Convert a list of values representing an ATOM entry to an ATOM entry -def writeATOMEntry(l): +def writeATOMEntry(ll): + l = ll if isNil(ll) else car(ll) return writeXML((entryElement(l),), True) # Convert a list of values representing an ATOM feed to an ATOM feed -def writeATOMFeed(l): +def writeATOMFeed(ll): + l = ll if isNil(ll) else car(ll) + lt = filter(selector((element, "'title")), l) + t = '' if isNil(lt) else elementValue(car(lt)) + li = filter(selector((element, "'id")), l) + i = '' if isNil(li) else elementValue(car(li)) f = (element, "'feed", (attribute, "'xmlns", "http://www.w3.org/2005/Atom"), - (element, "'title", (attribute, "'type", "text"), car(l)), - (element, "'id", cadr(l))) - if isNil(cddr(l)): - return writeXML((f,), True) - fe = append(f, entriesElements(cddr(l))) - return writeXML((fe,), True) + (element, "'title", (attribute, "'type", "text"), t), + (element, "'id", i)) -# Convert an ATOM entry containing a value to an ATOM entry containing an item element -def entryValuesToElements(v): - if isList(caddr(v)): - return cons(car(v), cons(cadr(v), valuesToElements((cons("'item", caddr(v)),)))) - return cons(car(v), cons(cadr(v), valuesToElements((("'item", caddr(v)),)))) + # Write ATOM entries + le = filter(selector((element, "'entry")), l) + if isNil(le): + return writeXML((f,), True) -# Convert an ATOM feed containing values to an ATOM feed containing elements -def feedValuesToElementsLoop(v): - if isNil(v): - return v - return cons(entryValuesToElements(car(v)), feedValuesToElementsLoop(cdr(v))) + # Write a single ATOM entry element with a list of values + if not isNil(le) and not isNil(car(le)) and isList(car(caddr(car(le)))): + fe = append(f, entriesElements(caddr(car(le)))) + return writeXML((fe,), True) -def feedValuesToElements(v): - return cons(car(v), cons(cadr(v), feedValuesToElementsLoop(cddr(v)))) + # Write separate ATOM entry elements + fe = append(f, entriesElements(le)) + return writeXML((fe,), True) diff --git a/sca-cpp/trunk/modules/wsgi/composite.py b/sca-cpp/trunk/modules/wsgi/composite.py index 28a38ad386..baea7aa053 100755 --- a/sca-cpp/trunk/modules/wsgi/composite.py +++ b/sca-cpp/trunk/modules/wsgi/composite.py @@ -179,18 +179,32 @@ def application(e, r): if m == "GET": v = comp("get", id) + # Write a simple value as a JSON value + if not isList(v): + return result(e, r, 200, (("Content-type", "application/json"),), writeJSON(valuesToElements((("'value", v),)))) + + # Write an empty list as a JSON empty value + if not isList(v): + return result(e, r, 200, (("Content-type", "application/json"),), writeJSON(())) + # Write content-type / content-list pair - if isString(car(v)) and isList(cadr(v)): + if isString(car(v)) and not isNil(cdr(v)) and isList(cadr(v)): return result(e, r, 200, (("Content-type", car(v)),), cadr(v)) - # Write an ATOM feed or entry - if isString(car(v)) and isString(cadr(v)): - if isNil(id): - return result(e, r, 200, (("Content-type", "application/atom+xml"),), writeATOMFeed(feedValuesToElements(v))) - return result(e, r, 200, (("Content-type", "application/atom+xml"),), writeATOMEntry(entryValuesToElements(v))) + # Convert list of values to element values + ve = valuesToElements(v) + + # Write an assoc result as a JSON value + if isList(car(ve)) and not isNil(car(ve)): + el = car(ve) + if isSymbol(car(el)) and car(el) == element and not isNil(cdr(el)) and isSymbol(cadr(el)): + if cadr(el) == "'feed": + return result(e, r, 200, (("Content-type", "application/atom+xml"),), writeATOMFeed(ve)) + if cadr(el) == "'entry": + return result(e, r, 200, (("Content-type", "application/atom+xml"),), writeATOMEntry(ve)) # Write a JSON value - return result(e, r, 200, (("Content-type", "application/json"),), writeJSON(valuesToElements(v))) + return result(e, r, 200, (("Content-type", "application/json"),), writeJSON(ve)) if m == "POST": ct = requestContentType(e) @@ -208,7 +222,7 @@ def application(e, r): # Handle an ATOM entry POST if contains(ct, "application/atom+xml"): - ae = entryValue(readATOMEntry(requestBody(e))) + ae = elementsToValues(readATOMEntry(requestBody(e))) v = comp("post", id, ae) if isNil(v): return failure(e, r, 500) @@ -217,7 +231,7 @@ def application(e, r): if m == "PUT": # Handle an ATOM entry PUT - ae = entryValue(readATOMEntry(requestBody(e))) + ae = elementsToValues(readATOMEntry(requestBody(e))) v = comp("put", id, ae) if v == False: return failure(e, r, 404) diff --git a/sca-cpp/trunk/modules/wsgi/server-test.py b/sca-cpp/trunk/modules/wsgi/server-test.py index 28f88efefc..610ec05075 100644 --- a/sca-cpp/trunk/modules/wsgi/server-test.py +++ b/sca-cpp/trunk/modules/wsgi/server-test.py @@ -25,14 +25,15 @@ def echo(x): def get(id): if id == ("index.html",): return ("text/plain", ("It works!",)) + if id == (): - return ("Sample Feed", "123456789", - ("Item", "111", (("'name", "Apple"), ("'currencyCode", "USD"), ("'currencySymbol", "$"), ("'price", 2.99))), - ("Item", "222", (("'name", "Orange"), ("'currencyCode", "USD"), ("'currencySymbol", "$"), ("'price", 3.55))), - ("Item", "333", (("'name", "Pear"), ("'currencyCode", "USD"), ("'currencySymbol", "$"), ("'price", 1.55)))) - - entry = (("'name", "Apple"), ("'currencyCode", "USD"), ("'currencySymbol", "$"), ("'price", 2.99)) - return ("Item", id[0], entry) + return (("'feed", ("'title", "Sample Feed"), ("'id", "123456789"), ("'entry", + ((("'title", "Item"), ("'id", "111"), ("'content", ("'item", ("'name", "Apple"), ("'currencyCode", "USD"), ("'currencySymbol", "$"), ("'price", 2.99)))), + (("'title", "Item"), ("'id", "222"), ("'content", ("'item", ("'name", "Orange"), ("'currencyCode", "USD"), ("'currencySymbol", "$"), ("'price", 3.55)))), + (("'title", "Item"), ("'id", "333"), ("'content", ("'item", ("'name", "Pear"), ("'currencyCode", "USD"), ("'currencySymbol", "$"), ("'price", 1.55))))))),) + + content = ("'content", ("'item", ("'name", "Apple"), ("'currencyCode", "USD"), ("'currencySymbol", "$"), ("'price", 2.99))) + return (("'entry", ("'title", "Item"), ("'id", id[0]), content),) def post(collection, item): return ("123456789",) diff --git a/sca-cpp/trunk/modules/wsgi/util.py b/sca-cpp/trunk/modules/wsgi/util.py index 80bc7db101..24467fd2cb 100644 --- a/sca-cpp/trunk/modules/wsgi/util.py +++ b/sca-cpp/trunk/modules/wsgi/util.py @@ -43,6 +43,12 @@ def cddr(l): def caddr(l): return car(cddr(l)) +def cdddr(l): + return cdr(cdr(cdr(l))) + +def cadddr(l): + return car(cdddr(l)) + def append(a, b): return a + b |