#-*- coding:utf8 -*- """ Various generic tools for creating and using HTML. """ import os import sys import olx import olex import olexex import time from datetime import date #import sys #sys.path.append(r".\src") from olexFunctions import OlexFunctions OV = OlexFunctions() last_mode = None last_mode_options = None current_tooltip_number = 0 HaveModeBox = False global formula global formula_string formula = "" formula_string = "" global tutorial_box_initialised tutorial_box_initialised = False def makeHtmlTable(list): """ Pass a list of dictionaries, with one dictionary for each table row. In each dictionary set at least the 'varName':(the name of the variable) and 'itemName':(the text to go in the first column). If you require a combo box set 'items':(a semi-colon separated list of items). If you want a multiline box set 'multiline':'multiline'. If you want more than one input box in a row, set 'varName' and 'itemName' plus anything else under a sub-dictionary called 'box1', 'box2','box3'. If you wish to change any of the defaults such as bgcolor, height, width, etc., these can be set in the dictionary to be passed. """ text = '' for input_d in list: row_d = {} row_d.setdefault('itemName',input_d['itemName']) row_d.setdefault('ctrl_name', "SET_%s" %str.upper(input_d['varName']).replace('.','_')) boxText = '' for box in ['box1','box2','box3']: if box in input_d.keys(): box_d = input_d[box] box_d.setdefault('ctrl_name', "SET_%s" %str.upper(box_d['varName']).replace('.','_')) box_d.setdefault('bgcolor','spy.bgcolor(~name~)') if box_d['varName'].startswith('_'): # treat cif items differently box_d.setdefault('value', '$spy.get_cif_item(%(varName)s,?)' %box_d) box_d.setdefault('onchange',"spy.set_cif_item(%(varName)s,GetValue(~name~))>>spy.changeBoxColour(%(ctrl_name)s,#FFDCDC)" %box_d) else: box_d.setdefault('value', '$spy.GetParam(%(varName)s)' %box_d) box_d.setdefault('onchange',"spy.SetParam(%(varName)s,GetValue(~name~))>>spy.AddVariableToUserInputList(%(varName)s)>>spy.changeBoxColour(~name~,#FFDCDC)" %box_d) boxText += makeHtmlInputBox(box_d) if boxText: row_d.setdefault('input',boxText) else: input_d.setdefault('ctrl_name', "SET_%s" %str.upper(input_d['varName']).replace('.','_')) if input_d['varName'].startswith('_'): # treat cif items differently input_d.setdefault('value', '$spy.get_cif_item(%(varName)s,?)' %input_d) input_d.setdefault('onchange',"spy.set_cif_item(%(varName)s,GetValue(~name~))>>spy.changeBoxColour(~name~,#FFDCDC)" %input_d) elif input_d['varName'] == 'snum.report.date_collected': # treat date fields differently try: cd = float(OV.GetParam('snum.report.date_collected')) cd = date.fromtimestamp(cd) time_str = cd.strftime("%d-%m-%Y") input_d.setdefault('value', time_str) except: input_d.setdefault('value', OV.GetParam('snum.report.date_collected')) input_d.setdefault('onchange',"spy.SetParam(%(varName)s,GetValue(~name~))>>spy.AddVariableToUserInputList(%(varName)s)>>spy.changeBoxColour(~name~,#FFDCDC)" %input_d) else: input_d.setdefault('value', '$spy.GetParam(%(varName)s)' %input_d) input_d.setdefault('onchange',"spy.SetParam(%(varName)s,GetValue(~name~))>>spy.AddVariableToUserInputList(%(varName)s)>>spy.changeBoxColour(~name~,#FFDCDC)" %input_d) input_d.setdefault('bgcolor','spy.bgcolor(~name~)') row_d.setdefault('input',makeHtmlInputBox(input_d)) row_d.update(input_d) text += makeHtmlTableRow(row_d) return OV.Translate(text) def makeHtmlInputBox(inputDictionary): if inputDictionary.has_key('items'): inputDictionary.setdefault('type','combo') inputDictionary.setdefault('readonly','readonly') if inputDictionary.has_key('multiline'): inputDictionary.setdefault('height','35') dictionary = { 'width':'95%%', 'height':'$spy.GetParam(gui.html.input_height)', 'onchange':'', 'items':'', 'multiline':'', 'type':'text', 'readonly':'', 'manage':'', 'data':'', 'label':'', 'valign':'center', 'bgcolor':'', } dictionary.update(inputDictionary) htmlInputBoxText = ''' '''%dictionary return htmlInputBoxText def makeHtmlTableRow(dictionary): dictionary.setdefault('font', 'size=%s' %OV.GetParam('gui.html.font_size')) dictionary.setdefault('trVALIGN','center') dictionary.setdefault('trALIGN','left') dictionary.setdefault('fieldWidth','30%%') dictionary.setdefault('fieldVALIGN','center') dictionary.setdefault('fieldALIGN','left') if 'chooseFile' in dictionary.keys(): chooseFile_dict = dictionary['chooseFile'] if 'file_type' in chooseFile_dict.keys(): href = "spy.set_source_file(%(file_type)s,FileOpen('%(caption)s','%(filter)s','%(folder)s'))>>updatehtml" %chooseFile_dict else: href = "%(function)sFileOpen('%(caption)s','%(filter)s','%(folder)s'))>>updatehtml" %chooseFile_dict pass chooseFileText = '''
''' return html def make_html_closing(): html = '''
''' return html def make_help_href(name, popout, image='normal'): help = ''' $spy.MakeHoverButton(btn-info@%s,spy.make_help_box -name='%s' -popout='%s') ''' %(name, name, popout) return help def make_input_text_box(d): name = d.get('ctrl_name') dic = {'height':'$spy.GetParam(gui.html.input_height)', 'bgcolor':'$spy.GetParam(gui.html.input_bg_colour)', 'value':'$spy.GetParam(%(varName)s)', 'width':'45', 'onchange':'', 'label':name, 'valign':'center', 'data':'', 'manage':'', } dic.update(d) html = ''' ''' %dic return html def make_combo_text_box(d): name = d.get('ctrl_name') dic = {'height':"$spy.GetParam(gui.html.combo_height)", 'bgcolor':'$spy.GetParam(gui.html.input_bg_colour)', 'value':'$spy.GetParam(%(varName)s)', 'label':'', 'valign':'center', 'halign':'left', 'width':'70', 'onchange':'', 'data':'', 'manage':'manage', 'readonly':'', } dic.update(d) html = ''' ''' %dic return html def make_tick_box_input(d): name = d.get('ctrl_name') dic = {'height':'$spy.GetParam(gui.html.checkbox_height)', 'bgcolor':'$spy.GetParam(gui.html.table_bg_colour)', 'fgcolor':'$spy.GetParam(gui.html.font_colour)', 'value':'$spy.GetParam(%(varName)s)', 'width':'$spy.GetParam(gui.html.checkbox_height)', 'onchange':'', 'value':'%s '%name, 'oncheck':'', 'onuncheck':'', 'data':'', 'manage':'', 'state':'', } dic.update(d) if dic.has_key('checked'): dic['checked'] = "checked='%s'" %dic.get('checked') else: dic.setdefault('checked','') html = """ """ %dic return html def make_spin_input(d): name = d.get('ctrl_name') dic = {'width':'12', 'height':'$spy.GetParam(gui.html.spin_height)', 'bgcolor':'$spy.GetParam(gui.html.input_bg_colour)', 'value':'$spy.GetParam(%(varName)s)', 'max':'99', 'min':'0', 'width':'45', 'onchange':'', 'label':"%s: " %name, 'valign':'center', 'halign':'left', 'manage':'manage', } dic.update(d) html = """ """ %dic return html def make_input_button(d): dic = {'ondown':'', 'onup':'', 'onclick':'', 'hint':'', 'height':"$spy.GetParam(gui.html.button_height)", 'bgcolor':"$spy.GetParam(gui.html.input_bg_colour)", 'valign':'center', 'halign':'left' } dic.update(d) html = ''' ''' %dic elif dic['ondown'] or dic['onup']: html += ''' ondown="%(ondown)s" onup="%(onup)s" > ''' %dic else: html += '\n>\n' return html def format_help(string): import re d = {} # initialise a dictionary, which will be used to store metadata. ## find all occurences of strings between **..**. These should be comma separated things to highlight. regex = re.compile(r"\*\* (.*?) \*\*", re.X) l = regex.findall(string) if l: l = l[0].split(",") string = regex.sub(r"", string) for item in l: regex = re.compile(r"((?P\1
", string)
## find all occurences of strings between n^..^n. These are the notes.
regex = re.compile(r"n \^ (.*?) \^ n", re.X)
string = regex.sub(r"Note: \1 |
<%s>>>\2%s> |