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

依赖ipython

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

[options]
admin_passwd = admin
db_host = localhost
db_port = 5432
db_user = username
db_password = password
db_name=dbname

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

# -*- coding: utf-8 -*-
import sys
import os
import logging
import ConfigParser
from openerp.modules.registry import RegistryManager
from IPython import embed


_logger = logging.getLogger(__name__)

# 添加项目目录到环境变量 为了导入openerp
openerp_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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值