# Path: orbkit/read/tools.py
# def find_itype(fname, extension=None):
#   '''
#   This function is used by the high-level read
#   to determine what reader to use.
#   Filetypes are determined either by extension or
#   by looking for magic strings within the files.
#   
#   **Parameters:**
#   
#   fname: str, file descriptor
#     Specifies the filename for the input file.
#     fname can also be used with a file descriptor instad of a filename.
#   extension: str, optional
#     If extension is not None it will be used to attempt determining
#     filetypes by extension in place of fname.
#     
#   **Returns:**
#   
#   filetype, str
#     Filetype for the file specified by fname
#   
#   filetypes determied from file extension:
#     - .fchk
#     - .wfx
#     - .wfn
#     - .npz
#     - .hdf5 / .h5
#     
#   filetypes determined from magic strings:
#     - Molden
#     - Gamess US
#     - Gaussian log
#     - AOMix
#   '''
# 
#   #Don't like this was_str stuff... but I don't know what to do
#   if isinstance(fname, str):
#     filename = fname
#     fname = descriptor_from_file(filename, index=0)
#     was_str = True
#   else:
#     filename = fname.name
#     was_str = False
# 
#   if not extension:
#     extension = filename.split('.')[-1]
#   if extension.lower() in ['fchk', 'wfx', 'wfn']:
#     return extension
#   elif extension.lower() in ['numpy', 'npz', 'hdf5', 'h5']:
#     return 'native'
#   
#   molden_regex = re.compile(r"\[[ ]{,}[Mm]olden[ ]+[Ff]ormat[ ]{,}\]")
#   gamess_regex = re.compile(r"[Gg][Aa][Mm][Ee][Ss][Ss]") #This might be too weak - Can someone who knows Gamess please check?
#   gaussian_regex = re.compile(r"[Cc]opyright[,\s\(\)c0-9]+[Gg]aussian\s{,},\s+Inc.")
#   aomix_regex = re.compile(r"\[[ ]{,}[Aa][Oo][Mm]ix[ ]+[Ff]ormat[ ]{,}\]")
#   
#   regexes = {'molden': molden_regex, 'gamess': gamess_regex, 'gaussian_log': gaussian_regex, 'aomix': aomix_regex}
#   
#   itypes = ['molden', 'gamess', 'gaussian_log', 'aomix']  
#   
#   from io import TextIOWrapper
#   if isinstance(fname, TextIOWrapper):
#     text = fname.read()
#   else:
#     text = fname.read().decode("iso-8859-1")
#   fname.seek(0)
# 
#   for regname in itypes:
#     if regexes[regname].search(text):
#       return regname
# 
#   if was_str:
#     fname.close()
#   
#   raise NotImplementedError('File format not reccognized or reader not implemented!')


from orbkit.read.tools import find_itype
import os, inspect

tests_home = os.path.dirname(inspect.getfile(inspect.currentframe()))
folder = os.path.join(tests_home, '../outputs_for_testing')

files = {'fchk': 'gaussian/h2o_rhf_cart.fchk',
         'gaussian_log': 'gaussian/h2o_rhf_cart.inp.log',
         'gamess': 'gamess/formaldehyde.log',
         'molden': 'molpro/h2o_rhf_sph.molden',
         'aomix': 'turbomole/h2o_rhf_sph/aomix.in',
         'wfx': 'orca/1.wfx',
         'wfn': 'gamess/water_gamess-us.wfn'}

for fname in files:

# Path: orbkit/read/tools.py
# def find_itype(fname, extension=None):
#   '''
#   This function is used by the high-level read
#   to determine what reader to use.
#   Filetypes are determined either by extension or
#   by looking for magic strings within the files.
#   
#   **Parameters:**
#   
#   fname: str, file descriptor
#     Specifies the filename for the input file.
#     fname can also be used with a file descriptor instad of a filename.
#   extension: str, optional
#     If extension is not None it will be used to attempt determining
#     filetypes by extension in place of fname.
#     
#   **Returns:**
#   
#   filetype, str
#     Filetype for the file specified by fname
#   
#   filetypes determied from file extension:
#     - .fchk
#     - .wfx
#     - .wfn
#     - .npz
#     - .hdf5 / .h5
#     
#   filetypes determined from magic strings:
#     - Molden
#     - Gamess US
#     - Gaussian log
#     - AOMix
#   '''
# 
#   #Don't like this was_str stuff... but I don't know what to do
#   if isinstance(fname, str):
#     filename = fname
#     fname = descriptor_from_file(filename, index=0)
#     was_str = True
#   else:
#     filename = fname.name
#     was_str = False
# 
#   if not extension:
#     extension = filename.split('.')[-1]
#   if extension.lower() in ['fchk', 'wfx', 'wfn']:
#     return extension
#   elif extension.lower() in ['numpy', 'npz', 'hdf5', 'h5']:
#     return 'native'
#   
#   molden_regex = re.compile(r"\[[ ]{,}[Mm]olden[ ]+[Ff]ormat[ ]{,}\]")
#   gamess_regex = re.compile(r"[Gg][Aa][Mm][Ee][Ss][Ss]") #This might be too weak - Can someone who knows Gamess please check?
#   gaussian_regex = re.compile(r"[Cc]opyright[,\s\(\)c0-9]+[Gg]aussian\s{,},\s+Inc.")
#   aomix_regex = re.compile(r"\[[ ]{,}[Aa][Oo][Mm]ix[ ]+[Ff]ormat[ ]{,}\]")
#   
#   regexes = {'molden': molden_regex, 'gamess': gamess_regex, 'gaussian_log': gaussian_regex, 'aomix': aomix_regex}
#   
#   itypes = ['molden', 'gamess', 'gaussian_log', 'aomix']  
#   
#   from io import TextIOWrapper
#   if isinstance(fname, TextIOWrapper):
#     text = fname.read()
#   else:
#     text = fname.read().decode("iso-8859-1")
#   fname.seek(0)
# 
#   for regname in itypes:
#     if regexes[regname].search(text):
#       return regname
# 
#   if was_str:
#     fname.close()
#   
#   raise NotImplementedError('File format not reccognized or reader not implemented!')


# Path: orbkit/test/read/find_filetype.py
from orbkit.read.tools import find_itype
import os, inspect


tests_home = os.path.dirname(inspect.getfile(inspect.currentframe()))
folder = os.path.join(tests_home, '../outputs_for_testing')

files = {'fchk': 'gaussian/h2o_rhf_cart.fchk',
         'gaussian_log': 'gaussian/h2o_rhf_cart.inp.log',
         'gamess': 'gamess/formaldehyde.log',
         'molden': 'molpro/h2o_rhf_sph.molden',
         'aomix': 'turbomole/h2o_rhf_sph/aomix.in',
         'wfx': 'orca/1.wfx',
         'wfn': 'gamess/water_gamess-us.wfn'}

for fname in files:

  assert fname == find_itype(os.path.join(folder, files[fname]))