File: tools.py

package info (click to toggle)
python-odoorpc 0.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 604 kB
  • sloc: python: 3,461; makefile: 154; sh: 36
file content (125 lines) | stat: -rw-r--r-- 3,539 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- coding: utf-8 -*-
# Copyright 2014 Sébastien Alix
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl)
"""This module contains the :class:`Config <odoorpc.config.Config>` class which
manage the configuration related to an instance of
:class:`ODOO <odoorpc.ODOO>`, and some useful helper functions used internally
in `OdooRPC`.
"""
try:
    from collections.abc import MutableMapping
except ImportError:  # Python 2.7 compatibility
    from collections import MutableMapping
import re

from .error import InternalError

MATCH_VERSION = re.compile(r'[^\d.]')


class Config(MutableMapping):
    """Class which manage the configuration of an
    :class:`ODOO <odoorpc.ODOO>` instance.

    .. note::
        This class have to be used through the :attr:`odoorpc.ODOO.config`
        property.

    >>> import odoorpc
    >>> odoo = odoorpc.ODOO('localhost')    # doctest: +SKIP
    >>> type(odoo.config)
    <class 'odoorpc.tools.Config'>
    """

    def __init__(self, odoo, options):
        super(Config, self).__init__()
        self._odoo = odoo
        self._options = options or {}

    def __getitem__(self, key):
        return self._options[key]

    def __setitem__(self, key, value):
        """Handle ``timeout`` option to set the timeout on the connector."""
        if key == 'timeout':
            self._odoo._connector.timeout = value
        self._options[key] = value

    def __delitem__(self, key):
        raise InternalError("Operation not allowed")

    def __iter__(self):
        return self._options.__iter__()

    def __len__(self):
        return len(self._options)

    def __str__(self):
        return self._options.__str__()

    def __repr__(self):
        return self._options.__repr__()


def clean_version(version):
    """Clean a version string.

        >>> from odoorpc.tools import clean_version
        >>> clean_version('7.0alpha-20121206-000102')
        '7.0'

    :return: a cleaner version string
    """
    version = MATCH_VERSION.sub('', version.split('-')[0])
    return version


def v(version):
    """Convert a version string to a tuple. The tuple can be use to compare
    versions between them.

        >>> from odoorpc.tools import v
        >>> v('7.0')
        [7, 0]
        >>> v('6.1')
        [6, 1]
        >>> v('7.0') < v('6.1')
        False

    :return: the version as tuple
    """
    return [int(x) for x in clean_version(version).split(".")]


def get_encodings(hint_encoding='utf-8'):
    """Used to try different encoding.
    Function copied from Odoo 11.0 (odoo.loglevels.get_encodings).
    This piece of code is licensed under the LGPL-v3 and so it is compatible
    with the LGPL-v3 license of OdooRPC::

        - https://github.com/odoo/odoo/blob/11.0/LICENSE
        - https://github.com/odoo/odoo/blob/11.0/COPYRIGHT
    """
    fallbacks = {
        'latin1': 'latin9',
        'iso-8859-1': 'iso8859-15',
        'cp1252': '1252',
    }
    if hint_encoding:
        yield hint_encoding
        if hint_encoding.lower() in fallbacks:
            yield fallbacks[hint_encoding.lower()]

    # some defaults (also taking care of pure ASCII)
    for charset in ['utf8', 'latin1', 'ascii']:
        if not hint_encoding or (charset.lower() != hint_encoding.lower()):
            yield charset

    from locale import getpreferredencoding

    prefenc = getpreferredencoding()
    if prefenc and prefenc.lower() != 'utf-8':
        yield prefenc
        prefenc = fallbacks.get(prefenc.lower())
        if prefenc:
            yield prefenc