# 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/utils.py
# def start_queue(subfolder=None, max_count=None):
#     '''
#     start queue will be used to move new Batches (jobs) from the QUEUE to be
#     run with celery tasks. The status is changed from QUEUE to NEW when this is done.
#     If the QUEUE is empty, we parse the filesystem (and queue new jobs) again.
#     This job submission is done all at once to ensure that we don't have race
#     conditions of multiple workers trying to grab a job at the same time.
#     '''
#     from sendit.apps.main.tasks import import_dicomdir
# 
#     contenders = Batch.objects.filter(status="QUEUE")
#     if len(contenders) == 0:
#         update_cached(subfolder)
#         contenders = Batch.objects.filter(status="QUEUE")
# 
#     started = 0
#     for batch in contenders:
#         # not seen folders in queue
#         dicom_dir = batch.logs.get('DICOM_DIR')
#         if dicom_dir is not None:
#             import_dicomdir.apply_async(kwargs={"dicom_dir":dicom_dir})
#             started +=1
#         if max_count is not None:
#             if started >= max_count:
#                 break
# 
#     print("Added %s tasks to the active queue." %started)


from sendit.logger import bot
from django.core.management.base import BaseCommand
from sendit.apps.main.utils import start_queue
import sys
import os


class Command(BaseCommand):
    help = '''start queue will parse over:

              1. First preference: a list of subfolders DATA_INPUT_FOLDERS
              2. Second preference,  a single subfolder at the base (eg /data/<subfolder>) 
              3. The data base alone (/data)

           and submit async jobs to the queue for all new findings. We run this once,
           with the same process, to prevent race conditions if different workers
           were looking for new folders at the same time.
           '''

    def add_arguments(self, parser):
        parser.add_argument('--number', dest='number', default=1, type=int)
        parser.add_argument('--subfolder', dest='base', default='/data', type=str)

    def handle(self,*args, **options):
        number = options['number']
        base = options['base']

# 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/utils.py
# def start_queue(subfolder=None, max_count=None):
#     '''
#     start queue will be used to move new Batches (jobs) from the QUEUE to be
#     run with celery tasks. The status is changed from QUEUE to NEW when this is done.
#     If the QUEUE is empty, we parse the filesystem (and queue new jobs) again.
#     This job submission is done all at once to ensure that we don't have race
#     conditions of multiple workers trying to grab a job at the same time.
#     '''
#     from sendit.apps.main.tasks import import_dicomdir
# 
#     contenders = Batch.objects.filter(status="QUEUE")
#     if len(contenders) == 0:
#         update_cached(subfolder)
#         contenders = Batch.objects.filter(status="QUEUE")
# 
#     started = 0
#     for batch in contenders:
#         # not seen folders in queue
#         dicom_dir = batch.logs.get('DICOM_DIR')
#         if dicom_dir is not None:
#             import_dicomdir.apply_async(kwargs={"dicom_dir":dicom_dir})
#             started +=1
#         if max_count is not None:
#             if started >= max_count:
#                 break
# 
#     print("Added %s tasks to the active queue." %started)


# Path: sendit/apps/main/management/commands/start_queue.py
from sendit.logger import bot
from django.core.management.base import BaseCommand
from sendit.apps.main.utils import start_queue
import sys
import os



class Command(BaseCommand):
    help = '''start queue will parse over:

              1. First preference: a list of subfolders DATA_INPUT_FOLDERS
              2. Second preference,  a single subfolder at the base (eg /data/<subfolder>) 
              3. The data base alone (/data)

           and submit async jobs to the queue for all new findings. We run this once,
           with the same process, to prevent race conditions if different workers
           were looking for new folders at the same time.
           '''

    def add_arguments(self, parser):
        parser.add_argument('--number', dest='number', default=1, type=int)
        parser.add_argument('--subfolder', dest='base', default='/data', type=str)

    def handle(self,*args, **options):
        number = options['number']
        base = options['base']

        start_queue(max_count=number, subfolder=base)