-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
69 lines (56 loc) · 2.85 KB
/
utils.py
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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
GuidedOfflineEditingPlugin project_context_manager.py
A QGIS plugin
Extend the built-in Offline Editing Plugin providing automated processes
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2019-06-08
git sha : $Format:%H$
copyright : (C) 2019 by Yann Voté
email : ygversil@lilo.org
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import traceback
from qgis.core import Qgis, QgsMessageLog
def log_exception(err, level='Info', feedback=False, iface=None, duration=5):
"""Output the given exception in QGIS logs using the given level.
If ``feedback`` is ``True``, then ``iface`` must reference a valid
``QgisInterface`` instance so that the message can be shown in the message
bar in QGIS interface.
"""
log_message(str(err), level=level, feedback=feedback, iface=iface,
duration=duration)
log_message(traceback.format_exc(), level=level)
def log_message(msg, level='Info', feedback=False, iface=None,
duration=5):
"""Output the given message in QGIS logs using the given level.
If ``feedback`` is ``True``, then ``iface`` must reference a valid
``QgisInterface`` instance so that the message can be shown in the message
bar in QGIS interface.
"""
level = getattr(Qgis, level)
QgsMessageLog.logMessage(msg, 'GuidedOfflineEditing', level=level)
if feedback:
assert iface is not None
iface.messageBar().pushMessage('GuidedOfflineEditing', msg,
level=level, duration=duration)
def path_relative_to(path, parent):
"""Return a version of ``path`` (a filesystem path) relative to ``parent``,
that is the relative sub-path of ``path`` without ``parent``.
Return ``None`` if ``path`` is not a relative path to ``parent``.
``path`` and ``parent`` must be Python ``pathlib.Path`` objects.
"""
try:
return path.relative_to(parent)
except ValueError:
return None