from cmd3.shell import command
from cloudmesh_common.logger import LOGGER
from pprint import pprint
from cloudmesh.cm_mongo import cm_mongo_status
from cloudmesh_common.tables import two_column_table
import json
from subprocess import call

log = LOGGER(__file__)

class cm_shell_status:

    def activate_cm_shell_status(self):
        self.register_command_topic('cloud', 'status')

    @command
    def do_status(self, args, arguments):
        """
        Usage:
            status mongo 
            status celery 
            status rabbitmq

            Shows system status
        """
        # TODO: temp design
        
        if arguments['mongo']:
            stat = cm_mongo_status()
            pprint(stat.serverStatus())
            #func = getattr(self, "_print_" + str(arguments['--format']))
            #func(stat.serverStatus())
        elif arguments['celery']:
            call(['celery','inspect','ping'])
            '''
            import celery
            if arguments['--simple'] or arguments['-s']:
                res = celery.current_app.control.inspect().ping()
            else: 
                res = celery.current_app.control.inspect().stats()
            func = getattr(self, "_print_" + str(arguments['--format']))
            func(res)
            '''
        elif arguments['rabbitmq']:
            call(['sudo','rabbitmqctl','status'])
        
    '''
    def _print_None(self, data):
        self._print_table(data)

    def _print_json(self, data):
        pprint (data)
      
    def _print_table(self, data):
        print two_column_table(data)
    '''
@@ -140,16 +140,18 @@
 _status%0A
+# 
 from clo
@@ -198,48 +198,8 @@
 able
-%0Aimport json%0Afrom subprocess import call
 %0A%0Alo
@@ -220,16 +220,17 @@
 ile__)%0A%0A
+%0A
 class cm
@@ -447,17 +447,42 @@
 us mongo
- 
+%0A            status celery
 %0A       
@@ -500,16 +500,52 @@
  celery 
+ping%0A            status celery stats
 %0A       
@@ -638,24 +638,16 @@
  design%0A
-        
 %0A       
@@ -756,24 +756,25 @@
            #
+ 
 func = getat
@@ -835,16 +835,17 @@
        #
+ 
 func(sta
@@ -854,32 +854,33 @@
 serverStatus())%0A
+%0A
         elif arg
@@ -899,109 +899,569 @@
 ry'%5D
-:%0A            call(%5B'celery','inspect','ping'%5D)%0A            '''%0A            import celery%0A           
+ and arguments%5B'ping'%5D:%0A%0A            import celery%0A            res = celery.current_app.control.inspect().ping()%0A            print res%0A%0A        elif arguments%5B'celery'%5D and arguments%5B'stats'%5D:%0A%0A            import celery%0A            res = celery.current_app.control.inspect().stats()%0A            pprint (res)%0A%0A        elif arguments%5B'celery'%5D:%0A%0A            # from subprocess import call%0A            # call(%5B'celery','inspect','ping'%5D)%0A%0A            import celery%0A            res = celery.current_app.control.inspect().ping()%0A            pprint(res)%0A%0A            #
  if 
@@ -1505,32 +1505,34 @@
 s'%5D:%0A           
+ #
      res = celer
@@ -1585,15 +1585,16 @@
     
+ #
  else:
- 
 %0A   
@@ -1597,24 +1597,26 @@
 %0A           
+ #
      res = c
@@ -1666,24 +1666,26 @@
 %0A           
+ #
  func = geta
@@ -1726,32 +1726,33 @@
 s%5B'--format'%5D))%0A
+%0A
             func
@@ -1746,16 +1746,18 @@
         
+ #
  func(re
@@ -1763,23 +1763,8 @@
 es)%0A
-            '''
 %0A   
@@ -1792,24 +1792,76 @@
 rabbitmq'%5D:%0A
+            from subprocess import call            %0A
             
@@ -1873,16 +1873,17 @@
 %5B'sudo',
+ 
 'rabbitm
@@ -1888,16 +1888,17 @@
 tmqctl',
+ 
 'status'
@@ -1900,24 +1900,16 @@
 atus'%5D)%0A
-        
 %0A    '''
@@ -2030,22 +2030,16 @@
  (data)%0A
-      
 %0A    def
@@ -2101,16 +2101,17 @@
 le(data)%0A    '''
+%0A

test celery status