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, glob
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 = """
15 
16  ost [ost options] [script to execute] [script parameters]
17 
18 or
19  ost [action name] [action options]
20 
21 """
22 
23 action_path = os.path.abspath(os.environ.get("OST_EXEC_DIR", ""))
24 
25 usage += 'Following actions are available:\n'
26 for action in sorted(glob.glob(os.path.join(action_path, 'ost-*'))):
27  usage += " %s\n" % action[len(action_path)+5:]
28 usage += '\nEach action should respond to "--help".\n'
29 
30 class OstOptionParser(optparse.OptionParser):
31  def __init__(self, **kwargs):
32  optparse.OptionParser.__init__(self, **kwargs)
33  def exit(self, status_code, error_message):
34  print error_message,
35  sys.exit(-1)
36 
37 parser=OstOptionParser(usage=usage,conflict_handler="resolve", prog='ost''')
38 parser.add_option("-i", "--interactive", action="callback", callback=interactive_flag, help="start interpreter interactively (must be first parameter, ignored otherwise)")
39 parser.add_option("-h", "--help", action="callback", callback=show_help, help="show this help message and exit")
40 parser.add_option("-v", "--verbosity_level", action="store", type="int", dest="vlevel", default=2, help="sets the verbosity level [default: %default]")
41 parser.disable_interspersed_args()
42 (options, args) = parser.parse_args()
43 
44 _site_packs='python%d.%d/site-packages' % sys.version_info[0:2]
45 _base_dir=os.getenv('DNG_ROOT')
46 sys.path.insert(0, os.path.join(_base_dir, 'lib64', _site_packs))
47 
48 from ost import SetPrefixPath, GetSharedDataPath, conop
49 
50 SetPrefixPath(_base_dir)
51 
52 def _InitRuleBasedProcessor():
53  compound_lib_path=os.path.join(GetSharedDataPath(), 'compounds.chemlib')
54  if os.path.exists(compound_lib_path):
55  compound_lib=conop.CompoundLib.Load(compound_lib_path)
56  conop.SetDefaultLib(compound_lib)
57 
58 # switch to rule-based processor, if compound library is available
59 _InitRuleBasedProcessor()
60 from ost import *
61 import ost
62 
63 import os.path
64 HistoryFile=os.path.expanduser('~/.ost_history')
65 
66 # we are not in GUI mode.
67 gui_mode=False
68 
69 sys.ps1='ost> '
70 sys.ps2='..... '
71 sys.argv=sys.argv[1:]
72 home = os.getenv('HOME') or os.getenv('USERPROFILE')
73 _ostrc=os.path.join(home, '.ostrc')
74 if os.path.exists(_ostrc):
75  try:
76  exec(open(_ostrc))
77  except Exception, e:
78  print e
79 PushVerbosityLevel(options.vlevel)
80 
81 # this should probably only be added when running an interactive shell
82 sys.path.append(".")
83 
84 if len(parser.rargs)>0 :
85  script=parser.rargs[0]
86  sys_argv_backup=sys.argv
87  sys.argv=parser.rargs
88  try:
89  execfile(script)
90  finally:
91  sys.argv=sys_argv_backup
92 
def interactive_flag
Definition: ost_startup.py:8