| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright 2014 Sébastien Alix |
| 3 |
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl) |
| 4 |
"""This module contains some helper functions used to save and load sessions |
| 5 |
in `OdooRPC`. |
| 6 |
""" |
| 7 |
import os |
| 8 |
import stat |
| 9 |
import sys |
| 10 |
|
| 11 |
# Python 2 |
| 12 |
if sys.version_info[0] < 3: |
| 13 |
from ConfigParser import SafeConfigParser as ConfigParser |
| 14 |
# Python >= 3 |
| 15 |
else: |
| 16 |
from configparser import ConfigParser |
| 17 |
|
| 18 |
|
| 19 |
def get_all(rc_file='~/.odoorpcrc'): |
| 20 |
"""Return all session configurations from the `rc_file` file. |
| 21 |
|
| 22 |
>>> import odoorpc |
| 23 |
>>> from pprint import pprint as pp |
| 24 |
>>> pp(odoorpc.session.get_all()) # doctest: +SKIP |
| 25 |
{'foo': {'database': 'db_name', |
| 26 |
'host': 'localhost', |
| 27 |
'passwd': 'password', |
| 28 |
'port': 8069, |
| 29 |
'protocol': 'jsonrpc', |
| 30 |
'timeout': 120, |
| 31 |
'type': 'ODOO', |
| 32 |
'user': 'admin'}, |
| 33 |
...} |
| 34 |
|
| 35 |
.. doctest:: |
| 36 |
:hide: |
| 37 |
|
| 38 |
>>> import odoorpc |
| 39 |
>>> session = '%s_session' % DB |
| 40 |
>>> odoo.save(session) |
| 41 |
>>> data = odoorpc.session.get_all() |
| 42 |
>>> data[session]['host'] == HOST |
| 43 |
True |
| 44 |
>>> data[session]['protocol'] == PROTOCOL |
| 45 |
True |
| 46 |
>>> data[session]['port'] == int(PORT) |
| 47 |
True |
| 48 |
>>> data[session]['database'] == DB |
| 49 |
True |
| 50 |
>>> data[session]['user'] == USER |
| 51 |
True |
| 52 |
>>> data[session]['passwd'] == PWD |
| 53 |
True |
| 54 |
>>> data[session]['type'] == 'ODOO' |
| 55 |
True |
| 56 |
""" |
| 57 |
conf = ConfigParser() |
| 58 |
conf.read([os.path.expanduser(rc_file)]) |
| 59 |
sessions = {} |
| 60 |
for name in conf.sections(): |
| 61 |
sessions[name] = { |
| 62 |
'type': conf.get(name, 'type'), |
| 63 |
'host': conf.get(name, 'host'), |
| 64 |
'protocol': conf.get(name, 'protocol'), |
| 65 |
'port': conf.getint(name, 'port'), |
| 66 |
'timeout': conf.getfloat(name, 'timeout'), |
| 67 |
'user': conf.get(name, 'user'), |
| 68 |
'passwd': conf.get(name, 'passwd'), |
| 69 |
'database': conf.get(name, 'database'), |
| 70 |
} |
| 71 |
return sessions |
| 72 |
|
| 73 |
|
| 74 |
def get(name, rc_file='~/.odoorpcrc'): |
| 75 |
"""Return the session configuration identified by `name` |
| 76 |
from the `rc_file` file. |
| 77 |
|
| 78 |
>>> import odoorpc |
| 79 |
>>> from pprint import pprint as pp |
| 80 |
>>> pp(odoorpc.session.get('foo')) # doctest: +SKIP |
| 81 |
{'database': 'db_name', |
| 82 |
'host': 'localhost', |
| 83 |
'passwd': 'password', |
| 84 |
'port': 8069, |
| 85 |
'protocol': 'jsonrpc', |
| 86 |
'timeout': 120, |
| 87 |
'type': 'ODOO', |
| 88 |
'user': 'admin'} |
| 89 |
|
| 90 |
.. doctest:: |
| 91 |
:hide: |
| 92 |
|
| 93 |
>>> import odoorpc |
| 94 |
>>> session = '%s_session' % DB |
| 95 |
>>> odoo.save(session) |
| 96 |
>>> data = odoorpc.session.get(session) |
| 97 |
>>> data['host'] == HOST |
| 98 |
True |
| 99 |
>>> data['protocol'] == PROTOCOL |
| 100 |
True |
| 101 |
>>> data['port'] == int(PORT) |
| 102 |
True |
| 103 |
>>> data['database'] == DB |
| 104 |
True |
| 105 |
>>> data['user'] == USER |
| 106 |
True |
| 107 |
>>> data['passwd'] == PWD |
| 108 |
True |
| 109 |
>>> data['type'] == 'ODOO' |
| 110 |
True |
| 111 |
|
| 112 |
:raise: `ValueError` (wrong session name) |
| 113 |
""" |
| 114 |
conf = ConfigParser() |
| 115 |
conf.read([os.path.expanduser(rc_file)]) |
| 116 |
if not conf.has_section(name): |
| 117 |
raise ValueError( |
| 118 |
"'{}' session does not exist in {}".format(name, rc_file) |
| 119 |
) |
| 120 |
return { |
| 121 |
'type': conf.get(name, 'type'), |
| 122 |
'host': conf.get(name, 'host'), |
| 123 |
'protocol': conf.get(name, 'protocol'), |
| 124 |
'port': conf.getint(name, 'port'), |
| 125 |
'timeout': conf.getfloat(name, 'timeout'), |
| 126 |
'user': conf.get(name, 'user'), |
| 127 |
'passwd': conf.get(name, 'passwd'), |
| 128 |
'database': conf.get(name, 'database'), |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
def save(name, data, rc_file='~/.odoorpcrc'): |
| 133 |
"""Save the `data` session configuration under the name `name` |
| 134 |
in the `rc_file` file. |
| 135 |
|
| 136 |
>>> import odoorpc |
| 137 |
>>> odoorpc.session.save( |
| 138 |
... 'foo', |
| 139 |
... {'type': 'ODOO', 'host': 'localhost', 'protocol': 'jsonrpc', |
| 140 |
... 'port': 8069, 'timeout': 120, 'database': 'db_name' |
| 141 |
... 'user': 'admin', 'passwd': 'password'}) # doctest: +SKIP |
| 142 |
|
| 143 |
.. doctest:: |
| 144 |
:hide: |
| 145 |
|
| 146 |
>>> import odoorpc |
| 147 |
>>> session = '%s_session' % DB |
| 148 |
>>> odoorpc.session.save( |
| 149 |
... session, |
| 150 |
... {'type': 'ODOO', 'host': HOST, 'protocol': PROTOCOL, |
| 151 |
... 'port': PORT, 'timeout': 120, 'database': DB, |
| 152 |
... 'user': USER, 'passwd': PWD}) |
| 153 |
""" |
| 154 |
conf = ConfigParser() |
| 155 |
conf.read([os.path.expanduser(rc_file)]) |
| 156 |
if not conf.has_section(name): |
| 157 |
conf.add_section(name) |
| 158 |
for key in data: |
| 159 |
value = data[key] |
| 160 |
conf.set(name, key, str(value)) |
| 161 |
with open(os.path.expanduser(rc_file), 'w') as file_: |
| 162 |
os.chmod(os.path.expanduser(rc_file), stat.S_IREAD | stat.S_IWRITE) |
| 163 |
conf.write(file_) |
| 164 |
|
| 165 |
|
| 166 |
def remove(name, rc_file='~/.odoorpcrc'): |
| 167 |
"""Remove the session configuration identified by `name` |
| 168 |
from the `rc_file` file. |
| 169 |
|
| 170 |
>>> import odoorpc |
| 171 |
>>> odoorpc.session.remove('foo') # doctest: +SKIP |
| 172 |
|
| 173 |
.. doctest:: |
| 174 |
:hide: |
| 175 |
|
| 176 |
>>> import odoorpc |
| 177 |
>>> session = '%s_session' % DB |
| 178 |
>>> odoorpc.session.remove(session) |
| 179 |
|
| 180 |
:raise: `ValueError` (wrong session name) |
| 181 |
""" |
| 182 |
conf = ConfigParser() |
| 183 |
conf.read([os.path.expanduser(rc_file)]) |
| 184 |
if not conf.has_section(name): |
| 185 |
raise ValueError( |
| 186 |
"'{}' session does not exist in {}".format(name, rc_file) |
| 187 |
) |
| 188 |
conf.remove_section(name) |
| 189 |
with open(os.path.expanduser(rc_file), 'wb') as file_: |
| 190 |
conf.write(file_) |