1. Dan Connolly
  2. quacken

Commits

Dan Connolly  committed ae7c294

factor gckey keyring access module out of dbbak

  • Participants
  • Parent commits 93dbee6
  • Branches default

Comments (0)

Files changed (2)

File dbbak.py

View file
 from contextlib import contextmanager
 import logging
 
+from gckey import findMaker
+
 log = logging.getLogger(__name__)
 
 
 
     __ http://blogs.codecommunity.org/mindbending/bending-gnome-keyring-with-python-part-2/
 
+
+    >>> commands = []
+    >>> main([__name__, MockGK.my_object, 'out_dir'],
+    ...      lambda n: (),
+    ...      MockGK().find_network_password_sync,
+    ...      StringIO.StringIO,
+    ...      MockNTF,
+    ...      lambda args, stdout=None: commands.append(args))
+    >>> commands
+    ... #doctest: +NORMALIZE_WHITESPACE
+    [('mysqldump', '--defaults-file=tmp_config', '--tab=out_dir',
+      '--skip-dump-date', '--skip-comments', 'object0')]
     '''
     logging.basicConfig(level=level)
     set_application_name(__name__)
 
-    client_config = configMaker(NamedTemporaryFile, find_network_password_sync)
+    findcreds = findMaker(find_network_password_sync)
+    client_config = configMaker(NamedTemporaryFile, findcreds)
 
     if '--splits' in argv:
         db, destpath = argv[2:4]
 
 class MySQLRunner(object):
     def __init__(self, db,
-                 check_call, client_config,
-                 server='localhost', protocol='mysql'):
+                 check_call, client_config):
         self.db = db
         self._run = check_call
 
     cp.write(fp)
 
 
-def configMaker(NamedTemporaryFile, find_network_password_sync,
-                server='localhost', protocol='mysql'):
+def configMaker(NamedTemporaryFile, findcreds):
     '''Maker to get db credentials and store in temporary mysql config file.
 
     @param NamedTemporaryFile: a la python tempfile module
 
     Actually, the config file will get re-opened by name, but...
 
-    >>> cc = configMaker(MockNTF, MockGK().find_network_password_sync)
+    >>> cc = configMaker(MockNTF,
+    ...                  findMaker(MockGK().find_network_password_sync))
     >>> with cc(MockGK.my_object) as conf:
     ...     print conf.name
     ...     print conf.readline().strip()
     [client]
     password = sekret
     '''
-    def findcreds(db):
-        log.info('looking for keys: %s',
-                 dict(protocol=protocol, server=server, object=db))
-        ans = find_network_password_sync(protocol=protocol, server=server,
-                                         object=db)
-        log.debug('creds: %s', ans[0].keys())
-        return dict([(k, ans[0][k]) for k in ('user', 'password')])
-
     @contextmanager
     def client_config(db):
         conf = NamedTemporaryFile(suffix='.ini')
         self.name = 'tmp_config'
 
 
-class MockGK(object):
-    my_object = 'object0'
-    my_protocol = 'mysql'
-    my_server = 'localhost'
-
-    my_user = 'user0'
-    my_password = 'sekret'
-
-    def find_network_password_sync(self,
-                                   user=None,
-                                   domain=None,
-                                   server=None,
-                                   object=None,
-                                   protocol=None,
-                                   authtype=None,
-                                   port=0):
-        if (object == self.my_object
-            and protocol == self.my_protocol
-            and server == self.my_server):
-            return [dict(user=self.my_user, password=self.my_password)]
-
-        raise IOError  # well, really some other error, but...
-
-
 class FinDB(MySQLRunner):
     def split_detail(self, dest,
                      sql=('select * from split_detail'

File gckey.py

View file
+'''gckey -- gnome keyring access for GnuCash mysql database
+'''
+
+import logging
+
+log = logging.getLogger(__name__)
+
+
+def findMaker(find_network_password_sync,
+              server='localhost', protocol='mysql'):
+    '''Maker to get db credentials.
+
+    @param find_network_password_sync: a la gnome keyring API
+
+    The first keyring entry matching `server`, `protocol`, and the
+    database name will be used.
+
+    Actually, the config file will get re-opened by name, but...
+
+    >>> f = findMaker(MockGK().find_network_password_sync)
+    >>> f(MockGK.my_object)
+    {'password': 'sekret', 'user': 'user0'}
+    '''
+    def findcreds(db):
+        log.info('looking for keys: %s',
+                 dict(protocol=protocol, server=server, object=db))
+        ans = find_network_password_sync(protocol=protocol, server=server,
+                                         object=db)
+        log.debug('creds: %s', ans[0].keys())
+        return dict([(k, ans[0][k]) for k in ('user', 'password')])
+
+    return findcreds
+
+
+class MockGK(object):
+    my_object = 'object0'
+    my_protocol = 'mysql'
+    my_server = 'localhost'
+
+    my_user = 'user0'
+    my_password = 'sekret'
+
+    def find_network_password_sync(self,
+                                   user=None,
+                                   domain=None,
+                                   server=None,
+                                   object=None,
+                                   protocol=None,
+                                   authtype=None,
+                                   port=0):
+        if (object == self.my_object
+            and protocol == self.my_protocol
+            and server == self.my_server):
+            return [dict(user=self.my_user, password=self.my_password)]
+
+        raise IOError  # well, really some other error, but...
+
+
+def _integration_test(db, find_network_password_sync):
+    find = findMaker(find_network_password_sync)
+    print find(db)
+
+
+if __name__ == '__main__':
+    def _initial_capabilities():
+        import sys
+        import gnomekeyring as gk
+
+        db = sys.argv[1]
+        return dict(db=db,
+                    find_network_password_sync=gk.find_network_password_sync)
+
+    _integration_test(**_initial_capabilities())