Coverage for src/robotide/usages/UsageRunner.py: 27%
65 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-06 10:40 +0100
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-06 10:40 +0100
1# Copyright 2008-2015 Nokia Networks
2# Copyright 2016- Robot Framework Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15import os.path 1ab
16import time 1ab
17from threading import Thread 1ab
19import wx 1ab
21from robotide.usages import commands 1ab
22from . import usagesdialog 1ab
25class Usages(object): 1ab
27 def __init__(self, controller, highlight, name=None, kw_info=None): 1ab
28 from ..controller.filecontrollers import ResourceFileController
29 self._name = name or controller.name
30 self._controller = controller
31 self._highlight = highlight
32 if kw_info is None and not isinstance(controller, ResourceFileController):
33 self._kw_info = self._controller.get_keyword_info(name)
34 else:
35 self._kw_info = kw_info
36 # self.prefix = (os.path.basename(self._controller.data.source)
37 # .replace('robot', '').replace('resource', '')).split('.')
38 if self._kw_info:
39 self.prefix = self._kw_info.source.replace('robot', '').replace('resource', '').split('.')[0]
40 else:
41 self.prefix = os.path.basename(controller.source).split('.')[0]
42 """ DEBUG
43 self.prefix = os.path.basename(self._controller.data.source).split('.')
44 if len(self.prefix) == 2 and self.prefix[-1] in ['resource', 'robot']:
45 self.prefix = self.prefix[0]
46 else:
47 self.prefix = ''
48 """
49 # print(f"DEBUG: UsageRunner.py Usages INIT prefix={self.prefix} {self._kw_info=}")
50 self._dlg = self._usages_dialog()
51 self._worker = Thread(target=self._run)
52 self._dialog_closed = False
54 def _usages_dialog(self): 1ab
55 # print(f"DEBUG: UsageRunner.py Usages _usages_dialog ENTER name={self._name},"
56 # f" controller_name={self._controller.name} prefix={self.prefix}")
57 if self._controller.name == self._name:
58 return usagesdialog.UsagesDialogWithUserKwNavigation(self._name, self._highlight, self._controller,
59 prefix=self.prefix)
60 return usagesdialog.UsagesDialog(self._name, prefix=self.prefix)
62 def show(self): 1ab
63 self._dlg.add_selection_listener(self._highlight)
64 self._dlg.Bind(wx.EVT_CLOSE, self._stop)
65 self._dlg.Show()
66 self._worker.start()
68 def _run(self): 1ab
69 wx.CallAfter(self._begin_search)
70 names = [ self._name ]
71 # if self.prefix != '' and '.' not in self._name:
72 # names.append(f'{self.prefix}.{self._name}')
73 # print(f"DEBUG: UsageRunner.py Usages _run before loop with _find_usages names={names}")
74 for name in names: # DEBUG
75 for usage in self._find_usages(name):
76 time.sleep(0) # GIVE SPACE TO OTHER THREADS -- Thread.yield in Java
77 if self._dialog_closed:
78 return
79 wx.CallAfter(self._add_usage, usage)
80 wx.CallAfter(self._end_search)
82 def _find_usages(self, name): 1ab
83 # print(f"DEBUG: UsageRunner.py Usages _find_usages ENTER name={name} kw_info={self._kw_info}")
84 return self._controller.execute(commands.FindUsages(name, self._kw_info, prefix=self.prefix))
86 def _begin_search(self): 1ab
87 if not self._dialog_closed:
88 self._dlg.begin_searching()
90 def _add_usage(self, usage): 1ab
91 if not self._dialog_closed:
92 self._dlg.add_usage(usage)
94 def _end_search(self): 1ab
95 if not self._dialog_closed:
96 self._dlg.end_searching()
98 def _stop(self, event): 1ab
99 self._dialog_closed = True
100 event.Skip()
103class ResourceFileUsages(Usages): 1ab
105 def __init__(self, controller, highlight): 1ab
106 Usages.__init__(self, controller, highlight)
108 def _usages_dialog(self): 1ab
109 return usagesdialog.resource_import_usage_dialog(self._controller.display_name, self._highlight,
110 self._controller)
112 def _find_usages(self, name=None): 1ab
113 return self._controller.execute(commands.FindResourceUsages())
116class VariableUsages(Usages): 1ab
118 def _find_usages(self, name=None): 1ab
119 return self._controller.execute(commands.FindVariableUsages(self._name))