OpenStructure
init_cl.py
Go to the documentation of this file.
1 import sys, os, platform
2 import optparse
3 
4 interactive=False
5 
6 def show_help(option, opt, value, parser):
7  parser.print_help()
8  sys.exit(-1)
9 
10 def interactive_flag(option, opt, value, parser):
11  pass
12 
13 def stop():
14  sys.exit(0)
15 
16 usage = 'usage: ost [ost options] [script to execute] [script parameters]'
17 class OstOptionParser(optparse.OptionParser):
18  def __init__(self, **kwargs):
19  optparse.OptionParser.__init__(self, **kwargs)
20  def exit(self, status_code, error_message):
21  print error_message,
22  sys.exit(-1)
23 
24 parser=OstOptionParser(usage=usage,conflict_handler="resolve", prog='ost''')
25 parser.add_option("-i", "--interactive", action="callback", callback=interactive_flag, help="start interpreter interactively (must be first parameter, ignored otherwise)")
26 parser.add_option("-h", "--help", action="callback", callback=show_help, help="show this help message and exit")
27 parser.add_option("-v", "--verbosity_level", action="store", type="int", dest="vlevel", default=2, help="sets the verbosity level [default: %default]")
28 parser.disable_interspersed_args()
29 (options, args) = parser.parse_args()
30 
31 if platform.machine()=='x86_64':
32  sys.path.insert(0, os.getenv('DNG_ROOT')+'/lib64/openstructure')
33 else:
34  sys.path.insert(0,os.getenv('DNG_ROOT')+'/lib/openstructure')
35 
36 from ost import *
37 import ost
38 
39 ost.SetPrefixPath(os.getenv('DNG_ROOT'))
40 def _InitRuleBasedBuilder():
41  compound_lib_path=os.path.join(ost.GetSharedDataPath(), 'compounds.chemlib')
42  if os.path.exists(compound_lib_path):
43  conop_inst=conop.Conopology.Instance()
44  compound_lib=conop.CompoundLib.Load(compound_lib_path)
45  conop_inst.RegisterBuilder(conop.RuleBasedBuilder(compound_lib), 'RBB')
46  conop_inst.SetDefaultBuilder('RBB')
47 
48 # switch to rule-based builder for high fidelity if compounds.chemlib is
49 # available
50 _InitRuleBasedBuilder()
51 import os.path
52 HistoryFile=os.path.expanduser('~/.ost_history')
53 InGUIMode=False
54 
55 sys.ps1='ost> '
56 sys.ps2='..... '
57 sys.argv=sys.argv[1:]
58 home = os.getenv('HOME') or os.getenv('USERPROFILE')
59 _ostrc=os.path.join(home, '.ostrc')
60 if os.path.exists(_ostrc):
61  try:
62  exec(open(_ostrc))
63  except Exception, e:
64  print e
65 else:
66  rcfile=open(_ostrc,"w")
67  print >> rcfile, '# This python file is parsed by ost and dng at startup'
68  print >> rcfile, '# Its content is be made available in the global namespace'
69  print >> rcfile, '# It can be used to define custom variables and functions'
70  print >> rcfile, '# For example:'
71  print >> rcfile, '# IMPORTANT_DIR="path/to/important/dir"'
72  rcfile.close()
73 
74 PushVerbosityLevel(options.vlevel)
75 
76 sys.path.append(".")
77 
78 if len(parser.rargs)>0 :
79  script=parser.rargs[0]
80  sys_argv_backup=sys.argv
81  sys.argv=parser.rargs
82  try:
83  execfile(script)
84  finally:
85  sys.argv=sys_argv_backup
86