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

你可能感兴趣的:(shell,openerp)