summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRune F. Halvorsen <runefh@gmail.com>2009-07-21 14:44:37 +0200
committerRune F. Halvorsen <runefh@gmail.com>2009-07-21 14:44:37 +0200
commit819e12240de5b7b6a3f778ae124fe51fa204eb1e (patch)
tree3b8553fcdf345164e8ab2fab61d74f1f817542dc
parentd69effa02c490ed311ca5e5a0e978029728570bd (diff)
downloadanyjson-819e12240de5b7b6a3f778ae124fe51fa204eb1e.tar.gz
Added handing of __name__=="__main__". This should fix #1
-rw-r--r--anyjson.py44
1 files changed, 34 insertions, 10 deletions
diff --git a/anyjson.py b/anyjson.py
index 6603751..08770ae 100644
--- a/anyjson.py
+++ b/anyjson.py
@@ -104,14 +104,38 @@ def force_implementation(modname):
raise ImportError("No module named: %s" % modname)
-for modspec in _modules:
- try:
- implementation = _JsonImplementation(modspec)
- break
- except ImportError:
- pass
-else:
- raise ImportError("No supported JSON module found")
-serialize = lambda value: implementation.serialize(value)
-deserialize = lambda value: implementation.deserialize(value)
+def main():
+ installed = []
+ for modspec in _modules:
+ try:
+ __import__(modspec[0])
+ installed.append(modspec[0])
+ except ImportError:
+ pass
+
+ if installed:
+ print "Supported JSON modules found:", ", ".join(installed)
+ return 0
+ else:
+ print "No supported JSON modules found"
+ return 1
+
+if __name__ == "__main__":
+ # If run as a script, we simply print what is installed that we support.
+ # We do NOT try to load a compatible module because that may throw an
+ # exception, which renders the package uninstallable with easy_install
+ # (It trys to execfile the script when installing, to make sure it works)
+ sys.exit(main())
+else:
+ for modspec in _modules:
+ try:
+ implementation = _JsonImplementation(modspec)
+ break
+ except ImportError:
+ pass
+ else:
+ raise ImportError("No supported JSON module found")
+
+ serialize = lambda value: implementation.serialize(value)
+ deserialize = lambda value: implementation.deserialize(value)