<commit_before># -*- coding: utf-8 -*-

import re
import urlparse

from django.core import exceptions
from django.utils import importlib
from django.utils import translation
from django.conf import settings

from urli18n import app_settings


def show_language(language):
    """Simple helper method to determine if the language
    passed should be shown in the url or not according to 
   `` URLI18N_ALWAYS_SHOW_LANGUAGE`` flag in
    ``app_settings``. 
    
    Params:
        - ``language`` - the language shortcut which should be checked
    
    Returns:
        A Boolean: ``True`` if language should be should, ``False`` otherwise
    """
    return not (app_settings.URLI18N_ALWAYS_SHOW_LANGUAGE is False and language == settings.LANGUAGE_CODE)

def is_included_path(path, strict_mode=True):
    """Helper to determine if a given path should be
    transformed to the show the language in the url. This
    is checked against ``URLI18N_INCLUDE_PATHS`` in 
    ``app_settings``. The ``MEDIA_URL`` and ``STATIC_URL``
    from ``django.conf.settings`` are excluded automatically.
    
    Params:
        - ``path``: the url path which should be checked
        - ``strict_mode``: a boolean which indicates if the path should be checked strict or non-strict. This affects check within the function ``_edit_path_exp`` only.
    
    Returns:
        A Boolean: ``True`` if the given url path should be included,
        ``False`` otherwise.
    """
    path = urlparse.urlparse(path).path
    media_url = getattr(settings, 'MEDIA_URL')
    static_url = getattr(settings, 'STATIC_URL')
    if not (media_url and path.startswith(media_url))\
    and not (static_url and path.startswith(static_url)):
        for p in app_settings.URLI18N_INCLUDE_PATHS:
            p = _edit_path_exp(p, mode)
            regex_path = re.compile(p, re.UNICODE)
            if regex_path.match(path):
                return True
    return False

def _edit_path_exp(path_expression, strict_mode=True):
    """Private helper function transform a regular expression
    given in ``app_settings.URLI18N_INCLUDE_PATHS`` for
    checking. This can be performed in strict or non-strict mode.
    If strict mode it will make sure the given expression ends with
    ``/$``. In non strict mode it will simply not care how the
    expression ends. In both cases it will transform an expression
    to start with ``^/`` though.
    
    Params:
        - ``path_expression``: a regular expression to check against a url path
        - ``strict_mode``: a boolean indicating string or non-strict mode. See explainations above.
        
    Returns:
        - ``path_expression`` (modified if needed)
    """
    if not path_expression.startswith('^/'):
        if path_expression.startswith('^'):
            path_expression = path_expression.replace('^', '^/')
        elif path_expression.startswith('/'):
            path_expression = '^%s' % path_expression
        else:<commit_msg>fixing a NameError in `urli18n/utils.py` `_edit_path_exp`. Thx @pigletto<commit_after># -*- coding: utf-8 -*-

import re
import urlparse

from django.core import exceptions
from django.utils import importlib
from django.utils import translation
from django.conf import settings

from urli18n import app_settings


def show_language(language):
    """Simple helper method to determine if the language
    passed should be shown in the url or not according to 
   `` URLI18N_ALWAYS_SHOW_LANGUAGE`` flag in
    ``app_settings``. 
    
    Params:
        - ``language`` - the language shortcut which should be checked
    
    Returns:
        A Boolean: ``True`` if language should be should, ``False`` otherwise
    """
    return not (app_settings.URLI18N_ALWAYS_SHOW_LANGUAGE is False and language == settings.LANGUAGE_CODE)

def is_included_path(path, strict_mode=True):
    """Helper to determine if a given path should be
    transformed to the show the language in the url. This
    is checked against ``URLI18N_INCLUDE_PATHS`` in 
    ``app_settings``. The ``MEDIA_URL`` and ``STATIC_URL``
    from ``django.conf.settings`` are excluded automatically.
    
    Params:
        - ``path``: the url path which should be checked
        - ``strict_mode``: a boolean which indicates if the path should be checked strict or non-strict. This affects check within the function ``_edit_path_exp`` only.
    
    Returns:
        A Boolean: ``True`` if the given url path should be included,
        ``False`` otherwise.
    """
    path = urlparse.urlparse(path).path
    media_url = getattr(settings, 'MEDIA_URL')
    static_url = getattr(settings, 'STATIC_URL')
    if not (media_url and path.startswith(media_url))\
    and not (static_url and path.startswith(static_url)):
        for p in app_settings.URLI18N_INCLUDE_PATHS:
            p = _edit_path_exp(p, strict_mode)
            regex_path = re.compile(p, re.UNICODE)
            if regex_path.match(path):
                return True
    return False

def _edit_path_exp(path_expression, strict_mode=True):
    """Private helper function transform a regular expression
    given in ``app_settings.URLI18N_INCLUDE_PATHS`` for
    checking. This can be performed in strict or non-strict mode.
    If strict mode it will make sure the given expression ends with
    ``/$``. In non strict mode it will simply not care how the
    expression ends. In both cases it will transform an expression
    to start with ``^/`` though.
    
    Params:
        - ``path_expression``: a regular expression to check against a url path
        - ``strict_mode``: a boolean indicating string or non-strict mode. See explainations above.
        
    Returns:
        - ``path_expression`` (modified if needed)
    """
    if not path_expression.startswith('^/'):
        if path_expression.startswith('^'):
            path_expression = path_expression.replace('^', '^/')
        elif path_expression.startswith('/'):
            path_expression = '^%s' % path_expression
        else:
{'fraction_non_alphanumeric': 0.09154694286668894, 'fraction_numerical': 0.004677581022385567, 'mean_word_length': 3.058983050847458, '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': '6565285', 'n_tokens_mistral': 1732, 'n_tokens_neox': 1666, 'n_words': 600}