<commit_before>"""
The custom manager for Scripts.
"""

from django.db.models import Q
from evennia.typeclasses.managers import TypedObjectManager, TypeclassManager
from evennia.typeclasses.managers import returns_typeclass_list
from evennia.utils.utils import make_iter
__all__ = ("ScriptManager",)
_GA = object.__getattribute__

VALIDATE_ITERATION = 0


class ScriptDBManager(TypedObjectManager):
    """
    This Scriptmanager implements methods for searching
    and manipulating Scripts directly from the database.

    Evennia-specific search methods (will return Typeclasses or
    lists of Typeclasses, whereas Django-general methods will return
    Querysets or database objects).

    dbref (converter)
    get_id  (or dbref_search)
    get_dbref_range
    object_totals
    typeclass_search
    get_all_scripts_on_obj
    get_all_scripts
    delete_script
    remove_non_persistent
    validate
    script_search (equivalent to evennia.search_script)
    copy_script

    """
    @returns_typeclass_list
    def get_all_scripts_on_obj(self, obj, key=None):
        """
        Find all Scripts related to a particular object.

        Args:
            obj (Object): Object whose Scripts we are looking for.
            key (str, optional): Script identifier - can be given as a
                dbref or name string. If given, only scripts matching the
                key on the object will be returned.
        Returns:
            matches (list): Matching scripts.

        """
        if not obj:
            return []
        player = _GA(_GA(obj, "__dbclass__"), "__name__") == "PlayerDB"
        if key:
            dbref = self.dbref(key)
            if dbref or dbref == 0:
                if player:
                    return self.filter(db_player=obj, id=dbref)
                else:
                    return self.filter(db_obj=obj, id=dbref)
            elif player:
                return self.filter(db_player=obj, db_key=key)
            else:
                return self.filter(db_obj=obj, db_key=key)
        elif player:
            return self.filter(db_player=obj)
        else:
            return self.filter(db_obj=obj)

    @returns_typeclass_list
    def get_all_scripts(self, key=None):
        """
        Get all scripts in the database.

        Args:
            key (str, optional): Restrict result to only those
                with matching key or dbref.

        Returns:
            scripts (list): All scripts found, or those matching `key`.

        """
        if key:
            script = []
            dbref = self.dbref(key)
            if dbref or dbref == 0:
                script = [self.dbref_search(dbref)]
            if not script:
                script = self.filter(db_key=key)
            return script
        return self.all()

    def delete_script(self, dbref):
        """
        This stops and deletes a specific script directly from the
        script database.

        Args:
            dbref (int): Database unique id.

        Notes:
            This might be needed for global scripts not tied to a
            specific game object

        """
        scripts = self.get_id(dbref)
        for script in make_iter(scripts):
            script.stop()

    def remove_non_persistent(self, obj=None):
        """
        This cleans up the script database of all non-persistent
        scripts. It is called every time the server restarts.
<commit_msg>Return an empty list when searching for a script by #dbref and there is no such script.<commit_after>"""
The custom manager for Scripts.
"""

from django.db.models import Q
from evennia.typeclasses.managers import TypedObjectManager, TypeclassManager
from evennia.typeclasses.managers import returns_typeclass_list
from evennia.utils.utils import make_iter
__all__ = ("ScriptManager",)
_GA = object.__getattribute__

VALIDATE_ITERATION = 0


class ScriptDBManager(TypedObjectManager):
    """
    This Scriptmanager implements methods for searching
    and manipulating Scripts directly from the database.

    Evennia-specific search methods (will return Typeclasses or
    lists of Typeclasses, whereas Django-general methods will return
    Querysets or database objects).

    dbref (converter)
    get_id  (or dbref_search)
    get_dbref_range
    object_totals
    typeclass_search
    get_all_scripts_on_obj
    get_all_scripts
    delete_script
    remove_non_persistent
    validate
    script_search (equivalent to evennia.search_script)
    copy_script

    """
    @returns_typeclass_list
    def get_all_scripts_on_obj(self, obj, key=None):
        """
        Find all Scripts related to a particular object.

        Args:
            obj (Object): Object whose Scripts we are looking for.
            key (str, optional): Script identifier - can be given as a
                dbref or name string. If given, only scripts matching the
                key on the object will be returned.
        Returns:
            matches (list): Matching scripts.

        """
        if not obj:
            return []
        player = _GA(_GA(obj, "__dbclass__"), "__name__") == "PlayerDB"
        if key:
            dbref = self.dbref(key)
            if dbref or dbref == 0:
                if player:
                    return self.filter(db_player=obj, id=dbref)
                else:
                    return self.filter(db_obj=obj, id=dbref)
            elif player:
                return self.filter(db_player=obj, db_key=key)
            else:
                return self.filter(db_obj=obj, db_key=key)
        elif player:
            return self.filter(db_player=obj)
        else:
            return self.filter(db_obj=obj)

    @returns_typeclass_list
    def get_all_scripts(self, key=None):
        """
        Get all scripts in the database.

        Args:
            key (str, optional): Restrict result to only those
                with matching key or dbref.

        Returns:
            scripts (list): All scripts found, or those matching `key`.

        """
        if key:
            script = []
            dbref = self.dbref(key)
            if dbref or dbref == 0:
                script = filter(None, [self.dbref_search(dbref)])
            if not script:
                script = self.filter(db_key=key)
            return script
        return self.all()

    def delete_script(self, dbref):
        """
        This stops and deletes a specific script directly from the
        script database.

        Args:
            dbref (int): Database unique id.

        Notes:
            This might be needed for global scripts not tied to a
            specific game object

        """
        scripts = self.get_id(dbref)
        for script in make_iter(scripts):
            script.stop()

    def remove_non_persistent(self, obj=None):
        """
        This cleans up the script database of all non-persistent
        scripts. It is called every time the server restarts.

{'fraction_non_alphanumeric': 0.06924848833861215, 'fraction_numerical': 0.0008638065073423554, 'mean_word_length': 2.0086617583369426, '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': '4594963', 'n_tokens_mistral': 1892, 'n_tokens_neox': 1833, 'n_words': 613}