<commit_before>import datetime
import fcntl
import logging
import math
import pickle
import queue
import subprocess
import time
from enum import Enum
from glob import glob
from multiprocessing import Process
from multiprocessing.connection import Listener
from pathlib import Path
from threading import Event, Thread
from typing import List, Optional

# --- setup dir for trigger related files --------------------------------------
trigger_dir = Path().home() / ".pegasus/triggers"
trigger_dir.mkdir(parents=True, exist_ok=True)

# --- messages -----------------------------------------------------------------
class _TriggerManagerMessageType(Enum):
    """Message types to be handled by TriggerManager"""

    STOP_TRIGGER = 1
    START_PATTERN_INTERVAL_TRIGGER = 2
    STATUS = 3


class TriggerManagerMessage:
    """Messages to be handled by TriggerManager"""

    STOP_TRIGGER = _TriggerManagerMessageType.STOP_TRIGGER
    START_PATTERN_INTERVAL_TRIGGER = (
        _TriggerManagerMessageType.START_PATTERN_INTERVAL_TRIGGER
    )
    STATUS = _TriggerManagerMessageType.STATUS

    def __init__(self, _type: _TriggerManagerMessageType, **kwargs):
        self._type = _type
        self.kwargs = kwargs

    def __str__(self):
        return "<TriggerManagerMessage msg_type={}, kwargs={}>".format(
            self._type, self.kwargs
        )


# --- manager ------------------------------------------------------------------
class TriggerManager(Process):
    """
    Manager to be spawned by the pegasus-em server process. It will listen for 
    commands (in the form of TriggerManagerMessage's) by a peagsus-em client. 
    Any triggers created will be managed by this class. 
    """

    def __init__(self):
        Process.__init__(self, daemon=True)

        self.log = logging.getLogger("trigger.manager")

        # messages to be consumed by worker
        self.mailbox = queue.Queue()

        self.dispatcher = _TriggerDispatcher(self.mailbox)

    def run(self):
        # TODO: make configurable
        address = ("localhost", 3000)
        self.log.info("starting and will listen on: {}".format(address))

        # start up trigger manager worker
        self.dispatcher.start()

        # TODO: change authkey
        with Listener(address, authkey=b"123") as listener:
            while True:
                self.log.debug("listening for new connection")
                with listener.accept() as conn:<commit_msg>PM-1607 switched trigger manager from process to thread to resolve issue when running with py38<commit_after>import datetime
import fcntl
import logging
import math
import pickle
import queue
import subprocess
import time
from enum import Enum
from glob import glob
from multiprocessing.connection import Listener
from pathlib import Path
from threading import Event, Thread
from typing import List, Optional

# --- setup dir for trigger related files --------------------------------------
trigger_dir = Path().home() / ".pegasus/triggers"
trigger_dir.mkdir(parents=True, exist_ok=True)

# --- messages -----------------------------------------------------------------
class _TriggerManagerMessageType(Enum):
    """Message types to be handled by TriggerManager"""

    STOP_TRIGGER = 1
    START_PATTERN_INTERVAL_TRIGGER = 2
    STATUS = 3


class TriggerManagerMessage:
    """Messages to be handled by TriggerManager"""

    STOP_TRIGGER = _TriggerManagerMessageType.STOP_TRIGGER
    START_PATTERN_INTERVAL_TRIGGER = (
        _TriggerManagerMessageType.START_PATTERN_INTERVAL_TRIGGER
    )
    STATUS = _TriggerManagerMessageType.STATUS

    def __init__(self, _type: _TriggerManagerMessageType, **kwargs):
        self._type = _type
        self.kwargs = kwargs

    def __str__(self):
        return "<TriggerManagerMessage msg_type={}, kwargs={}>".format(
            self._type, self.kwargs
        )


# --- manager ------------------------------------------------------------------
class TriggerManager(Thread):
    """
    Manager to be spawned by the pegasus-em server process. It will listen for 
    commands (in the form of TriggerManagerMessage's) by a peagsus-em client. 
    Any triggers created will be managed by this class. 
    """

    def __init__(self):
        Thread.__init__(self, daemon=True)

        self.log = logging.getLogger("trigger.manager")

        # messages to be consumed by worker
        self.mailbox = queue.Queue()
        self.dispatcher = _TriggerDispatcher(self.mailbox)

    def run(self):
        # TODO: make configurable
        address = ("localhost", 3000)
        self.log.info("starting and will listen on: {}".format(address))

        # start up trigger manager worker
        self.dispatcher.start()

        # TODO: change authkey
        with Listener(address, authkey=b"123") as listener:
            while True:
                self.log.debug("listening for new connection")
                with listener.accept() as conn:
{'fraction_non_alphanumeric': 0.14418981950922732, 'fraction_numerical': 0.005272764145203813, 'mean_word_length': 2.394356503785272, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4554900', 'n_tokens_mistral': 1321, 'n_tokens_neox': 1238, 'n_words': 395}