openerp增加交互环境shell.py脚本

来源:互联网 发布:淘宝联系卖家的电话 编辑:程序博客网 时间:2024/05/02 04:05

依赖ipython

在工程环境下建立配置文件openerp-server.conf

[options]admin_passwd = admindb_host = localhostdb_port = 5432db_user = usernamedb_password = passworddb_name=dbname

在工程目录下增加shell.py 保存如下内容

# -*- coding: utf-8 -*-import sysimport osimport loggingimport ConfigParserfrom openerp.modules.registry import RegistryManagerfrom IPython import embed_logger = logging.getLogger(__name__)# 添加项目目录到环境变量 为了导入openerpopenerp_path = os.getcwd()sys.path.insert(0, openerp_path)import openerp# 读取数据库连接配置db_config = ConfigParser.ConfigParser()with open('%s/openerp-server.conf' % openerp_path) as conf:    db_config.readfp(conf)    config = openerp.tools.config    config['db_password'] = db_config.get('options', 'db_password')    config['db_user'] = db_config.get('options', 'db_user')    config['db_name'] = db_config.get('options', 'db_name')    config['db_port'] = db_config.get('options', 'db_port')def shell_script():    uid, context = 1, None    registry = RegistryManager.get(config['db_name'])    with registry.cursor() as cr:        user = registry.get('res.users').browse(cr, 1, 1)        class Dummy_self(object):            """为了和OE统一方便调用 造一个假的self.pool"""            pool = registry        self = Dummy_self()        embed()if __name__ == '__main__':    """        依赖            sudo easy_install ipython            #sudo easy_install readline        交互环境运行            python shell.py        使用列子            self.pool.get('res.users')    """    shell_script()

运行交互环境

python shell.py

0 0