improved_qty
[odoo/odoo.git] / bin / pooler.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 pool_dic = {}
24
25 def get_db_and_pool(db_name, force_demo=False, status=None, update_module=False):
26     if not status:
27         status={}
28  
29     db = get_db_only(db_name)
30
31     if db_name in pool_dic:
32         pool = pool_dic[db_name]
33     else:
34         import addons
35         import osv.osv
36         pool = osv.osv.osv_pool()
37         pool_dic[db_name] = pool
38         addons.load_modules(db, force_demo, status, update_module)
39         cr = db.cursor()
40         try:
41             pool.init_set(cr, False)
42             cr.commit()
43         finally:
44             cr.close()
45
46         if not update_module:
47             import report
48             report.interface.register_all(db)
49             pool.get('ir.cron')._poolJobs(db.dbname)
50     return db, pool
51
52
53 def restart_pool(db_name, force_demo=False, update_module=False):
54     del pool_dic[db_name]
55     return get_db_and_pool(db_name, force_demo, update_module=update_module)
56
57
58 def get_db_only(db_name):
59     # ATTENTION:
60     # do not put this import outside this function
61     # sql_db must not be loaded before the logger is initialized.
62     # sql_db import psycopg2.tool which create a default logger if there is not.
63     # this resulting of having the logs outputed twice...
64     import sql_db
65     db = sql_db.db_connect(db_name)
66     return db
67
68
69 def get_db(db_name):
70     return get_db_and_pool(db_name)[0]
71
72
73 def get_pool(db_name, force_demo=False, status=None, update_module=False):
74     pool = get_db_and_pool(db_name, force_demo, status, update_module)[1]
75     return pool
76
77 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
78