From cff6c8bac612ae4ed598ef222ada02d3a83ecaa1 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Sat, 22 Nov 2008 19:13:10 +0000 Subject: 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 --- lib/thandy/ClientCLI.py | 17 ++++++++++-- lib/thandy/encodeToXML.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++ lib/thandy/formats.py | 3 +- lib/thandy/tests.py | 2 ++ 4 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 lib/thandy/encodeToXML.py (limited to 'lib') 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("&", "&") + s = s.replace("<", "<") + s = s.replace(">", ">") + 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("\n") + for item in obj: + outf("") + _encodeToXML(item, outf) + outf(" ") + outf("\n") + elif isinstance(obj, dict): + outf("\n") + for k,v in sorted(obj.items()): + isAscii = isAsciiName(k) + if isAscii: + outf("<%s>"%k) + _encodeToXML(v, outf) + outf("\n"%k) + else: + outf("%s"%xml_str_encoder(k)) + _encodeToXML(v, outf) + outf("\n") + outf("\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)) -- cgit v1.2.3