<commit_before>"""Implementation of Python wrapper for OpenFortranParserXML."""

import logging
import pathlib
import subprocess
import typing as t
import xml.etree.ElementTree as ET

from .config import JAVA as java_config

_LOG = logging.getLogger(__name__)


def execute_parser(
        input_path: pathlib.Path, output_path: t.Optional[pathlib.Path],
        verbosity: int = 100) -> subprocess.CompletedProcess:
    """Execute Open Fortran Parser according to current configuration and function parameters."""

    command = [str(java_config['executable'])]
    if java_config['classpath'] is not None:
        command += ['-cp', str(java_config['classpath'])]
    if java_config['options'] is not None:
        command += java_config['options']
    command.append(java_config['ofp_class'])
    command += ['--class', java_config['ofp_xml_class'], '--verbosity', str(verbosity)]
    if output_path is not None:
        command += ['--output', str(output_path)]
    command.append(str(input_path))

    _LOG.debug('Executing %s...', command)
    return subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)


def parse(
        input_path: pathlib.Path, verbosity: int = 100, raise_on_error: bool = False) -> ET.Element:
    """Parse given Fortran file and return parse tree as XML."""

    process = execute_parser(input_path, None, verbosity)
    if process.returncode != 0:
        _LOG.warning('%s', process.stdout.decode())
        _LOG.error('Open Fortran Parser returned %i', process.returncode)
    if process.stderr:
        _LOG.warning(process.stderr.decode())
    if raise_on_error:
        process.check_returncode()

    return ET.fromstring(process.stdout)
<commit_msg>prepare for --only-tokenize flag in the future<commit_after>"""Implementation of Python wrapper for OpenFortranParserXML."""

import logging
import pathlib
import subprocess
import typing as t
import xml.etree.ElementTree as ET

from .config import JAVA as java_config

_LOG = logging.getLogger(__name__)


def execute_parser(
        input_path: pathlib.Path, output_path: t.Optional[pathlib.Path],
        verbosity: int = 100, tokenize_instead: bool = False) -> subprocess.CompletedProcess:
    """Execute Open Fortran Parser according to current configuration and function parameters.

    If tokenize_instead is True, given file will not be parsed, but just tokenized instead.
    """

    command = [str(java_config['executable'])]
    if java_config['classpath'] is not None:
        command += ['-cp', str(java_config['classpath'])]
    if java_config['options'] is not None:
        command += java_config['options']
    command.append(java_config['ofp_class'])
    if tokenize_instead:
        command.append('--tokens')
    command += ['--class', java_config['ofp_xml_class'], '--verbosity', str(verbosity)]
    if output_path is not None:
        command += ['--output', str(output_path)]
    command.append(str(input_path))

    _LOG.debug('Executing %s...', command)
    return subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)


def parse(
        input_path: pathlib.Path, verbosity: int = 100, raise_on_error: bool = False) -> ET.Element:
    """Parse given Fortran file and return parse tree as XML."""

    process = execute_parser(input_path, None, verbosity)
    if process.returncode != 0:
        _LOG.warning('%s', process.stdout.decode())
        _LOG.error('Open Fortran Parser returned %i', process.returncode)
    if process.stderr:
        _LOG.warning(process.stderr.decode())
    if raise_on_error:
        process.check_returncode()

    return ET.fromstring(process.stdout)

{'fraction_non_alphanumeric': 0.12022008253094911, 'fraction_numerical': 0.0038514442916093533, 'mean_word_length': 3.880536912751678, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '7470980', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1058, 'n_words': 290}