Django

Code

Changeset 874

Show
Ignore:
Timestamp:
10/14/05 18:32:35 (3 years ago)
Author:
hugo
Message:

i18n: merged to [873] from trunk

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/i18n/django/conf/global_settings.py

    r869 r874  
    7171# Extension on all templates. 
    7272TEMPLATE_FILE_EXTENSION = '.html' 
     73 
     74# List of callables that know how to import templates from various sources. 
     75# See the comments in django/core/template/loader.py for interface 
     76# documentation. 
     77TEMPLATE_LOADERS = ( 
     78    'django.core.template.loaders.filesystem.load_template_source', 
     79#     'django.core.template.loaders.eggs.load_template_source', 
     80) 
    7381 
    7482# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a 
  • django/branches/i18n/django/core/template/loader.py

    r869 r874  
    1 "Wrapper for loading templates from storage of some sort (e.g. files or db)" 
    2  
     1# Wrapper for loading templates from storage of some sort (e.g. filesystem, database). 
     2
     3# This uses the TEMPLATE_LOADERS setting, which is a list of loaders to use. 
     4# Each loader is expected to have this interface: 
     5
     6#    callable(name, dirs=[]) 
     7
     8# name is the template name. 
     9# dirs is an optional list of directories to search instead of TEMPLATE_DIRS. 
     10
     11# Each loader should have an "is_usable" attribute set. This is a boolean that 
     12# specifies whether the loader can be used in this Python installation. Each 
     13# loader is responsible for setting this when it's initialized. 
     14
     15# For example, the eggs loader (which is capable of loading templates from 
     16# Python eggs) sets is_usable to False if the "pkg_resources" module isn't 
     17# installed, because pkg_resources is necessary to read eggs. 
     18 
     19from django.core.exceptions import ImproperlyConfigured 
    320from django.core.template import Template, Context, Node, TemplateDoesNotExist, TemplateSyntaxError, resolve_variable_with_filters, register_tag 
    4 from django.core.template.loaders.filesystem import load_template_source 
     21from django.conf.settings import TEMPLATE_LOADERS 
     22 
     23template_source_loaders = [] 
     24for path in TEMPLATE_LOADERS: 
     25    i = path.rfind('.') 
     26    module, attr = path[:i], path[i+1:] 
     27    try: 
     28        mod = __import__(module, globals(), locals(), [attr]) 
     29    except ImportError, e: 
     30        raise ImproperlyConfigured, 'Error importing template source loader %s: "%s"' % (module, e) 
     31    try: 
     32        func = getattr(mod, attr) 
     33    except AttributeError: 
     34        raise ImproperlyConfigured, 'Module "%s" does not define a "%s" callable template source loader' % (module, attr) 
     35    if not func.is_usable: 
     36        import warnings 
     37        warnings.warn("Your TEMPLATE_LOADERS setting includes %r, but your Python installation doesn't support that type of template loading. Consider removing that line from TEMPLATE_LOADERS." % path) 
     38    else: 
     39        template_source_loaders.append(func) 
     40 
     41def load_template_source(name, dirs=None): 
     42    for loader in template_source_loaders: 
     43        try: 
     44            return loader(name, dirs) 
     45        except TemplateDoesNotExist: 
     46            pass 
     47    raise TemplateDoesNotExist, name 
    548 
    649class ExtendsError(Exception): 
  • django/branches/i18n/django/core/template/loaders/filesystem.py

    r869 r874  
    2020        error_msg = "Your TEMPLATE_DIRS settings is empty. Change it to point to at least one template directory." 
    2121    raise TemplateDoesNotExist, error_msg 
     22load_template_source.is_usable = True 
  • django/branches/i18n/ez_setup.py

    r66 r874  
    1414This file can also be run as a script to install or upgrade setuptools. 
    1515""" 
    16  
    17 DEFAULT_VERSION = "0.5a12" 
    18 DEFAULT_URL     = "http://www.python.org/packages/source/s/setuptools/" 
     16import sys 
     17DEFAULT_VERSION = "0.6a5" 
     18DEFAULT_URL     = "http://cheeseshop.python.org/packages/%s/s/setuptools/" % sys.version[:3] 
     19 
     20md5_data = { 
     21    'setuptools-0.5a13-py2.3.egg': '85edcf0ef39bab66e130d3f38f578c86', 
     22    'setuptools-0.5a13-py2.4.egg': 'ede4be600e3890e06d4ee5e0148e092a', 
     23    'setuptools-0.6a1-py2.3.egg': 'ee819a13b924d9696b0d6ca6d1c5833d', 
     24    'setuptools-0.6a1-py2.4.egg': '8256b5f1cd9e348ea6877b5ddd56257d', 
     25    'setuptools-0.6a2-py2.3.egg': 'b98da449da411267c37a738f0ab625ba', 
     26    'setuptools-0.6a2-py2.4.egg': 'be5b88bc30aed63fdefd2683be135c3b', 
     27    'setuptools-0.6a3-py2.3.egg': 'ee0e325de78f23aab79d33106dc2a8c8', 
     28    'setuptools-0.6a3-py2.4.egg': 'd95453d525a456d6c23e7a5eea89a063', 
     29    'setuptools-0.6a4-py2.3.egg': 'e958cbed4623bbf47dd1f268b99d7784', 
     30    'setuptools-0.6a4-py2.4.egg': '7f33c3ac2ef1296f0ab4fac1de4767d8', 
     31    'setuptools-0.6a5-py2.3.egg': '748408389c49bcd2d84f6ae0b01695b1', 
     32    'setuptools-0.6a5-py2.4.egg': '999bacde623f4284bfb3ea77941d2627', 
     33
    1934 
    2035import sys, os 
    2136 
    22  
    23  
    24  
    25  
    26  
    27  
    28  
    29  
    30  
    31  
    32  
    33  
    34  
    35  
    36  
    37  
    38  
    39  
     37def _validate_md5(egg_name, data): 
     38    if egg_name in md5_data: 
     39        from md5 import md5 
     40        digest = md5(data).hexdigest() 
     41        if digest != md5_data[egg_name]: 
     42            print >>sys.stderr, ( 
     43                "md5 validation of %s failed!  (Possible download problem?)" 
     44                % egg_name 
     45            ) 
     46            sys.exit(2) 
     47    return data     
    4048 
    4149 
    4250def use_setuptools( 
    43     version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir 
     51    version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, 
     52    download_delay=15 
    4453): 
    4554    """Automatically find/download setuptools and make it available on sys.path 
     
    4857    as an egg for download under the `download_base` URL (which should end with 
    4958    a '/').  `to_dir` is the directory where setuptools will be downloaded, if 
    50     it is not already available. 
    51  
    52     If an older version of setuptools is installed, this will print a message 
    53     to ``sys.stderr`` and raise SystemExit in an attempt to abort the calling 
    54     script. 
     59    it is not already available.  If `download_delay` is specified, it should 
     60    be the number of seconds that will be paused before initiating a download, 
     61    should one be required.  If an older version of setuptools is installed, 
     62    this routine will print a message to ``sys.stderr`` and raise SystemExit in 
     63    an attempt to abort the calling script.   
    5564    """ 
    5665    try: 
     
    6271            ) 
    6372            sys.exit(2) 
    64  
    6573    except ImportError: 
    66         egg = download_setuptools(version, download_base, to_dir
     74        egg = download_setuptools(version, download_base, to_dir, download_delay
    6775        sys.path.insert(0, egg) 
    6876        import setuptools; setuptools.bootstrap_install_from = egg 
     
    8290 
    8391def download_setuptools( 
    84     version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir 
     92    version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, 
     93    delay = 15 
    8594): 
    8695    """Download setuptools from a specified location and return its filename 
     
    8998    as an egg for download under the `download_base` URL (which should end 
    9099    with a '/'). `to_dir` is the directory where the egg will be downloaded. 
     100    `delay` is the number of seconds to pause before an actual download attempt. 
    91101    """ 
    92102    import urllib2, shutil 
    93103    egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3]) 
    94     url = download_base + egg_name + '.zip'  # XXX 
     104    url = download_base + egg_name 
    95105    saveto = os.path.join(to_dir, egg_name) 
    96106    src = dst = None 
    97  
    98107    if not os.path.exists(saveto):  # Avoid repeated downloads 
    99108        try: 
    100109            from distutils import log 
     110            if delay: 
     111                log.warn(""" 
     112--------------------------------------------------------------------------- 
     113This script requires setuptools version %s to run (even to display 
     114help).  I will attempt to download it for you (from 
     115%s), but 
     116you may need to enable firewall access for this script first. 
     117I will start the download in %d seconds. 
     118---------------------------------------------------------------------------""", 
     119                    version, download_base, delay 
     120                ); from time import sleep; sleep(delay) 
    101121            log.warn("Downloading %s", url) 
    102122            src = urllib2.urlopen(url) 
    103123            # Read/write all in one block, so we don't create a corrupt file 
    104124            # if the download is interrupted. 
    105             data = src.read() 
    106             dst = open(saveto,"wb") 
    107             dst.write(data) 
     125            data = _validate_md5(egg_name, src.read()) 
     126            dst = open(saveto,"wb"); dst.write(data) 
    108127        finally: 
    109128            if src: src.close() 
    110129            if dst: dst.close() 
    111  
    112130    return os.path.realpath(saveto) 
    113  
    114  
    115  
    116  
    117  
    118  
    119  
    120  
    121  
    122  
    123131 
    124132def main(argv, version=DEFAULT_VERSION): 
     
    131139        tmpdir = tempfile.mkdtemp(prefix="easy_install-") 
    132140        try: 
    133             egg = download_setuptools(version, to_dir=tmpdir
     141            egg = download_setuptools(version, to_dir=tmpdir, delay=0
    134142            sys.path.insert(0,egg) 
    135143            from setuptools.command.easy_install import main 
     
    151159        except ImportError: 
    152160            from easy_install import main 
    153         main(list(argv)+[download_setuptools()]) 
     161        main(list(argv)+[download_setuptools(delay=0)]) 
    154162        sys.exit(0) # try to force an exit 
    155163    else: 
     
    160168            print "Setuptools version",version,"or greater has been installed." 
    161169            print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)' 
     170 
     171 
     172             
     173def update_md5(filenames): 
     174    """Update our built-in md5 registry""" 
     175 
     176    import re 
     177    from md5 import md5 
     178 
     179    for name in filenames: 
     180        base = os.path.basename(name) 
     181        f = open(name,'rb')        
     182        md5_data[base] = md5(f.read()).hexdigest() 
     183        f.close() 
     184 
     185    data = ["    %r: %r,\n" % it for it in md5_data.items()] 
     186    data.sort() 
     187    repl = "".join(data) 
     188 
     189    import inspect 
     190    srcfile = inspect.getsourcefile(sys.modules[__name__]) 
     191    f = open(srcfile, 'rb'); src = f.read(); f.close() 
     192 
     193    match = re.search("\nmd5_data = {\n([^}]+)}", src) 
     194    if not match: 
     195        print >>sys.stderr, "Internal error!" 
     196        sys.exit(2) 
     197 
     198    src = src[:match.start(1)] + repl + src[match.end(1):] 
     199    f = open(srcfile,'w') 
     200    f.write(src) 
     201    f.close() 
     202 
     203 
    162204if __name__=='__main__': 
    163     main(sys.argv[1:]) 
    164  
     205    if len(sys.argv)>2 and sys.argv[1]=='--md5update': 
     206        update_md5(sys.argv[2:]) 
     207    else: 
     208        main(sys.argv[1:]) 
     209 
     210 
     211 
     212 
     213 
  • django/branches/i18n/tests/othertests/templates.py

    r869 r874  
    261261} 
    262262 
    263 # This replaces the standard template loader. 
    264263def test_template_loader(template_name, template_dirs=None): 
     264    "A custom template loader that loads the unit-test templates." 
    265265    try: 
    266266        return TEMPLATE_TESTS[template_name][0] 
     
    269269 
    270270def run_tests(verbosity=0, standalone=False): 
    271     loader.load_template_source, old_template_loader = test_template_loader, loader.load_template_source 
     271    # Register our custom template loader. 
     272    old_template_loaders = loader.template_source_loaders 
     273    loader.template_source_loaders = [test_template_loader] 
     274 
    272275    failed_tests = [] 
    273276    tests = TEMPLATE_TESTS.items() 
     
    296299                print "Template test: %s -- FAILED. Expected %r, got %r" % (name, vals[2], output) 
    297300            failed_tests.append(name) 
    298     loader.load_template_source = old_template_loader 
     301    loader.template_source_loaders = old_template_loaders 
    299302 
    300303    if failed_tests and not standalone: