<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals
import importlib
import pathlib
import logging
from typing import OrderedDict

from django.apps import AppConfig, apps
from django.conf import settings
from django.db.utils import OperationalError, ProgrammingError
from django.conf.urls import url, include
from django.urls import clear_url_caches
from django.contrib import admin

try:
    from importlib import metadata
except:
    import importlib_metadata as metadata

from plugin import plugins as inventree_plugins
from plugin.integration import IntegrationPluginBase

logger = logging.getLogger('inventree')


class PluginAppConfig(AppConfig):
    name = 'plugin'

    def ready(self):
        if not settings.INTEGRATION_PLUGINS_RELOADING:
            self._collect_plugins()
            self.load_plugins()

    # region public plugin functions
    def load_plugins(self):
        """load and activate all IntegrationPlugins"""
        logger.info('Start loading plugins')
        try:
            # we are using the db so for migrations etc we need to try this block
            self._init_plugins()
            self._activate_plugins()
        except (OperationalError, ProgrammingError):
            # Exception if the database has not been migrated yet
            logger.info('Database not accessible while loading plugins')
        logger.info('Finished loading plugins')

    def unload_plugins(self):
        """unload and deactivate all IntegrationPlugins"""
        logger.info('Start unloading plugins')
        # remove all plugins from registry
        # plugins = settings.INTEGRATION_PLUGINS
        settings.INTEGRATION_PLUGINS = {}
        # plugins_inactive = settings.INTEGRATION_PLUGINS_INACTIVE
        settings.INTEGRATION_PLUGINS_INACTIVE = {}

        # deactivate all integrations
        self._deactivate_plugins()
        logger.info('Finished unloading plugins')

    def reload_plugins(self):
        """safely reload IntegrationPlugins"""
        logger.info('Start reloading plugins')
        self.unload_plugins()
        self.load_plugins()
        logger.info('Finished reloading plugins')
    # endregion

    # region general plugin managment mechanisms
    def _collect_plugins(self):
        """collect integration plugins from all possible ways of loading"""
        # Collect plugins from paths
        for plugin in settings.PLUGIN_DIRS:
            modules = inventree_plugins.get_plugins(importlib.import_module(plugin), IntegrationPluginBase, True)
            if modules:
                [settings.PLUGINS.append(item) for item in modules]

        # check if running in testing mode and apps should be loaded from hooks
        if (not settings.PLUGIN_TESTING) or (settings.PLUGIN_TESTING and settings.PLUGIN_TESTING_SETUP):
            # Collect plugins from setup entry points
            for entry in metadata.entry_points().get('inventree_plugins', []):
                plugin = entry.load()
                plugin.is_package = True
                settings.PLUGINS.append(plugin)

        # Log found plugins
        logger.info(f'Found {len(settings.PLUGINS)} plugins!')
        logger.info(", ".join([a.__module__ for a in settings.PLUGINS]))<commit_msg>add todo regarding reload safety<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals
import importlib
import pathlib
import logging
from typing import OrderedDict

from django.apps import AppConfig, apps
from django.conf import settings
from django.db.utils import OperationalError, ProgrammingError
from django.conf.urls import url, include
from django.urls import clear_url_caches
from django.contrib import admin

try:
    from importlib import metadata
except:
    import importlib_metadata as metadata

from plugin import plugins as inventree_plugins
from plugin.integration import IntegrationPluginBase

logger = logging.getLogger('inventree')


class PluginAppConfig(AppConfig):
    name = 'plugin'

    def ready(self):
        if not settings.INTEGRATION_PLUGINS_RELOADING:
            self._collect_plugins()
            self.load_plugins()

    # region public plugin functions
    def load_plugins(self):
        """load and activate all IntegrationPlugins"""
        logger.info('Start loading plugins')
        try:
            # we are using the db so for migrations etc we need to try this block
            self._init_plugins()
            self._activate_plugins()
        except (OperationalError, ProgrammingError):
            # Exception if the database has not been migrated yet
            logger.info('Database not accessible while loading plugins')
        logger.info('Finished loading plugins')

    def unload_plugins(self):
        """unload and deactivate all IntegrationPlugins"""
        logger.info('Start unloading plugins')
        # remove all plugins from registry
        # plugins = settings.INTEGRATION_PLUGINS
        settings.INTEGRATION_PLUGINS = {}
        # plugins_inactive = settings.INTEGRATION_PLUGINS_INACTIVE
        settings.INTEGRATION_PLUGINS_INACTIVE = {}

        # deactivate all integrations
        self._deactivate_plugins()
        logger.info('Finished unloading plugins')

    def reload_plugins(self):
        """safely reload IntegrationPlugins"""
        # TODO check if the system is in maintainance mode before reloading
        logger.info('Start reloading plugins')
        self.unload_plugins()
        self.load_plugins()
        logger.info('Finished reloading plugins')
    # endregion

    # region general plugin managment mechanisms
    def _collect_plugins(self):
        """collect integration plugins from all possible ways of loading"""
        # Collect plugins from paths
        for plugin in settings.PLUGIN_DIRS:
            modules = inventree_plugins.get_plugins(importlib.import_module(plugin), IntegrationPluginBase, True)
            if modules:
                [settings.PLUGINS.append(item) for item in modules]

        # check if running in testing mode and apps should be loaded from hooks
        if (not settings.PLUGIN_TESTING) or (settings.PLUGIN_TESTING and settings.PLUGIN_TESTING_SETUP):
            # Collect plugins from setup entry points
            for entry in metadata.entry_points().get('inventree_plugins', []):
                plugin = entry.load()
                plugin.is_package = True
                settings.PLUGINS.append(plugin)

        # Log found plugins
        logger.info(f'Found {len(settings.PLUGINS)} plugins!')
        logger.info(", ".join([a.__module__ for a in settings.PLUGINS]))
{'fraction_non_alphanumeric': 0.0721336370539104, 'fraction_numerical': 0.00030372057706909645, 'mean_word_length': 2.9555555555555557, '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': '5290173', 'n_tokens_mistral': 1686, 'n_tokens_neox': 1654, 'n_words': 522}