Debian Codegraph @

odoorpc/tools.py

raw
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 the :class:`Config <odoorpc.config.Config>` class which
5 manage the configuration related to an instance of
6 :class:`ODOO <odoorpc.ODOO>`, and some useful helper functions used internally
7 in `OdooRPC`.
8 """
9 try:
10 from collections.abc import MutableMapping
11 except ImportError: # Python 2.7 compatibility
12 from collections import MutableMapping
13 import re
14
15 from .error import InternalError
16
17 MATCH_VERSION = re.compile(r'[^\d.]')
18
19
20 class Config(MutableMapping):
21 """Class which manage the configuration of an
22 :class:`ODOO <odoorpc.ODOO>` instance.
23
24 .. note::
25 This class have to be used through the :attr:`odoorpc.ODOO.config`
26 property.
27
28 >>> import odoorpc
29 >>> odoo = odoorpc.ODOO('localhost') # doctest: +SKIP
30 >>> type(odoo.config)
31 <class 'odoorpc.tools.Config'>
32 """
33
34 def __init__(self, odoo, options):
35 super(Config, self).__init__()
36 self._odoo = odoo
37 self._options = options or {}
38
39 def __getitem__(self, key):
40 return self._options[key]
41
42 def __setitem__(self, key, value):
43 """Handle ``timeout`` option to set the timeout on the connector."""
44 if key == 'timeout':
45 self._odoo._connector.timeout = value
46 self._options[key] = value
47
48 def __delitem__(self, key):
49 raise InternalError("Operation not allowed")
50
51 def __iter__(self):
52 return self._options.__iter__()
53
54 def __len__(self):
55 return len(self._options)
56
57 def __str__(self):
58 return self._options.__str__()
59
60 def __repr__(self):
61 return self._options.__repr__()
62
63
64 def clean_version(version):
65 """Clean a version string.
66
67 >>> from odoorpc.tools import clean_version
68 >>> clean_version('7.0alpha-20121206-000102')
69 '7.0'
70
71 :return: a cleaner version string
72 """
73 version = MATCH_VERSION.sub('', version.split('-')[0])
74 return version
75
76
77 def v(version):
78 """Convert a version string to a tuple. The tuple can be use to compare
79 versions between them.
80
81 >>> from odoorpc.tools import v
82 >>> v('7.0')
83 [7, 0]
84 >>> v('6.1')
85 [6, 1]
86 >>> v('7.0') < v('6.1')
87 False
88
89 :return: the version as tuple
90 """
91 return [int(x) for x in clean_version(version).split(".")]
92
93
94 def get_encodings(hint_encoding='utf-8'):
95 """Used to try different encoding.
96 Function copied from Odoo 11.0 (odoo.loglevels.get_encodings).
97 This piece of code is licensed under the LGPL-v3 and so it is compatible
98 with the LGPL-v3 license of OdooRPC::
99
100 - https://github.com/odoo/odoo/blob/11.0/LICENSE
101 - https://github.com/odoo/odoo/blob/11.0/COPYRIGHT
102 """
103 fallbacks = {
104 'latin1': 'latin9',
105 'iso-8859-1': 'iso8859-15',
106 'cp1252': '1252',
107 }
108 if hint_encoding:
109 yield hint_encoding
110 if hint_encoding.lower() in fallbacks:
111 yield fallbacks[hint_encoding.lower()]
112
113 # some defaults (also taking care of pure ASCII)
114 for charset in ['utf8', 'latin1', 'ascii']:
115 if not hint_encoding or (charset.lower() != hint_encoding.lower()):
116 yield charset
117
118 from locale import getpreferredencoding
119
120 prefenc = getpreferredencoding()
121 if prefenc and prefenc.lower() != 'utf-8':
122 yield prefenc
123 prefenc = fallbacks.get(prefenc.lower())
124 if prefenc:
125 yield prefenc