[CalendarServer-changes] [13033] CalendarServer/branches/users/sagen/move2who-4/txdav/dps/json.py

source_changes at macosforge.org source_changes at macosforge.org
Fri Mar 28 14:30:15 PDT 2014


Revision: 13033
          http://trac.calendarserver.org//changeset/13033
Author:   wsanchez at apple.com
Date:     2014-03-28 14:30:15 -0700 (Fri, 28 Mar 2014)
Log Message:
-----------
Start on expressionFromJSONText()

Modified Paths:
--------------
    CalendarServer/branches/users/sagen/move2who-4/txdav/dps/json.py

Modified: CalendarServer/branches/users/sagen/move2who-4/txdav/dps/json.py
===================================================================
--- CalendarServer/branches/users/sagen/move2who-4/txdav/dps/json.py	2014-03-28 20:57:18 UTC (rev 13032)
+++ CalendarServer/branches/users/sagen/move2who-4/txdav/dps/json.py	2014-03-28 21:30:15 UTC (rev 13033)
@@ -35,7 +35,8 @@
 
 
 def expressionAsJSONText(expression):
-    return to_json_text(expressionAsJSON(expression))
+    json = expressionAsJSON(expression)
+    return to_json_text(json)
 
 
 def expressionAsJSON(expression):
@@ -70,13 +71,71 @@
 
 
 def expressionFromJSONText(jsonText):
-    Operand
-    MatchType, MatchFlags
-    from_json_text
+    json = from_json_text(jsonText)
+    return expressionFromJSON(json)
+
+
+def expressionFromJSON(json):
+    if not isinstance(json, dict):
+        raise TypeError("JSON expression must be a dict.")
+
+    try:
+        json_type = json["type"]
+    except KeyError as e:
+        raise ValueError("JSON expression must have {!r} key.".format(e[0]))
+
+    if json_type == "CompoundExpression":
+        return compoundExpressionFromJSON(json)
+
+    if json_type == "MatchExpression":
+        return matchExpressionFromJSON(json)
+
+    raise NotImplementedError(
+        "Unknown expression type: {}".format(json_type)
+    )
+
+
+def compoundExpressionFromJSON(json):
+    try:
+        expressions_json = json["expressions"]
+        operand_json = json["operand"]
+    except KeyError as e:
+        raise ValueError(
+            "JSON compound expression must have {!r} key.".format(e[0])
+        )
+
+    expressions = tuple(expressionFromJSON(e) for e in expressions_json)
+    operand = Operand.lookupByName(operand_json)
+
+    return CompoundExpression(expressions, operand)
+
+
+def matchExpressionFromJSON(json):
+    try:
+        field_json = json["field"]
+        value_json = json["value"]
+        match_json = json["match"]
+        flags_json = json["flags"]
+    except KeyError as e:
+        raise ValueError(
+            "JSON match expression must have {!r} key.".format(e[0])
+        )
+
     raise NotImplementedError()
 
+    fieldName = NotImplemented, field_json   # Need service...
+    fieldValue = NotImplemented, value_json  # Need to cast to correct value
+    matchType = MatchType.lookupByName(match_json)
+    flags = NotImplemented, flags_json       # Need to handle composite flags
 
+    MatchFlags  # Shh, flakes
 
+    return MatchExpression(
+        fieldName, fieldValue,
+        matchType=matchType, flags=flags,
+    )
+
+
 def to_json_text(obj):
     """
     Convert an object into JSON text.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140328/29c6db3e/attachment.html>


More information about the calendarserver-changes mailing list