<commit_before>"""Commands to interact with vega/vega-lite nodejs executables"""
from __future__ import print_function

import os
import json
from contextlib import contextmanager
from xml.etree import ElementTree
from subprocess import Popen, PIPE, check_output, CalledProcessError


COMMANDS = ['vl2vg', 'vl2png', 'vl2svg']
SUPPORTED_FILETYPES = ['png', 'svg']
TEST_SPEC = {'data': {'values': [{'x': 1, 'y': 1}, {'x': 2, 'y': 2}]},
             'encoding': {'x': {'field': 'x', 'type': 'quantitative'},
                          'y': {'field': 'y', 'type': 'quantitative'}},
             'mark': 'point'}


def consistent_with_png(filename):
    """Check that the file has a header consistent with PNG"""
    with open(filename, 'rb') as f:
        arr = f.read()
    return arr.startswith(b'\x89PNG\r\n')


def consistent_with_svg(filename):
    """Check that the file has a structure consistent with SVG"""
    try:
        e = ElementTree.parse(filename)
    except ElementTree.ParseError:
        return False
    else:
        keys = e.getroot().keys()
        return set(keys) == {'width', 'version', 'class', 'height'}


@contextmanager
def ensure_npm_bin_in_path(verbose=False):
    if verbose:
        print('> npm bin')
    try:
        npm_bin = check_output(['npm', 'bin'])
    except OSError:
        yield
    else:
        if hasattr(npm_bin, 'decode'):
            npm_bin = npm_bin.decode('utf-8')
        npm_bin = npm_bin.strip()

        old_path = os.environ['PATH']

        if npm_bin not in os.environ['PATH'].split(os.pathsep):
            os.environ["PATH"] += os.pathsep + npm_bin
        yield
        os.environ["PATH"] = old_path
<commit_msg>TST: make consistent_with_svg test more robust<commit_after>"""Commands to interact with vega/vega-lite nodejs executables"""
from __future__ import print_function

import os
import json
from contextlib import contextmanager
from xml.etree import ElementTree
from subprocess import Popen, PIPE, check_output, CalledProcessError


COMMANDS = ['vl2vg', 'vl2png', 'vl2svg']
SUPPORTED_FILETYPES = ['png', 'svg']
TEST_SPEC = {'data': {'values': [{'x': 1, 'y': 1}, {'x': 2, 'y': 2}]},
             'encoding': {'x': {'field': 'x', 'type': 'quantitative'},
                          'y': {'field': 'y', 'type': 'quantitative'}},
             'mark': 'point'}


def consistent_with_png(filename):
    """Check that the file has a header consistent with PNG"""
    with open(filename, 'rb') as f:
        arr = f.read()
    return arr.startswith(b'\x89PNG\r\n')


def consistent_with_svg(filename):
    """Check that the file has a structure consistent with SVG"""
    with open(filename) as file_obj:
        element_tag = ""
        try:
            for event, element in ElementTree.iterparse(file_obj, ('start',)):
                element_tag = element.tag
                break
        except ElementTree.ParseError:
            pass
    return element_tag == '{http://www.w3.org/2000/svg}svg'


@contextmanager
def ensure_npm_bin_in_path(verbose=False):
    if verbose:
        print('> npm bin')
    try:
        npm_bin = check_output(['npm', 'bin'])
    except OSError:
        yield
    else:
        if hasattr(npm_bin, 'decode'):
            npm_bin = npm_bin.decode('utf-8')
        npm_bin = npm_bin.strip()

        old_path = os.environ['PATH']

        if npm_bin not in os.environ['PATH'].split(os.pathsep):
            os.environ["PATH"] += os.pathsep + npm_bin
        yield
        os.environ["PATH"] = old_path

{'fraction_non_alphanumeric': 0.14465408805031446, 'fraction_numerical': 0.00714694110920526, 'mean_word_length': 2.840834248079034, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 2}, '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': '2088572', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1093, 'n_words': 302}