Robot Framework Integrated Development Environment (RIDE)
recentfiles.py
Go to the documentation of this file.
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.
15 
16 import os.path
17 
18 import wx
19 
20 from ..pluginapi import Plugin
21 from ..action import ActionInfo, SeparatorInfo
22 from ..publish import RideOpenSuite, RideFileNameChanged
23 from ..publish.messages import RideNewProject, RideSaved
24 
25 
26 def normalize_path(path):
27  if os.path.basename(path).startswith('__init__.'):
28  return os.path.dirname(path)
29  return os.path.abspath(path)
30 
31 
33  return [f for f in paths if os.path.exists(f)]
34 
35 
36 
38 
39  def __init__(self, application=None):
40  settings = {'recent_files': [], 'max_number_of_files': 4}
41  Plugin.__init__(self, application, default_settings=settings)
42  self.recent_filesrecent_files = remove_non_existing_paths(self.recent_filesrecent_files)
43 
44  def enable(self):
45  self._save_currently_loaded_suite_save_currently_loaded_suite()
46  self._add_recent_files_to_menu_add_recent_files_to_menu()
47  self._new_project_path_new_project_path = None
48  self.subscribesubscribe(self.OnSuiteOpenedOnSuiteOpened, RideOpenSuite)
49  self.subscribesubscribe(self.OnFileNameChangedOnFileNameChanged, RideFileNameChanged)
50  self.subscribesubscribe(self.OnNewProjectOpenedOnNewProjectOpened, RideNewProject)
51  self.subscribesubscribe(self.OnSavedOnSaved, RideSaved)
52 
53  def disable(self):
54  self.unregister_actionsunregister_actions()
55  self.unsubscribe_allunsubscribe_all()
56 
57  def OnSuiteOpened(self, message):
58  # Update menu with CallAfter to ensure ongoing menu selection
59  # handling has finished before menu is changed
60  wx.CallAfter(self._add_to_recent_files_add_to_recent_files, message.path)
61  self._new_project_path_new_project_path = None
62 
63  def OnFileNameChanged(self, message):
64  self._new_project_path_new_project_path = None
65  if not message.old_filename:
66  return
67  old_filename = normalize_path(message.old_filename)
68  new_filename = normalize_path(message.datafile.filename)
69  if old_filename not in self.recent_filesrecent_files:
70  return
71  index = self.recent_filesrecent_files.index(old_filename)
72  self.recent_filesrecent_files[index] = new_filename
73  self._save_settings_and_update_file_menu_save_settings_and_update_file_menu()
74 
75  def OnNewProjectOpened(self, message):
76  self._new_project_path_new_project_path = message.path
77 
78  def OnSaved(self, message):
79  if self._new_project_path_new_project_path is not None:
80  wx.CallAfter(self._add_to_recent_files_add_to_recent_files, self._new_project_path_new_project_path)
81  self._new_project_path_new_project_path = None
82 
83  def _get_file_menu(self):
84  menubar = self.get_menu_bar()
85  pos = menubar.FindMenu('File')
86  file_menu = menubar.GetMenu(pos)
87  return file_menu
88 
90  model = self.modelmodel
91  if model and model.suite:
92  self._add_to_recent_files_add_to_recent_files(model.suite.source)
93 
94  def _add_to_recent_files(self, path):
95  if not path:
96  return
97  path = normalize_path(path)
98  if path in self.recent_filesrecent_files:
99  self.recent_filesrecent_files.remove(path)
100  self.recent_filesrecent_files.insert(0, path)
101  self._save_settings_and_update_file_menu_save_settings_and_update_file_menu()
102 
104  self.recent_filesrecent_files = remove_non_existing_paths(self.recent_filesrecent_files)
105  self.recent_filesrecent_files = self.recent_filesrecent_files[:self.max_number_of_files]
106  self.save_settingsave_setting('recent_files', self.recent_filesrecent_files)
107  self.unregister_actionsunregister_actions()
108  self._add_recent_files_to_menu_add_recent_files_to_menu()
109 
111  if not self.recent_filesrecent_files:
112  action = ActionInfo('File', 'No recent files')
113  action.set_menu_position(before='Exit')
114  self.register_actionregister_action(action)
115  else:
116  for n, path in enumerate(self.recent_filesrecent_files):
117  self._add_file_to_menu_add_file_to_menu(path, n)
118  sep = SeparatorInfo('File')
119  sep.set_menu_position(before='Exit')
120  self.register_actionregister_action(sep)
121 
122  def _add_file_to_menu(self, path, n):
123  entry = RecentFileEntry(n+1, path, self)
124  self.register_actionregister_action(entry.get_action_info())
125 
126 
128 
129  def __init__(self, index, file, plugin):
130  self.filefile = file
131  self.indexindex = index
132  self.pathpath = normalize_path(self.filefile)
133  self.filenamefilename = os.path.basename(file)
134  self.pluginplugin = plugin
135  self.labellabel = '&%s: %s' % (index, self.filenamefilename)
136  self.docdoc = 'Open %s' % self.pathpath
137 
138  def OnOpenRecent(self, event):
139  if not self.pluginplugin.frame.check_unsaved_modifications():
140  return
141  self.pluginplugin.open_suite(self.pathpath)
142 
143  def get_action_info(self):
144  action_info = ActionInfo('File', self.labellabel, self.OnOpenRecentOnOpenRecent,
145  doc=self.docdoc)
146  action_info.set_menu_position(before='Exit')
147  return action_info
Used to create menu entries, keyboard shortcuts and/or toolbar buttons.
Definition: actioninfo.py:176
Used to create separators to menus.
Definition: actioninfo.py:262
Entry point to RIDE plugin API – all plugins must extend this class.
Definition: plugin.py:50
def subscribe(self, listener, *topics)
Start to listen to messages with the given topics.
Definition: plugin.py:396
def unsubscribe_all(self)
Stops to listen to all messages this plugin has subscribed to.
Definition: plugin.py:411
def register_action(self, action_info)
Registers a menu entry and optionally a shortcut and a toolbar icon.
Definition: plugin.py:205
def unregister_actions(self)
Unregisters all actions registered by this plugin.
Definition: plugin.py:230
def save_setting(self, name, value, override=True, delay=0)
Saves the specified setting into the RIDE configuration file.
Definition: plugin.py:148
def __init__(self, index, file, plugin)
Definition: recentfiles.py:129
Add recently opened files to the file menu.
Definition: recentfiles.py:37
def enable(self)
This method is called by RIDE when the plugin is enabled.
Definition: recentfiles.py:44
def disable(self)
Called by RIDE when the plugin is disabled.
Definition: recentfiles.py:53