<commit_before>"""Parser responsible for reading the ``*det<n>.m`` files"""

from warnings import warn
from six import iteritems
from numpy import empty

from serpentTools.utils import str2vec
from serpentTools.utils.compare import getKeyMatchingShapes
from serpentTools.engines import KeywordParser
from serpentTools.detectors import detectorFactory
from serpentTools.parsers.base import BaseReader
from serpentTools.messages import SerpentToolsException


# After py2 removal, subclass this from collections.abc.Mapping
# Gain full dictionary-like behavior by defining a few methods
class DetectorReader(BaseReader):
    """
    Parser responsible for reading and working with detector files.

    Parameters
    ----------
    filePath: str
        path to the depletion file

    Attributes
    ----------
    detectors : dict
        Dictionary where key, value pairs correspond to detector names
        and their respective :class:`~serpentTools.objects.detector.Detector`
        instances
    """

    def __init__(self, filePath):
        BaseReader.__init__(self, filePath, 'detector')
        self.detectors = {}

    def __getitem__(self, name):
        """Retrieve a detector from :attr:`detectors`"""
        return self.detectors[name]

    def __len__(self):
        return len(self.detectors)

    def __contains__(self, key):
        return key in self.detectors

    def __iter__(self):
        return iter(self.detectors)

    def iterDets(self):
        """Yield name, detector pairs by iterating over :attr:`detectors`."""
        for key, det in iteritems(self.detectors):
            yield key, det

    def _read(self):
        """Read the file and store the detectors."""
        currentName = ""
        grids = {}
        bins = None
        with KeywordParser(self.filePath, ["DET"], ["\n", "];"]) as parser:
            for chunk in parser.yieldChunks():
                name, data = cleanDetChunk(chunk)
                if currentName and name[:len(currentName)] != currentName:
                    self._processDet(currentName, bins, grids)
                    bins = data
                    grids = {}
                    currentName = name
                elif bins is None:
                    currentName = name
                    bins = data
                else:
                    gridName = name[len(currentName):]
                    grids[gridName] = data
            self._processDet(currentName, bins, grids)

    def _processDet(self, name, bins, grids):
        """Add this detector with it's grid data to the reader."""
        if self.settings['names'] and name in self.settings['names']:
            return
        if name in self.detectors:
            raise KeyError("Detector {} already stored on reader"
                           .format(name))
        self.detectors[name] = detectorFactory(name, bins, grids)

    def _precheck(self):
        """ Count how many detectors are in the file."""
        with open(self.filePath) as fh:
            for line in fh:
                sline = line.split()
                if not sline:
                    continue
                if 'DET' in sline[0]:<commit_msg>BUG Fix detector.names setting<commit_after>"""Parser responsible for reading the ``*det<n>.m`` files"""

from warnings import warn
from six import iteritems
from numpy import empty

from serpentTools.utils import str2vec
from serpentTools.utils.compare import getKeyMatchingShapes
from serpentTools.engines import KeywordParser
from serpentTools.detectors import detectorFactory
from serpentTools.parsers.base import BaseReader
from serpentTools.messages import SerpentToolsException


# After py2 removal, subclass this from collections.abc.Mapping
# Gain full dictionary-like behavior by defining a few methods
class DetectorReader(BaseReader):
    """
    Parser responsible for reading and working with detector files.

    Parameters
    ----------
    filePath: str
        path to the depletion file

    Attributes
    ----------
    detectors : dict
        Dictionary where key, value pairs correspond to detector names
        and their respective :class:`~serpentTools.objects.detector.Detector`
        instances
    """

    def __init__(self, filePath):
        BaseReader.__init__(self, filePath, 'detector')
        self.detectors = {}

    def __getitem__(self, name):
        """Retrieve a detector from :attr:`detectors`"""
        return self.detectors[name]

    def __len__(self):
        return len(self.detectors)

    def __contains__(self, key):
        return key in self.detectors

    def __iter__(self):
        return iter(self.detectors)

    def iterDets(self):
        """Yield name, detector pairs by iterating over :attr:`detectors`."""
        for key, det in iteritems(self.detectors):
            yield key, det

    def _read(self):
        """Read the file and store the detectors."""
        currentName = ""
        grids = {}
        bins = None
        with KeywordParser(self.filePath, ["DET"], ["\n", "];"]) as parser:
            for chunk in parser.yieldChunks():
                name, data = cleanDetChunk(chunk)
                if currentName and name[:len(currentName)] != currentName:
                    self._processDet(currentName, bins, grids)
                    bins = data
                    grids = {}
                    currentName = name
                elif bins is None:
                    currentName = name
                    bins = data
                else:
                    gridName = name[len(currentName):]
                    grids[gridName] = data
            self._processDet(currentName, bins, grids)

    def _processDet(self, name, bins, grids):
        """Add this detector with it's grid data to the reader."""
        if self.settings['names'] and name not in self.settings['names']:
            return
        if name in self.detectors:
            raise KeyError("Detector {} already stored on reader"
                           .format(name))
        self.detectors[name] = detectorFactory(name, bins, grids)

    def _precheck(self):
        """ Count how many detectors are in the file."""
        with open(self.filePath) as fh:
            for line in fh:
                sline = line.split()
                if not sline:
                    continue
                if 'DET' in sline[0]:
{'fraction_non_alphanumeric': 0.09211147850732168, 'fraction_numerical': 0.000944733112895607, 'mean_word_length': 2.0985365853658537, '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': '7227076', 'n_tokens_mistral': 1677, 'n_tokens_neox': 1566, 'n_words': 533}