From 4d39af8d89233001d607519a6d94a82d67818779 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 15 Oct 2008 21:27:16 +0000 Subject: In python 2.6 and later, simplejson is built-in as json. git-svn-id: file:///home/or/svnrepo/updater/trunk@17112 55e972cd-5a19-0410-ae62-a4d7a52db4cd --- lib/thandy/ServerCLI.py | 11 +++++++---- lib/thandy/SignerCLI.py | 21 ++++++++++++--------- lib/thandy/formats.py | 8 ++++++-- lib/thandy/keys.py | 10 +++++++--- lib/thandy/repository.py | 8 ++++++-- lib/thandy/util.py | 7 +++++-- 6 files changed, 43 insertions(+), 22 deletions(-) (limited to 'lib/thandy') diff --git a/lib/thandy/ServerCLI.py b/lib/thandy/ServerCLI.py index 2090ed9..794e119 100644 --- a/lib/thandy/ServerCLI.py +++ b/lib/thandy/ServerCLI.py @@ -5,7 +5,10 @@ import sys import getopt import time -import simplejson +try: + import json +except: + import simplejson as json import thandy.formats import thandy.util @@ -24,7 +27,7 @@ def snarf(fname): def snarfObj(fname): f = open(fname, 'r') try: - return simplejson.load(f) + return json.load(f) finally: f.close() @@ -65,7 +68,7 @@ def insert(args): continue try: - obj = simplejson.loads(content) + obj = json.loads(content) except ValueError, e: print "Couldn't decode %s: %s"%(fn, e) continue @@ -165,7 +168,7 @@ def timestamp(args): for k in keydb.iterkeys(): thandy.formats.sign(signable, k) - content = simplejson.dumps(signable, sort_keys=True) + content = json.dumps(signable, sort_keys=True) thandy.util.replaceFile(tsFname, content) def usage(): diff --git a/lib/thandy/SignerCLI.py b/lib/thandy/SignerCLI.py index d002565..524981a 100644 --- a/lib/thandy/SignerCLI.py +++ b/lib/thandy/SignerCLI.py @@ -4,7 +4,10 @@ import os import getopt import sys import logging -import simplejson +try: + import json +except ImportError: + import simplejson as json import thandy.keys import thandy.formats @@ -75,7 +78,7 @@ def makepackage(args): location = os.path.split(package['location'])[-1] print "Writing signed package to %s"%location f = open(location, 'w') - simplejson.dump(signable, f, indent=1) + json.dump(signable, f, indent=1) f.close() def makebundle(args): @@ -93,7 +96,7 @@ def makebundle(args): for pkgFile in args[1:]: print "Loading", pkgFile f = open(pkgFile, 'r') - p = simplejson.load(f) + p = json.load(f) f.close() _, r, _ = thandy.formats.checkSignedObj(p) if r != 'package': @@ -115,7 +118,7 @@ def makebundle(args): location = os.path.split(bundleObj['location'])[-1] print "Writing signed bundle to %s"%location f = open(location, 'w') - simplejson.dump(signable, f, indent=1) + json.dump(signable, f, indent=1) f.close() # ------------------------------ @@ -143,14 +146,14 @@ def makekeylist(args): print "writing signed keylist to keys.txt" thandy.util.replaceFile("keys.txt", - simplejson.dumps(signable, indent=1, sort_keys=True), + json.dumps(signable, indent=1, sort_keys=True), textMode=True) def signkeylist(args): if len(args) != 1: usage() - keylist = simplejson.load(open(args[0], 'r')) + keylist = json.load(open(args[0], 'r')) thandy.formats.SIGNED_SCHEMA.checkMatch(keylist) thandy.formats.KEYLIST_SCHEMA.checkMatch(keylist['signed']) @@ -162,7 +165,7 @@ def signkeylist(args): print "writing signed keylist to keys.txt" thandy.util.replaceFile("keys.txt", - simplejson.dumps(keylist, indent=1, sort_keys=True), + json.dumps(keylist, indent=1, sort_keys=True), textMode=True) def makemirrorlist(args): @@ -189,7 +192,7 @@ def makemirrorlist(args): print "writing signed mirrorlist to mirrors.txt" thandy.util.replaceFile("mirrors.txt", - simplejson.dumps(signable, indent=1, sort_keys=True), + json.dumps(signable, indent=1, sort_keys=True), textMode=True) # ------------------------------ @@ -277,7 +280,7 @@ def dumpkey(args): for k in keys: data = k.format(private=includeSecret, includeRoles=True) - print "Key(", simplejson.dumps(data, indent=2), ")" + print "Key(", json.dumps(data, indent=2), ")" def usage(): print "Known commands:" diff --git a/lib/thandy/formats.py b/lib/thandy/formats.py index c44c9cf..42e3d79 100644 --- a/lib/thandy/formats.py +++ b/lib/thandy/formats.py @@ -1,6 +1,10 @@ # Copyright 2008 The Tor Project, Inc. See LICENSE for licensing information. -import simplejson +try: + import json +except ImportError: + import simplejson as json + import time import re import binascii @@ -154,7 +158,7 @@ def checkSignatures(signed, keyDB, role=None, path=None): return SignatureStatus(goodSigs, badSigs, unknownSigs, tangentialSigs) def _encodeCanonical(obj, outf): - # Helper for encodeCanonical. Older versions of simplejson.encoder don't + # Helper for encodeCanonical. Older versions of json.encoder don't # even let us replace the separators. def canonical_str_encoder(s): diff --git a/lib/thandy/keys.py b/lib/thandy/keys.py index f4bcac9..5442b26 100644 --- a/lib/thandy/keys.py +++ b/lib/thandy/keys.py @@ -11,9 +11,13 @@ import logging import os import struct import sys -import simplejson import getpass +try: + import json +except ImportError: + import simplejson as json + import thandy.formats import thandy.util @@ -380,7 +384,7 @@ class KeyStore(thandy.formats.KeyDB): if self._encrypted: contents = decryptSecret(contents, password) - listOfKeys = simplejson.loads(contents) + listOfKeys = json.loads(contents) self._passwd = password # It worked. if not listOfKeys.has_key('keys'): listOfKeys['keys'] = [] @@ -409,7 +413,7 @@ class KeyStore(thandy.formats.KeyDB): [ key.format(private=True, includeRoles=True) for key in self._keys.values() ] } - contents = simplejson.dumps(listOfKeys) + contents = json.dumps(listOfKeys) if self._encrypted: contents = encryptSecret(contents, password) thandy.util.replaceFile(self._fname, contents) diff --git a/lib/thandy/repository.py b/lib/thandy/repository.py index 32108c0..1385dd6 100644 --- a/lib/thandy/repository.py +++ b/lib/thandy/repository.py @@ -3,7 +3,11 @@ import thandy.formats import thandy.util -import simplejson +try: + import json +except ImportError: + import simplejson as json + import logging import os import threading @@ -70,7 +74,7 @@ class RepositoryFile: def _checkContent(self, content): try: - obj = simplejson.loads(content) + obj = json.loads(content) except ValueError, e: raise thandy.FormatException("Couldn't decode content: %s"%e) diff --git a/lib/thandy/util.py b/lib/thandy/util.py index 2b1c572..b7281ae 100644 --- a/lib/thandy/util.py +++ b/lib/thandy/util.py @@ -4,7 +4,10 @@ import os import sys import tempfile -import simplejson +try: + import json +except ImportError: + import simplejson as json import thandy.formats import thandy.keys @@ -60,7 +63,7 @@ def getKeylist(keys_fname, checkKeys=True): if keys_fname and os.path.exists(keys_fname): f = open(keys_fname, 'r') try: - obj = simplejson.load(f) + obj = json.load(f) finally: f.close() ss, role, path = thandy.formats.checkSignedObj(obj, keydb) -- cgit v1.2.3