# HG changeset patch # User Radek Brich # Date 1363707937 -3600 # Node ID 64c62ac8f65de0aa9b4b9baca16bf47f05014ee9 # Parent 2cfef775f518cf0b9e1da54e6f1d0b341e5f408d Rename config module to configparser. diff -r 2cfef775f518 -r 64c62ac8f65d loopquery.py --- a/loopquery.py Fri Mar 15 15:13:42 2013 +0100 +++ b/loopquery.py Tue Mar 19 16:45:37 2013 +0100 @@ -6,7 +6,7 @@ """ -from pgtoolkit import toolbase, config +from pgtoolkit import toolbase import logging.handlers import time diff -r 2cfef775f518 -r 64c62ac8f65d pgtoolkit/coloredformatter.py --- a/pgtoolkit/coloredformatter.py Fri Mar 15 15:13:42 2013 +0100 +++ b/pgtoolkit/coloredformatter.py Tue Mar 19 16:45:37 2013 +0100 @@ -9,9 +9,7 @@ def format(self, record): color = () - if record.levelno == 21: - color = (2, None) - if record.levelno == logging.ERROR and not record.exc_info is None: + if record.levelno == logging.ERROR and record.exc_info is not None: color = (11, 1) if color: origmsg = record.msg diff -r 2cfef775f518 -r 64c62ac8f65d pgtoolkit/config.py --- a/pgtoolkit/config.py Fri Mar 15 15:13:42 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,45 +0,0 @@ -import logging - - -class ConfigParser: - def __init__(self): - self.args = {} # config file arguments - self.registered_args = {} - self.log = logging.getLogger('config') - - def add_argument(self, name, type=str, default=None): - self.registered_args[name] = {'type':type, 'default':default} - self.args[name] = default - - def load(self, fname): - # parse config file - with open(fname) as f: - exec(f.read(), self.args) - # check contents - return self.check() - - def check(self): - ok = True - for key in self.args.keys(): - if key == '__builtins__': - continue - if key in self.registered_args: - # arg registered, check type - type = self.registered_args[key]['type'] - if not isinstance(self.args[key], type) and not self.args[key] is None: - ok = False - self.log.error("Bad type of config parameter '%s': is %s but should be %s", - key, type(self.args[key]), type) - else: - # arg not registered - ok = False - self.log.error("Unknown config parameter '%s'.", key) - return ok - - def __getattr__(self, name): - if name in self.args: - return self.args[name] - - # raise error if not found - raise AttributeError() - diff -r 2cfef775f518 -r 64c62ac8f65d pgtoolkit/configparser.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pgtoolkit/configparser.py Tue Mar 19 16:45:37 2013 +0100 @@ -0,0 +1,45 @@ +import logging + + +class ConfigParser: + def __init__(self): + self.args = {} # config file arguments + self.registered_args = {} + self.log = logging.getLogger('config') + + def add_argument(self, name, type=str, default=None): + self.registered_args[name] = {'type':type, 'default':default} + self.args[name] = default + + def load(self, fname): + # parse config file + with open(fname) as f: + exec(f.read(), self.args) + # check contents + return self.check() + + def check(self): + ok = True + for key in self.args.keys(): + if key == '__builtins__': + continue + if key in self.registered_args: + # arg registered, check type + type = self.registered_args[key]['type'] + if not isinstance(self.args[key], type) and not self.args[key] is None: + ok = False + self.log.error("Bad type of config parameter '%s': is %s but should be %s", + key, type(self.args[key]), type) + else: + # arg not registered + ok = False + self.log.error("Unknown config parameter '%s'.", key) + return ok + + def __getattr__(self, name): + if name in self.args: + return self.args[name] + + # raise error if not found + raise AttributeError() + diff -r 2cfef775f518 -r 64c62ac8f65d pgtoolkit/toolbase.py --- a/pgtoolkit/toolbase.py Fri Mar 15 15:13:42 2013 +0100 +++ b/pgtoolkit/toolbase.py Tue Mar 19 16:45:37 2013 +0100 @@ -2,7 +2,7 @@ import logging import re -from pgtoolkit import pgmanager, pgbrowser, config +from pgtoolkit import pgmanager, pgbrowser, configparser from pgtoolkit.coloredformatter import ColoredFormatter from pgtoolkit.highlight import highlight @@ -21,7 +21,7 @@ self.parser.add_argument('-d', dest='debug', action='store_true', help='Debug mode - print database queries.') - self.config = config.ConfigParser() + self.config = configparser.ConfigParser() self.config.add_argument('databases', type=dict) self.config.add_argument('meta_db') self.config.add_argument('meta_query') diff -r 2cfef775f518 -r 64c62ac8f65d runquery.py --- a/runquery.py Fri Mar 15 15:13:42 2013 +0100 +++ b/runquery.py Tue Mar 19 16:45:37 2013 +0100 @@ -6,7 +6,7 @@ """ -from pgtoolkit import toolbase, config +from pgtoolkit import toolbase import logging.handlers import time diff -r 2cfef775f518 -r 64c62ac8f65d tests/delayedquery.py --- a/tests/delayedquery.py Fri Mar 15 15:13:42 2013 +0100 +++ b/tests/delayedquery.py Tue Mar 19 16:45:37 2013 +0100 @@ -3,34 +3,34 @@ import logging import time -from config import Config +from tests.config import Config from pgtoolkit import pgmanager from pgtoolkit.delayedquery import DelayedQuery if __name__ == '__main__': cfg = Config('tests.conf') - + pgm = pgmanager.get_instance() pgm.create_conn(isolation_level = 'autocommit', **cfg) - + handler = logging.StreamHandler() logger = logging.getLogger('pgmanager') logger.addHandler(handler) logger.setLevel(logging.DEBUG) dq = DelayedQuery(pgm) - + print('add 1') dq.add(10, 'INSERT INTO test (name) VALUES (%s)', ['spam1'], queryid=1) dq.add(10, 'INSERT INTO test (name) VALUES (%s)', ['spam1'], queryid=1) print('after 1') - + time.sleep(5) - + print('add 2') dq.add(10, 'INSERT INTO test (name) VALUES (%s)', ['spam2']) - + time.sleep(5) print('after 10') diff -r 2cfef775f518 -r 64c62ac8f65d tests/multiprocess.py --- a/tests/multiprocess.py Fri Mar 15 15:13:42 2013 +0100 +++ b/tests/multiprocess.py Tue Mar 19 16:45:37 2013 +0100 @@ -9,7 +9,7 @@ where it was created -- no sharing between processes. get_conn and cursor will still work after fork. Connections from parent process will -be forgotten, that cannot be used un child process. +be forgotten, that cannot be used un child process. Connections cannot be shared between processes, DO NOT: * get_conn, then fork, then put_conn @@ -25,7 +25,7 @@ import multiprocessing -from config import Config +from tests.config import Config from pgtoolkit import pgmanager @@ -52,12 +52,12 @@ pgm.create_conn(**cfg) with pgm.cursor() as curs: - print('[%d] pgconn: %d' % (multiprocessing.current_process().pid, curs.connection.get_backend_pid())) + print('[%d] pgconn: %d' % (multiprocessing.current_process().pid, curs.connection.get_backend_pid())) print('[%d] insert' % multiprocessing.current_process().pid) curs.execute('''INSERT INTO test (name) VALUES ('multi') RETURNING id''') id = curs.fetchone()[0] curs.connection.commit() - + print('[%d] update' % multiprocessing.current_process().pid) curs.execute('''UPDATE test SET name = 'multi-main' WHERE id=%s''', [id]) # not committed