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
126
127
128
129
130
131
|
# -*- coding: utf-8 -*-
#
# AWL simulator - GUI code validator scheduling.
#
# Copyright 2017-2020 Michael Buesch <m@bues.ch>
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from __future__ import division, absolute_import, print_function, unicode_literals
#from awlsim.common.cython_support cimport * #@cy
from awlsim.common.compat import *
from awlsim.common.codevalidator import *
from awlsim.gui.util import *
__all__ = [
"GuiValidatorSched",
]
class GuiValidatorSched(QObject):
"""Code validator scheduling.
"""
TIMEOUT = AwlValidator.TIMEOUT
# A new result is available.
# The parameter may be None: No error occurred.
# Or an instance of AwlSimError.
haveValidationResult = Signal(object)
__singleton = None
@classmethod
def get(cls):
instance = cls.__singleton
if instance is None:
cls.__singleton = instance = cls()
return instance
def __init__(self):
QObject.__init__(self)
self.__project = None
self.__startTimer = QTimer(self)
self.__startTimer.setSingleShot(True)
self.__startTimer.timeout.connect(self.__startAsyncValidation)
self.__pollTimer = QTimer(self)
self.__pollTimer.setInterval(100)
self.__pollTimer.setSingleShot(True)
self.__pollTimer.timeout.connect(self.__pollValidationResult)
def startAsyncValidation(self, project, delaySec=0.0):
"""Start an asynchronous background document validation.
"""
printVerbose("Requesting asynchronous validation "
"(delay = %.1f s)" % delaySec)
self.__project = project
self.__startTimer.start(int(round(delaySec * 1000.0)))
def __startAsyncValidation(self):
project, self.__project = self.__project, None
if project is None:
return
# Get the actual project.
if callable(project):
project = project()
if not project.getGuiSettings().getEditorValidationEn():
return # Validation disabled.
validator = AwlValidator.get()
if not validator:
return
printVerbose("Starting asynchronous validation.")
validator.validate(project=project)
self.__pollTimer.start()
def __pollValidationResult(self):
"""Poll the background source code validation result.
"""
validator = AwlValidator.get()
if not validator:
return
running, exception = validator.getState()
if not running:
printVerbose("Finished asynchronous validation: %s" % (
"Not Ok" if exception else "Ok"))
self.haveValidationResult.emit(exception)
if running:
self.__pollTimer.start()
def syncValidation(self, project):
"""Start a validation and synchronously wait for it.
May return None: No error.
AwlSimError instance: Validation detected an error.
TIMEOUT: Validation failed due to timeout.
"""
validator = AwlValidator.get()
if not validator:
return None
self.__project = None
self.__startTimer.stop()
exception = validator.validateSync(project=project,
sleepFunc=sleepWithEventLoop)
if exception is self.TIMEOUT:
printVerbose("Synchronous validation timeout.")
else:
printVerbose("Finished synchronous validation: %s" % (
"Not Ok" if exception else "Ok"))
self.haveValidationResult.emit(exception)
return exception
|