Commit 7adb267c authored by eric pellegrini's avatar eric pellegrini
Browse files

Removed unused packages/modules

parent 0ae79e26
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library 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
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''
Created on Jun 16, 2015
:author: Eric C. Pellegrini
'''
import cPickle
import glob
import optparse
import os
import subprocess
import sys
from MDANSE.Core.Error import Error
from MDANSE import ELEMENTS, PLATFORM, REGISTRY
from MDANSE.Framework.Jobs.JobStatus import JobState
class CommandLineParserError(Error):
pass
class CommandLineParser(optparse.OptionParser):
'''A sublcass of OptionParser.
Creates the MDANSE commad line parser.
'''
def add_mmtk_definition(self, option, opt_str, value, parser):
if len(parser.rargs) != 3:
raise CommandLineParserError("Invalid number of arguments for %r option" % opt_str)
from MDANSE.Framework.MMTKDefinitions import MMTK_DEFINITIONS
MMTK_DEFINITIONS.add(*parser.rargs)
MMTK_DEFINITIONS.save()
def check_job(self, option, opt_str, value, parser):
'''Display the jobs list
@param option: the option that triggered the callback.
@type option: optparse.Option instance
@param opt_str: the option string seen on the command line.
@type opt_str: str
@param value: the argument for the option.
@type value: str
@param parser: the MDANSE option parser.
@type parser: instance of MDANSEOptionParser
'''
if len(parser.rargs) != 1:
raise CommandLineParserError("Invalid number of arguments for %r option" % opt_str)
basename = parser.rargs[0]
filename = os.path.join(PLATFORM.temporary_files_directory(),basename)
if not os.path.exists(filename):
raise CommandLineParserError("Invalid job name")
# Open the job temporary file
try:
f = open(filename, 'rb')
info = cPickle.load(f)
f.close()
# If the file could not be opened/unpickled for whatever reason, try at the next checkpoint
except:
raise CommandLineParserError("The job %r could not be opened properly." % basename)
# The job file could be opened and unpickled properly
else:
# Check that the unpickled object is a JobStatus object
if not isinstance(info,JobState):
raise CommandLineParserError("Invalid contents for job %r." % basename)
print "Information about %s job:" % basename
for k,v in info.iteritems():
print "%-20s [%s]" % (k,v)
def display_element_info(self, option, opt_str, value, parser):
if len(parser.rargs) != 1:
raise CommandLineParserError("Invalid number of arguments for %r option" % opt_str)
element = parser.rargs[0]
try:
print ELEMENTS.info(element)
except ValueError:
raise CommandLineParserError("The entry %r is not registered in the database" % element)
def display_jobs_list(self, option, opt_str, value, parser):
'''Display the jobs list
@param option: the option that triggered the callback.
@type option: optparse.Option instance
@param opt_str: the option string seen on the command line.
@type opt_str: str
@param value: the argument for the option.
@type value: str
@param parser: the MDANSE option parser.
@type parser: instance of MDANSEOptionParser
'''
if len(parser.rargs) != 0:
raise CommandLineParserError("Invalid number of arguments for %r option" % opt_str)
jobs = [f for f in glob.glob(os.path.join(PLATFORM.temporary_files_directory(),'*'))]
for j in jobs:
# Open the job temporary file
try:
f = open(j, 'rb')
info = cPickle.load(f)
f.close()
# If the file could not be opened/unpickled for whatever reason, try at the next checkpoint
except:
continue
# The job file could be opened and unpickled properly
else:
# Check that the unpickled object is a JobStatus object
if not isinstance(info,JobState):
continue
print "%-20s [%s]" % (os.path.basename(j),info["state"])
def display_trajectory_contents(self, option, opt_str, value, parser):
'''Displays trajectory contents
@param option: the option that triggered the callback.
@type option: optparse.Option instance
@param opt_str: the option string seen on the command line.
@type opt_str: str
@param value: the argument for the option.
@type value: str
@param parser: the MDANSE option parser.
@type parser: instance of MDANSEOptionParser
'''
trajName = parser.rargs[0]
inputTraj = REGISTRY["input_data"]["mmtk_trajectory"](trajName)
print inputTraj.info()
def error(self, msg):
'''Called when an error occured in the command line.
@param msg: the error message.
@type msg: str
'''
self.print_help(sys.stderr)
print "\n"
self.exit(2, "Error: %s\n" % msg)
def query_classes_registry(self, option, opt_str, value, parser):
'''
Callback that displays the list of the jobs available in MDANSE
@param option: the Option instance calling the callback.
@param opt_str: the option string seen on the command-line triggering the callback
@param value: the argument to this option seen on the command-line.
@param parser: the MDANSEOptionParser instance.
'''
if len(parser.rargs) == 0:
print "Registered interfaces:"
for interfaceName in REGISTRY.get_interfaces():
print "\t- %s" % interfaceName
elif len(parser.rargs) == 1:
val = parser.rargs[0]
print REGISTRY.info(val.lower())
else:
raise CommandLineParserError("Invalid number of arguments for %r option" % opt_str)
def run_job(self, option, opt_str, value, parser):
'''Run job file(s).
@param option: the option that triggered the callback.
@type option: optparse.Option instance
@param opt_str: the option string seen on the command line.
@type opt_str: str
@param value: the argument for the option.
@type value: str
@param parser: the MDANSE option parser.
@type parser: instance of MDANSEOptionParser
'''
if len(parser.rargs) != 1:
raise CommandLineParserError("Invalid number of arguments for %r option" % opt_str)
filename = parser.rargs[0]
if not os.path.exists(filename):
raise CommandLineParserError("The job file %r could not be executed" % filename)
subprocess.Popen([sys.executable, filename])
def save_job_template(self, option, opt_str, value, parser):
'''
Save job templates.
@param option: the option that triggered the callback.
@type option: optparse.Option instance
@param opt_str: the option string seen on the command line.
@type opt_str: str
@param value: the argument for the option.
@type value: str
@param parser: the MDANSE option parser.
@type parser: instance of MDANSEOptionParser
'''
if len(parser.rargs) != 1:
raise CommandLineParserError("Invalid number of arguments for %r option" % opt_str)
jobs = REGISTRY["job"]
name = parser.rargs[0]
# A name for the template is built.
filename = os.path.abspath('template_%s.py' % name.lower())
jobs[name].save(filename)
# Try to save the template for the job.
try:
jobs[name].save(filename)
# Case where an error occured when writing the template.
except IOError:
raise CommandLineParserError("Could not write the job template as %r" % filename)
# If the job class has no save method, thisis not a valid MDANSE job.
except KeyError:
raise CommandLineParserError("The job %r is not a valid MDANSE job" % name)
# Otherwise, print some information about the saved template.
else:
print "Saved template for job %r as %r" % (name, filename)
import wx.combo
class ComboCheckbox(wx.combo.ComboPopup):
def __init__(self, items, maxNumberOfItems=None):
wx.combo.ComboPopup.__init__(self)
self._items = items
self._maxNumberOfItems = maxNumberOfItems
@property
def items(self):
return self._items
@property
def checklistbox(self):
return self._checklistbox
def Create(self, parent):
self._checklistbox = wx.CheckListBox(parent, -1, choices=self._items)
self._checklistbox.Bind(wx.EVT_CHECKLISTBOX, self.on_check_item)
if not self._checklistbox.IsEmpty():
self._checklistbox.Check(0)
return True
def GetControl(self):
return self._checklistbox
def GetAdjustedSize(self, minWidth, prefHeight, maxHeight):
return self._checklistbox.GetSize()
def GetStringValue(self):
return self._checklistbox.GetCheckedStrings()
def on_check_item(self, event):
if self._maxNumberOfItems is None:
return
nCheckedItems = len(self._checklistbox.GetChecked())
if nCheckedItems > self._maxNumberOfItems:
self._checklistbox.Check(event.GetInt(), False)
\ No newline at end of file
import collections
import os
import wx.combo
import wx.lib.filebrowsebutton as wxfile
class CheckboxComboPopup(wx.combo.ComboPopup):
def __init__(self, items, maxNumberOfItems=None):
wx.combo.ComboPopup.__init__(self)
self._items = items
self._maxNumberOfItems = maxNumberOfItems
@property
def items(self):
return self._items
@property
def checklistbox(self):
return self._checklistbox
def Create(self, parent):
self._checklistbox = wx.CheckListBox(parent, -1, choices=self._items)
self._checklistbox.Bind(wx.EVT_CHECKLISTBOX, self.on_check_item)
if not self._checklistbox.IsEmpty():
self._checklistbox.Check(0)
return True
def GetControl(self):
return self._checklistbox
def GetAdjustedSize(self, minWidth, prefHeight, maxHeight):
return self._checklistbox.GetSize()
def GetStringValue(self):
return self._checklistbox.GetCheckedStrings()
def on_check_item(self, event):
if self._maxNumberOfItems is None:
return
nCheckedItems = len(self._checklistbox.GetChecked())
if nCheckedItems > self._maxNumberOfItems:
self._checklistbox.Check(event.GetInt(), False)
class ComboPanel(wx.Panel):
def __init__(self, parent, *args, **kwargs):
wx.Panel.__init__(self, parent, *args, **kwargs)
class ComboCheckableMenu(ComboPanel):
def __init__(self, parent, choices, exclusive=False, labelText="", menuText="", *args, **kwargs):
ComboPanel.__init__(self, parent, *args, **kwargs)
self._choices = collections.OrderedDict().fromkeys(choices, False)
self._exclusive = exclusive
self._labelText = labelText
self._menuText = menuText
self.build_panel()
self.build_layout()
def build_panel(self):
self._button = self.create_menubutton()
def build_layout(self):
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self._button, 1, flag=wx.EXPAND)
self.SetSizer(sizer)
def create_menubutton(self):
button = wx.Button(self, wx.ID_ANY, label=self._menuText)
button.Bind(wx.EVT_BUTTON, self.on_display_menu)
return button
def get_value(self):
selection = [label for label,state in self._choices.items() if state]
return selection
def on_check_menuitem(self, event):
for item in event.GetEventObject().GetMenuItems():
self._choices[item.GetLabel()] = item.IsChecked()
if self._exclusive:
item = event.GetEventObject().FindItemById(event.GetId())
self._button.SetLabel(item.GetLabel())
def on_display_menu(self, event):
menu = wx.Menu()
for label, state in self._choices.items():
if self._exclusive:
item = menu.AppendRadioItem(wx.ID_ANY, label)
else:
item = menu.AppendCheckItem(wx.ID_ANY, label)
item.Check(state)
x,y = self._button.GetPosition()
w,h = self._button.GetSize()
self.Bind(wx.EVT_MENU, self.on_check_menuitem)
self._button.PopupMenu(menu, (x+w/2,y+h/2))
class ComboCheckbox(ComboPanel):
def __init__(self,
parent,
parameters=None,
sizerParameters=None,
checked=False,
*args, **kwargs):
ComboPanel.__init__(self, parent, *args, **kwargs)
self._parameters = parameters
self._checked = checked
if sizerParameters is None:
sizerParameters = {"hgap" : 5, "vgap" : 5}
self._sizerParameters = sizerParameters
self.build_panel()
self.build_layout()
self.on_toggle_checkbox_state()
def build_panel(self):
check = self._parameters.setdefault("check",{})
cParams = check.setdefault("w_parameters",{})
self._checkbox = wx.CheckBox(self, **cParams)
self._widget = self._parameters.setdefault("widget",None)
if self._widget is not None:
widget, wParams = self._widget
wParams.setdefault("w_parameters",{})
self._widget = widget(self, **wParams["w_parameters"])
self._checkbox.SetValue(self._checked)
self._checkbox.Bind(wx.EVT_CHECKBOX, self.on_toggle_checkbox_state)
def build_layout(self):
sizer = wx.GridBagSizer(**self._sizerParameters)
sParams = self._parameters["check"].setdefault("s_parameters",{})
sizer.Add(self._checkbox, **sParams)
if self._widget is not None:
sParams = self._parameters["widget"][1].setdefault("s_parameters",{})
sizer.Add(self._widget, **sParams)
self.SetSizer(sizer)
def get_value(self):
checked = self._checkbox.GetValue()
if checked:
try:
value = self._widget.get_value()
except AttributeError:
try:
value = self._widget.GetValue()
except AttributeError:
value = None
else:
value = None
return (checked,value)
def on_toggle_checkbox_state(self, event=None):
checked = self._checkbox.GetValue()
try:
self._widget.Enable(checked)
except:
pass
class ComboOutputFile(ComboPanel):
def __init__(self, parent, formats, sizerParameters=None, *args, **kwargs):
ComboPanel.__init__(self, parent, *args, **kwargs)
self._formats = formats
if sizerParameters is None:
sizerParameters = {"hgap" : 5, "vgap" : 5}
self._sizerParameters = sizerParameters
self.build_panel()
self.build_layout()
@property
def directory(self):
return self._directory
@property
def basename(self):
return self._basename
def build_panel(self):
self._directory = wxfile.DirBrowseButton(self, labelText="Directory", startDirectory=os.getcwd(), newDirectory=True)
self._directory.label.SetWindowStyle(wx.ALIGN_LEFT)
self._basenameLabel = wx.StaticText(self, label="Basename", style=wx.ALIGN_LEFT)
self._basename = wx.TextCtrl(self, value="")
self._formats = ComboCheckableMenu(self, self._formats, labelText="File formats", menuText="Formats")
def build_layout(self):
sizer = wx.GridBagSizer(**self._sizerParameters)
sizer.Add(self._directory, pos=(0,0), span=(1,3), flag=wx.ALIGN_CENTER_VERTICAL|wx.EXPAND)
sizer.Add(self._basenameLabel, pos=(1,0), flag=wx.ALIGN_CENTER_VERTICAL)
sizer.Add(self._basename, pos=(1,1), flag=wx.EXPAND)
sizer.Add(self._formats, pos=(1,2), flag=wx.EXPAND)
sizer.AddGrowableCol(1)
self.SetSizer(sizer)
def get_value(self):
directory = self._directory.GetValue()
basename = self._basename.GetValue().strip()