<commit_before># Purpose: auditor module
# Created: 10.03.2017
# Copyright (C) 2017, <NAME>
# License: MIT License
"""
audit(drawing, stream): check a DXF drawing for errors.
"""
from __future__ import unicode_literals
import sys
from ezdxf.lldxf.types import is_pointer_code
from ezdxf.lldxf.const import Error
from ezdxf.lldxf.validator import is_valid_layer_name, is_adsk_special_layer
from ezdxf.dxfentity import DXFEntity

REQUIRED_ROOT_DICT_ENTRIES = ('ACAD_GROUP', 'ACAD_PLOTSTYLENAME')


class ErrorEntry(object):
    def __init__(self, code, message='', dxf_entity=None, data=None):
        self.code = code
        self.entity = dxf_entity
        self.message = message
        self.data = data


def target_pointers(tags):
    for tag in tags:
        if is_pointer_code(tag.code):
            yield tag


class Auditor(object):
    def __init__(self, drawing):
        self.drawing = drawing
        self.errors = []
        self.undefined_targets = set()

    def reset(self):
        self.errors = []
        self.undefined_targets = set()

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

    def __bool__(self):
        return self.__len__() > 0

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

    def print_report(self, errors=None, stream=None):
        def entity_str(count, code, entity):
            if entity is not None:
                return "{:4d}. Issue [{}] in {} #{}".format(count, code, entity.dxftype(), entity.dxf.handle)
            else:
                return "{:4d}. Issue [{}]".format(count, code)

        if errors is None:
            errors = self.errors
        else:
            errors = list(errors)  # generator?

        if stream is None:
            stream = sys.stdout

        if len(errors) == 0:
            stream.write('No issues found.\n\n')
        else:
            stream.write('{} issues found.\n\n'.format(len(errors)))
            for count, error in enumerate(errors):
                stream.write(entity_str(count+1, error.code, error.entity) + '\n')
                stream.write('   '+error.message+'\n\n')

    @staticmethod
    def filter_zero_pointers(errors):
        for error in errors:
            if error.code == Error.POINTER_TARGET_NOT_EXISTS and error.data == '0':
                continue
            yield error

    def add_error(self, code, message='', dxf_entity=None, data=None):
        error = ErrorEntry(code, message, dxf_entity, data)
        self.errors.append(error)

    def run(self):
        self.reset()
        dxfversion = self.drawing.dxfversion
        if dxfversion > 'AC1009':  # modern style DXF13 or later
            self.check_root_dict()
        self.check_table_entries()
        self.check_database_entities()
        return self.errors<commit_msg>bugfix for zero pointer check<commit_after># Purpose: auditor module
# Created: 10.03.2017
# Copyright (C) 2017, <NAME>
# License: MIT License
"""
audit(drawing, stream): check a DXF drawing for errors.
"""
from __future__ import unicode_literals
import sys
from ezdxf.lldxf.types import is_pointer_code
from ezdxf.lldxf.const import Error
from ezdxf.lldxf.validator import is_valid_layer_name, is_adsk_special_layer
from ezdxf.dxfentity import DXFEntity

REQUIRED_ROOT_DICT_ENTRIES = ('ACAD_GROUP', 'ACAD_PLOTSTYLENAME')


class ErrorEntry(object):
    def __init__(self, code, message='', dxf_entity=None, data=None):
        self.code = code
        self.entity = dxf_entity
        self.message = message
        self.data = data


def target_pointers(tags):
    for tag in tags:
        if is_pointer_code(tag.code):
            yield tag


class Auditor(object):
    def __init__(self, drawing):
        self.drawing = drawing
        self.errors = []
        self.undefined_targets = set()

    def reset(self):
        self.errors = []
        self.undefined_targets = set()

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

    def __bool__(self):
        return self.__len__() > 0

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

    def print_report(self, errors=None, stream=None):
        def entity_str(count, code, entity):
            if entity is not None:
                return "{:4d}. Issue [{}] in {} #{}".format(count, code, entity.dxftype(), entity.dxf.handle)
            else:
                return "{:4d}. Issue [{}]".format(count, code)

        if errors is None:
            errors = self.errors
        else:
            errors = list(errors)  # generator?

        if stream is None:
            stream = sys.stdout

        if len(errors) == 0:
            stream.write('No issues found.\n\n')
        else:
            stream.write('{} issues found.\n\n'.format(len(errors)))
            for count, error in enumerate(errors):
                stream.write(entity_str(count+1, error.code, error.entity) + '\n')
                stream.write('   '+error.message+'\n\n')

    @staticmethod
    def filter_zero_pointers(errors):
        for error in errors:
            if error.code == Error.POINTER_TARGET_NOT_EXISTS and error.data.value == '0':
                continue
            yield error

    def add_error(self, code, message='', dxf_entity=None, data=None):
        error = ErrorEntry(code, message, dxf_entity, data)
        self.errors.append(error)

    def run(self):
        self.reset()
        dxfversion = self.drawing.dxfversion
        if dxfversion > 'AC1009':  # modern style DXF13 or later
            self.check_root_dict()
        self.check_table_entries()
        self.check_database_entities()
        return self.errors
{'fraction_non_alphanumeric': 0.1108117816091954, 'fraction_numerical': 0.008620689655172414, 'mean_word_length': 2.379247572815534, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '7520183', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1671, 'n_words': 425}