<commit_before># coding=utf-8

import csv
import os
import re

from typing import List

from util import most_common, warn

from constants import ColumnDescriptors, TemplateFields, TemplateFieldDescriptors


class TemplateField(object):
    """ Represents a data field in a template. """

    def __init__(self, name: str, start_index: int, end_index: int):
        self.name = name  # the inner value of the template field; i.e. the name
        self.start_index = start_index  # the index of the first '{' wrapping character
        self.end_index = end_index  # the index of the last '}' wrapping character


def image_tag_from_path(image_path: str, definitions: dict=None) -> (str, str):
    """ Constructs an HTML-compliant image tag using the specified image path. """

    actual_image_path = image_path

    # determine whether a size has been explicitly specified; e.g. "images/name-of-image.svg:16x16"
    size_index = image_path.rfind(':')

    # determine whether the : actually represents a protocol specification; i.e. http:// or similar
    if image_path[size_index + 1:size_index + 1 + 2] == '//':
        # in case it is, then ignore anything beyond the protocol specification
        size_index = -1

    copy_only = image_path.endswith(TemplateFieldDescriptors.COPY_ONLY)

    if copy_only:
        actual_image_path = image_path.replace(TemplateFieldDescriptors.COPY_ONLY, '')

        # ignore any size specification since an <img> tag will not be created for this image
        size_index = -1

    explicit_width = None
    explicit_height = None

    if size_index is not -1:
        # get rid of the size specification to have a clean image path
        actual_image_path = image_path[:size_index]

        if definitions is not None and actual_image_path in definitions:
            # the path is actually a symbol definition; e.g. "enemy" or similar.
            actual_image_path = definitions.get(actual_image_path)

        # get the size specification; i.e. whatever is on the right hand size of the ':' splitter
        size = image_path[size_index + 1:].strip()

        # then, determine whether the value is a size specified in the metadata;
        # if it is, use that size specification.
        if definitions is not None and size in definitions:
            size = definitions.get(size)

        # get each size specification separately (removing blanks)
        size = list(filter(None, size.split('x')))

        if len(size) > 0:
            width_specification = size[0]

            try:
                explicit_width = int(width_specification)
            except ValueError:
                warn('The size specification \'{0}\' has not been defined. '
                     'Image might not display as expected.'.format(width_specification),
                     in_context=actual_image_path)
                explicit_width = None
            else:
                if explicit_width < 0:
                    explicit_width = None

        if len(size) > 1:
            explicit_height = int(size[1]) if size[1].isdigit() else None

            if explicit_height is not None and explicit_height < 0:
                explicit_height = None
        else:
            # default to a squared size using the width specification
            explicit_height = explicit_width

    if is_image(actual_image_path):
        if copy_only:
            # the image should only be copied - so the "tag" is simply the image path
            image_tag = actual_image_path
        elif (explicit_width is not None and
              explicit_height is not None):
                image_tag = '<img src="{0}" width="{1}" height="{2}">'.format(
                    actual_image_path, explicit_width, explicit_height)
        else:
            image_tag = '<img src="{0}">'.format(actual_image_path)
    else:
        actual_image_path = ''
        image_tag = ''

    return image_tag, actual_image_path
<commit_msg>Fix warning on undefined image size specification<commit_after># coding=utf-8

import csv
import os
import re

from typing import List

from util import most_common, WarningContext, warn

from constants import ColumnDescriptors, TemplateFields, TemplateFieldDescriptors


class TemplateField(object):
    """ Represents a data field in a template. """

    def __init__(self, name: str, start_index: int, end_index: int):
        self.name = name  # the inner value of the template field; i.e. the name
        self.start_index = start_index  # the index of the first '{' wrapping character
        self.end_index = end_index  # the index of the last '}' wrapping character


def image_tag_from_path(image_path: str, definitions: dict=None) -> (str, str):
    """ Constructs an HTML-compliant image tag using the specified image path. """

    actual_image_path = image_path

    # determine whether a size has been explicitly specified; e.g. "images/name-of-image.svg:16x16"
    size_index = image_path.rfind(':')

    # determine whether the : actually represents a protocol specification; i.e. http:// or similar
    if image_path[size_index + 1:size_index + 1 + 2] == '//':
        # in case it is, then ignore anything beyond the protocol specification
        size_index = -1

    copy_only = image_path.endswith(TemplateFieldDescriptors.COPY_ONLY)

    if copy_only:
        actual_image_path = image_path.replace(TemplateFieldDescriptors.COPY_ONLY, '')

        # ignore any size specification since an <img> tag will not be created for this image
        size_index = -1

    explicit_width = None
    explicit_height = None

    if size_index is not -1:
        # get rid of the size specification to have a clean image path
        actual_image_path = image_path[:size_index]

        if definitions is not None and actual_image_path in definitions:
            # the path is actually a symbol definition; e.g. "enemy" or similar.
            actual_image_path = definitions.get(actual_image_path)

        # get the size specification; i.e. whatever is on the right hand size of the ':' splitter
        size = image_path[size_index + 1:].strip()

        # then, determine whether the value is a size specified in the metadata;
        # if it is, use that size specification.
        if definitions is not None and size in definitions:
            size = definitions.get(size)

        # get each size specification separately (removing blanks)
        size = list(filter(None, size.split('x')))

        if len(size) > 0:
            width_specification = size[0]

            try:
                explicit_width = int(width_specification)
            except ValueError:
                warn('The size specification \'{0}\' has not been defined. '
                     'Image might not display as expected.'.format(width_specification),
                     in_context=WarningContext(actual_image_path))
                explicit_width = None
            else:
                if explicit_width < 0:
                    explicit_width = None

        if len(size) > 1:
            explicit_height = int(size[1]) if size[1].isdigit() else None

            if explicit_height is not None and explicit_height < 0:
                explicit_height = None
        else:
            # default to a squared size using the width specification
            explicit_height = explicit_width

    if is_image(actual_image_path):
        if copy_only:
            # the image should only be copied - so the "tag" is simply the image path
            image_tag = actual_image_path
        elif (explicit_width is not None and
              explicit_height is not None):
                image_tag = '<img src="{0}" width="{1}" height="{2}">'.format(
                    actual_image_path, explicit_width, explicit_height)
        else:
            image_tag = '<img src="{0}">'.format(actual_image_path)
    else:
        actual_image_path = ''
        image_tag = ''

    return image_tag, actual_image_path

{'fraction_non_alphanumeric': 0.07712632108706592, 'fraction_numerical': 0.006039255158530448, 'mean_word_length': 2.453084274543875, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, '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': '1242305', 'n_tokens_mistral': 2199, 'n_tokens_neox': 2098, 'n_words': 745}