<commit_before>from .exceptions import LemonpyError

import uuid


class DuplicateBarId(LemonpyError):
    """
    The requested bar ID (name, PID, etc) already exists
    """
    pass


class AlreadyManaged(LemonpyError):
    """
    The lemonbar instance is already managed by a LemonbarServer
    """
    pass


class LemonbarServer(object):
    def __init__(self,):
        # Map of local IDs to actual lemonbar instances
        self._bar_map = dict()

        # Map of user provided names to local IDs
        self._name_map = dict()

    def close(self, close_owned_bars=True):
        for _, bar in self._bar_map.items():
            bar.close()

    def close_bar(self, name):
        bar = self._bar_from_name(name)
        bar.close()

    def _bar_from_name(self, name):
        bar = self._bar_map.get(self._name_map.get(name))
        if bar is None:
            raise LemonpyError('{} is not a managed lemonbar name')
        return bar

    def register(self, name, bar):
        """
        Register a lemonbar instance with this server.
        """
        # May want to register by name, pid, or otherwise, so use separate maps in case those
        # namespaces conflict
        if self._key_exists(name, self._name_map):
            raise DuplicateBarId('A lemonbar with the name {} is already being managed'.format(name))

        local_id = uuid.uuid4()
        self._name_map.update({name: local_id})

        self._register_by_id(local_id, bar)

    def _register_by_id(self, local_id, bar):
        if self._bar_is_managed(bar):
            raise AlreadyManaged('Lemonbar instance is already managed (pid: {})'.format(bar.bar_pid))

        self._bar_map.update({local_id: bar})
        bar.managed = True

    def _key_exists(self, key, dictionary):
        return True if dictionary.get(key, None) is not None else False
<commit_msg>Add update_bar method to LemonbarServer<commit_after>from .exceptions import LemonpyError

import uuid


class DuplicateBarId(LemonpyError):
    """
    The requested bar ID (name, PID, etc) already exists
    """
    pass


class AlreadyManaged(LemonpyError):
    """
    The lemonbar instance is already managed by a LemonbarServer
    """
    pass


class LemonbarServer(object):
    def __init__(self,):
        # Map of local IDs to actual lemonbar instances
        self._bar_map = dict()

        # Map of user provided names to local IDs
        self._name_map = dict()

    def close(self, close_owned_bars=True):
        for _, bar in self._bar_map.items():
            bar.close()

    def close_bar(self, name):
        bar = self._bar_from_name(name)
        bar.close()

    def _bar_from_name(self, name):
        bar = self._bar_map.get(self._name_map.get(name))
        if bar is None:
            raise LemonpyError('{} is not a managed lemonbar name')
        return bar

    def update_bar(self, name, content):
        bar = self._bar_from_name(name)
        bar.update(content)

    def register(self, name, bar):
        """
        Register a lemonbar instance with this server.
        """
        # May want to register by name, pid, or otherwise, so use separate maps in case those
        # namespaces conflict
        if self._key_exists(name, self._name_map):
            raise DuplicateBarId('A lemonbar with the name {} is already being managed'.format(name))

        local_id = uuid.uuid4()
        self._name_map.update({name: local_id})

        self._register_by_id(local_id, bar)

    def _register_by_id(self, local_id, bar):
        if self._bar_is_managed(bar):
            raise AlreadyManaged('Lemonbar instance is already managed (pid: {})'.format(bar.bar_pid))

        self._bar_map.update({local_id: bar})
        bar.managed = True

    def _key_exists(self, key, dictionary):
        return True if dictionary.get(key, None) is not None else False

{'fraction_non_alphanumeric': 0.09383100051840332, 'fraction_numerical': 0.0005184033177812338, 'mean_word_length': 2.579777365491651, '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': '3466364', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1119, 'n_words': 345}