"""
Module to associate default readers to file extensions.
The module reads an xml file to get the readers for each file extension.
The readers are tried in order they appear when reading a file.
"""
############################################################################
#This software was developed by the University of Tennessee as part of the
#Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
#project funded by the US National Science Foundation.
#If you use DANSE applications to do scientific research that leads to
#publication, we ask that you acknowledge the use of the software with the
#following sentence:
#This work benefited from DANSE software developed under NSF award DMR-0520547.
#############################################################################
import os
import sys
import logging
import json

FILE_NAME = 'defaults.json'

"""
Read the specified settings file to associate

:param settings: path to the json settings file [string]
"""

# If we can't find the file in the installation
# directory, look into the execution directory.
if not os.path.isfile(path):
path = os.path.join(os.getcwd(), settings)
if not os.path.isfile(path):
path = os.path.join(sys.path[0], settings)
if not os.path.isfile(path):
path = settings
if not os.path.isfile(path):
path = "./%s" % settings
if os.path.isfile(path):
with open(path) as fh:

# Read in the file extension associations

# For each FileType entry, get the associated reader and extension
for entry in entry_list:
ext = entry['-extension']

if reader is not None and ext is not None:
# Associate the extension with a particular reader
# TODO: Modify the Register code to be case-insensitive
# and remove the extra line below.
try:
except:
msg += " for %s\n  %s" % (ext.lower(), sys.exc_value)
logging.error(msg)
else:
print "Could not find reader association settings\n  %s [%s]" % (__file__, os.getcwd())

"""
Function called by the registry/loader object to register
all default readers using a call back function.

:WARNING: this method is now obsolete

:param registry_function: function to be called to register each reader
"""