"""
File management for SNNet.

Robert A McDougal, 2011
"""

import os
import platform
import time

def delete_file(filename, num_attempts=10, delay=3):
    """Delete the file named filename.
    
    Tries up to num_attempts times, with delay between tries measured in sec.
    Returns True on success, else false.
    """
    # sometimes os.remove seems not to work, so try just using rm
    if platform.system() in ('Linux', 'Darwin'):
        rv = os.system('rm -f ' + filename)
        if rv == 0:
            return True
        
    # try repeatedly in case file in use (Windows issue)
    for i in range(num_attempts):
        if os.path.exists(filename):
            try:
                os.remove(filename)
                return True
            except:
                time.sleep(delay)
    return False


def process_temp_dir(temp_dir):
    if hasattr(temp_dir, '__call__'):
        return temp_dir()
    else:
        return temp_dir

