summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain <syt@logilab.fr>2006-09-19 11:38:16 +0200
committerSylvain <syt@logilab.fr>2006-09-19 11:38:16 +0200
commitc60bed25b0d53ab9f604f353bb6f97e90a499847 (patch)
treeb462c0b0a1c7d62dcc1fd06e0dec7a41c6634238
parentcf88be2a16e01a12785ad0cfc9ba5cc69a143409 (diff)
downloadlogilab-common-c60bed25b0d53ab9f604f353bb6f97e90a499847.tar.gz
* db:
- fixed bug when querying boolean on sqlite using python's bool type - added "drop_on_commit" argument to create_temporary_table on db helper - added missing implementation of executemany on pysqlite2 wrapper to support pyargs correctly like execute * optik_ext: fixed "named" type option to support csv values and to return a dictionary
-rw-r--r--ChangeLog9
-rw-r--r--configuration.py4
-rw-r--r--db.py17
-rw-r--r--optik_ext.py18
-rw-r--r--test/unittest_configuration.py20
5 files changed, 53 insertions, 15 deletions
diff --git a/ChangeLog b/ChangeLog
index fa95d55..bf7fa24 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,8 +2,13 @@ ChangeLog for logilab.common
============================
--
- * db: fixed bug when querying boolean on sqlite using python's bool type
-
+ * db:
+ - fixed bug when querying boolean on sqlite using python's bool type
+ - added "drop_on_commit" argument to create_temporary_table on db helper
+ - added missing implementation of executemany on pysqlite2 wrapper to
+ support pyargs correctly like execute
+ * optik_ext: fixed "named" type option to support csv values and to return
+ a dictionary
2006-09-05 -- 0.19.0
* new umessage module which provides a class similar to the standard
diff --git a/configuration.py b/configuration.py
index dd149d5..81934de 100644
--- a/configuration.py
+++ b/configuration.py
@@ -258,8 +258,10 @@ def comment(string):
def format_option_value(optdict, value):
"""return the user input's value from a 'compiled' value"""
- if type(value) in (type(()), type([])):
+ if isinstance(value, (list, tuple)):
value = ','.join(value)
+ elif isinstance(value, dict):
+ value = ','.join(['%s:%s' % (k,v) for k,v in value.items()])
elif hasattr(value, 'match'): # optdict.get('type') == 'regexp'
# compiled regexp
value = value.pattern
diff --git a/db.py b/db.py
index d27145d..75ffbd7 100644
--- a/db.py
+++ b/db.py
@@ -313,6 +313,10 @@ class _PySqlite2Adapter(DBAPIAdapter):
else:
self.__class__.__bases__[0].execute(self, sql)
+ def executemany(self, sql, kwargss):
+ sql = re.sub(r'%\(([^\)]+)\)s', r':\1', sql)
+ self.__class__.__bases__[0].executemany(self, sql, kwargss)
+
class PySqlite2CnxWrapper:
def __init__(self, cnx):
self._cnx = cnx
@@ -402,9 +406,10 @@ INSERT INTO %s VALUES (0);''' % (seq_name, seq_name)
return ('UPDATE %s SET last=last+1;' % seq_name,
'SELECT last FROM %s;' % seq_name)
- def sql_temporary_table(self, table_name, table_schema):
+ def sql_temporary_table(self, table_name, table_schema,
+ drop_on_commit=True):
return "CREATE TEMPORARY TABLE %s (%s);" % (table_name,
- table_schema)
+ table_schema)
def increment_sequence(self, cursor, seq_name):
for sql in self.sqls_increment_sequence(seq_name):
@@ -429,9 +434,13 @@ class _PGAdvFuncHelper(_GenericAdvFuncHelper):
def sqls_increment_sequence(self, seq_name):
return ("SELECT nextval('%s');" % seq_name,)
- def sql_temporary_table(self, table_name, table_schema):
+ def sql_temporary_table(self, table_name, table_schema,
+ drop_on_commit=True):
+ if not drop_on_commit:
+ return "CREATE TEMPORARY TABLE %s (%s);" % (table_name,
+ table_schema)
return "CREATE TEMPORARY TABLE %s (%s) ON COMMIT DROP;" % (table_name,
- table_schema)
+ table_schema)
def sql_current_date(self):
return "'TODAY'"
diff --git a/optik_ext.py b/optik_ext.py
index 17f0513..6eb73e6 100644
--- a/optik_ext.py
+++ b/optik_ext.py
@@ -78,7 +78,7 @@ def check_csv(option, opt, value):
return get_csv(value)
except ValueError:
raise OptionValueError(
- "option %s: invalid regexp value: %r" % (opt, value))
+ "option %s: invalid csv value: %r" % (opt, value))
def check_yn(option, opt, value):
"""check a yn value
@@ -95,14 +95,18 @@ def check_yn(option, opt, value):
def check_named(option, opt, value):
"""check a named value
- return a 2-uple (name, value)
+ return a dictionnary containing (name, value) associations
"""
- if isinstance(value, (list, tuple)) and len(value) % 2 == 0:
+ if isinstance(value, dict):
return value
- if value.find('=') != -1:
- return value.split('=', 1)
- if value.find(':') != -1:
- return value.split(':', 1)
+ values = []
+ for value in check_csv(option, opt, value):
+ if value.find('=') != -1:
+ values.append(value.split('=', 1))
+ elif value.find(':') != -1:
+ values.append(value.split(':', 1))
+ if values:
+ return dict(values)
msg = "option %s: invalid named value %r, should be <NAME>=<VALUE> or \
<NAME>:<VALUE>"
raise OptionValueError(msg % (opt, value))
diff --git a/test/unittest_configuration.py b/test/unittest_configuration.py
index f904360..bbcdb31 100644
--- a/test/unittest_configuration.py
+++ b/test/unittest_configuration.py
@@ -1,6 +1,7 @@
import tempfile
import os
from cStringIO import StringIO
+from sys import version_info
from logilab.common.testlib import TestCase, unittest_main
from logilab.common.configuration import Configuration, OptionValueError, \
@@ -150,7 +151,24 @@ multiple-choice=yo,ye
# it is not unlikely some optik/optparse versions do print -v<string>
# so accept both
help = help.replace(' -v <string>, ', ' -v<string>, ')
- self.assertLinesEquals(help, """usage: Just do it ! (tm)
+ if version_info >= (2, 4):
+ self.assertLinesEquals(help, """usage: Just do it ! (tm)
+
+options:
+ -h, --help show this help message and exit
+ --dothis=<y or n>
+ -v<string>, --value=<string>
+ --multiple=<comma separated values>
+ you can also document the option [current: none]
+ --number=<int>
+ --choice=<yo|ye>
+ --multiple-choice=<yo|ye>
+
+ Bonus:
+ a nice additional help
+""".strip())
+ else:
+ self.assertLinesEquals(help, """usage: Just do it ! (tm)
options:
-h, --help show this help message and exit