OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
init_cl.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 if platform.machine()=='x86_64':
31  sys.path.insert(0, os.path.join(os.getenv('DNG_ROOT'), 'lib64', _site_packs))
32 else:
33  sys.path.insert(0,os.path.join(os.getenv('DNG_ROOT'), 'lib', _site_packs))
34 
35 from ost import *
36 import ost
37 
38 ost.SetPrefixPath(os.getenv('DNG_ROOT'))
39 def _InitRuleBasedBuilder():
40  compound_lib_path=os.path.join(ost.GetSharedDataPath(), 'compounds.chemlib')
41  if os.path.exists(compound_lib_path):
42  compound_lib=conop.CompoundLib.Load(compound_lib_path)
43  conop.RegisterBuilder(conop.RuleBasedBuilder(compound_lib), 'RBB')
44  conop.SetDefaultBuilder('RBB')
45 
46 # switch to rule-based builder for high fidelity if compounds.chemlib is
47 # available
48 _InitRuleBasedBuilder()
49 import os.path
50 HistoryFile=os.path.expanduser('~/.ost_history')
51 
52 # we are not in GUI mode.
53 gui_mode=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