# 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__


import os, time
import util
from memoize import memoize

__all__ = ["clean_v_file", "clean_extra_coq_files", "write_bytes_to_file", "read_bytes_from_file", "write_to_file", "read_from_file", "restore_file"]

def clean_extra_coq_files(v_file_name, extra_exts=tuple()):
    for pre in ('', '.'):
        for ext in tuple(list(extra_exts) + ['.glob', '.vo', '.d', '.v.d', '.aux', '.vos', '.vok']):
            name = ''.join((os.path.dirname(v_file_name[:-2]), os.sep, pre, os.path.basename(v_file_name[:-2]), ext))
            if os.path.exists(name):
                os.remove(name)

def clean_v_file(file_name):
    clean_extra_coq_files(file_name, extra_exts=('.v',))

def backup(file_name, ext='.bak'):
    if not ext:
        raise ValueError
    if os.path.exists(file_name):
        backup(file_name + ext, ext=ext)
        os.rename(file_name, file_name + ext)


# 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
import os, time
import util
from memoize import memoize


__all__ = ["clean_v_file", "clean_extra_coq_files", "write_bytes_to_file", "read_bytes_from_file", "write_to_file", "read_from_file", "restore_file"]

def clean_extra_coq_files(v_file_name, extra_exts=tuple()):
    for pre in ('', '.'):
        for ext in tuple(list(extra_exts) + ['.glob', '.vo', '.d', '.v.d', '.aux', '.vos', '.vok']):
            name = ''.join((os.path.dirname(v_file_name[:-2]), os.sep, pre, os.path.basename(v_file_name[:-2]), ext))
            if os.path.exists(name):
                os.remove(name)

def clean_v_file(file_name):
    clean_extra_coq_files(file_name, extra_exts=('.v',))

def backup(file_name, ext='.bak'):
    if not ext:
        raise ValueError
    if os.path.exists(file_name):
        backup(file_name + ext, ext=ext)
        os.rename(file_name, file_name + ext)


@memoize