| 1 |
<?xml version="1.0" ?> |
| 2 |
<!DOCTYPE html |
| 3 |
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
| 4 |
"DTD/xhtml1-strict.dtd"> |
| 5 |
|
| 6 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
| 7 |
<head> |
| 8 |
<link href="css/style.css" rel="stylesheet" type="text/css"/> |
| 9 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> |
| 10 |
<title>Using BKChem in batch mode</title> |
| 11 |
</head> |
| 12 |
<body> |
| 13 |
<h2>Using BKChem in batch mode</h2> |
| 14 |
|
| 15 |
<h3>Foreword</h3> |
| 16 |
|
| 17 |
<p>In version <b>0.9.0</b> of BKChem I have introduced an undocumented batch mode. |
| 18 |
Depending on command line switches BKChem converted all given files of one type to another etc. |
| 19 |
The problem of this approach was that it would be necessary to implement a huge number of |
| 20 |
command line switches to give the user at least some power to do what he wants.</p> |
| 21 |
|
| 22 |
<p>From version <b>0.10.0-pre1</b> I have decided to use a completely different approach, based on my |
| 23 |
experience with custom plugins. In this version, there is only one command line switch (-b) that |
| 24 |
tells BKChem to start in batch mode. The following argument is a name of a Python script to run. |
| 25 |
</p> |
| 26 |
|
| 27 |
|
| 28 |
<h3>How does a batch script work</h3> |
| 29 |
|
| 30 |
<p>When you start BKChem in batch mode it at first starts all its vital parts and |
| 31 |
then gives control to the Python script given on command line. All this happens without actually |
| 32 |
displaying a BKChem window (even though the window is in fact created, so it does not work |
| 33 |
without X on Unix). After the script has finished its work and the control returns back |
| 34 |
to BKChem, the program simply terminates.</p> |
| 35 |
|
| 36 |
<p>The anatomy of a batch script is very similar to that of a |
| 37 |
<a href="custom_plugins_en.html">custom plugin</a>. The script receives a global variable |
| 38 |
<i>App</i>, which is the actual BKChem instance. In case some further arguments are given on the |
| 39 |
command line on startup, they are passed as a list in a global variable <i>Args</i>. |
| 40 |
The script can use these variables in any way it wants, it can also use any standard Python |
| 41 |
functions and modules.</p> |
| 42 |
|
| 43 |
|
| 44 |
<h3>Sample batch script</h3> |
| 45 |
|
| 46 |
<p>In this <a href="scripts/batch_demo1.py">file</a> you will find complete code of the example |
| 47 |
discussed bellow. It is a simple batch script that loads all BKChem files in all directories |
| 48 |
specified on command line, sets font size of all atoms to 12 and re-saves the files. |
| 49 |
The most important parts of the batch script are discussed bellow. |
| 50 |
</p> |
| 51 |
|
| 52 |
<pre> |
| 53 |
if Args: |
| 54 |
for arg in Args: |
| 55 |
update_svgs_in_path( arg) |
| 56 |
else: |
| 57 |
print "You must supply a path as first argument to the batch script" |
| 58 |
</pre> |
| 59 |
|
| 60 |
<p><i>Args</i> is together with <i>App</i> a global variable containing a list of command line |
| 61 |
arguments given after the script name.</p> |
| 62 |
|
| 63 |
<pre> |
| 64 |
if App.load_CDML( f, replace=1): |
| 65 |
print "OK" |
| 66 |
for mol in App.paper.molecules: |
| 67 |
for atom in mol.atoms: |
| 68 |
atom.font_size = 12 |
| 69 |
App.save_CDML() |
| 70 |
return 1 |
| 71 |
else: |
| 72 |
print "ignoring" |
| 73 |
return 0 |
| 74 |
</pre> |
| 75 |
|
| 76 |
<p><i>App.load_CDML</i> returns true when a file is successfully loaded. The replace argument |
| 77 |
tells BKChem to load the file replacing the current one (instead of opening a new tab), thus we |
| 78 |
don't have to deal with closing tabs.</p> |
| 79 |
|
| 80 |
<p>From the few lines presented above (and the whole script) in fact only 4 or 5 are really |
| 81 |
interesting and do really do something. The rest is just a glue, comments and debugging |
| 82 |
output. In fact writing a batch script could not be easier :)</p> |
| 83 |
|
| 84 |
<p>Please feel free to send me any comments on my English, clarity of the above text and other |
| 85 |
problems you may have with this brief tutorial.</p> |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
</body> |
| 90 |
</html> |