class QueueOrganizer:
    def __init__(self, report, tracker, queue_path):
        self.tracker = tracker
        self.report = report
        self.queue_path = queue_path
        self.compressed_file_manager = CompressedFile(self.report)


    def archive_and_extract_files(self, archive_path, extracted_path, report_sender):   
        for compressed in os.listdir(self.queue_path):
            self.tracker.register(compressed, 'begin-queue')
            
            self.report.write('Archive ' + self.queue_path + ' in ' + extracted_path + '/' + compressed, True, False, True)
            self.archive(self.queue_path + '/' + compressed, archive_path)
            
            self.report.write('Extract files from ' + self.queue_path + ' to ' + extracted_path + '/' + compressed, True, False, True)
            self.compressed_file_manager.extract_files(self.queue_path + '/' + compressed, extracted_path + '/' + compressed)
            
            attached_files = []
            text =  'Files in the package\n' + '\n'.join(os.listdir(extracted_path + '/' + compressed))
            self.report.write(text, True, False, False)

            if os.path.exists(archive_path + '/' + compressed):
                self.report.write('Delete ' + self.queue_path + '/' + compressed, True, False, False)
                os.remove(self.queue_path + '/' + compressed)
            self.tracker.register(compressed, 'end-queue')

    def archive(self, filename, archive_path):
        """
        Archive a copy of filename 
        """
        if not os.path.exists(archive_path):
            os.makedirs(archive_path)

        if os.path.exists(archive_path):
            name = os.path.basename(filename)
            if os.path.exists(archive_path + '/' + name):
                new_name = add_date_to_filename(name, False)
                shutil.copyfile(filename, archive_path + '/' + new_name)
            shutil.copy(filename, archive_path)
 def __init__(self, report, tracker, queue_path):
     self.tracker = tracker
     self.report = report
     self.queue_path = queue_path
     self.compressed_file_manager = CompressedFile(self.report)
     self.temp_dir = os.path.dirname(self.queue_path) + '/tmp'
class QueueOrganizer:
    def __init__(self, report, tracker, queue_path):
        self.tracker = tracker
        self.report = report
        self.queue_path = queue_path
        self.compressed_file_manager = CompressedFile(self.report)
        self.temp_dir = os.path.dirname(self.queue_path) + '/tmp'

    def archive_and_extract_files(self, archive_path, work_path, report_sender):   

        for package_folder in os.listdir(work_path):
            to_delete = work_path + '/' + package_folder
            if os.path.isfile(to_delete):
                os.unlink(to_delete)
            elif os.path.isdir(to_delete):
                shutil.rmtree(to_delete)

        for zip_filename in os.listdir(self.queue_path):
            self.tracker.register(zip_filename, 'begin-queue')

            package_filename = self.queue_path + '/' + zip_filename
            package_path = work_path + '/' + zip_filename

            if os.path.exists(package_path):
                for f in os.listdir(package_path):
                    os.unlink(package_path + '/' + f)
                
            self.report.write('Archive ' + package_filename + ' in ' + archive_path, True, False, True)

            self.report.write(str(os.stat(package_filename).st_size), True, False, True)
            self.archive(package_filename, archive_path)
            
            self.report.write('Extract files from ' + package_filename + ' to ' + package_path, True, False, True)

            from datetime import datetime
            self.compressed_file_manager.extract_files(package_filename, package_path, self.temp_dir + datetime.now().isoformat()[12:16].replace(':', ''))
            
            
            text =  'Files in the package\n' + '\n'.join(os.listdir(package_path))
            self.report.write(text, True, False, False)

            if os.path.exists(archive_path + '/' + zip_filename):
                self.report.write('Delete ' + package_filename, True, False, False)
                os.unlink(package_filename)
            self.tracker.register(zip_filename, 'end-queue')

    def archive(self, filename, archive_path):
        """
        Archive a copy of filename 
        """
        archive_path2 = archive_path + '_previous'
        if not os.path.exists(archive_path):
            os.makedirs(archive_path)
        if not os.path.exists(archive_path2):
            os.makedirs(archive_path2)

        if os.path.exists(archive_path):
            name = os.path.basename(filename)
            archived_file = archive_path + '/' + name
            if os.path.exists(archived_file):
                new_name = add_date_to_filename(name, False)
                #shutil.copyfile(archived_file, archive_path2 + '/' + new_name)
            shutil.copy(filename, archive_path)
 def __init__(self, report, tracker, queue_path):
     self.tracker = tracker
     self.report = report
     self.queue_path = queue_path
     self.compressed_file_manager = CompressedFile(self.report)