<commit_before># This code is licensed under the 3-Clause BSD License.

import os
import threading
import urllib.parse

from gi.repository import Gtk, GLib

import quodlibet
from quodlibet import _
from quodlibet import app, qltk
from quodlibet.plugins.events import EventPlugin
from quodlibet.plugins import PluginConfig
from quodlibet.qltk import Icons
from quodlibet.qltk.msg import Message
from quodlibet.util.dprint import print_d
from quodlibet.errorreport import errorhook
from quodlibet import commands

LOGGING_IS_ENABLED = False

class QlqwError(RuntimeError):
    pass

class QlqwBackend:
    """Provides threaded I/O for queue writing."""

    FILE_PREFIX = "file://"
    TARGET_PATH = os.path.join(quodlibet.get_user_dir(), "qlqw.txt")

    def __init__(self):
        self.called_event = threading.Event()
        self.last_queue_hash = None

    def parse_queue(self, dumped_queue_string):
        parsed = list()

        for line in urllib.parse.unquote(dumped_queue_string).splitlines():
            if not line.startswith(self.FILE_PREFIX):
                raise QlqwError("unexpected queue entry: ``{}''".format(line))

            path = line[len(self.FILE_PREFIX):]
            if not os.path.exists(path):
                raise QlqwError("nonexistent queue entry: ``{}''".format(path))

            parsed.append(path);

        return parsed

    def get_queue(self):
        dumped_string = commands.registry.run(app, "dump-queue")
        queue_hash = hash(dumped_string)
        if queue_hash == self.last_queue_hash:
            return None

        self.last_queue_hash = queue_hash
        return self.parse_queue(dumped_string)

    def commit_queue(self, queue_list):
        with open(self.TARGET_PATH, "w") as qfp:
            for line in queue_list:
                qfp.write(line)
                qfp.write("\n")

    def get_and_commit_queue(self):
        queue_list = self.get_queue()
        if queue_list is not None:
            self.commit_queue(queue_list)

    def fire(self):
        """
        Arms this backend to write the queue. Called from the main
        plugin context.
        """
        self.called_event.set()

    def _run_loop(self):
        self.called_event.wait()
        self.called_event.clear()
        self.get_and_commit_queue()<commit_msg>qlqw: re-address empty queue case<commit_after># This code is licensed under the 3-Clause BSD License.

import os
import threading
import urllib.parse

from gi.repository import Gtk, GLib

import quodlibet
from quodlibet import _
from quodlibet import app, qltk
from quodlibet.plugins.events import EventPlugin
from quodlibet.plugins import PluginConfig
from quodlibet.qltk import Icons
from quodlibet.qltk.msg import Message
from quodlibet.util.dprint import print_d
from quodlibet.errorreport import errorhook
from quodlibet import commands

LOGGING_IS_ENABLED = False

class QlqwError(RuntimeError):
    pass

class QlqwBackend:
    """Provides threaded I/O for queue writing."""

    FILE_PREFIX = "file://"
    TARGET_PATH = os.path.join(
            os.getenv("XDG_RUNTIME_DIR", default=quodlibet.get_user_dir()),
            "qlqw.txt"
    )

    def __init__(self):
        self.called_event = threading.Event()
        self.last_queue_hash = None

    def parse_queue(self, dumped_queue_string):
        parsed = list()

        for line in urllib.parse.unquote(dumped_queue_string).splitlines():
            if not line.startswith(self.FILE_PREFIX):
                raise QlqwError("unexpected queue entry: ``{}''".format(line))

            path = line[len(self.FILE_PREFIX):]
            if not os.path.exists(path):
                raise QlqwError("nonexistent queue entry: ``{}''".format(path))

            parsed.append(path);

        return parsed

    def get_queue(self):
        dumped_string = commands.registry.run(app, "dump-queue")
        queue_hash = hash(dumped_string)
        if not dumped_string or queue_hash == self.last_queue_hash:
            return None

        self.last_queue_hash = queue_hash
        return self.parse_queue(dumped_string)

    def commit_queue(self, queue_list):
        with open(self.TARGET_PATH, "w") as qfp:
            for line in queue_list:
                qfp.write(line)
                qfp.write("\n")

    def get_and_commit_queue(self):
        queue_list = self.get_queue()
        if queue_list is not None:
            self.commit_queue(queue_list)

    def fire(self):
        """
        Arms this backend to write the queue. Called from the main
        plugin context.
        """
        self.called_event.set()

    def _run_loop(self):
        self.called_event.wait()
        self.called_event.clear()
        self.get_and_commit_queue()
{'fraction_non_alphanumeric': 0.09033213454622382, 'fraction_numerical': 0.0004231013327691982, 'mean_word_length': 2.6736596736596736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4161569', 'n_tokens_mistral': 1493, 'n_tokens_neox': 1456, 'n_words': 356}