summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2008-11-22 19:13:10 +0000
committerNick Mathewson <nickm@torproject.org>2008-11-22 19:13:10 +0000
commitcff6c8bac612ae4ed598ef222ada02d3a83ecaa1 (patch)
tree4a64951ef30e6795ef2dcebff20c302751885227 /lib
parentd077d211bbfabeecf0b175df6bfb8e4e96a30747 (diff)
Add a quick and dirty thandy-client json2xml command. I am going to regret this someday.
git-svn-id: file:///home/or/svnrepo/updater/trunk@17366 55e972cd-5a19-0410-ae62-a4d7a52db4cd
Diffstat (limited to 'lib')
-rw-r--r--lib/thandy/ClientCLI.py17
-rw-r--r--lib/thandy/encodeToXML.py70
-rw-r--r--lib/thandy/formats.py3
-rw-r--r--lib/thandy/tests.py2
4 files changed, 87 insertions, 5 deletions
diff --git a/lib/thandy/ClientCLI.py b/lib/thandy/ClientCLI.py
index 68be6a1..d6f3adc 100644
--- a/lib/thandy/ClientCLI.py
+++ b/lib/thandy/ClientCLI.py
@@ -5,6 +5,10 @@ import logging
import os
import sys
import time
+try:
+ import json
+except ImportError:
+ import simplejson as json
import thandy.formats
import thandy.util
@@ -13,6 +17,7 @@ import thandy.download
import thandy.master_keys
import thandy.packagesys.PackageSystem
import thandy.socksurls
+import thandy.encodeToXML
def update(args):
repoRoot = thandy.util.userFilename("cache")
@@ -153,14 +158,20 @@ def update(args):
logging.info("All downloads finished.")
-# Tell me what to install.
-
+def json2xml(args):
+ if len(args) != 1:
+ usage()
+ f = open(args[0], 'r')
+ obj = json.load(f)
+ f.close()
+ thandy.encodeToXML.encodeToXML(obj, sys.stdout.write)
def usage():
print "Known commands:"
print " update [--repo=repository] [--no-download] [--loop]"
print " [--no-packagesys] [--install] [--socks-port=port]"
print " [--debug|--info|--warn] [--force-check]"
+ print " json2xml file"
sys.exit(1)
def main():
@@ -169,7 +180,7 @@ def main():
usage()
cmd = sys.argv[1]
args = sys.argv[2:]
- if cmd in [ "update" ]:
+ if cmd in [ "update", "json2xml" ]:
globals()[cmd](args)
else:
usage()
diff --git a/lib/thandy/encodeToXML.py b/lib/thandy/encodeToXML.py
new file mode 100644
index 0000000..4387c88
--- /dev/null
+++ b/lib/thandy/encodeToXML.py
@@ -0,0 +1,70 @@
+# Copyright 2008 The Tor Project, Inc. See LICENSE for licensing information.
+
+import re
+
+def xml_str_encoder(s):
+ s = s.replace("&", "&amp;")
+ s = s.replace("<", "&lt;")
+ s = s.replace(">", "&gt;")
+ return s
+
+def isAsciiName(s):
+ """
+ Return true iff s is pure-ascii, and a syntactically valid XML name.
+
+ >>> isAsciiName("a")
+ True
+ >>> isAsciiName("ab.-dc")
+ True
+ >>> isAsciiName("")
+ False
+ >>> isAsciiName(".foo")
+ False
+ """
+ return re.match(r'^[A-Za-z\_\:][A-Za-z0-9\_\:\-\.]*$', s) != None
+
+def _encodeToXML(obj, outf, indent=0):
+ if isinstance(obj, basestring):
+ outf(xml_str_encoder(obj))
+ elif obj is True:
+ outf("true")
+ elif obj is False:
+ outf("false")
+ elif obj is None:
+ outf("null")
+ elif isinstance(obj, (int,long)):
+ outf(str(obj))
+ elif isinstance(obj, (tuple, list)):
+ istr = " "*indent
+ outf("<list>\n")
+ for item in obj:
+ outf("<item>")
+ _encodeToXML(item, outf)
+ outf("</item> ")
+ outf("</list>\n")
+ elif isinstance(obj, dict):
+ outf("<dict>\n")
+ for k,v in sorted(obj.items()):
+ isAscii = isAsciiName(k)
+ if isAscii:
+ outf("<%s>"%k)
+ _encodeToXML(v, outf)
+ outf("</%s>\n"%k)
+ else:
+ outf("<dict-entry><key>%s</key><val>"%xml_str_encoder(k))
+ _encodeToXML(v, outf)
+ outf("</val></dict-entry>\n")
+ outf("</dict>\n")
+ else:
+ raise thandy.FormatException("I can't encode %r"%obj)
+
+def encodeToXML(obj, outf=None):
+ """Convert a json-encodable object to a quick-and-dirty XML equivalent."""
+ result = None
+ if outf == None:
+ outf = result.append
+
+ _encodeToXML(obj, outf)
+ if result is not None:
+ return "".join(result)
+
diff --git a/lib/thandy/formats.py b/lib/thandy/formats.py
index cf9ff1f..1fd0eb1 100644
--- a/lib/thandy/formats.py
+++ b/lib/thandy/formats.py
@@ -173,7 +173,7 @@ def _encodeCanonical(obj, outf):
elif obj is True:
outf("true")
elif obj is False:
- outf("false")
+ outf("false")
elif obj is None:
outf("null")
elif isinstance(obj, (int,long)):
@@ -235,7 +235,6 @@ def encodeCanonical(obj, outf=None):
if result is not None:
return "".join(result)
-
def getDigest(obj, digestObj=None):
"""Update 'digestObj' (typically a SHA256 object) with the digest of
the canonical json encoding of obj. If digestObj is none,
diff --git a/lib/thandy/tests.py b/lib/thandy/tests.py
index 924fc82..a064188 100644
--- a/lib/thandy/tests.py
+++ b/lib/thandy/tests.py
@@ -9,6 +9,7 @@ import thandy.keys
import thandy.formats
import thandy.repository
import thandy.checkJson
+import thandy.encodeToXML
import thandy.util
import thandy.tests
@@ -122,6 +123,7 @@ def suite():
suite.addTest(doctest.DocTestSuite(thandy.formats))
suite.addTest(doctest.DocTestSuite(thandy.keys))
suite.addTest(doctest.DocTestSuite(thandy.checkJson))
+ suite.addTest(doctest.DocTestSuite(thandy.encodeToXML))
loader = unittest.TestLoader()
suite.addTest(loader.loadTestsFromModule(thandy.tests))