/
p_import_sedml.py
241 lines (215 loc) · 10.4 KB
/
p_import_sedml.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# -*- coding:utf-8 -*-
#
# Copyright © 2014 Lucian Smith
# Based on the 'pylint' plugin, and the spyderlib/plugins/editor core plugin.
# Licensed under the terms of the MIT License
# (see spyderlib/__init__.py for details)
"""Import SED-ML to Python Plugin"""
import os, time
import re
import spyderplugins.widgets.SedmlToRr as se
from spyderlib.baseconfig import get_translation
from spyderlib.config import CONF
from spyderlib.plugins import SpyderPluginMixin, PluginConfigPage
from spyderlib.py3compat import getcwd, to_text_string, is_text_string
from spyderlib.qt.QtCore import SIGNAL
from spyderlib.qt.QtGui import QVBoxLayout, QGroupBox, QWidget, QApplication, QMessageBox, QMenu
from spyderlib.qt.compat import getopenfilenames
from spyderlib.utils import encoding, sourcecode
from spyderlib.utils.qthelpers import get_icon, create_action, add_actions
from spyderlib.widgets.sourcecode.codeeditor import CodeEditor
_ = get_translation("p_import_sedml", dirname="spyderplugins")
#Temp. disabled (Not needed)
#class S2PConfigPage(PluginConfigPage):
# def setup_page(self):
# settings_group = QGroupBox(_("Settings"))
# save_box = self.create_checkbox(_("Placeholder in case we at some point need a settings page."),
# 'tick_value', default=True)
#
# settings_layout = QVBoxLayout()
# settings_layout.addWidget(save_box)
# settings_group.setLayout(settings_layout)
#
# vlayout = QVBoxLayout()
# vlayout.addWidget(settings_group)
# vlayout.addStretch(1)
# self.setLayout(vlayout)
class S2P(QWidget, SpyderPluginMixin):
"""Import a SED-ML file as a python script"""
CONF_SECTION = 's2p'
#CONFIGWIDGET_CLASS = S2PConfigPage
def __init__(self, parent=None):
QWidget.__init__(self, parent=parent)
SpyderPluginMixin.__init__(self, parent)
layout = QVBoxLayout()
self.setLayout(layout)
# Initialize plugin
self.initialize_plugin()
#------ SpyderPluginWidget API --------------------------------------------
def get_plugin_title(self):
"""Return widget title"""
return _("Import SED-ML as Python")
def get_focus_widget(self):
"""
Return the widget to give focus to when
this plugin's dockwidget is raised on top-level
"""
return None
def get_plugin_actions(self):
"""Return a list of actions related to plugin"""
# Font
return []
def on_first_registration(self):
"""Action to be performed on first plugin registration"""
self.main.tabify_plugins(self.main.inspector, self)
self.dockwidget.hide()
def register_plugin(self):
"""Register plugin in Spyder's main window"""
self.connect(self, SIGNAL("edit_goto(QString,int,QString)"),
self.main.editor.load)
self.connect(self, SIGNAL('redirect_stdio(bool)'),
self.main.redirect_internalshell_stdio)
self.main.add_dockwidget(self)
s2p_act = create_action(self, _("Import SED-ML as Python"),
triggered=self.run_s2p)
s2p_act.setEnabled(True)
#self.register_shortcut(s2p_act, context="SED-ML to Python",
# name="Import SED-ML file", default="Alt-I")
for item in self.main.file_menu_actions:
try:
menu_title = item.title()
except AttributeError:
pass
else:
if not is_text_string(menu_title): # string is a QString
menu_title = to_text_string(menu_title.toUtf8)
if item.title() == str("Import"):
item.addAction(s2p_act)
# s2p_actions = (None, s2p_act)
# import_menu = QMenu(_("Import"))
# add_actions(import_menu, s2p_actions)
# self.main.file_menu_actions.insert(8, import_menu)
def refresh_plugin(self):
"""Refresh hello widget"""
pass
def closing_plugin(self, cancelable=False):
"""Perform actions before parent main window is closed"""
return True
def apply_plugin_settings(self, options):
"""Apply configuration file's plugin settings"""
self.hello_number = self.get_option('hello_number', 1)
pass
def run_s2p(self):
"""Prompt the user to load a SED-ML file, translate to Python, and display in a new window"""
editorwindow = None #Used in editor.load
processevents=True #Used in editor.load
editor = self.main.editor
basedir = getcwd()
if CONF.get('workingdir', 'editor/open/browse_scriptdir'):
c_fname = editor.get_current_filename()
if c_fname is not None and c_fname != editor.TEMPFILE_PATH:
basedir = os.path.dirname(c_fname)
editor.emit(SIGNAL('redirect_stdio(bool)'), False)
parent_widget = editor.get_current_editorstack()
selectedfilter = ''
filters = 'SED-ML files (*.sedml *.xml);;All files (*.*)'
filenames, _selfilter = getopenfilenames(parent_widget,
_("Open SED-ML file"), basedir, filters,
selectedfilter=selectedfilter)
editor.emit(SIGNAL('redirect_stdio(bool)'), True)
if filenames:
filenames = [os.path.normpath(fname) for fname in filenames]
if CONF.get('workingdir', 'editor/open/auto_set_to_basedir'):
directory = os.path.dirname(filenames[0])
editor.emit(SIGNAL("open_dir(QString)"), directory)
else:
#The file dialog box was closed without selecting a file.
return
focus_widget = QApplication.focusWidget()
if editor.dockwidget and not editor.ismaximized and\
(not editor.dockwidget.isAncestorOf(focus_widget)\
and not isinstance(focus_widget, CodeEditor)):
editor.dockwidget.setVisible(True)
editor.dockwidget.setFocus()
editor.dockwidget.raise_()
def _convert(fname):
fname = os.path.abspath(encoding.to_unicode_from_fs(fname))
if os.name == 'nt' and len(fname) >= 2 and fname[1] == ':':
fname = fname[0].upper()+fname[1:]
return fname
if hasattr(filenames, 'replaceInStrings'):
# This is a QStringList instance (PyQt API #1), converting to list:
filenames = list(filenames)
if not isinstance(filenames, list):
filenames = [_convert(filenames)]
else:
filenames = [_convert(fname) for fname in list(filenames)]
for index, filename in enumerate(filenames):
p = re.compile( '(.xml$|.sedml$)')
pythonfile = p.sub( '_sedml.py', filename)
if (pythonfile == filename):
pythonfile = filename + "_sedml.py"
current_editor = editor.set_current_filename(pythonfile, editorwindow)
if current_editor is not None:
# -- TODO: Do not open an already opened file
pass
else:
# -- Not an existing opened file:
if not os.path.isfile(filename):
continue
# --
current_es = editor.get_current_editorstack(editorwindow)
# Creating the editor widget in the first editorstack (the one
# that can't be destroyed), then cloning this editor widget in
# all other editorstacks:
finfo, newname = self.load_and_translate(filename, pythonfile, editor)
finfo.path = editor.main.get_spyder_pythonpath()
editor._clone_file_everywhere(finfo)
current_editor = current_es.set_current_filename(newname)
#if (current_editor is not None):
# editor.register_widget_shortcuts("Editor", current_editor)
current_es.analyze_script()
if (current_editor is not None):
current_editor.clearFocus()
current_editor.setFocus()
current_editor.window().raise_()
if processevents:
QApplication.processEvents()
def load_and_translate(self, sedmlfile, pythonfile, editor, set_current=True):
"""
Read filename as SED-ML file, translate it to Python, and
create an editor instance and return it
*Warning* This is loading file, creating editor but not executing
the source code analysis -- the analysis must be done by the editor
plugin (in case multiple editorstack instances are handled)
"""
#sedmlfile = to_text_string(sedmlfile)
sedmlfile = str(sedmlfile)
self.emit(SIGNAL('starting_long_process(QString)'),
_("Loading %s...") % sedmlfile)
text, enc = encoding.read(sedmlfile)
fname = os.path.basename(sedmlfile)
temp = "# -*- coding: utf-8 -*-\n\n" + '"Generated by Import SED-ML plugin ' + time.strftime("%m/%d/%Y") + '"\n"Extracted from ' + fname + '"\n\n'
text = temp + se.sedml_to_python(sedmlfile)
widgeteditor = editor.editorstacks[0]
finfo = widgeteditor.create_new_editor(pythonfile, enc, text, set_current, new=True)
index = widgeteditor.data.index(finfo)
widgeteditor._refresh_outlineexplorer(index, update=True)
self.emit(SIGNAL('ending_long_process(QString)'), "")
if widgeteditor.isVisible() and widgeteditor.checkeolchars_enabled \
and sourcecode.has_mixed_eol_chars(text):
name = os.path.basename(pythonfile)
QMessageBox.warning(self, widgeteditor.title,
_("<b>%s</b> contains mixed end-of-line "
"characters.<br>Spyder will fix this "
"automatically.") % name,
QMessageBox.Ok)
widgeteditor.set_os_eol_chars(index)
widgeteditor.is_analysis_done = False
finfo.editor.set_cursor_position('eof')
finfo.editor.insert_text(os.linesep)
return finfo, sedmlfile
#==============================================================================
# The following statements are required to register this 3rd party plugin:
#==============================================================================
PLUGIN_CLASS = S2P