<commit_before>from gi.repository import GLib, Gio, GObject
from hashlib import sha256
import random
from . import logger, _

ACCEPTED = ['application/xml', 'image/jpeg', 'image/png']


class Profile(GObject.GObject):
    __gsignals__ = {
        'preview': (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING, ))
    }

    def __init__(self, name, settings, auto_load=True):
        logger.debug('created new profile %s', name)
        GObject.GObject.__init__(self)
        self._name = name
        self._settings = settings
        # initialize variables
        self._hash = None
        self._history = []
        self._monitors = []
        self._position = 0
        self._queue = []
        self._wallpapers = []
        if auto_load:
            self.load()
        self._handler_profiles = self._settings.connect('changed::profiles', self._changed_profiles)
        self._handler_random = self._settings.connect('changed::random', self._changed_random)

    def __repr__(self):
        return '%s(name=%s)' % (self.__class__.__name__, self.name)

    def emit(self, signal, *args):
        logger.debug('%s::%s %s', str(self), signal, str(args))
        GObject.GObject.emit(self, signal, *args)

    @GObject.Property(type=GObject.TYPE_STRV)
    def history(self):
        """
        
        :return: Current history of profile
        :rtype: list
        """
        return self._history

    def load(self):
        logger.debug('loading profile %s', self._name)
        # First clear everything out
        self._remove_monitors()
        self._history = []
        self._position = 0
        self._queue = []
        self._wallpapers = []
        # Now grab the
        items = self._settings.get_value('profiles').unpack().get(self._name)
        self._hash = sha256(str(items).encode('utf-8'))
        if items is None:
            logger.critical('failed to load profile %s because it does not exist', self._name)
            return False
        for (uri, recursive) in items:
            logger.debug('loading %s for profile %s%s', uri, self._name, ' recursively' if recursive else '')
            try:
                location = Gio.File.new_for_uri(uri)
                self._load_profile_location(location, recursive, False, True)
            except GLib.Error as e:
                logger.warning('failed to load %s for profile %s: %s', uri, self._name, str(e.args))
        if len(self._wallpapers) == 0:
            logger.critical('no wallpapers were loaded for profile %s - wallpaper will not change', self._name)
            raise NoWallpapersError(self)
        if len(self._wallpapers) < 100:
            logger.warning('available total wallpapers is under 100 (%d) - strict random checking is disabled',
                           len(self._wallpapers))
        self._wallpapers.sort()
        if self._settings.get_boolean('remember-profile-state'):
            self.restore_state()
        self._load_next()
        logger.info('profile %s has been loaded with %d wallpapers', self._name, len(self._wallpapers))
        self.emit('preview', self._queue[0])

    @GObject.Property(type=str)
    def name(self):
        return self._name

    def next(self, current=None):
        if len(self._wallpapers) == 0:
            logger.critical('no wallpapers are currently available for %s', self.name)
            raise NoWallpapersError(self)
        wallpaper = self._queue.pop(0)
        if current:
            self._history.append(current)
        if not self._settings.get_boolean('random'):
            self._position += 1
        self._load_next(wallpaper)
        self.emit('preview', self._queue[0])
        return wallpaper

    def prev(self, current=None):
        if len(self._wallpapers) == 0:
            logger.critical('no wallpapers are currently available for %s', self.name)
            raise NoWallpapersError(self)
        if len(self._history) == 0:
            return False
        wallpaper = self._history.pop(0)
        if current:
            self._queue.insert(0, current)
        self.emit('preview', self._queue[0])
        return wallpaper

    @GObject.Property(type=GObject.TYPE_STRV)
    def queue(self):
        """
        
        :return: Current queue of profile
        :rtype: list
        """
        return self._queue

    def release(self):
        logger.debug('destroying profile %s', self._name)
        self._remove_monitors()
        self._settings.disconnect(self._handler_profiles)
        self._settings.disconnect(self._handler_random)

    def restore_state(self):
        logger.info('restoring state of profile %s', self.name)
        states = dict(self._settings.get_value('profile-state').unpack())
        if self.name not in states:
            logger.debug('no previous state for %s', self.name)
            return
        self._queue = list(states[self.name])
        if not self._settings.get_boolean('random'):
            self._position = self._wallpapers.index(self._queue[len(self._queue) - 1])
        del states[self.name]<commit_msg>Fixing how history is recalled... oops<commit_after>from gi.repository import GLib, Gio, GObject
from hashlib import sha256
import random
from . import logger, _

ACCEPTED = ['application/xml', 'image/jpeg', 'image/png']


class Profile(GObject.GObject):
    __gsignals__ = {
        'preview': (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING, ))
    }

    def __init__(self, name, settings, auto_load=True):
        logger.debug('created new profile %s', name)
        GObject.GObject.__init__(self)
        self._name = name
        self._settings = settings
        # initialize variables
        self._hash = None
        self._history = []
        self._monitors = []
        self._position = 0
        self._queue = []
        self._wallpapers = []
        if auto_load:
            self.load()
        self._handler_profiles = self._settings.connect('changed::profiles', self._changed_profiles)
        self._handler_random = self._settings.connect('changed::random', self._changed_random)

    def __repr__(self):
        return '%s(name=%s)' % (self.__class__.__name__, self.name)

    def emit(self, signal, *args):
        logger.debug('%s::%s %s', str(self), signal, str(args))
        GObject.GObject.emit(self, signal, *args)

    @GObject.Property(type=GObject.TYPE_STRV)
    def history(self):
        """
        
        :return: Current history of profile
        :rtype: list
        """
        return self._history

    def load(self):
        logger.debug('loading profile %s', self._name)
        # First clear everything out
        self._remove_monitors()
        self._history = []
        self._position = 0
        self._queue = []
        self._wallpapers = []
        # Now grab the
        items = self._settings.get_value('profiles').unpack().get(self._name)
        self._hash = sha256(str(items).encode('utf-8'))
        if items is None:
            logger.critical('failed to load profile %s because it does not exist', self._name)
            return False
        for (uri, recursive) in items:
            logger.debug('loading %s for profile %s%s', uri, self._name, ' recursively' if recursive else '')
            try:
                location = Gio.File.new_for_uri(uri)
                self._load_profile_location(location, recursive, False, True)
            except GLib.Error as e:
                logger.warning('failed to load %s for profile %s: %s', uri, self._name, str(e.args))
        if len(self._wallpapers) == 0:
            logger.critical('no wallpapers were loaded for profile %s - wallpaper will not change', self._name)
            raise NoWallpapersError(self)
        if len(self._wallpapers) < 100:
            logger.warning('available total wallpapers is under 100 (%d) - strict random checking is disabled',
                           len(self._wallpapers))
        self._wallpapers.sort()
        if self._settings.get_boolean('remember-profile-state'):
            self.restore_state()
        self._load_next()
        logger.info('profile %s has been loaded with %d wallpapers', self._name, len(self._wallpapers))
        self.emit('preview', self._queue[0])

    @GObject.Property(type=str)
    def name(self):
        return self._name

    def next(self, current=None):
        if len(self._wallpapers) == 0:
            logger.critical('no wallpapers are currently available for %s', self.name)
            raise NoWallpapersError(self)
        wallpaper = self._queue.pop(0)
        if current:
            self._history.append(current)
        if not self._settings.get_boolean('random'):
            self._position += 1
        self._load_next(wallpaper)
        self.emit('preview', self._queue[0])
        return wallpaper

    def prev(self, current=None):
        if len(self._wallpapers) == 0:
            logger.critical('no wallpapers are currently available for %s', self.name)
            raise NoWallpapersError(self)
        if len(self._history) == 0:
            return False
        wallpaper = self._history.pop()
        if current:
            self._queue.insert(0, current)
        self.emit('preview', self._queue[0])
        return wallpaper

    @GObject.Property(type=GObject.TYPE_STRV)
    def queue(self):
        """
        
        :return: Current queue of profile
        :rtype: list
        """
        return self._queue

    def release(self):
        logger.debug('destroying profile %s', self._name)
        self._remove_monitors()
        self._settings.disconnect(self._handler_profiles)
        self._settings.disconnect(self._handler_random)

    def restore_state(self):
        logger.info('restoring state of profile %s', self.name)
        states = dict(self._settings.get_value('profile-state').unpack())
        if self.name not in states:
            logger.debug('no previous state for %s', self.name)
            return
        self._queue = list(states[self.name])
        if not self._settings.get_boolean('random'):
            self._position = self._wallpapers.index(self._queue[len(self._queue) - 1])
        del states[self.name]
{'fraction_non_alphanumeric': 0.11636399526253455, 'fraction_numerical': 0.005230951440979076, 'mean_word_length': 2.3125204315135663, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2273189', 'n_tokens_mistral': 2950, 'n_tokens_neox': 2829, 'n_words': 680}