OpenStructure
Loading...
Searching...
No Matches
init.py
Go to the documentation of this file.
1import __main__
2import sys
3import os.path
4import optparse
5from ost import io, mol, seq, geom, conop, gui, settings, gfx
6
7import ost
8try:
9 from ost import img
10 import ost.img.alg
11 _img_present=True
12except ImportError:
13 _img_present=False
14 pass
15
16import http.client
17
18from PyQt5 import QtGui, QtWidgets, QtCore
19from ost.gui.scene.init_inspector import _InitInspector
20from ost.gui.init_menubar import _InitMenuBar
21from ost.gui.init_spacenav import _InitSpaceNav
22from ost.gui.init_context_menu import _InitContextMenu
23from ost.gui.init_splash import _InitSplash
24from ost.gui.dng import termuse
25from ost.gui.dng import superpositiondialog
26from ost.gui.scene.remote import RemoteLoader
27
29
30def _my_exit(code):
31 QtWidgets.QApplication.instance().quit()
32 gui.GostyApp.Instance().ProcessEvents()
33 sys._exit(code)
34
35sys._exit=sys.exit
36sys.exit=_my_exit
37
39 compound_lib_path=os.path.join(ost.GetSharedDataPath(), 'compounds.chemlib')
40 if os.path.exists(compound_lib_path):
41 conop_inst=conop.Conopology.Instance()
42 compound_lib=conop.CompoundLib.Load(compound_lib_path)
43 conop_inst.SetDefaultLib(compound_lib)
44 io.profiles['DEFAULT'].processor = conop.RuleBasedProcessor(compound_lib)
45
46# switch to rule-based processor for high fidelity if compounds.chemlib is
47# available
49
51 settings = QtCore.QSettings()
52 restore = settings.value("restore_settings",QtCore.QVariant(False))
53 if not restore:
54 settings.clear()
55 settings.setValue("restore_settings",QtCore.QVariant(True))
56
57def _InitPanels(app):
58 panels = app.perspective.panels
59 panels.AddWidgetToPool('ost.gui.FileBrowser', -1)
60 panels.AddWidgetToPool('ost.gui.PythonShell', 1)
61 panels.AddWidgetToPool('ost.gui.SceneWin', 1)
62 panels.AddWidgetToPool('ost.gui.SequenceViewer', 1)
63 panels.AddWidgetToPool('ost.gui.MessageWidget', 1)
64 if not panels.Restore("ui/perspective/panels"):
65 panels.AddWidget(gui.PanelPosition.LEFT_PANEL, app.scene_win)
66 panels.AddWidgetByName(gui.PanelPosition.LEFT_PANEL,
67 'ost.gui.FileBrowser', False)
68 panels.AddWidget(gui.PanelPosition.BOTTOM_PANEL, app.seq_viewer)
69 panels.AddWidget(gui.PanelPosition.BOTTOM_PANEL, app.py_shell)
70 panels.AddWidget(gui.PanelPosition.RIGHT_PANEL, app.message_widget)
71 return False
72 return True
73
74
75def _InitFrontEnd(try_stereo):
77 app=gui.GostyApp.Instance()
78 app.SetAppTitle("DNG")
79 app.TryStereo(try_stereo)
80 main_area=app.perspective.main_area
81 _InitSpaceNav(app)
82 _InitContextMenu(app)
83 main_area.AddPersistentWidget("3D Scene", "gl_win" , app.gl_win,
84 int(QtCore.Qt.WindowMaximized))
85 _InitInspector(app)
87 app.perspective.Restore()
88 additional_modules=getattr(__main__, 'ADDITIONAL_GUI_MODULES', [])
89 for module_name in additional_modules:
90 __import__(module_name)
91 app.ProcessEvents()
92
93
94
95 if not _InitPanels(app):
96 _InitSplash()
97
99 input_files=[_SplitIDSel(arg) for arg in loading_list]
100 for f in input_files:
101 selection=_get_selection_query(f[1])
102 gui.FileLoader.LoadObject(f[0],selection)
103
105 if len(options.query)>0:
106 if len(sel)>0:
107 return '(%s) and (%s)' % (options.query, sel)
108 else:
109 return options.query
110 elif len(sel)>0:
111 return sel
112 return ""
113
115 script=script_argv[0]
116 sys_argv_backup=sys.argv
117 sys.argv=script_argv
118 try:
119 exec(compile(open(script).read(), script, 'exec'), __main__.__dict__)
120 finally:
121 sys.argv=sys_argv_backup
122
123def show_help(option, opt, value, parser):
124 parser.print_help()
125 QtWidgets.QApplication.instance().exit()
126 sys.exit(-1)
127
128def parse_script_option(option, opt, value, parser):
129 script_argv.append(value)
130 for arg in parser.rargs:
131 script_argv.append(arg)
132 del parser.rargs[0:len(parser.rargs)]
133
134def _SplitIDSel(name):
135 pos=name.find('[')
136 if pos>-1:
137 return name[:pos], name[pos+1:-1]
138 return name, ''
139
140def stop():
141 gui.GostyApp.Instance().StopScript()
142
143
144loading_list=[]
145script_argv=[]
146#images=[]
147#viewers=[]
148usage = 'usage: dng [options] [files to load]'
149class OstOptionParser(optparse.OptionParser):
150 def __init__(self, **kwargs):
151 optparse.OptionParser.__init__(self, **kwargs)
152 def exit(self, status_code, error_message):
153 print(error_message, end=' ')
154 QtWidgets.QApplication.instance().exit()
155 sys.exit(-1)
156
157parser=OstOptionParser(usage=usage,conflict_handler="resolve")
158parser.add_option("-h", "--help", action="callback", callback=show_help, help="show this help message and exit")
159parser.add_option("-v", "--verbosity_level", action="store", type="int", dest="vlevel", default=2,
160 help="sets the verbosity level [default: %default]")
161parser.add_option("-s", "--script", action="callback", default=[], dest="script", type="string", callback=parse_script_option, help="executes a script (syntax: -s SCRIPT [options] [args]) Anything that follows this option is passed to the script")
162parser.add_option("-p", "--processor", dest="processor", default="HEURISTIC", help="Type of processor used by the progam (either RULE_BASED or HEURISTIC) [default: %default]")
163parser.add_option("-c", "--compound_library", dest="complib", default="compounds.chemlib", help="Compound library for the RULE_BASED processor (only used if --processor option is set to RULE_BASED, otherwise ignored [default: %default]")
164parser.add_option("-q", "--query", dest="query", default="", help="Selection query to be highlighted automatically upon loading (only used when a PDB file is loaded, otherwise ignored [default: None]")
165parser.add_option("-S","--stereo", dest="try_stereo", default=False, action="store_true",help="try to get a quad-buffer stereo visual")
166parser.disable_interspersed_args()
167(options, args) = parser.parse_args()
168
169if len(parser.rargs)!=0:
170 for rargs_string in parser.rargs:
171 if not rargs_string.endswith('.py'):
172 loading_list.append(rargs_string)
173 else:
174 print('Error: one of the files to load is a Python script, use -s flag to execute it\n')
175 QtWidgets.QApplication.instance().exit()
176 sys.exit(-1)
177
178if len(options.script)!=0:
179 script_argv=options.script
180
181if options.processor=="RULE_BASED":
182 from ost import conop
183 if os.path.exists(options.complib):
184 compound_lib=conop.CompoundLib.Load(options.complib)
185 conop.SetDefaultLib(compound_lib)
186 io.profiles['DEFAULT'].processor = conop.RuleBasedProcessor(compound_lib)
187
188home = os.getenv('HOME') or os.getenv('USERPROFILE')
189_ostrc=os.path.join(home, '.ostrc')
190if os.path.exists(_ostrc):
191 try:
192 exec(compile(open(_ostrc).read(), _ostrc, 'exec'))
193 except Exception as e:
194 print(e)
195else:
196 rcfile=open(_ostrc,"w")
197 print('# This python file is parsed by ost and dng at startup', file=rcfile)
198 print('# Its content is made available in the global namespace', file=rcfile)
199 print('# It can be used to define custom variables and functions', file=rcfile)
200 print('# For example:', file=rcfile)
201 print('# IMPORTANT_DIR="path/to/important/dir"', file=rcfile)
202 rcfile.close()
203
204ost.gui.PushVerbosityLevel(options.vlevel)
205working_dir=settings.GetValue("DNG_WORKING_DIR",None)
206
207if working_dir != None and os.path.isdir(working_dir):
208 os.chdir(working_dir)
209
210_InitFrontEnd(options.try_stereo)
211
212if len(loading_list)!=0:
214 gfx.Scene().Autoslab()
215if len(script_argv)!=0:
main class for organization and root for the graphical display
Definition scene.hh:80
_my_exit(code)
Definition init.py:30
_InitPanels(app)
Definition init.py:57
show_help(option, opt, value, parser)
Definition init.py:123
_SplitIDSel(name)
Definition init.py:134
_InitRuleBasedProcessor()
Definition init.py:38
parse_script_option(option, opt, value, parser)
Definition init.py:128
_get_selection_query(sel)
Definition init.py:104
_InitFrontEnd(try_stereo)
Definition init.py:75
PushVerbosityLevel(value)
Definition __init__.py:229
String DLLEXPORT_OST_BASE GetSharedDataPath()