# Path: sendit/logger.py
# ABRT = -4
# ERROR = -3
# WARNING = -2
# LOG = -1
# INFO = 1
# QUIET = 0
# VERBOSE = VERBOSE1 = 2
# VERBOSE2 = 3
# VERBOSE3 = 4
# DEBUG = 5
#         COLORIZE = get_user_color_preference()
#     COLORIZE = os.environ.get('SENDIT_COLORIZE',None)
#         COLORIZE = convert2boolean(COLORIZE)
# class SenditMessage:
#     def __init__(self,MESSAGELEVEL=None):
#     def useColor(self):
#     def addColor(self,level,text):
#     def emitError(self,level):
#     def emitOutput(self,level):
#     def isEnabledFor(self,messageLevel):
#     def emit(self,level,message,prefix=None):
#     def write(self,stream,message):
#     def get_logs(self,join_newline=True):
#     def show_progress(self,iteration,total,length=40,min_level=0,prefix=None,
#                       carriage_return=True,suffix=None,symbol=None):
#     def abort(self,message):
#     def error(self,message):
#     def warning(self,message):
#     def log(self,message):
#     def info(self,message):
#     def verbose(self,message):
#     def verbose1(self,message):
#     def verbose2(self,message):
#     def verbose3(self,message):
#     def debug(self,message):
#     def is_quiet(self):
# def get_logging_level():
# def get_user_color_preference():
# def convert2boolean(arg):
# 
# Path: sendit/apps/main/models.py
# class Batch(models.Model):
#     '''A batch has one or more images for some number of patients, each of which
#     is associated with a Study or Session. A batch maps cleanly to a folder that is
#     dropped into data for processing, and the application moves through tasks based
#     on batches.
#     '''
#     uid = models.CharField(max_length=200, null=False, unique=True)
# 
#     status = models.CharField(choices=BATCH_STATUS,
#                               default="NEW",
#                               max_length=250)
# 
#     add_date = models.DateTimeField('date added', auto_now_add=True)
#     has_error = models.BooleanField(choices=ERROR_STATUS, 
#                                     default=False,
#                                     verbose_name="HasError")
# 
#     qa = JSONField(default=dict())
#     logs = JSONField(default=dict())
#     modify_date = models.DateTimeField('date modified', auto_now=True)
#     tags = TaggableManager()
# 
#     def change_images_status(self,status):
#         '''change all images to have the same status'''
#         for dcm in self.image_set.all():
#             dcm.status = status
#             dcm.save()
#     
# 
#     def get_image_paths(self):
#         '''return file paths for all images associated
#         with a batch'''
#         image_files = []
#         for dcm in self.image_set.all():
#             try:
#                 if hasattr(dcm.image, 'file'):
#                     dicom_file = dcm.image.path
#                     if os.path.exists(dicom_file):
#                         image_files.append(dicom_file)
# 
#             # Image object has no file associated with it
#             except ValueError:
#                 pass
# 
#         return image_files
# 
#     def get_finished(self):
#         '''return file paths that aren't in PHI folder'''
#         return [ x for x in self.get_image_paths() if "/PHI/" not in x ]
# 
#     def get_path(self):
#         return "%s/%s" %(MEDIA_ROOT,self.id)
# 
#     def get_absolute_url(self):
#         return reverse('batch_details', args=[str(self.id)])
# 
#     def __str__(self):
#         return "%s-%s" %(self.id,self.uid)
# 
#     def __unicode__(self):
#         return "%s-%s" %(self.id,self.uid)
# 
#     def get_label(self):
#         return "batch"
# 
#     class Meta:
#         app_label = 'main'


from sendit.logger import bot
from sendit.apps.main.models import Batch
from django.core.management.base import (
    BaseCommand
)
import sys


class Command(BaseCommand):
    help = '''show batch logs with errors'''

    def add_arguments(self, parser):
        parser.add_argument('bid', nargs='*', type=int)

    def handle(self,*args, **options):
        
        nbids = len(options['bid'])
        if nbids > 0:

# Path: sendit/logger.py
# ABRT = -4
# ERROR = -3
# WARNING = -2
# LOG = -1
# INFO = 1
# QUIET = 0
# VERBOSE = VERBOSE1 = 2
# VERBOSE2 = 3
# VERBOSE3 = 4
# DEBUG = 5
#         COLORIZE = get_user_color_preference()
#     COLORIZE = os.environ.get('SENDIT_COLORIZE',None)
#         COLORIZE = convert2boolean(COLORIZE)
# class SenditMessage:
#     def __init__(self,MESSAGELEVEL=None):
#     def useColor(self):
#     def addColor(self,level,text):
#     def emitError(self,level):
#     def emitOutput(self,level):
#     def isEnabledFor(self,messageLevel):
#     def emit(self,level,message,prefix=None):
#     def write(self,stream,message):
#     def get_logs(self,join_newline=True):
#     def show_progress(self,iteration,total,length=40,min_level=0,prefix=None,
#                       carriage_return=True,suffix=None,symbol=None):
#     def abort(self,message):
#     def error(self,message):
#     def warning(self,message):
#     def log(self,message):
#     def info(self,message):
#     def verbose(self,message):
#     def verbose1(self,message):
#     def verbose2(self,message):
#     def verbose3(self,message):
#     def debug(self,message):
#     def is_quiet(self):
# def get_logging_level():
# def get_user_color_preference():
# def convert2boolean(arg):
# 
# Path: sendit/apps/main/models.py
# class Batch(models.Model):
#     '''A batch has one or more images for some number of patients, each of which
#     is associated with a Study or Session. A batch maps cleanly to a folder that is
#     dropped into data for processing, and the application moves through tasks based
#     on batches.
#     '''
#     uid = models.CharField(max_length=200, null=False, unique=True)
# 
#     status = models.CharField(choices=BATCH_STATUS,
#                               default="NEW",
#                               max_length=250)
# 
#     add_date = models.DateTimeField('date added', auto_now_add=True)
#     has_error = models.BooleanField(choices=ERROR_STATUS, 
#                                     default=False,
#                                     verbose_name="HasError")
# 
#     qa = JSONField(default=dict())
#     logs = JSONField(default=dict())
#     modify_date = models.DateTimeField('date modified', auto_now=True)
#     tags = TaggableManager()
# 
#     def change_images_status(self,status):
#         '''change all images to have the same status'''
#         for dcm in self.image_set.all():
#             dcm.status = status
#             dcm.save()
#     
# 
#     def get_image_paths(self):
#         '''return file paths for all images associated
#         with a batch'''
#         image_files = []
#         for dcm in self.image_set.all():
#             try:
#                 if hasattr(dcm.image, 'file'):
#                     dicom_file = dcm.image.path
#                     if os.path.exists(dicom_file):
#                         image_files.append(dicom_file)
# 
#             # Image object has no file associated with it
#             except ValueError:
#                 pass
# 
#         return image_files
# 
#     def get_finished(self):
#         '''return file paths that aren't in PHI folder'''
#         return [ x for x in self.get_image_paths() if "/PHI/" not in x ]
# 
#     def get_path(self):
#         return "%s/%s" %(MEDIA_ROOT,self.id)
# 
#     def get_absolute_url(self):
#         return reverse('batch_details', args=[str(self.id)])
# 
#     def __str__(self):
#         return "%s-%s" %(self.id,self.uid)
# 
#     def __unicode__(self):
#         return "%s-%s" %(self.id,self.uid)
# 
#     def get_label(self):
#         return "batch"
# 
#     class Meta:
#         app_label = 'main'


# Path: sendit/apps/main/management/commands/batch_logs.py
from sendit.logger import bot
from sendit.apps.main.models import Batch
from django.core.management.base import (
    BaseCommand
)
import sys



class Command(BaseCommand):
    help = '''show batch logs with errors'''

    def add_arguments(self, parser):
        parser.add_argument('bid', nargs='*', type=int)

    def handle(self,*args, **options):
        
        nbids = len(options['bid'])
        if nbids > 0:

            bot.debug("Inspecting for errors for %s batch ids" %nbids)