[Code] [PATCH 2/3] Enable the use of PostgreSQL in the example consumer

Daniel Kahn Gillmor dkg at fifthhorseman.net
Thu Apr 4 18:39:40 UTC 2013


Without this patch, examples/consumer.py can only be used to test the
FileStore and the MemoryStore.  With this patch, we can test the
PostgreSQLStore as well by running something like:

 python consumer.py -P 'dbname=openid user=foo'

Note that the database must be initialized before use with something
like:

  python -c 'import psycopg2; from openid.store import sqlstore; sqlstore.PostgreSQLStore(psycopg2.connect("dbname=openid user=foo")).createTables();'
---
 examples/consumer.py |   15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/examples/consumer.py b/examples/consumer.py
index 968cd42..3424700 100755
--- a/examples/consumer.py
+++ b/examples/consumer.py
@@ -35,6 +35,7 @@ distribution.""")
 
 from openid.store import memstore
 from openid.store import filestore
+from openid.store import sqlstore
 from openid.consumer import consumer
 from openid.oidutil import appendArgs
 from openid.cryptutil import randomString
@@ -449,12 +450,15 @@ Content-type: text/html; charset=UTF-8
 </html>
 ''' % (quoteattr(self.buildURL('verify')), quoteattr(form_contents)))
 
-def main(host, port, data_path, weak_ssl=False):
+def main(host, port, data_path, weak_ssl=False, postgresql=None):
     # Instantiate OpenID consumer store and OpenID consumer.  If you
     # were connecting to a database, you would create the database
     # connection and instantiate an appropriate store here.
     if data_path:
         store = filestore.FileOpenIDStore(data_path)
+    elif postgresql:
+        import psycopg2
+        store = sqlstore.PostgreSQLStore(psycopg2.connect(postgresql))
     else:
         store = memstore.MemoryStore()
 
@@ -472,6 +476,7 @@ if __name__ == '__main__':
     host = 'localhost'
     port = 8001
     weak_ssl = False
+    postgresql = None
 
     try:
         import optparse
@@ -484,6 +489,11 @@ if __name__ == '__main__':
             help='Data directory for storing OpenID consumer state. '
             'Setting this option implies using a "FileStore."')
         parser.add_option(
+            '-P', '--postgresql', dest='postgresql',
+            help='connection string (e.g. "dbname=openid user=foo") for storing '
+            'OpenID consumer state in a PostgreSQL database. '
+            'Setting this option implies using a "PostgreSQLStore"')
+        parser.add_option(
             '-p', '--port', dest='port', type='int', default=port,
             help='Port on which to listen for HTTP requests. '
             'Defaults to port %default.')
@@ -503,5 +513,6 @@ if __name__ == '__main__':
         port = options.port
         data_path = options.data_path
         weak_ssl = options.weakssl
+        postgresql = options.postgresql
 
-    main(host, port, data_path, weak_ssl)
+    main(host, port, data_path, weak_ssl, postgresql)
-- 
1.7.10.4



More information about the Code mailing list