Ejemplo n.º 1
0
 def __init__(self):
     """"""
     self.__slots = Slots(limit=20)  #checker stots
     self.__pending_downloads = OrderedDict()  #{id_item: download_item, }
     self.__checking_downloads = {}  #{id_item: download_item, }
     self.__ready_downloads = {
     }  #{id_item: download_item, } checked_downloads
     self.__thread_checking_downloads = {}  #{id_item: th, }
Ejemplo n.º 2
0
 def __init__(self, aggregatedData):
     self.__aData = aggregatedData
     self.__currentType = CUSTOMIZATION_TYPE.CAMOUFLAGE
     self.__currentSlotIdx = 0
     self.__currentDuration = 0
     self.__carouselItems = []
     self.filter = Filter(self.__aData.availableGroupNames)
     self.filter.changed += self.__updateCarouselData
     self.slots = Slots(self.__aData)
     self.slots.selected += self.__onSlotSelected
     self.slots.updated += self.__onSlotUpdated
     self.updated = Event()
    def _generate_data(self, data):
        '''Generate data for the generation'''
        solution_fitness_items = []
        solution_items = {}
        solutions = []

        for i in range(0, self.population):
            rooms = Rooms(ROOMS_CSV)
            slots = Slots(rooms)
            courses = Courses(CLASSES_CSV)
            schedule = data[i] if data else Schedule(rooms, slots, courses)

            solutions.append(schedule)
            schedule_fitness = schedule.get_fitness()
            solution_fitness_items.append(schedule_fitness)
            solution_fitness_items.sort()

            if schedule_fitness in solution_items.keys():
                solution_items[schedule_fitness][schedule.id] = schedule
            else:
                solution_items[schedule_fitness] = {}
                solution_items[schedule_fitness][schedule.id] = schedule

        self.elites = self._generate_elite_list(solution_items)
        self.avg_solution_fitness = round(
            sum(solution_fitness_items) / len(solution_fitness_items))
        return solutions
 def __init__(self):
     """"""
     self.__slots = Slots(limit=20) #checker stots
     self.__pending_downloads = OrderedDict() #{id_item: download_item, }
     self.__checking_downloads = {} #{id_item: download_item, }
     self.__ready_downloads = {} #{id_item: download_item, } checked_downloads
     self.__thread_checking_downloads = {} #{id_item: th, }
Ejemplo n.º 5
0
 def __init__(self, aggregatedData):
     self.__aData = aggregatedData
     self.__currentType = CUSTOMIZATION_TYPE.CAMOUFLAGE
     self.__currentSlotIdx = 0
     self.__currentDuration = 0
     self.__carouselItems = []
     self.filter = Filter(self.__aData.availableGroupNames)
     self.filter.changed += self.__updateCarouselData
     self.slots = Slots(self.__aData)
     self.slots.selected += self.__onSlotSelected
     self.slots.updated += self.__onSlotUpdated
     self.updated = Event()
Ejemplo n.º 6
0
 def __init__(self, config, section):
     """"""
     Slots.__init__(self, config.get_slots(section),
                    config.get_wait(section))
     self.active_downloads = {}
Ejemplo n.º 7
0
class DownloadManager(DownloadCore, ThreadManager):
    """
    DownloadCore:
    .Contiene las listas con los items (tipos de clase, DownloadItem) de descarga, y los metodos para modificar esas listas.
    .Atributos heredados:
    self.active_downloads, self.queue_downloads, self.complete_downloads, self.stopped_downloads
    .Metodos heredados:
    -
    
    ThreadManager:
    .Contiene el diccionario con los threads (de descarga, clase Downloader) instanciados de las descargas activas.
    .Atributos heredados:
    self.thread_downloads (DICT)
    .Metodos heredados:
    get_thread, add_thread, delete_thread, stop_thread, stop_all, get_thread_status
    """
    def __init__(self):
        """"""
        DownloadCore.__init__(self) #download_core.py
        ThreadManager.__init__(self) #thread_manager.py
        self.global_slots = Slots() #slots.py
    
    def start_all(self, id_order_list):
        """"""
        self.queue_downloads.update(self.stopped_downloads)
        self.stopped_downloads.clear()
        self.reorder_queue(id_order_list)
        for download_item in self.queue_downloads.values():
            download_item.reset_fail_count()
            self.download_starter(download_item)
    
    def stop_all(self, filter_host_list=None):
        """"""
        filter_host_list = filter_host_list or []
        for id_item, download_item in self.active_downloads.iteritems():
            if download_item.host not in filter_host_list:
                self.stop_thread(id_item)
        for id_item, download_item in self.queue_downloads.items():
            if download_item.host not in filter_host_list:
                self.stopped_downloads[id_item] = download_item
                del self.queue_downloads[id_item]

    def start_download(self, id_item):
        """"""
        try:
            download_item = self.stopped_downloads.pop(id_item)
        except KeyError:
            return False
        else:
            self.downloader_init([download_item, ], download_item.path)
            return True

    def stop_download(self, id_item):
        """"""
        try:
            download_item = self.active_downloads[id_item]
        except KeyError:
            try:
                download_item = self.queue_downloads.pop(id_item)
                self.stopped_downloads[id_item] = download_item
            except KeyError:
                return False
            else:
                return True
        else:
            self.stop_thread(id_item)
            return True

    def delete_download(self, id_items_list, remove_file=False):
        """"""
        for id_item in id_items_list:
            is_active = False
            try:
                download_item = self.active_downloads.pop(id_item)
                is_active = True
            except KeyError:
                try:
                    download_item = self.stopped_downloads.pop(id_item)
                except KeyError:
                    try:
                        download_item = self.queue_downloads.pop(id_item)
                    except KeyError:
                        try:
                            download_item = self.complete_downloads.pop(id_item)
                        except KeyError:
                            #bug: error on remove complete item from the gui.
                            raise

            if is_active:
                th = self.get_thread(id_item)
                self.stop_thread(id_item)
                self.delete_thread(id_item)
                self.global_slots.remove_slot()
                self.next_download()
            else:
                th = None

            if remove_file:
                threading.Thread(group=None, target=self.remove_file, name=None, args=(download_item, th)).start()

    def remove_file(self, download_item, th):
        """"""
        if th is not None:
            th.join()
        try:
            os.remove(os.path.join(download_item.path, download_item.name))
        except Exception as err:
            logger.warning(err)

    def update_active_downloads(self):
        """
        Roba ciclos.
        This may change the active_downloads dict, you should get a dict copy before calling this method.
        """
        for id_item, download_item in self.active_downloads.items():
            item_data = self.get_thread_update(id_item) #threadmanager
            download_item.update(*item_data)
            limit_exceeded = self.is_limit_exceeded(id_item) #threadmanager
            old_status = download_item.status
            if old_status in (cons.STATUS_STOPPED, cons.STATUS_FINISHED, cons.STATUS_ERROR):
                if old_status == cons.STATUS_STOPPED:
                    self.stopped_downloads[id_item] = download_item
                elif old_status == cons.STATUS_FINISHED:
                    self.complete_downloads[id_item] = download_item
                else: #status == cons.STATUS_ERROR
                    download_item.fail_count += 1
                    if download_item.fail_count > conf.get_retries_limit():
                        download_item.status = cons.STATUS_STOPPED
                        self.stopped_downloads[id_item] = download_item
                    else:
                        download_item.status = cons.STATUS_QUEUE
                        self.queue_downloads[id_item] = download_item
                self.delete_thread(id_item) #threadmanager
                del self.active_downloads[id_item]
                self.global_slots.remove_slot()
                self.next_download()
                if old_status == cons.STATUS_FINISHED:
                    events.download_complete.emit(download_item)
                if not self.active_downloads and old_status != cons.STATUS_STOPPED:
                    events.all_downloads_complete.emit()
                if limit_exceeded and self.active_downloads and old_status == cons.STATUS_ERROR:
                    events.limit_exceeded.emit()

    def update_download_item(self, download_item):
        pass

    def downloader_init(self, item_list, path):
        """
        Crea los threads para la descarga de cada archivo.
        """
        #if not self.active_downloads: #after this method completes, there will be one active download at least.
            #events.trigger_downloading_process_pre_start()
        for download_item in item_list:
            download_item.path = path
            download_item.fail_count = 0
            self.queue_downloads[download_item.id] = download_item
            self.download_starter(download_item)

    def next_download(self):
        """
        Init next download on queue. This is called when an active download is stopped or has finished.
        """
        for download_item in self.queue_downloads.values():
            if self.global_slots.available_slot():
                self.download_starter(download_item)
            else:
                break

    def download_starter(self, download_item):
        """"""
        if self.global_slots.available_slot():
            slot = True
            if host_accounts.get_account(download_item.host) is None: #not premium.
                if not self.is_host_slot_available(download_item.host):
                    slot = False
            if slot:
                self.global_slots.add_slot()
                self.create_thread(download_item) #threadmanager
                self.active_downloads[download_item.id] = download_item
                del self.queue_downloads[download_item.id]

    def is_host_slot_available(self, host):
        """"""
        count = 0
        host_slots = plugins_parser.get_plugin_item(host).get_slots_limit()
        if host_slots > 0: #-1 or 0 means unlimited slots
            for download_item in self.active_downloads.itervalues():
                if host == download_item.host:
                    count += 1
                if count >= host_slots:
                    return False
        return True

    def new_slot_limit(self, new_limit):
        """"""
        current_limit = self.global_slots.get_limit()
        self.global_slots.set_limit(new_limit)
        if new_limit > current_limit:
            self.next_download()
Ejemplo n.º 8
0
class Carousel(object):

    def __init__(self, aggregatedData):
        self.__aData = aggregatedData
        self.__currentType = CUSTOMIZATION_TYPE.CAMOUFLAGE
        self.__currentSlotIdx = 0
        self.__currentDuration = 0
        self.__carouselItems = []
        self.filter = Filter(self.__aData.availableGroupNames)
        self.filter.changed += self.__updateCarouselData
        self.slots = Slots(self.__aData)
        self.slots.selected += self.__onSlotSelected
        self.slots.updated += self.__onSlotUpdated
        self.updated = Event()

    def fini(self):
        self.slots.selected -= self.__onSlotSelected
        self.slots.updated -= self.__onSlotUpdated
        self.filter.changed -= self.__updateCarouselData
        self.__carouselItems = None
        self.__aData = None
        self.slots.fini()
        self.filter.fini()
        return

    @property
    def items(self):
        return self.__carouselItems

    @property
    def currentType(self):
        return self.__currentType

    @property
    def currentSlotIdx(self):
        return self.__currentSlotIdx

    def applyItem(self, carouselItemIdx):
        self.slots.updateSlot(self.__carouselItems[carouselItemIdx], duration=self.__currentDuration)

    def previewItem(self, carouselItemIdx):
        previewItemID = self.__carouselItems[carouselItemIdx]['id']
        if self.__currentType == CUSTOMIZATION_TYPE.CAMOUFLAGE:
            g_hangarSpace.space.updateVehicleCamouflage(camouflageID=previewItemID)
        else:
            self.__updateItemOnTank3DModel(previewItemID)

    def changeDuration(self, duration):
        self.__currentDuration = duration
        self.__updateCarouselData()

    def __updateItemOnTank3DModel(self, previewItemID):
        cType = self.__currentType
        slotIdx = self.__currentSlotIdx
        slotItem = self.slots.getData()['data'][cType]['data'][slotIdx]
        changedPreviewModel = copy.deepcopy(self.__aData.viewModel[1:3])
        rawInstalledItem = [previewItemID, time.time(), 0]
        if cType == CUSTOMIZATION_TYPE.INSCRIPTION:
            rawInstalledItem.append(0)
        changedPreviewModel[cType - 1][slotItem['spot'] + self.slots.calculateVehicleIndex(slotIdx, cType)] = rawInstalledItem
        g_hangarSpace.space.updateVehicleSticker(changedPreviewModel)

    def __onSlotSelected(self, newType, newSlotIdx):
        self.__currentType = newType
        self.__currentSlotIdx = newSlotIdx
        self.filter.setTypeAndIdx(newType, newSlotIdx)
        if newType == CUSTOMIZATION_TYPE.CAMOUFLAGE:
            self.filter.set(FILTER_TYPE.GROUP, CAMOUFLAGE_GROUP_MAPPING[newSlotIdx])
        self.filter.apply()

    def __getBtnTooltip(self, installedInSlot):
        if installedInSlot:
            params = (TOOLTIPS.CUSTOMIZATION_CAROUSEL_SLOT_REMOVE_HEADER, TOOLTIPS.CUSTOMIZATION_CAROUSEL_SLOT_REMOVE_BODY)
        else:
            params = (TOOLTIPS.CUSTOMIZATION_CAROUSEL_SLOT_SELECT_HEADER, TOOLTIPS.CUSTOMIZATION_CAROUSEL_SLOT_SELECT_BODY)
        return makeTooltip(*params)

    def __onSlotUpdated(self, newSlotData):
        self.__updateCarouselData()

    def __updateCarouselData(self):
        oldItemsCount = len(self.__carouselItems)
        del self.__carouselItems[:]
        appliedItems = []
        purchasedItems = []
        otherItems = []
        currentSlotItem = None
        installedItemID = self.slots.getInstalledItem(self.__currentSlotIdx, self.__currentType).getID()
        if self.__currentType == CUSTOMIZATION_TYPE.CAMOUFLAGE:
            displayedItems = {}
            for itemID, item in self.__aData.displayed[self.__currentType].iteritems():
                if item.getGroup() == CAMOUFLAGE_GROUP_MAPPING[self.__currentSlotIdx]:
                    displayedItems[itemID] = item

        else:
            displayedItems = self.__aData.displayed[self.__currentType]
        for itemID, item in displayedItems.iteritems():
            if not self.filter.check(item):
                continue
            appliedToCurrentSlot = itemID == self.slots.getSelectedSlotItemID()
            installedInSlot = itemID == installedItemID
            carouselItem = {'id': itemID,
             'object': item,
             'appliedToCurrentSlot': appliedToCurrentSlot,
             'price': item.getPrice(self.__currentDuration),
             'priceIsGold': item.priceIsGold(self.__currentDuration),
             'isInDossier': item.isInDossier,
             'buttonTooltip': self.__getBtnTooltip(installedInSlot),
             'duration': self.__currentDuration,
             'installedInSlot': installedInSlot}
            if appliedToCurrentSlot:
                currentSlotItem = carouselItem
            if installedInSlot:
                appliedItems.append(carouselItem)
            elif item.isInDossier:
                purchasedItems.append(carouselItem)
            else:
                otherItems.append(carouselItem)

        self.__carouselItems = appliedItems + purchasedItems + otherItems
        if currentSlotItem is not None:
            goToIndex = currentSlotCarouselItemIdx = self.__carouselItems.index(currentSlotItem)
        else:
            currentSlotCarouselItemIdx = -1
            goToIndex = -1 if oldItemsCount == len(self.__carouselItems) else 0
        self.updated({'items': self.__carouselItems,
         'rendererWidth': _RENDERER_WIDTH[self.__currentType],
         'selectedIndex': currentSlotCarouselItemIdx,
         'goToIndex': goToIndex,
         'unfilteredLength': len(displayedItems)})
        return
Ejemplo n.º 9
0
    if count >= 10:
        reset = 0
        count = 0
    elif reset >= 10:
        os.system('rosnode kill /main_ai')


if __name__ == '__main__':
    rospy.init_node('main_ai')
    rospy.Subsciber('/planner_switch', Bool, getSwitchState, queue_size=1)
    gate = Gate()
    path1 = Path()
    shoot_craps = ShootCraps()
    path2 = Path()
    slots = Slots()
    roulette = Roulette()
    cash_in = CashIn()

    gate.run()

    path1.run()
    if path1.failed:
        auv.turnAbs(cons.SC_DEGREES)

    shoot_craps.run()

    path2.run()
    if path2.failed:
        auv.turnAbs(cons.SLOTS_DEGREES)
Ejemplo n.º 10
0
class Carousel(object):

    def __init__(self, aggregatedData):
        self.__aData = aggregatedData
        self.__currentType = CUSTOMIZATION_TYPE.CAMOUFLAGE
        self.__currentSlotIdx = 0
        self.__currentDuration = 0
        self.__carouselItems = []
        self.filter = Filter(self.__aData.availableGroupNames)
        self.filter.changed += self.__updateCarouselData
        self.slots = Slots(self.__aData)
        self.slots.selected += self.__onSlotSelected
        self.slots.updated += self.__onSlotUpdated
        self.updated = Event()

    def fini(self):
        self.slots.selected -= self.__onSlotSelected
        self.slots.updated -= self.__onSlotUpdated
        self.filter.changed -= self.__updateCarouselData
        self.__carouselItems = None
        self.__aData = None
        self.slots.fini()
        self.filter.fini()
        return

    @property
    def items(self):
        return self.__carouselItems

    @property
    def currentType(self):
        return self.__currentType

    @property
    def currentSlotIdx(self):
        return self.__currentSlotIdx

    def applyItem(self, carouselItemIdx):
        carouselItem = self.__carouselItems[carouselItemIdx]
        self.slots.applyItem(carouselItem, duration=self.__currentDuration)

    def changeDuration(self, duration):
        self.__currentDuration = duration
        self.__updateCarouselData()

    def __onSlotSelected(self, newType, newSlotIdx):
        self.__currentType = newType
        self.__currentSlotIdx = newSlotIdx
        self.filter.setTypeAndIdx(newType, newSlotIdx)
        if newType == CUSTOMIZATION_TYPE.CAMOUFLAGE:
            self.filter.set(FILTER_TYPE.GROUP, CAMOUFLAGE_GROUP_MAPPING[newSlotIdx])
        self.filter.apply()

    def __onSlotUpdated(self, newSlotData):
        if self.__currentType == newSlotData['type'] and self.__currentSlotIdx == newSlotData['idx']:
            self.filter.setTypeAndIdx(newSlotData['type'], newSlotData['idx'])
            self.__updateCarouselData()

    def __updateCarouselData(self):
        oldItemsCount = len(self.__carouselItems)
        del self.__carouselItems[:]
        appliedItems = defaultdict(list)
        purchasedItems = defaultdict(list)
        otherItems = defaultdict(list)
        allItems = [appliedItems, purchasedItems, otherItems]
        currentSlotItem = None
        installedItemID = self.slots.getInstalledItem(self.__currentSlotIdx, self.__currentType).getID()
        if self.__currentType == CUSTOMIZATION_TYPE.CAMOUFLAGE:
            displayedItems = {}
            for itemID, item in self.__aData.displayed[self.__currentType].iteritems():
                if item.getGroup() == CAMOUFLAGE_GROUP_MAPPING[self.__currentSlotIdx]:
                    displayedItems[itemID] = item

        else:
            displayedItems = self.__aData.displayed[self.__currentType]
        filterExceptions = {FILTER_TYPE.SHOW_IN_DOSSIER: self.__aData.installed[self.__currentType]}
        for itemID, item in displayedItems.iteritems():
            if self.filter.check(item, filterExceptions):
                appliedToCurrentSlot = itemID == self.slots.getSelectedSlotItemID()
                installedInSlot = itemID == installedItemID
                isInQuests = item.isInQuests and not item.isInDossier and self.filter.purchaseType == PURCHASE_TYPE.QUEST
                carouselItem = {'id': itemID,
                 'object': item,
                 'appliedToCurrentSlot': appliedToCurrentSlot,
                 'price': item.getPrice(self.__currentDuration),
                 'priceIsGold': item.priceIsGold(self.__currentDuration),
                 'isInDossier': item.isInDossier,
                 'isInQuests': isInQuests,
                 'duration': self.__currentDuration,
                 'installedInSlot': installedInSlot}
                if appliedToCurrentSlot:
                    currentSlotItem = carouselItem
                if installedInSlot:
                    group = appliedItems[item.getGroup()]
                elif item.isInDossier:
                    group = purchasedItems[item.getGroup()]
                else:
                    group = otherItems[item.getGroup()]
                if item.isFeatured:
                    group.insert(0, carouselItem)
                else:
                    group.append(carouselItem)

        for groupedItems in allItems:
            self.__carouselItems += chain(*groupedItems.values())

        if currentSlotItem is not None:
            goToIndex = currentSlotCarouselItemIdx = self.__carouselItems.index(currentSlotItem)
        else:
            currentSlotCarouselItemIdx = -1
            goToIndex = -1 if oldItemsCount == len(self.__carouselItems) else 0
        self.updated({'items': self.__carouselItems,
         'rendererWidth': _RENDERER_WIDTH[self.__currentType],
         'selectedIndex': currentSlotCarouselItemIdx,
         'goToIndex': goToIndex,
         'unfilteredLength': len(displayedItems)})
        return
Ejemplo n.º 11
0
	def __init__(self):
		""""""
		Slots.__init__(self, 1)
		DownloadPlugin.__init__(self)
Ejemplo n.º 12
0
 def __init__(self):
     """"""
     DownloadCore.__init__(self)  #inicializar download_core.py
     ThreadManager.__init__(self)  #inicializar thread_manager.py
     self.global_slots = Slots()  #slots.py
Ejemplo n.º 13
0
class DownloadManager(DownloadCore, ThreadManager):  #herencia multiple
    """
    DownloadCore:
    .Contiene las listas con los items (tipos de clase, DownloadItem) de descarga, y los metodos para modificar esas listas.
    .Atributos heredados:
    self.active_downloads, self.queue_downloads, self.complete_downloads, self.stopped_downloads
    .Metodos heredados:
    -
    
    ThreadManager:
    .Contiene el diccionario con los threads (de descarga, clase Downloader) instanciados de las descargas activas.
    .Atributos heredados:
    self.thread_downloads (DICT)
    .Metodos heredados:
    get_thread, add_thread, delete_thread, stop_thread, stop_all, get_thread_status
    """
    def __init__(self):
        """"""
        DownloadCore.__init__(self)  #inicializar download_core.py
        ThreadManager.__init__(self)  #inicializar thread_manager.py
        self.global_slots = Slots()  #slots.py

    def start_all(self, id_order_list):
        """"""
        self.queue_downloads.update(self.stopped_downloads)
        self.stopped_downloads.clear()
        self.reorder_queue(id_order_list)
        for download_item in self.queue_downloads.values():
            download_item.reset_fail_count()
            self.download_starter(download_item)

    def stop_all(self, filter_host_list=None):
        """"""
        if filter_host_list is None:
            filter_host_list = []
        for id_item, download_item in self.active_downloads.iteritems():
            if download_item.host not in filter_host_list:
                self.stop_thread(self.get_thread(id_item))
        for id_item, download_item in self.queue_downloads.items():
            if download_item.host not in filter_host_list:
                self.stopped_downloads[id_item] = download_item
                del self.queue_downloads[id_item]

    def start_download(self, id_item):  #iniciar descarga. Boton play.
        """"""
        try:
            download_item = self.stopped_downloads.pop(id_item)
        except KeyError:
            return False
        else:
            self.downloader_init([
                download_item,
            ], download_item.path)
            return True

    def stop_download(self, id_item):  #detener descarga. Boton stop.
        """"""
        try:
            download_item = self.active_downloads[id_item]
        except KeyError:
            try:
                download_item = self.queue_downloads.pop(id_item)
                self.stopped_downloads[id_item] = download_item
            except KeyError:
                return False
            else:
                return True
        else:
            self.stop_thread(self.get_thread(id_item))
            return True

    def delete_download(self, id_items_list):
        """"""
        for id_item in id_items_list:
            is_active = False
            try:
                download_item = self.active_downloads.pop(id_item)
                is_active = True
            except KeyError:
                try:
                    download_item = self.stopped_downloads.pop(id_item)
                except KeyError:
                    try:
                        download_item = self.queue_downloads.pop(id_item)
                    except KeyError as err:  #will crash after this and that's ok.
                        logger.exception(err)
            th = None
            if is_active:
                th = self.get_thread(id_item)
                self.stop_thread(th)
                self.delete_thread(id_item)
                self.global_slots.remove_slot()
                self.next_download()

            threading.Thread(group=None,
                             target=self.remove_file,
                             name=None,
                             args=(download_item, th)).start()

    def remove_file(self, download_item, th):
        """"""
        if th is not None:
            th.join()
        try:
            os.remove(os.path.join(download_item.path, download_item.name))
        except Exception as err:
            logger.warning(err)

    def get_items_update(self):
        """
        Roba ciclos.
        """
        result_list = []
        for id_item, download_item in self.active_downloads.items():
            item_data = self.get_thread_status(
                id_item)  #Metodo de threadManager heredado
            if item_data is not None:
                download_item.update(*item_data)
                limit_exceeded = self.is_limit_exceeded(download_item.id)
            else:
                download_item.status = cons.STATUS_ERROR
                limit_exceeded = False
            status = download_item.status
            result_list.append(download_item)
            if status in (cons.STATUS_STOPPED, cons.STATUS_FINISHED,
                          cons.STATUS_ERROR):
                if status == cons.STATUS_FINISHED:
                    self.complete_downloads[id_item] = download_item
                elif status == cons.STATUS_ERROR:
                    logger.warning("status error: {0}".format(
                        download_item.host))
                    download_item.fail_count += 1
                    if download_item.fail_count > conf.get_retries_limit():
                        download_item.status = cons.STATUS_STOPPED
                        self.stopped_downloads[id_item] = download_item
                    else:
                        download_item.status = cons.STATUS_QUEUE
                        self.queue_downloads[id_item] = download_item
                else:  #stopped
                    self.stopped_downloads[id_item] = download_item
                self.delete_thread(id_item)  #metodo de threadmanager heredado
                del self.active_downloads[id_item]
                self.global_slots.remove_slot(
                )  #remove the slot, so the next download can start.
                self.next_download()
                if status == cons.STATUS_FINISHED:
                    events.trigger_download_complete(download_item)
                if not self.active_downloads and status != cons.STATUS_STOPPED:
                    events.trigger_all_downloads_complete()
                elif limit_exceeded:  #cons.STATUS_ERROR
                    events.trigger_limit_exceeded()

        return result_list

    def downloader_init(
            self, item_list,
            save_to_path):  #start_thread. Crea el thread para el link dado.
        """
        Crea los threads para la descarga de cada archivo.
        """
        #if not self.active_downloads: #after this method completes, there will be one active download at least.
        #events.trigger_downloading_process_pre_start()
        for download_item in item_list:  #in self.pending_downloads:
            download_item.set_path(save_to_path)
            download_item.reset_fail_count()
            self.queue_downloads[download_item.id] = download_item
            self.download_starter(download_item)

    def next_download(self):
        """
        Init next download on queue. This is called when an active download is stopped or has finished.
        """
        for download_item in self.queue_downloads.values(
        ):  #iniciar la proxima descarga en la cola, o la proxima...
            if self.global_slots.available_slot():
                self.download_starter(
                    download_item
                )  #iniciar descarga si es posible (si ya no se esta bajando del mismo host)
            else:
                break

    def download_starter(self, download_item):
        """"""
        if self.global_slots.available_slot():
            slot = True
            if host_accounts.get_account(
                    download_item.host) is None:  #si no es premium entrar.
                if not self.is_host_slot_available(download_item.host):
                    slot = False
            if slot:
                self.global_slots.add_slot()
                self.add_thread(
                    download_item.id, download_item.name, download_item.path,
                    download_item.link, download_item.host,
                    download_item.chunks
                )  #crear thread y comenzar descarga. Metodo de threadmanager heredado
                self.active_downloads[download_item.id] = download_item
                del self.queue_downloads[download_item.id]

    def is_host_slot_available(self, host):
        """"""
        count = 0
        host_slots = plugins_parser.get_plugin_item(host).get_slots_limit()
        if host_slots > 0:  #-1 or 0 means unlimited slots
            for download_item in self.active_downloads.itervalues():
                if host == download_item.host:
                    count += 1
                if count >= host_slots:
                    return False
        return True

    def new_slot_limit(self, new_limit):
        """"""
        current_limit = self.global_slots.get_limit()
        self.global_slots.set_limit(new_limit)
        if new_limit > current_limit:
            self.next_download()
Ejemplo n.º 14
0
	def __init__(self, config, section):
		""""""
		Slots.__init__(self, config.get_slots(section), config.get_wait(section))
		self.active_downloads = {}
class AddDownloadsManager:
    """"""
    def __init__(self):
        """"""
        self.__slots = Slots(limit=20) #checker stots
        self.__pending_downloads = OrderedDict() #{id_item: download_item, }
        self.__checking_downloads = {} #{id_item: download_item, }
        self.__ready_downloads = {} #{id_item: download_item, } checked_downloads
        self.__thread_checking_downloads = {} #{id_item: th, }

    def get_checking_downloads(self):
        return self.__checking_downloads.copy()

    def get_all_checking_downloads(self):
        all_downloads = {}
        all_downloads.update(self.__pending_downloads)
        all_downloads.update(self.__checking_downloads)
        all_downloads.update(self.__ready_downloads)
        return all_downloads

    def clear_pending(self):
        """
        Erase pending_downloads dicts
        """
        self.__pending_downloads.clear()
        self.__checking_downloads.clear()
        self.__ready_downloads.clear()
        self.__thread_checking_downloads.clear()
        self.__slots.set_slots(slots=0)

    def create_download_item(self, file_name, link, copy_link=True):
        """"""
        host = misc.get_host(link)
        if plugins_parser.services_dict.get(host, None) is None:
            host = cons.UNSUPPORTED
        download_item = DownloadItem(file_name, host, link, can_copy_link=copy_link)
        self.__pending_downloads[download_item.id] = download_item
        return download_item
    
    def start_checking(self):
        """"""
        for id_item, download_item in self.__pending_downloads.items():
            if self.__slots.add_slot():
                th = LinkChecker(download_item.link)
                th.start()
                self.__thread_checking_downloads[id_item] = th
                self.__checking_downloads[id_item] = download_item
                del self.__pending_downloads[id_item]
            else:
                break

    def update_checking_downloads(self):
        """
        This may change the checking_downloads dict, you should get a dict copy before calling this method.
        """
        for id_item, download_item in self.__checking_downloads.items():
            th = self.__thread_checking_downloads[id_item]
            if not th.is_alive():
                download_item.link_status = th.link_status
                download_item.size = th.size
                download_item.status_msg = th.status_msg
                if download_item.name == cons.UNKNOWN: #may be downloading
                    download_item.name = th.file_name
                self.__ready_downloads[id_item] = download_item
                del self.__checking_downloads[id_item]
                del self.__thread_checking_downloads[id_item]
                self.__slots.remove_slot()
                self.start_checking()
    
    def recheck_items(self):
        """"""
        for id_item, download_item in self.__ready_downloads.items():
            if download_item.link_status not in (cons.LINK_CHECKING, cons.LINK_ALIVE):
                download_item.link_status = cons.LINK_CHECKING #safe
                self.__pending_downloads[id_item] = download_item
                del self.__ready_downloads[id_item]
                self.start_checking()

    def pop_checking_items(self, id_item_list):
        """"""
        result_list = []
        for id_item in id_item_list: # add this items.
            try:
                download_item = self.__checking_downloads.pop(id_item)
            except KeyError:
                try:
                    download_item = self.__ready_downloads.pop(id_item)
                except KeyError:
                    try:
                        download_item = self.__pending_downloads.pop(id_item)
                    except KeyError:
                        raise
            else:
                del self.__thread_checking_downloads[id_item]
                self.__slots.remove_slot()
                #self.start_checking()

            result_list.append(download_item)

        self.start_checking()
            
        return result_list
Ejemplo n.º 16
0
class DownloadManager(DownloadCore, ThreadManager): #herencia multiple
    """
    DownloadCore:
    .Contiene las listas con los items (tipos de clase, DownloadItem) de descarga, y los metodos para modificar esas listas.
    .Atributos heredados:
    self.active_downloads, self.queue_downloads, self.complete_downloads, self.stopped_downloads
    .Metodos heredados:
    -
    
    ThreadManager:
    .Contiene el diccionario con los threads (de descarga, clase Downloader) instanciados de las descargas activas.
    .Atributos heredados:
    self.thread_downloads (DICT)
    .Metodos heredados:
    get_thread, add_thread, delete_thread, stop_thread, stop_all, get_thread_status
    """
    def __init__(self):
        """"""
        DownloadCore.__init__(self) #inicializar download_core.py
        ThreadManager.__init__(self) #inicializar thread_manager.py
        self.global_slots = Slots() #slots.py
    
    def start_all(self, id_order_list):
        """"""
        self.queue_downloads.update(self.stopped_downloads)
        self.stopped_downloads.clear()
        self.reorder_queue(id_order_list)
        for download_item in self.queue_downloads.values():
            download_item.reset_fail_count()
            self.download_starter(download_item)
    
    def stop_all(self, filter_host_list=None):
        """"""
        if filter_host_list is None:
            filter_host_list = []
        for id_item, download_item in self.active_downloads.iteritems():
            if download_item.host not in filter_host_list:
                self.stop_thread(self.get_thread(id_item))
        for id_item, download_item in self.queue_downloads.items():
            if download_item.host not in filter_host_list:
                self.stopped_downloads[id_item] = download_item
                del self.queue_downloads[id_item]

    def start_download(self, id_item): #iniciar descarga. Boton play.
        """"""
        try:
            download_item = self.stopped_downloads.pop(id_item)
        except KeyError:
            return False
        else:
            self.downloader_init([download_item, ], download_item.path)
            return True

    def stop_download(self, id_item): #detener descarga. Boton stop.
        """"""
        try:
            download_item = self.active_downloads[id_item]
        except KeyError:
            try:
                download_item = self.queue_downloads.pop(id_item)
                self.stopped_downloads[id_item] = download_item
            except KeyError:
                return False
            else:
                return True
        else:
            self.stop_thread(self.get_thread(id_item))
            return True

    def delete_download(self, id_items_list):
        """"""
        for id_item in id_items_list:
            is_active = False
            try:
                download_item = self.active_downloads.pop(id_item)
                is_active = True
            except KeyError:
                try:
                    download_item = self.stopped_downloads.pop(id_item)
                except KeyError:
                    try:
                        download_item = self.queue_downloads.pop(id_item)
                    except KeyError as err: #will crash after this and that's ok.
                        logger.exception(err)
            th = None
            if is_active:
                th = self.get_thread(id_item)
                self.stop_thread(th)
                self.delete_thread(id_item)
                self.global_slots.remove_slot()
                self.next_download()
                
            threading.Thread(group=None, target=self.remove_file, name=None, args=(download_item, th)).start()

    def remove_file(self, download_item, th):
        """"""
        if th is not None:
            th.join()
        try:
            os.remove(os.path.join(download_item.path, download_item.name))
        except Exception as err:
            logger.warning(err)

    def get_items_update(self):
        """
        Roba ciclos.
        """
        result_list = []
        for id_item, download_item in self.active_downloads.items():
            item_data = self.get_thread_status(id_item) #Metodo de threadManager heredado
            if item_data is not None:
                download_item.update(*item_data)
                limit_exceeded = self.is_limit_exceeded(download_item.id)
            else:
                download_item.status = cons.STATUS_ERROR
                limit_exceeded = False
            status = download_item.status
            result_list.append(download_item)
            if status in (cons.STATUS_STOPPED, cons.STATUS_FINISHED, cons.STATUS_ERROR):
                if status == cons.STATUS_FINISHED:
                    self.complete_downloads[id_item] = download_item
                elif status == cons.STATUS_ERROR:
                    logger.warning("status error: {0}".format(download_item.host))
                    download_item.fail_count += 1
                    if download_item.fail_count > conf.get_retries_limit():
                        download_item.status = cons.STATUS_STOPPED
                        self.stopped_downloads[id_item] = download_item
                    else:
                        download_item.status = cons.STATUS_QUEUE
                        self.queue_downloads[id_item] = download_item
                else: #stopped
                    self.stopped_downloads[id_item] = download_item
                self.delete_thread(id_item) #metodo de threadmanager heredado
                del self.active_downloads[id_item]
                self.global_slots.remove_slot() #remove the slot, so the next download can start.
                self.next_download()
                if status == cons.STATUS_FINISHED:
                    events.trigger_download_complete(download_item)
                if not self.active_downloads and status != cons.STATUS_STOPPED:
                    events.trigger_all_downloads_complete()
                elif limit_exceeded: #cons.STATUS_ERROR
                    events.trigger_limit_exceeded()
        
        return result_list

    def downloader_init(self, item_list, save_to_path): #start_thread. Crea el thread para el link dado.
        """
        Crea los threads para la descarga de cada archivo.
        """
        #if not self.active_downloads: #after this method completes, there will be one active download at least.
            #events.trigger_downloading_process_pre_start()
        for download_item in item_list: #in self.pending_downloads:
            download_item.set_path(save_to_path)
            download_item.reset_fail_count()
            self.queue_downloads[download_item.id] = download_item
            self.download_starter(download_item)

    def next_download(self):
        """
        Init next download on queue. This is called when an active download is stopped or has finished.
        """
        for download_item in self.queue_downloads.values(): #iniciar la proxima descarga en la cola, o la proxima...
            if self.global_slots.available_slot():
                self.download_starter(download_item) #iniciar descarga si es posible (si ya no se esta bajando del mismo host)
            else:
                break

    def download_starter(self, download_item):
        """"""
        if self.global_slots.available_slot():
            slot = True
            if host_accounts.get_account(download_item.host) is None: #si no es premium entrar.
                if not self.is_host_slot_available(download_item.host):
                    slot = False
            if slot:
                self.global_slots.add_slot()
                self.add_thread(download_item.id, download_item.name, download_item.path, download_item.link, download_item.host, download_item.chunks) #crear thread y comenzar descarga. Metodo de threadmanager heredado
                self.active_downloads[download_item.id] = download_item
                del self.queue_downloads[download_item.id]

    def is_host_slot_available(self, host):
        """"""
        count = 0
        host_slots = plugins_parser.get_plugin_item(host).get_slots_limit()
        if host_slots > 0: #-1 or 0 means unlimited slots
            for download_item in self.active_downloads.itervalues():
                if host == download_item.host:
                    count += 1
                if count >= host_slots:
                    return False
        return True

    def new_slot_limit(self, new_limit):
        """"""
        current_limit = self.global_slots.get_limit()
        self.global_slots.set_limit(new_limit)
        if new_limit > current_limit:
            self.next_download()
Ejemplo n.º 17
0
 def __init__(self):
     """"""
     DownloadCore.__init__(self) #inicializar download_core.py
     ThreadManager.__init__(self) #inicializar thread_manager.py
     self.global_slots = Slots() #slots.py
Ejemplo n.º 18
0
 def send_slots(self, rest_api_url, pool_id, genesis_hash):
     slots = Slots(self._config_pool_tool, rest_api_url, pool_id,
                   genesis_hash)
     slots.process()
Ejemplo n.º 19
0
from audio import VoiceSettings, getYTDLInfo;
from rng import EightBall;
from memes import Memes;
import math;

def signal_handler(signal, frame):
	sys.exit(0);
signal.signal(signal.SIGINT, signal_handler);

client = discord.Client();

corpus = Corpus();

ping = None;

slots = Slots();

cmds = Commands();

# what the hell even is python
getid.client = client;

if not discord.opus.is_loaded():
	discord.opus.load_opus(find_library("opus"));

VoiceObj = None;
VoiceObjPlayer = None;
VoiceSubmitter = None;
vset = VoiceSettings();

eightBall = EightBall();
Ejemplo n.º 20
0
class AddDownloadsManager:
    """"""
    def __init__(self):
        """"""
        self.__slots = Slots(limit=20)  #checker stots
        self.__pending_downloads = OrderedDict()  #{id_item: download_item, }
        self.__checking_downloads = {}  #{id_item: download_item, }
        self.__ready_downloads = {
        }  #{id_item: download_item, } checked_downloads
        self.__thread_checking_downloads = {}  #{id_item: th, }

    def clear_pending(self):
        """
        Erase pending_downloads dicts
        """
        self.__pending_downloads.clear()
        self.__checking_downloads.clear()
        self.__ready_downloads.clear()
        self.__thread_checking_downloads.clear()
        self.__slots.set_slots(slots=0)

    def create_download_item(self, file_name, size, link, copy_link=True):
        """"""
        host = misc.get_host(link)
        download_item = DownloadItem(file_name,
                                     host,
                                     size,
                                     link,
                                     can_copy_link=copy_link)
        self.__pending_downloads[download_item.id] = download_item
        return download_item

    def start_checking(self):
        """"""
        for id_item, download_item in self.__pending_downloads.items():
            if self.__slots.add_slot():
                th = LinkChecker(download_item.link)
                th.start()
                self.__thread_checking_downloads[id_item] = th
                self.__checking_downloads[id_item] = download_item
                del self.__pending_downloads[id_item]
            else:
                break

    def get_checking_update(self):
        """"""
        result_list = []
        for id_item, download_item in self.__checking_downloads.items():
            result_list.append(download_item)
            th = self.__thread_checking_downloads[id_item]
            if not th.is_alive():
                download_item.host = th.host
                download_item.link_status = th.link_status
                download_item.size = th.size
                download_item.status_msg = th.status_msg
                if download_item.name == cons.UNKNOWN:  #may be downloading
                    download_item.name = th.file_name
                self.__ready_downloads[id_item] = download_item
                del self.__checking_downloads[id_item]
                del self.__thread_checking_downloads[id_item]
                self.__slots.remove_slot()
                self.start_checking()
        return result_list

    def recheck_items(self):
        """"""
        for id_item, download_item in self.__ready_downloads.items():
            if download_item.link_status not in (cons.LINK_CHECKING,
                                                 cons.LINK_ALIVE):
                download_item.link_status = cons.LINK_CHECKING  #safe
                self.__pending_downloads[id_item] = download_item
                del self.__ready_downloads[id_item]
                self.start_checking()

    def get_added_items(
            self, id_add_list):  #get download_items (added) from pending.
        """"""
        result_list = []
        for id_item in id_add_list:  #add this items.
            try:
                download_item = self.__checking_downloads.pop(id_item)
                del self.__thread_checking_downloads[id_item]
                self.__slots.remove_slot()
            except:
                try:
                    download_item = self.__ready_downloads.pop(id_item)
                except:
                    try:
                        download_item = self.__pending_downloads.pop(
                            id_item
                        )  #so we only keep the non-added items in the pending_dict
                    except Exception as err:
                        download_item = None
                        logger.warning(err)

            if download_item is not None:
                result_list.append(download_item)

        return result_list