def flush(self):
        with elapsed.clock(SyncStateTrakt, 'flush:collections'):
            # Flush trakt collections to disk
            self.cache.collections.flush()

        with elapsed.clock(SyncStateTrakt, 'flush:stores'):
            # Flush trakt stores to disk
            for key, store in self.cache.stores.items():
                log.debug('[%-38s] Flushing collection...', '/'.join(key))

                store.flush()

        # Store backup of trakt data
        group = os.path.join('trakt', str(self.task.account.id))

        BackupManager.database.backup(group, DatabaseManager.cache('trakt'), self.task.id, {
            'account': {
                'id': self.task.account.id,
                'name': self.task.account.name,

                'trakt': {
                    'username': self.task.account.trakt.username
                }
            }
        })
    def process_matched_episodes(self):
        # Iterate over plex episodes
        for ids, guid, (season_num, episode_num), p_show, p_season, p_episode in self.p_episodes:
            # Increment one step
            self.current.progress.group(Shows, 'matched:episodes').step()

            # Ensure `guid` is available
            if not guid or guid.service not in GUID_SERVICES:
                mark_unsupported(self.p_shows_unsupported, ids['show'], guid)
                continue

            key = (guid.service, guid.id)
            identifier = (season_num, episode_num)

            # Try retrieve `pk` for `key`
            pk = self.trakt.table('shows').get(key)

            with elapsed.clock(Shows, 'run:plex_episodes:execute_handlers'):
                for data in self.get_data(SyncMedia.Episodes):
                    with elapsed.clock(Shows, 'run:plex_episodes:t_objects'):
                        t_show, t_season, t_episode = self.t_objects(
                            self.trakt[(SyncMedia.Episodes, data)], pk,
                            season_num, episode_num
                        )

                    # Execute episode handlers
                    self.execute_handlers(
                        SyncMedia.Episodes, data,

                        key=ids['episode'],
                        identifier=identifier,

                        guid=guid,
                        p_show=p_show,
                        p_item=p_episode,

                        t_show=t_show,
                        t_item=t_episode
                    )

            # Remove episode from `pending_episodes`
            if pk in self.p_episodes_pending and identifier in self.p_episodes_pending[pk]:
                self.p_episodes_pending[pk].remove(identifier)

            # Task checkpoint
            self.checkpoint()

        # Stop progress group
        self.current.progress.group(Shows, 'matched:episodes').stop()
    def run(self):
        # TODO process seasons

        with elapsed.clock(Shows, 'run:shows'):
            # Process shows
            for sh_id, guid, p_show in self.p_shows:
                # Increment one step
                self.current.progress.group(Shows, 'shows').step()

                # Ensure `guid` is available
                if not guid or guid.service not in GUID_SERVICES:
                    mark_unsupported(self.p_shows_unsupported, sh_id, guid)
                    continue

                key = (guid.service, guid.id)

                # Try retrieve `pk` for `key`
                pk = self.trakt.table('shows').get(key)

                # Store in item map
                self.current.map.add(p_show.get('library_section'), sh_id, [key, pk])

                if pk is None:
                    # No `pk` found
                    continue

                # Iterate over changed data
                for key, result in self.trakt.changes:
                    media, data = key[0:2]

                    if media != SyncMedia.Shows:
                        # Ignore changes that aren't for episodes
                        continue

                    if data == SyncData.Watchlist:
                        # Ignore watchlist data
                        continue

                    if not self.is_data_enabled(data):
                        # Data type has been disabled
                        continue

                    data_name = Cache.Data.get(data)

                    if data_name not in result.changes:
                        # No changes for collection
                        continue

                    for action, shows in result.changes[data_name].items():
                        t_show = shows.get(pk)

                        if t_show is None:
                            # Unable to find matching show in trakt data
                            continue

                        # Execute show handlers
                        self.execute_handlers(
                            SyncMedia.Shows, data,
                            action=action,

                            key=sh_id,

                            p_item=p_show,
                            t_item=t_show
                        )

        # Stop progress group
        self.current.progress.group(Shows, 'shows').stop()

        with elapsed.clock(Shows, 'run:episodes'):
            # Process episodes
            for ids, guid, (season_num, episode_num), p_show, p_season, p_episode in self.p_episodes:
                # Increment one step
                self.current.progress.group(Shows, 'episodes').step()

                # Ensure `guid` is available
                if not guid or guid.service not in GUID_SERVICES:
                    mark_unsupported(self.p_shows_unsupported, ids['show'], guid)
                    continue

                key = (guid.service, guid.id)

                # Try retrieve `pk` for `key`
                pk = self.trakt.table('shows').get(key)

                if pk is None:
                    # No `pk` found
                    continue

                if not ids.get('episode'):
                    # Missing `episode` rating key
                    continue

                for key, result in self.trakt.changes:
                    media, data = key[0:2]

                    if media != SyncMedia.Episodes:
                        # Ignore changes that aren't for episodes
                        continue

                    if not self.is_data_enabled(data):
                        # Data type has been disabled
                        continue

                    data_name = Cache.Data.get(data)

                    if data_name not in result.changes:
                        # No changes for collection
                        continue

                    for action, shows in result.changes[data_name].items():
                        t_show = shows.get(pk)

                        if t_show is None:
                            # Unable to find matching show in trakt data
                            continue

                        t_season = t_show.get('seasons', {}).get(season_num)

                        if t_season is None:
                            # Unable to find matching season in `t_show`
                            continue

                        t_episode = t_season.get('episodes', {}).get(episode_num)

                        if t_episode is None:
                            # Unable to find matching episode in `t_season`
                            continue

                        self.execute_handlers(
                            SyncMedia.Episodes, data,
                            action=action,
                            key=ids['episode'],

                            p_item=p_episode,
                            t_item=t_episode
                        )

                # Task checkpoint
                self.checkpoint()

        # Stop progress group
        self.current.progress.group(Shows, 'episodes').stop()

        # Log details
        log_unsupported(log, 'Found %d unsupported show(s)\n%s', self.p_shows_unsupported)
    def flush(self):
        with elapsed.clock(SyncStatePlex, 'flush:matcher'):
            log.debug('Flushing matcher cache...')

            # Flush matcher cache to disk
            self.matcher_cache.flush(force=True)
    def flush(self):
        with elapsed.clock(SyncStatePlex, 'flush:matcher'):
            log.debug('Flushing matcher cache...')

            # Flush matcher cache to disk
            ModuleManager['matcher'].flush(force=True)
Exemple #6
0
    def two(cls):
        time.sleep(float(random.randint(0, 250)) / 1000)

        with elapsed.clock(Example, "two:sub"):
            for x in xrange(3):
                time.sleep(float(random.randint(0, 250)) / 1000)