# Path: argparse_compat.py

# 
# Path: custom_arguments.py
# def add_libname_arguments(parser): add_libname_arguments_gen(parser, passing='')
# 
# def update_env_with_libnames(env, args, default=(('.', 'Top'), ), include_passing=False, use_default=True, use_passing_default=True):
#     all_keys = ('libnames', 'non_recursive_libnames', 'ocaml_dirnames', '_CoqProject', '_CoqProject_v_files', '_CoqProject_unknown')
#     passing_opts = ('', ) if not include_passing else ('', 'passing_', 'nonpassing_')
#     for passing in passing_opts:
#         for key in all_keys:
#             env[passing + key] = []
#         if getattr(args, passing + 'CoqProjectFile'):
#             for f in getattr(args, passing + 'CoqProjectFile'):
#                 env[passing + '_CoqProject'] = f.read()
#                 f.close()
#         process_CoqProject(env, env[passing + '_CoqProject'], passing=passing)
#         env[passing + 'libnames'].extend(getattr(args, passing + 'libnames'))
#         env[passing + 'non_recursive_libnames'].extend(getattr(args, passing + 'non_recursive_libnames'))
#         env[passing + 'ocaml_dirnames'].extend(getattr(args, passing + 'ocaml_dirnames'))
#     if include_passing:
#         # The nonpassing_ prefix is actually temporary; the semantics
#         # are that, e.g., libnames = libnames + nonpassing_libnames,
#         # while passing_libnames = libnames + passing_libnames
#         for key in all_keys:
#             env['passing_' + key] = env[key] + env['passing_' + key]
#             env[key] = env[key] + env['nonpassing_' + key]
#             del env['nonpassing_' + key]
#     if use_default:
#         update_env_with_libname_default(env, args, default=default)
#     if use_passing_default and include_passing:
#         update_env_with_libname_default(env, args, default=default, passing='passing_')
# 
# def add_logging_arguments(parser):
#     parser.add_argument('--verbose', '-v', dest='verbose',
#                         action='count',
#                         help='display some extra information by default (does not impact --verbose-log-file)')
#     parser.add_argument('--quiet', '-q', dest='quiet',
#                         action='count',
#                         help='the inverse of --verbose')
#     parser.add_argument('--log-file', '-l', dest='log_files', nargs='+', type=argparse.FileType('w'),
#                         default=[sys.stderr],
#                         help='The files to log output to.  Use - for stdout.')
#     parser.add_argument('--verbose-log-file', dest='verbose_log_files', nargs='+', type=TupleType(int, argparse.FileType('w')),
#                         default=[],
#                         help=(('The files to log output to at verbosity other than the default verbosity (%d if no -v/-q arguments are passed); ' +
#                                'each argument should be a pair of a verbosity level and a file name. '
#                                'Use - for stdout.') % DEFAULT_VERBOSITY))
# 
# def process_logging_arguments(args):
#     if args.verbose is None: args.verbose = DEFAULT_VERBOSITY
#     if args.quiet is None: args.quiet = 0
#     args.verbose -= args.quiet
#     args.log = make_logger([(args.verbose, f) for f in args.log_files]
#                            + args.verbose_log_files)
#     del args.quiet
#     del args.verbose
#     return args
# 
# LOG_ALWAYS=None
# 
# Path: memoize.py
# def memoize(f):
#     """ Memoization decorator for a function taking one or more arguments. """
#     class memodict(dict):
#         def __getitem__(self, *key, **kwkey):
#             return dict.__getitem__(self, (tuple(key), tuple((k, kwkey[k]) for k in sorted(kwkey.keys()))))
# 
#         def __missing__(self, key):
#             args, kwargs = key
#             self[key] = ret = f(*args, **dict(kwargs))
#             return ret
# 
#     return memodict().__getitem__
# 
# Path: file_util.py
# def read_from_file(file_name):
#     return util.normalize_newlines(read_bytes_from_file(file_name).decode('utf-8'))
# 
# def write_to_file(file_name, contents, *args, **kwargs):
#     return write_bytes_to_file(file_name, contents.replace('\n', os.linesep).encode('utf-8'), *args, **kwargs)


import os, sys, re
from argparse_compat import argparse
from custom_arguments import add_libname_arguments, update_env_with_libnames, add_logging_arguments, process_logging_arguments, LOG_ALWAYS
from memoize import memoize
from file_util import read_from_file, write_to_file
#!/usr/bin/env python
from __future__ import with_statement

# TODO:
# - handle fake ambiguities from [Definition foo] in a comment
# - handle theorems inside proof blocks
# - do the right thing for [Theorem foo. Theorem bar. Proof. Qed. Qed.] (we do the wrong thing right now)
# - make use of glob file?

parser = argparse.ArgumentParser(description='Implement the suggestions of [Set Suggest Proof Using.]')
parser.add_argument('source', metavar='SOURCE_FILE', type=argparse.FileType('r'), nargs='?', default=sys.stdin,
                    help='the source of set suggest proof using messages; use - for stdin.')
parser.add_argument('--hide', dest='hide_reg', nargs='*', type=str,
                    default=['.*_subproof[0-9]*$'], #, '.*_Proper$'],
                    help=('Regular expressions to not display warnings about on low verbosity.  ' +
                          '[Set Suggest Proof Using] can give suggestions about hard-to-find ' +
                          'identifiers, and we might want to surpress them.'))
parser.add_argument('--no-hide', dest='hide_reg', action='store_const', const=[])
add_libname_arguments(parser)
add_logging_arguments(parser)

def mwrite_to_file(file_name, contents, do_backup=False):

# Path: argparse_compat.py

# 
# Path: custom_arguments.py
# def add_libname_arguments(parser): add_libname_arguments_gen(parser, passing='')
# 
# def update_env_with_libnames(env, args, default=(('.', 'Top'), ), include_passing=False, use_default=True, use_passing_default=True):
#     all_keys = ('libnames', 'non_recursive_libnames', 'ocaml_dirnames', '_CoqProject', '_CoqProject_v_files', '_CoqProject_unknown')
#     passing_opts = ('', ) if not include_passing else ('', 'passing_', 'nonpassing_')
#     for passing in passing_opts:
#         for key in all_keys:
#             env[passing + key] = []
#         if getattr(args, passing + 'CoqProjectFile'):
#             for f in getattr(args, passing + 'CoqProjectFile'):
#                 env[passing + '_CoqProject'] = f.read()
#                 f.close()
#         process_CoqProject(env, env[passing + '_CoqProject'], passing=passing)
#         env[passing + 'libnames'].extend(getattr(args, passing + 'libnames'))
#         env[passing + 'non_recursive_libnames'].extend(getattr(args, passing + 'non_recursive_libnames'))
#         env[passing + 'ocaml_dirnames'].extend(getattr(args, passing + 'ocaml_dirnames'))
#     if include_passing:
#         # The nonpassing_ prefix is actually temporary; the semantics
#         # are that, e.g., libnames = libnames + nonpassing_libnames,
#         # while passing_libnames = libnames + passing_libnames
#         for key in all_keys:
#             env['passing_' + key] = env[key] + env['passing_' + key]
#             env[key] = env[key] + env['nonpassing_' + key]
#             del env['nonpassing_' + key]
#     if use_default:
#         update_env_with_libname_default(env, args, default=default)
#     if use_passing_default and include_passing:
#         update_env_with_libname_default(env, args, default=default, passing='passing_')
# 
# def add_logging_arguments(parser):
#     parser.add_argument('--verbose', '-v', dest='verbose',
#                         action='count',
#                         help='display some extra information by default (does not impact --verbose-log-file)')
#     parser.add_argument('--quiet', '-q', dest='quiet',
#                         action='count',
#                         help='the inverse of --verbose')
#     parser.add_argument('--log-file', '-l', dest='log_files', nargs='+', type=argparse.FileType('w'),
#                         default=[sys.stderr],
#                         help='The files to log output to.  Use - for stdout.')
#     parser.add_argument('--verbose-log-file', dest='verbose_log_files', nargs='+', type=TupleType(int, argparse.FileType('w')),
#                         default=[],
#                         help=(('The files to log output to at verbosity other than the default verbosity (%d if no -v/-q arguments are passed); ' +
#                                'each argument should be a pair of a verbosity level and a file name. '
#                                'Use - for stdout.') % DEFAULT_VERBOSITY))
# 
# def process_logging_arguments(args):
#     if args.verbose is None: args.verbose = DEFAULT_VERBOSITY
#     if args.quiet is None: args.quiet = 0
#     args.verbose -= args.quiet
#     args.log = make_logger([(args.verbose, f) for f in args.log_files]
#                            + args.verbose_log_files)
#     del args.quiet
#     del args.verbose
#     return args
# 
# LOG_ALWAYS=None
# 
# Path: memoize.py
# def memoize(f):
#     """ Memoization decorator for a function taking one or more arguments. """
#     class memodict(dict):
#         def __getitem__(self, *key, **kwkey):
#             return dict.__getitem__(self, (tuple(key), tuple((k, kwkey[k]) for k in sorted(kwkey.keys()))))
# 
#         def __missing__(self, key):
#             args, kwargs = key
#             self[key] = ret = f(*args, **dict(kwargs))
#             return ret
# 
#     return memodict().__getitem__
# 
# Path: file_util.py
# def read_from_file(file_name):
#     return util.normalize_newlines(read_bytes_from_file(file_name).decode('utf-8'))
# 
# def write_to_file(file_name, contents, *args, **kwargs):
#     return write_bytes_to_file(file_name, contents.replace('\n', os.linesep).encode('utf-8'), *args, **kwargs)


# Path: proof-using-helper.py
import os, sys, re
from argparse_compat import argparse
from custom_arguments import add_libname_arguments, update_env_with_libnames, add_logging_arguments, process_logging_arguments, LOG_ALWAYS
from memoize import memoize
from file_util import read_from_file, write_to_file

#!/usr/bin/env python
from __future__ import with_statement

# TODO:
# - handle fake ambiguities from [Definition foo] in a comment
# - handle theorems inside proof blocks
# - do the right thing for [Theorem foo. Theorem bar. Proof. Qed. Qed.] (we do the wrong thing right now)
# - make use of glob file?

parser = argparse.ArgumentParser(description='Implement the suggestions of [Set Suggest Proof Using.]')
parser.add_argument('source', metavar='SOURCE_FILE', type=argparse.FileType('r'), nargs='?', default=sys.stdin,
                    help='the source of set suggest proof using messages; use - for stdin.')
parser.add_argument('--hide', dest='hide_reg', nargs='*', type=str,
                    default=['.*_subproof[0-9]*$'], #, '.*_Proper$'],
                    help=('Regular expressions to not display warnings about on low verbosity.  ' +
                          '[Set Suggest Proof Using] can give suggestions about hard-to-find ' +
                          'identifiers, and we might want to surpress them.'))
parser.add_argument('--no-hide', dest='hide_reg', action='store_const', const=[])
add_libname_arguments(parser)
add_logging_arguments(parser)

def mwrite_to_file(file_name, contents, do_backup=False):

    return write_to_file(file_name, contents, do_backup=do_backup, memoize=True)