OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ost_startup.py
Go to the documentation of this file.
1 import sys, os, platform
2 import optparse
3 
4 def show_help(option, opt, value, parser):
5  parser.print_help()
6  sys.exit(-1)
7 
8 def interactive_flag(option, opt, value, parser):
9  pass
10 
11 def stop():
12  sys.exit(0)
13 
14 usage = 'usage: ost [ost options] [script to execute] [script parameters]'
15 class OstOptionParser(optparse.OptionParser):
16  def __init__(self, **kwargs):
17  optparse.OptionParser.__init__(self, **kwargs)
18  def exit(self, status_code, error_message):
19  print error_message,
20  sys.exit(-1)
21 
22 parser=OstOptionParser(usage=usage,conflict_handler="resolve", prog='ost''')
23 parser.add_option("-i", "--interactive", action="callback", callback=interactive_flag, help="start interpreter interactively (must be first parameter, ignored otherwise)")
24 parser.add_option("-h", "--help", action="callback", callback=show_help, help="show this help message and exit")
25 parser.add_option("-v", "--verbosity_level", action="store", type="int", dest="vlevel", default=2, help="sets the verbosity level [default: %default]")
26 parser.disable_interspersed_args()
27 (options, args) = parser.parse_args()
28 
29 _site_packs='python%d.%d/site-packages' % sys.version_info[0:2]
30 _base_dir=os.getenv('DNG_ROOT')
31 sys.path.insert(0, os.path.join(_base_dir, 'lib64', _site_packs))
32 
33 from ost import *
34 import ost
35 
36 ost.SetPrefixPath(_base_dir)
37 
38 def _InitRuleBasedProcessor():
39  compound_lib_path=os.path.join(ost.GetSharedDataPath(), 'compounds.chemlib')
40  if os.path.exists(compound_lib_path):
41  compound_lib=conop.CompoundLib.Load(compound_lib_path)
42  conop.SetDefaultLib(compound_lib)
43  io.profiles['DEFAULT'].processor = conop.RuleBasedProcessor(compound_lib)
44 
45 # switch to rule-based processor, if compound library is available
46 _InitRuleBasedProcessor()
47 import os.path
48 HistoryFile=os.path.expanduser('~/.ost_history')
49 
50 # we are not in GUI mode.
51 gui_mode=False
52 
53 sys.ps1='ost> '
54 sys.ps2='..... '
55 sys.argv=sys.argv[1:]
56 home = os.getenv('HOME') or os.getenv('USERPROFILE')
57 _ostrc=os.path.join(home, '.ostrc')
58 if os.path.exists(_ostrc):
59  try:
60  exec(open(_ostrc))
61  except Exception, e:
62  print e
63 PushVerbosityLevel(options.vlevel)
64 
65 # this should probably only be added when running an interactive shell
66 sys.path.append(".")
67 
68 if len(parser.rargs)>0 :
69  script=parser.rargs[0]
70  sys_argv_backup=sys.argv
71  sys.argv=parser.rargs
72  try:
73  execfile(script)
74  finally:
75  sys.argv=sys_argv_backup
76