from __future__ import division import olex import olx import os import sys import OlexVFS from olexFunctions import OlexFunctions OV = OlexFunctions() debug = bool(OV.GetParam('olex2.debug',False)) timer = debug import glob global have_found_python_error have_found_python_error= False global last_formula last_formula = "" global last_element_html last_element_html = "" global current_sNum current_sNum = "" haveGUI = OV.HasGUI() import olexex import olex_gui import re import time global regex_l regex_l = {} class FolderView: root = None class node: name = None full_name = None parent = None content = None def __init__(self, name, full_name=None): self.name = name if full_name: self.full_name = full_name else: self.full_name = name def toStr(self, prefix=""): s = prefix+self.name s += "\n%s" %(self.full_name) if self.content: prefix += '\t' for c in self.content: s += '\n%s%s' %(prefix,c.toStr(prefix)) return s def expand(self, mask, fname=""): self.content = [] if not fname: fname = self.name for i in os.listdir(fname): dn = os.path.normpath(fname + '/' + i) if os.path.isdir(dn) and i != '.olex': dr = FolderView.node(i, dn) dr.expand(mask, dn) if len(dr.content): self.content.append(dr) else: if( os.path.splitext(i)[1] in mask): self.content.append(FolderView.node(i, dn)) def list(self, mask=".ins;.res;.cif;.oxm"): r = OV.GetParam('user.folder_view_root') if not r: r = "." try: f = olx.ChooseDir('Select folder', '%s' %r) except RuntimeError: f = None if f: self.root = FolderView.node(f) self.root.expand(mask=set(mask.split(';'))) olx.html.Update() def generateHtml(self): import OlexVFS if not self.root: return " " OV.SetParam('user.folder_view_root', self.root.name) data = self.root.toStr() OlexVFS.write_to_olex('folder_view.data', data.encode('utf-8')) return "" def loadStructure(self, v): if os.path.isfile(v): olex.m("reap '%s'" %v) fv = FolderView() olex.registerFunction(fv.list, False, "gui.tools.folder_view") olex.registerFunction(fv.generateHtml, False, "gui.tools.folder_view") olex.registerFunction(fv.loadStructure, False, "gui.tools.folder_view") def start_where(): if olx.IsFileLoaded() == "false": return from gui import SwitchPanel # if olx.xf.au.GetAtomCount() == "0" and olx.IsFileType('ires') == "true": # SwitchPanel('work') # flash_gui_control('btn-solve') # print "Use 'Solve' button in tab 'Work' to solve the structure." # return if olx.IsVar('start_where') == 'false': where = OV.GetParam('user.start_where').lower() SwitchPanel(where) olx.SetVar('start_where',False) olex.registerFunction(start_where, False, "gui.tools") def flash_gui_control(control, wait=300): ''' Flashes a control on the GUI in order to highlight it's position ''' if ';' in control: n = int(control.split(';')[1]) control = control.split(';')[0] else: n = 2 control_name = "IMG_%s" %control.upper() if '@' in control: print "@ in control" control_image = control.lower().split('@')[0] else: control_image = control if not olx.fs.Exists("%son.png" %control_image): print "This image %s does not exist. So I can't make it blink" %control_image return for i in xrange(n): if "element" in control: new_image = "up=%son.png" %control_image olx.html.SetImage(control_name,new_image) elif control.endswith('_bg'): cmd = 'html.setBG(%s,%s)' %(control_image.rstrip('_bg'), OV.GetParam('gui.html.highlight_colour','##ff0000')) olex.m(cmd) else: new_image = "up=%soff.png" %control_image olx.html.SetImage(control_name,new_image) OV.Refresh() olx.Wait(wait) if "element" in control: new_image = "up=%soff.png" %control olx.html.SetImage(control_name,new_image) elif control.endswith('_bg'): cmd = 'html.setBG(%s,%s)' %(control.rstrip('_bg'), '#fffff') olex.m(cmd) else: new_image = "up=%shighlight.png" %control_image olx.html.SetImage(control_name,new_image) OV.Refresh() olx.Wait(wait) if not control.endswith('_bg'): new_image = "up=%soff.png" %control_image olx.html.SetImage(control_name,new_image) olex.registerFunction(flash_gui_control, False, "gui.tools") def make_single_gui_image(img_txt="", img_type='h2'): import PilTools import OlexVFS TI = PilTools.TI states = ["on", "off", "highlight", "", "hover", "hoveron"] alias = img_type for state in states: if img_type == "h2": alias = "h1" elif img_type == "h1": alias = img_type img_type = "h2" image = TI.make_timage(item_type=alias, item=img_txt, state=state, titleCase=False) name = "%s-%s%s.png" %(img_type, img_txt.lower(), state) OlexVFS.save_image_to_olex(image, name, 0) def inject_into_tool(tool, t, where,befaf='before'): import OlexVFS txt = OlexVFS.read_from_olex('%s/%s' %(OV.BaseDir(),tool)) if befaf == 'before': txt = txt.replace(where, "%s%s" %(t, where)) else: txt = txt.replace(where, "%s%s" %(where, t)) OlexVFS.write_to_olex('%s%s' %(OV.BaseDir(), tool), u, txt) def add_tool_to_index(scope="", link="", path="", location="", before="", filetype="", level="h2", state="2"): import OlexVFS if not OV.HasGUI: return if not scope: return if not link: return if not path: return ''' Automatically add a link to GUI to an Olex2 index file. ''' if not location: location = OV.GetParam('%s.gui.location' %scope) if not before: before = OV.GetParam('%s.gui.before' %scope) if not location: return _ = r"%s/%s" %(OV.BaseDir(), location) if os.path.exists(_): file_to_write_to = _ else: file_to_write_to = r'%s/etc/gui/blocks/index-%s.htm' %(OV.BaseDir().replace(r"//","/"), location) if not os.path.exists(file_to_write_to): print "This location does not exist: %s" %file_to_write_to file_to_write_to = '%s/etc/gui/blocks/index-%s.htm' %(OV.BaseDir().replace(r"//","/"), "tools") before = "top" file_to_write_to = file_to_write_to.replace(r"//","/") txt = OlexVFS.read_from_olex(file_to_write_to) if not filetype: t = r''' ''' %(level, location, scope, link, path, link, link) else: t = r''' ''' %(filetype, level, location, scope, link, path, link, link) index_text = "" if t not in txt: if before.lower() == "top": u = "%s\n%s" %(t, txt) elif before not in txt or before.lower() == "end": u = "%s\n%s" %(txt, t) else: u = "" for line in txt.strip().split("\r\n"): if not line: continue li = line if r"" OV.registerFunction(makeFormulaForsNumInfo) def make_cell_dimensions_display(): t1 = time.time() global current_sNum #if OV.FileName() == current_sNum: #return "" l = ['a', 'b', 'c', 'alpha', 'beta', 'gamma'] d = {} for x in l: val = olx.xf.uc.CellEx(x) if "90.0" in val and "(" in val or '90(' in val and not "." in val: help_txt = "Help from File does not exist. Apologies." help = ''' $spy.MakeHoverButton('btn-info@cell@%s',"spy.make_help_box -name=cell-not-quite-90 -popout='False' -helpTxt='%s'")''' %(x, help_txt) _ = os.sep.join([OV.BaseDir(), "etc", "gui", "help", "cell_angle_not_quite_90.htm"]) if os.path.exists(_): help_txt = open(_,'r').read() href = "spy.make_help_box -name=cell-angle-not-quite-90 -popout=False -helpTxt='%s'" %help_txt val = '%s' %(href, OV.GetParam('gui.red').hexadecimal, val) d[x] = val d['volume'] = olx.xf.uc.VolumeEx() d['Z'] = olx.xf.au.GetZ() d['Zprime'] = olx.xf.au.GetZprime() t = '''