# Implementation of “gestalt” subcommand.

import inspect
import os
import os.path
import sys

import charliecloud as ch
import build_cache as bu
import cli
import image as im


### Classes ###

class Gestalt(cli.Root_CLI):
   name = "gestalt"
   doc = "Query configuration. Sub-sub-command required."
   flag_groups = {}  # no flags, just subcommands


class Gestalt_BU_Cache(cli.No_Storage, Gestalt):
   name = "bucache"
   doc = "Exit successfully if build cache available."
   flag_groups = {}  # no flags

   def main_real(self):
      bu.have_deps()


class Gestalt_BU_Cache_DOT(cli.No_Storage, Gestalt):
   name = "bucache-dot"
   doc = "Exit successfully if can produce DOT graphs of build cache."
   flag_groups = {}

   def main_real(self):
      # Note: For reasons I don’t understand, this have_deps() did not work if
      # we call via the superclass. I have not re-investigated.
      bu.have_deps()
      bu.have_dot()


class Gestalt_Dependencies(cli.No_Storage, Gestalt):
   name = "dependencies"
   doc = "Exit successfully if all dependencies present."
   flag_groups = {}

   def main_real(self):
      ch.VERBOSE("lark path: %s" % os.path.normpath(inspect.getfile(im.lark)))


class Gestalt_Logging(cli.No_Dependencies, Gestalt):
   name = "logging"
   doc = "Log a message at each log level."
   flag_groups = {
      None: {
         "fail":
            dict(action="store_true",
                 h="also generate a fatal error"),
      }
   }

   def main_real(self):
      ch.TRACE("trace")
      ch.DEBUG("debug")
      ch.VERBOSE("verbose")
      ch.INFO("info")
      ch.WARNING("warning")
      ch.ERROR("error")
      if (self.c.fail):
         ch.FATAL("the program failed explicably")


class Gestalt_Python_Path(cli.No_Dependencies, Gestalt):
   name = "python-path"
   doc = "Print path to Python interpreter being used."
   flag_groups = {}

   def main_real(self):
      print(sys.executable)


class Gestalt_Storage_Path(cli.No_Dependencies, Gestalt):
   name = "storage-path"
   doc = "Print path to the storage directory."
   flag_groups = {}

   def main_real(self):
      print(ch.storage.root)
