SemanticSBML

Documentation of the Custom Interactive Command Line User Interface

The module was created since no Python module (known to us) provides a simple, customizable interactive shell (similar to the Python interactive shell). On this page you can read about the following topics>

Example Session

Here is an example session.

test@test:~/test$ ./semanticsbml_console.py

<<< semanticSBML main menu >>>
l list all loaded models
o <FILENAME> open a model
d <DIR> open all models in the directory (without arguments last used)
c <MODEL_NR> display check results for a model
a <MODEL_NR> annotate a model with database identifiers
e export a svg image of a model
m [<MODEL_NR1> , ..] merge 2 or more models by inserting a list of models
s save model(s)
v save model(s) as
r <MODEL_NR> remove model
about about this software
i ID -> SBML Generate SBML files from Database Identifiers
commands: help, dir, rec, prec, play, hist, q, exit
you can use ctrl+D (win ctr+Z) to exit
>>>o models/test01.xml
opened test01

Concept of the CustomConsoleInterface

The console interface functionality is provided by the CustomInteractiveConsole module (see Figure 1). It consist of two classes: the main class CustomConsole and a singleton data storage class Singleton data store. The main class can be instantiated with a dictionary containing commands as keys (strings) and function pointers as values. When the run function is called a loop is started that prompts the user for an input and executes the according function if the input matches a keyword from the command dictionary. If the user did not enter a keyword all available commands are displayed. Since sub menus are needed e.g., main-menu annotate-menu it is possible to open a new CustomConsole instance within a running loop. The function raw input provides the ability that only single values can be retrieved. This function is also used internally to retrieve user input.

uml diagram of the custominteractive console

Interation of the CustomConsolIterface into Your Source Code

The integration is quite simple.
In the following example the module is loaded and a dictionary of commands and function as well as a help text is created and inserted into the CustomConsole class. The command loop is then started with the run function.

from semanticSBML.CustomInteractiveConsole import CustomConsole

self._locals = {
'l':(self.listFiles,'List Models'),
'i2s':(Id2rom semanticSBML.CustomInteractiveConsole import CustomConsole

self._locals = {
'l':(self.listFiles,'List Models'),
'i2s':(Id2rom semanticSBML.CustomInteractiveConsole import CustomConsole

self._locals = {
'l':(self.listFiles,'List Models'),
'i2s':(Id2Sbml_view,'ID -> SBML'),
'd':(self.openDirectory,'Open Directory')
}
self._help="
<<< semanticSBML main menu >>>
l list all loaded models
d <DIR> open all models in the directory (without arguments last used)
i2s ID -> SBML Generate SBML files from Database Identifiers"

cc = CustomConsole(self._locals,self._help).run()

The next example shows how to create a submenu. Notice that the class below will be crated when a use inserts "i2s" in the example above. By invoking the creation of the Id2Sbml view class a sub menu is created.

class Id2Sbml_view(QWidget):
def __init__(self):
help="<<< ID -> SBML >>>
e <ID1 ID2> Enter a List of KEGG Reaction Identifiers
q exit this menu"
cc = CustomConsole({'e':(self.slotNext_l,'insert list'),'q':(self.exit,'exit')},help).run('...')
...

Also user input can be returned directly without connecting it to a function.

input = CustomConsole().raw_input('Are you sure you want to do this? y/n:')

The raw input function of the CustomConsole class is used instead of the native Python raw input function since its input is captured and can be replayed. Just like the GUI, the CI interface consists of a main class and views for each of the functions of semanticSBML. The views can be found in the console views module and correspond in their make-up to the GUI views.

Features

Batch Processing

The CustomConsoleInterface enables batch processing for your application. Here is an example:

test@test:~/test$$ ./semanticsbml_console.py -q 'c 0;a 0' -p

This will put the commands c 0 and a 0 on a command queue and execute them right away. The source code to achieve this behaviour looks as follows.

parser = OptionParser('usage: %prog [options] ')         
parser.add_option("-q", "--cmdqueue", dest="cq", help="set the command queue",metavar='CQ')         
parser.add_option("-v", "--verbose", action="store_true",dest="verb", help="let functions crash")         
parser.add_option("-p", "--play", action="store_true",dest="play", help="play stored command queue on start")         
(options, args) = parser.parse_args()                          
CustomConsole(self._locals,self._help).run(options.verb,options.play,options.cq) 

Notice that there is also a verbose option. Usually incorrect user input will be caught and display the python exception without crashing the whole program. If you are debugging your application this might not be desired - using the verbose mode will disable the exception catching in the command loop.

Save application exit.

If your application manipulates documents you might want to catch and application exit to save modified documents first. This can be done by setting an on-exit function before starting the command loop. Here is an example:

cc = CustomConsole(self._locals,self._help,verbose)                 
cc.setOnExit(self.closeEvent)                
cc.run() 

 

Download and License

The module is independent from the rest of the source code and can be freely used in any open source project since it is under the GNU public license. You can download it directly from the SVN repository. The module is documented using Epydoc.