<commit_before>"""
od_task.py
Core component for OneDrive server-client interaction management.
:copyright: (c) <NAME> <<EMAIL>>
:license: MIT
"""

import logging
import threading


class TaskPool:
    """
    An in-memory storage for tasks.
    """

    def __init__(self):
        self.tasks_by_path = {}
        self.queued_tasks = []
        self.semaphore = threading.Semaphore(0)
        self._lock = threading.Lock()

    def close(self, n=1):
        for i in range(n):
            self.semaphore.release()

    def add_task(self, task):
        """
        Add a task to internal storage. It will not add if there is already a task on the path.
        :param onedrived.tasks.base.TaskBase task: The task to add.
        """
        logging.debug('Adding task %s...' % task)
        with self._lock:
            if task.local_abspath in self.tasks_by_path:
                return False
            self.queued_tasks.append(task)
            self.tasks_by_path[task.local_abspath] = task
        self.semaphore.release()
        return True

    def pop_task(self):
        """
        Pop the oldest task. It's required that the caller first acquire the semaphore.
        :return onedrived.tasks.base.TaskBase | None: The first qualified task, or None.
        """
        # logging.debug('Getting task...')
        with self._lock:
            ret = None
            if len(self.queued_tasks):
                ret = self.queued_tasks.pop(0)
                del self.tasks_by_path[ret.local_abspath]
            return ret

    @property
    def outstanding_task_count(self):
        with self._lock:
            return len(self.queued_tasks)

    def has_pending_task(self, local_path):
        with self._lock:
            return local_path in self.tasks_by_path

    def remove_children_tasks(self, local_parent_path):
        with self._lock:
            for t in self.queued_tasks[:]:
                if t.local_abspath.startswith(local_parent_path):
                    self.queued_tasks.remove(t)
                    del self.tasks_by_path[t.local_abspath]
<commit_msg>[task] A note about an unsuccessful attempt (working path set).<commit_after>"""
od_task.py
Core component for OneDrive server-client interaction management.
:copyright: (c) <NAME> <<EMAIL>>
:license: MIT
"""

import logging
import threading


class TaskPool:
    """
    An in-memory storage for tasks.

    Some notes:
      (1) Tried to let worker threads and inotify watcher communicate by reading/writing a "working path set" but
          because workers tend to delete path before watcher can read it.
    """

    def __init__(self):
        self.tasks_by_path = {}
        self.queued_tasks = []
        self.semaphore = threading.Semaphore(0)
        self._lock = threading.Lock()

    def close(self, n=1):
        for i in range(n):
            self.semaphore.release()

    def add_task(self, task):
        """
        Add a task to internal storage. It will not add if there is already a task on the path.
        :param onedrived.tasks.base.TaskBase task: The task to add.
        """
        logging.debug('Adding task %s...' % task)
        with self._lock:
            if task.local_abspath in self.tasks_by_path:
                return False
            self.queued_tasks.append(task)
            self.tasks_by_path[task.local_abspath] = task
        self.semaphore.release()
        return True

    def pop_task(self):
        """
        Pop the oldest task. It's required that the caller first acquire the semaphore.
        :return onedrived.tasks.base.TaskBase | None: The first qualified task, or None.
        """
        # logging.debug('Getting task...')
        with self._lock:
            ret = None
            if len(self.queued_tasks):
                ret = self.queued_tasks.pop(0)
                del self.tasks_by_path[ret.local_abspath]
            return ret

    @property
    def outstanding_task_count(self):
        with self._lock:
            return len(self.queued_tasks)

    def has_pending_task(self, local_path):
        with self._lock:
            return local_path in self.tasks_by_path

    def remove_children_tasks(self, local_parent_path):
        with self._lock:
            for t in self.queued_tasks[:]:
                if t.local_abspath.startswith(local_parent_path):
                    self.queued_tasks.remove(t)
                    del self.tasks_by_path[t.local_abspath]

{'fraction_non_alphanumeric': 0.09170996159927716, 'fraction_numerical': 0.001581206234470296, 'mean_word_length': 2.0664819944598336, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3265619', 'n_tokens_mistral': 1329, 'n_tokens_neox': 1289, 'n_words': 353}