
import os,sys
try:
  from CCP4ReportParser import *
except:
  execfile(os.path.join(os.environ['CCP4I2_TOP'],'bin/ccp4i2.pythonrc'))
  from CCP4ReportParser import *

# - - - - - - - - - - - - - - - - -
def displayFile(fileroot, parent, filenames, text):
  ''' display message with link to open file
  fileroot   root of file names, None if not set
  parent     where to put it
  filenames  list of possible names
  text       to display
  '''
  p = GenericElement('p')
  filefound = False
  fnames = filenames
  
  if fileroot != None:
    # prepend fileroot on list
    #print 'fileroot', fileroot
    fnames = []
    for filename in filenames:
      fnames.append(fileroot+filename)

  for filename in fnames:
    if os.path.isfile(filename):
      #print "Found"
      filefound = True
      break
    #      else:
    #        print "Not Found"

  #print filefound, filename
  if filefound:
    p.append(GenericElement('a',text,href=filename))
    parent.append(p)

# - - - - - - - - - - - - - - - - -
def colourText(text, colour, fontsize=None, fontstyle=None):
  fsz = ""
  if fontsize != None:
    fsz += '; font-size: '+fontsize
  if fontstyle != None:
    fsz += '; font-style: '+fontstyle
  return '<span style="color:'+colour+fsz+'">'+text+'</span>'

# - - - - - - - - - - - - - - - - -
def html_linebreak(line):
  # delineate lines by "<br/>"
  lines = line.splitlines()
  if len(lines) <= 1:
    return line
  linenew = ''
  for line in lines:
    linenew += line + '<br/>'
  return linenew
# - - - - - - - - - - - - - - - - -
def formatRange(range):
  # format two numbers as "n1 - n2"
  values = range.split()
  if len(values) == 2:
    return values[0] + ' - ' + values[1]
  return range

# - - - - - - - - - - - - - - - - -
