Skip to content

Commit 5f26542

Browse files
committed
Fixed the errors and warnings in test_macros.TestScriptEditor
1 parent e1d95af commit 5f26542

File tree

5 files changed

+96
-45
lines changed

5 files changed

+96
-45
lines changed

flika/app/script_editor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def __init__(self, parent=None, ):
120120

121121

122122
def resetNamespace(self):
123+
from .script_namespace import getnamespace
123124
self.terminal.shell.reset()
124125
self.terminal.shell.user_ns.update(getnamespace())
125126
self.terminal.namespace.update({'clear': self.terminal.clear, 'reset': self.resetNamespace})

flika/app/syntax.py

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def format(color, style=''):
99
_format = QtGui.QTextCharFormat()
1010
_format.setForeground(_color)
1111
if 'bold' in style:
12-
_format.setFontWeight(QtGui.QFont.Bold)
12+
_format.setFontWeight(QtGui.QFont.Weight.Bold if hasattr(QtGui.QFont, 'Weight') else QtGui.QFont.Bold)
1313
if 'italic' in style:
1414
_format.setFontItalic(True)
1515
return _format
@@ -48,25 +48,31 @@ class PythonHighlighter (QtGui.QSyntaxHighlighter):
4848
# Comparison
4949
'==', '!=', '<', '<=', '>', '>=',
5050
# Arithmetic
51-
'\+', '-', '\*', '/', '//', '\%', '\*\*',
51+
r'\+', '-', r'\*', '/', '//', r'%', r'\*\*',
5252
# In-place
53-
'\+=', '-=', '\*=', '/=', '\%=',
53+
r'\+=', '-=', r'\*=', '/=', r'%=',
5454
# Bitwise
55-
'\^', '\|', '\&', '\~', '>>', '<<',
55+
r'\^', r'\|', r'\&', r'\~', '>>', '<<',
5656
]
5757

5858
# Python braces
5959
braces = [
60-
'\{', '\}', '\(', '\)', '\[', '\]',
60+
r'\{', r'\}', r'\(', r'\)', r'\[', r'\]',
6161
]
62+
6263
def __init__(self, document):
6364
QtGui.QSyntaxHighlighter.__init__(self, document)
6465

6566
# Multi-line strings (expression, flag, style)
66-
# FIXME: The triple-quotes in these two lines will mess up the
67-
# syntax highlighting from this point onward
68-
self.tri_single = (QtCore.QRegularExpression("'''"), 1, STYLES['string2'])
69-
self.tri_double = (QtCore.QRegularExpression('"""'), 2, STYLES['string2'])
67+
# Use QRegExp if QRegularExpression is not available
68+
if hasattr(QtCore, 'QRegularExpression'):
69+
self.tri_single = (QtCore.QRegularExpression("'''"), 1, STYLES['string2'])
70+
self.tri_double = (QtCore.QRegularExpression('"""'), 2, STYLES['string2'])
71+
self.RegexClass = QtCore.QRegularExpression
72+
else:
73+
self.tri_single = (QtCore.QRegExp("'''"), 1, STYLES['string2'])
74+
self.tri_double = (QtCore.QRegExp('"""'), 2, STYLES['string2'])
75+
self.RegexClass = QtCore.QRegExp
7076

7177
rules = []
7278

@@ -102,8 +108,8 @@ def __init__(self, document):
102108
(r'\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b', 0, STYLES['numbers']),
103109
]
104110

105-
# Build a QtCore.QRegularExpression for each pattern
106-
self.rules = [(QtCore.QRegularExpression(pat), index, fmt)
111+
# Build a RegExp for each pattern
112+
self.rules = [(self.RegexClass(pat), index, fmt)
107113
for (pat, index, fmt) in rules]
108114

109115

@@ -112,12 +118,27 @@ def highlightBlock(self, text):
112118
"""
113119
# Do other syntax formatting
114120
for expression, nth, format in self.rules:
115-
match = expression.match(text, 0)
116-
while match.hasMatch():
117-
index = match.capturedStart()
118-
length = match.capturedLength()
119-
self.setFormat(index, length, format)
120-
match = expression.match(text, index + length)
121+
if hasattr(QtCore, 'QRegularExpression'):
122+
# For QRegularExpression
123+
match = expression.match(text, 0)
124+
while match.hasMatch():
125+
index = match.capturedStart()
126+
length = match.capturedLength()
127+
if nth > 0:
128+
index = match.capturedStart(nth)
129+
length = match.capturedLength(nth)
130+
self.setFormat(index, length, format)
131+
match = expression.match(text, index + length)
132+
else:
133+
# For QRegExp
134+
index = expression.indexIn(text)
135+
while index >= 0:
136+
length = expression.matchedLength()
137+
if nth > 0:
138+
index = expression.pos(nth)
139+
length = len(expression.cap(nth))
140+
self.setFormat(index, length, format)
141+
index = expression.indexIn(text, index + length)
121142

122143
self.setCurrentBlockState(0)
123144

@@ -129,7 +150,7 @@ def highlightBlock(self, text):
129150

130151
def match_multiline(self, text, delimiter, in_state, style):
131152
"""Do highlighting of multi-line strings. ``delimiter`` should be a
132-
``QtCore.QRegularExpression`` for triple-single-quotes or triple-double-quotes, and
153+
``QRegExp`` or ``QRegularExpression`` for triple-single-quotes or triple-double-quotes, and
133154
``in_state`` should be a unique integer to represent the corresponding
134155
state changes when inside those strings. Returns True if we're still
135156
inside a multi-line string when this function is finished.
@@ -140,17 +161,34 @@ def match_multiline(self, text, delimiter, in_state, style):
140161
add = 0
141162
# Otherwise, look for the delimiter on this line
142163
else:
143-
start = delimiter.indexIn(text)
144-
# Move past this match
145-
add = delimiter.matchedLength()
164+
if hasattr(QtCore, 'QRegularExpression'):
165+
# For QRegularExpression
166+
match = delimiter.match(text)
167+
start = match.capturedStart() if match.hasMatch() else -1
168+
add = match.capturedLength() if match.hasMatch() else 0
169+
else:
170+
# For QRegExp
171+
start = delimiter.indexIn(text)
172+
add = delimiter.matchedLength()
146173

147174
# As long as there's a delimiter match on this line...
148175
while start >= 0:
149176
# Look for the ending delimiter
150-
end = delimiter.indexIn(text, start + add)
177+
if hasattr(QtCore, 'QRegularExpression'):
178+
# For QRegularExpression
179+
match = delimiter.match(text, start + add)
180+
end = match.capturedStart() if match.hasMatch() else -1
181+
else:
182+
# For QRegExp
183+
end = delimiter.indexIn(text, start + add)
184+
151185
# Ending delimiter on this line?
152186
if end >= add:
153-
length = end - start + add + delimiter.matchedLength()
187+
# Fix the conditional to properly handle match objects
188+
if hasattr(QtCore, 'QRegularExpression') and match.hasMatch():
189+
length = end - start + add + match.capturedLength()
190+
else:
191+
length = end - start + add + delimiter.matchedLength()
154192
self.setCurrentBlockState(0)
155193
# No; multi-line string
156194
else:
@@ -159,7 +197,13 @@ def match_multiline(self, text, delimiter, in_state, style):
159197
# Apply formatting
160198
self.setFormat(start, length, style)
161199
# Look for the next match
162-
start = delimiter.indexIn(text, start + length)
200+
if hasattr(QtCore, 'QRegularExpression'):
201+
# For QRegularExpression
202+
match = delimiter.match(text, start + length)
203+
start = match.capturedStart() if match.hasMatch() else -1
204+
else:
205+
# For QRegExp
206+
start = delimiter.indexIn(text, start + length)
163207

164208
# Return True if still inside a multi-line string, False otherwise
165209
if self.currentBlockState() == in_state:

flika/app/terminal.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import sys
3+
import os
34
import atexit
45
from contextlib import contextmanager
56
from typing import Any, Dict, Optional, Type, Union
@@ -14,7 +15,11 @@
1415

1516
# ZMQ imports
1617
from zmq import ZMQError
17-
from zmq.eventloop import ioloop
18+
# Remove deprecated zmq.eventloop.ioloop import
19+
# from zmq.eventloop import ioloop
20+
# Import tornado's ioloop directly instead (if needed)
21+
import tornado.ioloop
22+
# Use current import location for ZMQStream
1823
from zmq.eventloop.zmqstream import ZMQStream
1924

2025
# Import version
@@ -158,9 +163,9 @@ def init_kernel(self) -> None:
158163

159164
def start(self) -> None:
160165
# Handoff between IOLoop and QApplication event loops
161-
loop = ioloop.IOLoop.instance()
166+
loop = tornado.ioloop.IOLoop.instance()
162167
# Use 1ms callback time to prevent application hanging
163-
stopper = ioloop.PeriodicCallback(loop.stop, 1, loop)
168+
stopper = tornado.ioloop.PeriodicCallback(loop.stop, 1, loop)
164169
self.timer = QtCore.QTimer()
165170
self.timer.timeout.connect(loop.start)
166171
self.timer.start(100)

flika/flika.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
import platform
66
import optparse
77
import warnings
8+
9+
# Set Jupyter to use platformdirs (fixes deprecation warning)
10+
os.environ['JUPYTER_PLATFORM_DIRS'] = '1'
11+
812
logger.debug("Started 'reading flika.py, importing numpy'")
913
import numpy as np # type: ignore
1014
logger.debug("Completed 'reading flika.py, importing numpy'")
1115
from .version import __version__
1216
from .app.application import FlikaApplication
1317

14-
15-
16-
17-
18+
# Filter out known warnings
1819
warnings.filterwarnings("ignore", category=np.exceptions.VisibleDeprecationWarning)
1920

2021
def parse_arguments(argv):

flika/tests/test_macros.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@
3535
# assert PluginManager.plugins[plugin_name].menu is None and not PluginManager.plugins[plugin_name].installed, "Plugin uninstall"
3636

3737

38-
# class TestScriptEditor():
39-
# def setup_method(self, method):
40-
# ScriptEditor.show()
41-
42-
# def teardown_method(self, method):
43-
# ScriptEditor.close()
44-
45-
# def test_from_window(self):
46-
# w1 = Window(np.random.random([10, 20, 20]))
47-
# from ..process import threshold
48-
# w2 = threshold(.5)
49-
# ScriptEditor.gui.actionFrom_Window.trigger()
50-
# text = str(ScriptEditor.gui.currentTab().toPlainText())
51-
# assert text == "threshold(value=0.5, darkBackground=False, keepSourceWindow=False)", "From window command not expected"
38+
class TestScriptEditor():
39+
def setup_method(self, method):
40+
ScriptEditor.show()
41+
42+
def teardown_method(self, method):
43+
ScriptEditor.close()
44+
45+
def test_from_window(self):
46+
w1 = Window(np.random.random([10, 20, 20]))
47+
from ..process import threshold
48+
w2 = threshold(.5)
49+
ScriptEditor.gui.actionFrom_Window.trigger()
50+
text = str(ScriptEditor.gui.currentTab().toPlainText())
51+
assert text == "threshold(value=0.5, darkBackground=False, keepSourceWindow=False)", "From window command not expected"

0 commit comments

Comments
 (0)