<commit_before>"""Helpers object, Paste Template config and misc. functionality.

The util module provides the main Helper object used by Pylons.

PylonsTemplate is a Paste Template sub-class that configures the source
directory and default plug-ins for a new Pylons project.
"""
import gettext
import os.path
from paste.deploy.config import CONFIG
from paste.script.templates import Template
import pylons

def log(msg):
    """Log a message to the output log."""
    pylons.request.environ['wsgi.errors'].write('=> %s\n'%str(msg))

def _(value):
    """Mark a string for translation
    
    Mark a string to be internationalized as follows:
    
    .. code-block:: Python
    
        h._('This should be in lots of languages')
    
    """
    return pylons.translator['translator'].gettext(value)

def set_lang(lang):
    """Set the language used"""
    project_name = CONFIG['app_conf']['package']
    pylons.translator['lang'] = lang
    if lang is None:
        pylons.translator['translator'] = _Translator()
    else:
        from pkg_resources import resource_exists
        from pylons.i18n.translation import egg_translation
        catalog_path = os.path.join('i18n', lang, 'LC_MESSAGES')
        if not resource_exists(project_name, catalog_path):
            raise LanguageError('Language catalog %s not found' % \
                                os.path.join(project_name, catalog_path))
        pylons.translator['translator'] = \
            egg_translation(project_name, lang=catalog_path)

def get_lang():
    return pylons.translator['lang']

def get_prefix(environ):
    if environ.has_key('paste.config'):
        prefix = environ['paste.config']['app_conf'].get('prefix', '')
    else:
        # Not ideal but if the error occurs before the paste.config is available not a lot we can do
        prefix = ''
    if not prefix:
        if environ.get('SCRIPT_NAME', '') != '':
            prefix = environ['SCRIPT_NAME']
    return prefix

def class_name_from_module_name(module_name):
    """Takes a module name and returns the name of the class it defines.

    If the module name contains dashes, they are replaced with underscores.
    
    Example::
    
        >>> class_name_from_module_name('with-dashes')
        'WithDashes'
        >>> class_name_from_module_name('with_underscores')
        'WithUnderscores'
        >>> class_name_from_module_name('oneword')
        'Oneword'
    
    """
    words = module_name.replace('-', '_').split('_')
    return ''.join([w.title() for w in words])

class ContextObj(object):
    """ The 'c' object, with strict attribute access (raises an Exception when
    the attribute does not exist) """
    pass

class AttribSafeContextObj(object):
    """ The 'c' object, with lax attribute access (returns '' when the attribute
    does not exist) """
    def __getattr__(self, name):
        try:
            return object.__getattribute__(self, name)
        except AttributeError:
            return ''

class LanguageError(Exception):
    """Exception raised when a problem occurs with changing languages"""
    pass

class _Translator(object):
    """An empty gettext translator which just returns the original string"""
    def gettext(self, value):
        return value

class PylonsTemplate(Template):
    _template_dir = 'templates/default_project'
    summary = 'Pylons application template'
    egg_plugins = ['Pylons', 'WebHelpers']

class MinimalPylonsTemplate(Template):
    _template_dir = 'templates/minimal_project'
    summary = 'Pylons minimal application template'
    egg_plugins = ['Pylons', 'WebHelpers']
    

__all__ = ['AttribSafeContextObj', 'ContextObj', 'Helpers', 'class_name_from_module_name',
    'log', '_', 'set_lang', 'get_lang']
__pudge_all__ = __all__ + ['PylonsTemplate']
<commit_msg>[svn] avoid set_lang's project name lookup when passed None<commit_after>"""Helpers object, Paste Template config and misc. functionality.

The util module provides the main Helper object used by Pylons.

PylonsTemplate is a Paste Template sub-class that configures the source
directory and default plug-ins for a new Pylons project.
"""
import gettext
import os.path
from paste.deploy.config import CONFIG
from paste.script.templates import Template
import pylons

def log(msg):
    """Log a message to the output log."""
    pylons.request.environ['wsgi.errors'].write('=> %s\n'%str(msg))

def _(value):
    """Mark a string for translation
    
    Mark a string to be internationalized as follows:
    
    .. code-block:: Python
    
        h._('This should be in lots of languages')
    
    """
    return pylons.translator['translator'].gettext(value)

def set_lang(lang):
    """Set the language used"""
    if lang is None:
        pylons.translator['translator'] = _Translator()
    else:
        from pkg_resources import resource_exists
        from pylons.i18n.translation import egg_translation
        project_name = CONFIG['app_conf']['package']
        catalog_path = os.path.join('i18n', lang, 'LC_MESSAGES')
        if not resource_exists(project_name, catalog_path):
            raise LanguageError('Language catalog %s not found' % \
                                os.path.join(project_name, catalog_path))
        pylons.translator['translator'] = \
            egg_translation(project_name, lang=catalog_path)

def get_lang():
    return pylons.translator.get('lang')

def get_prefix(environ):
    if environ.has_key('paste.config'):
        prefix = environ['paste.config']['app_conf'].get('prefix', '')
    else:
        # Not ideal but if the error occurs before the paste.config is available not a lot we can do
        prefix = ''
    if not prefix:
        if environ.get('SCRIPT_NAME', '') != '':
            prefix = environ['SCRIPT_NAME']
    return prefix

def class_name_from_module_name(module_name):
    """Takes a module name and returns the name of the class it defines.

    If the module name contains dashes, they are replaced with underscores.
    
    Example::
    
        >>> class_name_from_module_name('with-dashes')
        'WithDashes'
        >>> class_name_from_module_name('with_underscores')
        'WithUnderscores'
        >>> class_name_from_module_name('oneword')
        'Oneword'
    
    """
    words = module_name.replace('-', '_').split('_')
    return ''.join([w.title() for w in words])

class ContextObj(object):
    """ The 'c' object, with strict attribute access (raises an Exception when
    the attribute does not exist) """
    pass

class AttribSafeContextObj(object):
    """ The 'c' object, with lax attribute access (returns '' when the attribute
    does not exist) """
    def __getattr__(self, name):
        try:
            return object.__getattribute__(self, name)
        except AttributeError:
            return ''

class LanguageError(Exception):
    """Exception raised when a problem occurs with changing languages"""
    pass

class _Translator(object):
    """An empty gettext translator which just returns the original string"""
    def gettext(self, value):
        return value

class PylonsTemplate(Template):
    _template_dir = 'templates/default_project'
    summary = 'Pylons application template'
    egg_plugins = ['Pylons', 'WebHelpers']

class MinimalPylonsTemplate(Template):
    _template_dir = 'templates/minimal_project'
    summary = 'Pylons minimal application template'
    egg_plugins = ['Pylons', 'WebHelpers']
    

__all__ = ['AttribSafeContextObj', 'ContextObj', 'Helpers', 'class_name_from_module_name',
    'log', '_', 'set_lang', 'get_lang']
__pudge_all__ = __all__ + ['PylonsTemplate']

{'fraction_non_alphanumeric': 0.10811521767723267, 'fraction_numerical': 0.0010522162304353545, 'mean_word_length': 3.291196388261851, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '2523913', 'n_tokens_mistral': 2201, 'n_tokens_neox': 2172, 'n_words': 650}