from __future__ import absolute_import, division, print_function
import sys
import warnings
from libtbx.file_clutter import gather

# These flags are retained for backward compatibility and accept but ignore
# their arguments. Scheduled for removal in January 2027.
_DEPRECATED_FLAGS = ("--only_future", "--absolute_import", "--print_function")
_DEPRECATION_REMOVAL = "January 2027"

def run(args):
  flag_x = False
  flag_ni = False
  flag_dos_format = True
  flag_indentation = False
  verbose = False
  #
  only_whitespace = False
  only_dos = False
  #
  paths = []
  for arg in args:
    if (arg == "-x"):
      flag_x = True
    elif (arg == "-ni"):
      flag_ni = True
    elif (arg == "-ndos"):
      flag_dos_format = False
    elif (arg == "--verbose") or (arg == '-v'):
      verbose = True
    elif (arg == "--indentation"):
      flag_indentation = True
    elif (arg == "--only_whitespace"):
      only_whitespace = True
    elif (arg == "--only_dos"):
      only_dos = True
    elif arg in _DEPRECATED_FLAGS:
      # Scheduled for removal in January 2027.
      warnings.warn(
        "%s is deprecated, has no effect, and will be removed in %s"
          % (arg, _DEPRECATION_REMOVAL),
        DeprecationWarning, stacklevel=2)
    else:
      paths.append(arg)
  if (len(paths) == 0): paths = ["."]
  n_is_cluttered = 0
  n_bare_excepts = 0
  n_has_unused_imports = 0
  message_lines = []
  n_bad_indentation = 0
  for info in gather(paths=paths, find_unused_imports=not flag_ni,
      find_bad_indentation=flag_indentation):
    if (info.is_cluttered(flag_x=flag_x)):
      n_is_cluttered += 1
    if (info.n_bare_excepts > 0):
      n_bare_excepts += info.n_bare_excepts
    if (info.has_unused_imports()):
      n_has_unused_imports += 1
    if (info.bad_indentation is not None) and (flag_indentation):
      n_bad_indentation += 1
    info.show(
      flag_x=flag_x,
      flag_dos_format=flag_dos_format,
      append=message_lines.append,
      flag_indentation=flag_indentation,
      verbose=verbose)
  please_use = []
  if (n_is_cluttered != 0):
    please_use.append("libtbx.clean_clutter")
  if only_whitespace:
    def _is_whitespace(s):
      if s.find("tabs or trailing")>-1: return True
      return False
    message_lines = list(filter(_is_whitespace, message_lines))
  elif only_dos:
    def _is_dos(s):
      if s.find("dos format")>-1: return True
      return False
    message_lines = list(filter(_is_dos, message_lines))
  else:
    if (n_has_unused_imports != 0):
      please_use.append("libtbx.find_unused_imports_crude")
    if (len(please_use) != 0):
      message_lines.append("")
      message_lines.append(
        "*** To clean up please use: %s ***" % ", ".join(please_use))
    if (n_bare_excepts > 0):
      message_lines.append("")
      message_lines.extend("""\
  *** Please change bare excepts: ***
        Usually best:
          except Exception:
        Rarely necessary:
          except: # intentional
  """.splitlines())
    if (n_bad_indentation != 0):
      message_lines.append("")
      message_lines.append("*** Please fix indentation in a text editor ***")
  if (len(message_lines) != 0):
    print()
    print("\n".join(message_lines))
    print()
    return (1)
  return (0)

if (__name__ == "__main__"):
  sys.exit(run(sys.argv[1:]))
