<commit_before>"""
Core classes for validation.
"""

import declarative
import textwrap
import re
import os
try:
    from pkg_resources import resource_filename
except ImportError:
    resource_filename = None

__all__ = ['NoDefault', 'Invalid', 'Validator', 'Identity',
           'FancyValidator', 'is_validator']

import gettext

def get_localedir():
    """
    Retrieve the location of locales.

    If we're built as an egg, we need to find the resource within the egg.
    Otherwise, we need to look for the locales on the filesystem or in the
    system message catalog.
    """
    locale_dir = ''
    # Check the egg first
    if resource_filename is not None:
        try:
            locale_dir = resource_filename(__name__, "/i18n")
        except NotImplementedError:
            # resource_filename doesn't work with non-egg zip files
            pass
    if os.access(locale_dir, os.R_OK | os.X_OK):
        # If the resource is present in the egg, use it
        return locale_dir

    # Otherwise, search the filesystem
    locale_dir = os.path.join(os.path.dirname(__file__), 'i18n')
    if not os.access(locale_dir, os.R_OK | os.X_OK):
        # Fallback on the system catalog
        locale_dir = os.path.normpath('/usr/share/locale')

    return locale_dir

def set_stdtranslation(domain="FormEncode", languages=None, \
                       localedir = get_localedir()):

    t = gettext.translation(domain=domain, \
                            languages=languages, \
                            localedir=localedir, fallback=True)
    global _stdtrans
    _stdtrans = t.ugettext

set_stdtranslation()

def _(s): return s # dummy i18n translation function, nothing is translated here.
                   # Instead this is actually done in api.Validator.message.
                   # The surrounding _("string") of the strings is only for extracting
                   # the strings automatically
                   # if you run pygettext with this source comment this function out temporarly <commit_msg>fall back on in-package location when os.access doesn't exist<commit_after>"""
Core classes for validation.
"""

import declarative
import textwrap
import re
import os
try:
    from pkg_resources import resource_filename
except ImportError:
    resource_filename = None

__all__ = ['NoDefault', 'Invalid', 'Validator', 'Identity',
           'FancyValidator', 'is_validator']

import gettext

def get_localedir():
    """
    Retrieve the location of locales.

    If we're built as an egg, we need to find the resource within the egg.
    Otherwise, we need to look for the locales on the filesystem or in the
    system message catalog.
    """
    locale_dir = ''
    # Check the egg first
    if resource_filename is not None:
        try:
            locale_dir = resource_filename(__name__, "/i18n")
        except NotImplementedError:
            # resource_filename doesn't work with non-egg zip files
            pass
    if not hasattr(os, 'access'):
        # This happens on Google App Engine
        os.path.join(os.path.dirname(__file__), 'i18n')
    if os.access(locale_dir, os.R_OK | os.X_OK):
        # If the resource is present in the egg, use it
        return locale_dir

    # Otherwise, search the filesystem
    locale_dir = os.path.join(os.path.dirname(__file__), 'i18n')
    if not os.access(locale_dir, os.R_OK | os.X_OK):
        # Fallback on the system catalog
        locale_dir = os.path.normpath('/usr/share/locale')

    return locale_dir

def set_stdtranslation(domain="FormEncode", languages=None, \
                       localedir = get_localedir()):

    t = gettext.translation(domain=domain, \
                            languages=languages, \
                            localedir=localedir, fallback=True)
    global _stdtrans
    _stdtrans = t.ugettext

set_stdtranslation()

def _(s): return s # dummy i18n translation function, nothing is translated here.
                   # Instead this is actually done in api.Validator.message.
                   # The surrounding _("string") of the strings is only for extracting
                   # the strings automatically
                   # if you run pygettext with this source comment this function out temporarly 
{'fraction_non_alphanumeric': 0.07810287871637565, 'fraction_numerical': 0.0033034450212364322, 'mean_word_length': 2.480295566502463, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4579511', 'n_tokens_mistral': 1174, 'n_tokens_neox': 1137, 'n_words': 400}