#!/usr/bin/env python3
# Daniel B. Stribling
# Renne Lab, University of Florida
# Hybkit Project : http://www.github.com/RenneLab/hybkit

"""
Read one or more '.hyb' format files and check for errors.
"""

import sys
import os 
import argparse
import textwrap
import hybkit

# Import module-level dunder-names:
from hybkit.__about__ import __author__, __contact__, __credits__, __date__, __deprecated__, \
                             __email__, __license__, __maintainer__, __status__, __version__

# Divide docstring into argparse and full portions.
argparse_doc = __doc__ + '\nFor full script description and usage, see the hybkit documentation.'
__doc__ += textwrap.dedent("""
    This utility reads in one or more files in hyb-format (see the :ref:`Hybkit Specification`)
    and uses hybkit's internal file error checking to check for errors. This can be useful
    as an intial preparation step for further analyses.

    Example system calls
        ::

            hyb_check -i my_file_1.hyb 
            hyb_check -i ../my_file_2.hyb /path/to/myfile_3.hyb -v --custom_flags myflag
    """)

# Create Command-line Argument Parser
def make_parser():
    parser_components = [
                         hybkit.util.in_hybs_parser,
                         hybkit.util.gen_opts_parser,
                         hybkit.util.hybrecord_parser,
                         hybkit.util.hybfile_parser,
                        ]
    script_parser = argparse.ArgumentParser(
         parents=parser_components,
         description=argparse_doc,
         formatter_class=argparse.ArgumentDefaultsHelpFormatter,
         allow_abbrev=False,
         )
    return script_parser

# Define main script function.
def hyb_check(in_hyb_files, verbose=False, silent=False):
    """Perform main script function."""

    if not silent:
        print('\nPerforming Error Checking of Hyb Files...')
   
    for in_hyb_file in in_hyb_files:
        if verbose:
            print('Checking File:\n    ' + in_hyb_file) 
    
        with hybkit.HybFile.open(in_hyb_file, 'r') as in_hyb:
            for record in in_hyb:
                pass

    if verbose:
        print('\nError checking complete. No errors found.\n')


# Execute the script function
if __name__ == '__main__':
    script_parser = make_parser()
    args = script_parser.parse_args() 
    hybkit.util.validate_args(args)
    hybkit.HybRecord.set_namespace_settings(args, verbose=args.verbose)
    hybkit.HybFile.set_namespace_settings(args, verbose=args.verbose)
    hyb_check(args.in_hyb, verbose=args.verbose, silent=args.silent)


