Commits

Lars Yencken committed 163c99f

Add MySQL helper.

  • Participants
  • Parent commits 386d8b6

Comments (0)

Files changed (2)

drakeutil/__init__.py

 from shutil import copy, move, copytree, rmtree  # noqa
 from os import environ, path, rename, stat  # noqa
 from datetime_tz import datetime_tz
+from contextlib import contextmanager, closing
 
 import subprocess
 
         return datetime_tz.utcfromtimestamp(stat(filename).st_mtime)
     except OSError:
         return
+
+# optional MySQL support
+try:
+    import MySQLdb
+
+    @contextmanager
+    def mysql_cursor(host=None, port=3306, db=None, user=None, passwd=None):
+        with closing(MySQLdb.connect(host=host, port=port, db=db, user=user,
+                passwd=passwd)) as conn:
+            with closing(conn.cursor()) as cursor:
+                yield cursor
+
+except ImportError:
+    pass
             'drakeutil',
         ],
         install_requires=requires,
+        extras_require={
+                'mysql': ['MySQL-python>=1.2.3'],
+            }
     )