<commit_before>import os

from twisted.internet.defer import inlineCallbacks

from battlesnake.conf import settings
from battlesnake.core.ansi import ANSI_NORMAL, \
    ANSI_HI_GREEN, ANSI_HI_YELLOW, ANSI_HI_RED, ANSI_HI_MAGENTA
from battlesnake.core.inbound_command_handling.base import BaseCommand, \
    CommandError
from battlesnake.core.inbound_command_handling.btargparse import \
    BTMuxArgumentParser
from battlesnake.core.inbound_command_handling.command_table import \
    InboundCommandTable
from battlesnake.outbound_commands import mux_commands

from battlesnake.plugins.contrib.unit_library.api import get_unit_by_ref, \
    get_library_summary_list
from battlesnake.plugins.contrib.unit_library.outbound_commands import \
    load_ref_in_templater
from battlesnake.plugins.contrib.unit_library.unit_scanning.api import \
    scan_unit_from_templater


class ScanUnitCommand(BaseCommand):
    """
    Scans a unit into the unit library.
    """

    command_name = "ul_scanunit"

    @inlineCallbacks
    def run(self, protocol, parsed_line, invoker_dbref):
        reference = parsed_line.kwargs['reference']
        yield self._scan_unit(protocol, invoker_dbref, reference)

    @inlineCallbacks
    def _scan_unit(self, protocol, invoker_dbref, reference):
        templater_dbref = settings['unit_library']['templater_dbref']
        pval = "Re-loading %s in templater..." % reference
        yield mux_commands.remit(protocol, templater_dbref, pval)
        yield load_ref_in_templater(protocol, reference)
        yield scan_unit_from_templater(protocol, invoker_dbref)
        yield mux_commands.remit(
            protocol, templater_dbref,
            "Finished scanning %s" % reference)


class ScanLibraryCommand(ScanUnitCommand):
    """
    Scans all template files in the mechs directory into the template library.
    """

    command_name = "ul_scanlibrary"

    @inlineCallbacks
    def run(self, protocol, parsed_line, invoker_dbref):
        mechs_dir_path = settings['unit_library']['mechs_dir_path']
        templater_dbref = settings['unit_library']['templater_dbref']

        for template_file in os.listdir(mechs_dir_path):
            full_path = os.path.join(mechs_dir_path, template_file)
            if template_file == 'Mechwarrior' or template_file.startswith('.'):
                continue
            if not os.path.isfile(full_path):
                continue
            yield mux_commands.remit(
                protocol, templater_dbref,
                "Attempting to scan %s" % template_file)
            yield self._scan_unit(protocol, invoker_dbref, template_file)


class LoadUnitCommand(BaseCommand):
    """
    Loads a unit into the templater.
    """<commit_msg>Bandaid to prevent scanning hidden refs for now.<commit_after>import os

from twisted.internet.defer import inlineCallbacks

from battlesnake.conf import settings
from battlesnake.core.ansi import ANSI_NORMAL, \
    ANSI_HI_GREEN, ANSI_HI_YELLOW, ANSI_HI_RED, ANSI_HI_MAGENTA
from battlesnake.core.inbound_command_handling.base import BaseCommand, \
    CommandError
from battlesnake.core.inbound_command_handling.btargparse import \
    BTMuxArgumentParser
from battlesnake.core.inbound_command_handling.command_table import \
    InboundCommandTable
from battlesnake.outbound_commands import mux_commands

from battlesnake.plugins.contrib.unit_library.api import get_unit_by_ref, \
    get_library_summary_list
from battlesnake.plugins.contrib.unit_library.outbound_commands import \
    load_ref_in_templater
from battlesnake.plugins.contrib.unit_library.unit_scanning.api import \
    scan_unit_from_templater


class ScanUnitCommand(BaseCommand):
    """
    Scans a unit into the unit library.
    """

    command_name = "ul_scanunit"

    @inlineCallbacks
    def run(self, protocol, parsed_line, invoker_dbref):
        reference = parsed_line.kwargs['reference']
        yield self._scan_unit(protocol, invoker_dbref, reference)

    @inlineCallbacks
    def _scan_unit(self, protocol, invoker_dbref, reference):
        templater_dbref = settings['unit_library']['templater_dbref']
        pval = "Re-loading %s in templater..." % reference
        yield mux_commands.remit(protocol, templater_dbref, pval)
        yield load_ref_in_templater(protocol, reference)
        yield scan_unit_from_templater(protocol, invoker_dbref)
        yield mux_commands.remit(
            protocol, templater_dbref,
            "Finished scanning %s" % reference)


class ScanLibraryCommand(ScanUnitCommand):
    """
    Scans all template files in the mechs directory into the template library.
    """

    command_name = "ul_scanlibrary"

    @inlineCallbacks
    def run(self, protocol, parsed_line, invoker_dbref):
        mechs_dir_path = settings['unit_library']['mechs_dir_path']
        templater_dbref = settings['unit_library']['templater_dbref']
        # TODO: Import these but make them hidden in the DB.
        skipped_refs = [
            'MechWarrior', 'RadioTower',
        ]

        for template_file in os.listdir(mechs_dir_path):
            full_path = os.path.join(mechs_dir_path, template_file)
            if template_file in skipped_refs or template_file.startswith('.'):
                continue
            if not os.path.isfile(full_path):
                continue
            yield mux_commands.remit(
                protocol, templater_dbref,
                "Attempting to scan %s" % template_file)
            yield self._scan_unit(protocol, invoker_dbref, template_file)


class LoadUnitCommand(BaseCommand):
    """
    Loads a unit into the templater.
    """
{'fraction_non_alphanumeric': 0.075663165390778, 'fraction_numerical': 0.0, 'mean_word_length': 3.497998398718975, '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': '5525360', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1686, 'n_words': 355}