#!/usr/bin/python

#
# Copyright (c) 2007,2008,2009 MBARI
# MBARI Proprietary Information.  All Rights Reserved
#

import os
import commands
import ihooks, imp

# function definitions

# read a file and turns
# the entire file's contents
def readFile(filename):
  if not os.path.isfile(filename):
    return None
  f = open(filename, "r")
  data = f.read()
  f.close()
  return data

# reads a file and returns
# the file's lines
def readFileLines(filename):
  if not os.path.isfile(filename):
    return None
  f = open(filename, "r")
  lines = f.readlines()
  f.close()
  return lines

# writes lines to the file filename
def writeFileLines(filename, lines):
  f = open(filename, "w")
  f.writelines(lines)
  f.close()

# delete the file filename
def rmFile(filename):
  if os.path.isfile(filename):
    os.remove(filename)

# loads a module from file filename
def loadMod(filename):
  if not os.path.isfile(filename):
    return None
  loader = ihooks.BasicModuleLoader()
  name, ext = os.path.splitext(filename)
  m = loader.find_module(name)
  if not m:
    return None
  m = loader.load_module(name, m)
  return m

def getFilesInDir(path):
  if not os.path.isdir(path):
    return None
  list = []
  for f in os.listdir(path):
    if os.path.isfile(os.path.join(path, f)):
      list.append(f)
  return list

# is value with delta of targetvalue
def withinDelta(value, targetValue,  delta):
  if value >= targetValue - delta and \
     value <= targetValue + delta:
    return True
  return False

# Converts a python timedelta object to seconds
def timeDelta2Secs(tdelta):
  return (tdelta.microseconds / 1000000) \
          + tdelta.seconds + (tdelta.days * 3600 * 24)

