<commit_before># coding:utf-8
"""The domain-specific adapters.

An adapter is an object that provides domain-specific apis for another object,
called adaptee.

Examples are: XMLValidator and XMLPacker objects, which provide SciELO PS
validation behaviour and packaging functionality, respectively.
"""
from __future__ import unicode_literals
import logging
from copy import deepcopy
import os
try:
    import reprlib
except ImportError:
    import repr as reprlib

from lxml import etree, isoschematron

from . import utils, catalogs, checks, style_errors, exceptions


__all__ = ['XMLValidator', 'HTMLGenerator']


LOGGER = logging.getLogger(__name__)


# As a general rule, only the latest 2 versions are supported simultaneously.
CURRENTLY_SUPPORTED_VERSIONS = os.environ.get(
    'PACKTOOLS_SUPPORTED_SPS_VERSIONS', 'sps-1.3:sps-1.4').split(':')

ALLOWED_PUBLIC_IDS = (
    '-//NLM//DTD JATS (Z39.96) Journal Publishing DTD v1.0 20120330//EN',
)

# doctype public ids for sps <= 1.1
ALLOWED_PUBLIC_IDS_LEGACY = (
    '-//NLM//DTD JATS (Z39.96) Journal Publishing DTD v1.0 20120330//EN',
    '-//NLM//DTD Journal Publishing DTD v3.0 20080202//EN',
)


def _get_public_ids(sps_version):
    """Returns the set of allowed public ids for the XML based on its version.
    """
    if sps_version in ['pre-sps', 'sps-1.1']:
        return frozenset(ALLOWED_PUBLIC_IDS_LEGACY)
    else:
        return frozenset(ALLOWED_PUBLIC_IDS)


def _init_sps_version(xml_et, supported_versions=None):
    """Returns the SPS spec version for `xml_et` or raises ValueError.

    It also checks if the version is currently supported.

    :param xml_et: etree instance.
    :param supported_versions: (optional) the default value is set by env var `PACKTOOLS_SUPPORTED_SPS_VERSIONS`.
    """
    if supported_versions is None:
        supported_versions = CURRENTLY_SUPPORTED_VERSIONS

    try:
        version_from_xml = xml_et.getroot().attrib['specific-use']
    except KeyError:
        raise exceptions.XMLSPSVersionError('Missing SPS version at /article/@specific-use')

    if version_from_xml not in supported_versions:
        raise exceptions.XMLSPSVersionError('%s is not currently supported' % version_from_xml)
    else:
        return version_from_xml


def Schematron(file):
    with open(file, mode='rb') as fp:<commit_msg>Avoids exception chaining while retrieving the sps version defined in the XML document.<commit_after># coding:utf-8
"""The domain-specific adapters.

An adapter is an object that provides domain-specific apis for another object,
called adaptee.

Examples are: XMLValidator and XMLPacker objects, which provide SciELO PS
validation behaviour and packaging functionality, respectively.
"""
from __future__ import unicode_literals
import logging
from copy import deepcopy
import os
try:
    import reprlib
except ImportError:
    import repr as reprlib

from lxml import etree, isoschematron

from . import utils, catalogs, checks, style_errors, exceptions


__all__ = ['XMLValidator', 'HTMLGenerator']


LOGGER = logging.getLogger(__name__)


# As a general rule, only the latest 2 versions are supported simultaneously.
CURRENTLY_SUPPORTED_VERSIONS = os.environ.get(
    'PACKTOOLS_SUPPORTED_SPS_VERSIONS', 'sps-1.3:sps-1.4').split(':')

ALLOWED_PUBLIC_IDS = (
    '-//NLM//DTD JATS (Z39.96) Journal Publishing DTD v1.0 20120330//EN',
)

# doctype public ids for sps <= 1.1
ALLOWED_PUBLIC_IDS_LEGACY = (
    '-//NLM//DTD JATS (Z39.96) Journal Publishing DTD v1.0 20120330//EN',
    '-//NLM//DTD Journal Publishing DTD v3.0 20080202//EN',
)


def _get_public_ids(sps_version):
    """Returns the set of allowed public ids for the XML based on its version.
    """
    if sps_version in ['pre-sps', 'sps-1.1']:
        return frozenset(ALLOWED_PUBLIC_IDS_LEGACY)
    else:
        return frozenset(ALLOWED_PUBLIC_IDS)


def _init_sps_version(xml_et, supported_versions=None):
    """Returns the SPS spec version for `xml_et` or raises ValueError.

    It also checks if the version is currently supported.

    :param xml_et: etree instance.
    :param supported_versions: (optional) the default value is set by env var `PACKTOOLS_SUPPORTED_SPS_VERSIONS`.
    """
    if supported_versions is None:
        supported_versions = CURRENTLY_SUPPORTED_VERSIONS

    doc_root = xml_et.getroot()
    version_from_xml = doc_root.attrib.get('specific-use', None)
    if version_from_xml is None:
        raise exceptions.XMLSPSVersionError('Missing SPS version at /article/@specific-use')

    if version_from_xml not in supported_versions:
        raise exceptions.XMLSPSVersionError('%s is not currently supported' % version_from_xml)
    else:
        return version_from_xml


def Schematron(file):
    with open(file, mode='rb') as fp:
{'fraction_non_alphanumeric': 0.08777099557987791, 'fraction_numerical': 0.0202062723637129, 'mean_word_length': 4.066098081023454, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 19}, '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': '6772132', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1483, 'n_words': 459}