comparison libervia/desktop_kivy/core/kivy_hack.py @ 493:b3cedbee561d

refactoring: rename `cagou` to `libervia.desktop_kivy` + update imports and names following backend changes
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 18:26:16 +0200
parents cagou/core/kivy_hack.py@203755bbe0fe
children 59bdf78bd1d9
comparison
equal deleted inserted replaced
492:5114bbb5daa3 493:b3cedbee561d
1 #!/usr//bin/env python2
2
3
4 #Libervia Desktop-Kivy
5 # Copyright (C) 2016-2021 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 CONF_KIVY_LEVEL = 'log_kivy_level'
21
22
23 def do_hack():
24 """work around Kivy hijacking of logs and arguments"""
25 # we remove args so kivy doesn't use them
26 # this is need to avoid kivy breaking QuickApp args handling
27 import sys
28 ori_argv = sys.argv[:]
29 sys.argv = sys.argv[:1]
30 from .constants import Const as C
31 from libervia.backend.core import log_config
32 log_config.libervia_configure(C.LOG_BACKEND_STANDARD, C)
33
34 from . import config
35 kivy_level = config.config_get(C.CONFIG_SECTION, CONF_KIVY_LEVEL, 'follow').upper()
36
37 # kivy handles its own loggers, we don't want that!
38 import logging
39 root_logger = logging.root
40 kivy_logger = logging.getLogger('kivy')
41 ori_addHandler = kivy_logger.addHandler
42 kivy_logger.addHandler = lambda __: None
43 ori_setLevel = kivy_logger.setLevel
44 if kivy_level == 'FOLLOW':
45 # level is following SàT level
46 kivy_logger.setLevel = lambda level: None
47 elif kivy_level == 'KIVY':
48 # level will be set by Kivy according to its own conf
49 pass
50 elif kivy_level in C.LOG_LEVELS:
51 kivy_logger.setLevel(kivy_level)
52 kivy_logger.setLevel = lambda level: None
53 else:
54 raise ValueError("Unknown value for {name}: {value}".format(name=CONF_KIVY_LEVEL, value=kivy_level))
55
56 # during import kivy set its logging stuff
57 import kivy
58 kivy # to avoid pyflakes warning
59
60 # we want to separate kivy logs from other logs
61 logging.root = root_logger
62 from kivy import logger
63 sys.stderr = logger.previous_stderr
64
65 # we restore original methods
66 kivy_logger.addHandler = ori_addHandler
67 kivy_logger.setLevel = ori_setLevel
68
69 # we restore original arguments
70 sys.argv = ori_argv