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 if platform.machine()=='x86_64':
30  sys.path.insert(0, os.getenv('DNG_ROOT')+'/lib64/openstructure')
31 else:
32  sys.path.insert(0,os.getenv('DNG_ROOT')+'/lib/openstructure')
33 
34 from ost import *
35 import ost
36 
37 ost.SetPrefixPath(os.getenv('DNG_ROOT'))
38 def _InitRuleBasedBuilder():
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.RegisterBuilder(conop.RuleBasedBuilder(compound_lib), 'RBB')
43  conop.SetDefaultBuilder('RBB')
44 
45 # switch to rule-based builder for high fidelity if compounds.chemlib is
46 # available
47 _InitRuleBasedBuilder()
48 import os.path
49 HistoryFile=os.path.expanduser('~/.ost_history')
50 
51 # this is a silly name...
52 InGUIMode=False
53 # ... lets be more pythonic
54 gui_mode=False
55 
56 sys.ps1='ost> '
57 sys.ps2='..... '
58 sys.argv=sys.argv[1:]
59 home = os.getenv('HOME') or os.getenv('USERPROFILE')
60 _ostrc=os.path.join(home, '.ostrc')
61 if os.path.exists(_ostrc):
62  try:
63  exec(open(_ostrc))
64  except Exception, e:
65  print e
66 else:
67  rcfile=open(_ostrc,"w")
68  print >> rcfile, '# This python file is parsed by ost and dng at startup'
69  print >> rcfile, '# Its content is be made available in the global namespace'
70  print >> rcfile, '# It can be used to define custom variables and functions'
71  print >> rcfile, '# For example:'
72  print >> rcfile, '# IMPORTANT_DIR="path/to/important/dir"'
73  rcfile.close()
74 
75 PushVerbosityLevel(options.vlevel)
76 
77 sys.path.append(".")
78 
79 if len(parser.rargs)>0 :
80  script=parser.rargs[0]
81  sys_argv_backup=sys.argv
82  sys.argv=parser.rargs
83  try:
84  execfile(script)
85  finally:
86  sys.argv=sys_argv_backup
87